Commit cad1ce4852f449785fee1b3d2fc9d7bc3acc42e1
1 parent
747b2ebc
【增加】AI message 增加用户头像、role头像
Showing
4 changed files
with
36 additions
and
7 deletions
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/vo/message/AiChatMessageRespVO.java
| @@ -36,4 +36,11 @@ public class AiChatMessageRespVO { | @@ -36,4 +36,11 @@ public class AiChatMessageRespVO { | ||
| 36 | @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "2024-05-12 12:51") | 36 | @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "2024-05-12 12:51") |
| 37 | private LocalDateTime createTime; | 37 | private LocalDateTime createTime; |
| 38 | 38 | ||
| 39 | + // ========= 扩展字段 | ||
| 40 | + | ||
| 41 | + @Schema(description = "用户头像", requiredMode = Schema.RequiredMode.REQUIRED, example = "http://xxx") | ||
| 42 | + private String userAvatar; | ||
| 43 | + | ||
| 44 | + @Schema(description = "角色头像", requiredMode = Schema.RequiredMode.REQUIRED, example = "http://xxx") | ||
| 45 | + private String roleAvatar; | ||
| 39 | } | 46 | } |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java
| @@ -13,6 +13,7 @@ import cn.iocoder.yudao.module.ai.convert.AiChatMessageConvert; | @@ -13,6 +13,7 @@ import cn.iocoder.yudao.module.ai.convert.AiChatMessageConvert; | ||
| 13 | import cn.iocoder.yudao.module.ai.dal.dataobject.chat.AiChatConversationDO; | 13 | import cn.iocoder.yudao.module.ai.dal.dataobject.chat.AiChatConversationDO; |
| 14 | import cn.iocoder.yudao.module.ai.dal.dataobject.chat.AiChatMessageDO; | 14 | import cn.iocoder.yudao.module.ai.dal.dataobject.chat.AiChatMessageDO; |
| 15 | import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModelDO; | 15 | import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModelDO; |
| 16 | +import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatRoleDO; | ||
| 16 | import cn.iocoder.yudao.module.ai.dal.mysql.AiChatMessageMapper; | 17 | import cn.iocoder.yudao.module.ai.dal.mysql.AiChatMessageMapper; |
| 17 | import cn.iocoder.yudao.module.ai.service.AiChatService; | 18 | import cn.iocoder.yudao.module.ai.service.AiChatService; |
| 18 | import cn.iocoder.yudao.module.ai.service.chat.AiChatConversationService; | 19 | import cn.iocoder.yudao.module.ai.service.chat.AiChatConversationService; |
| @@ -216,16 +217,22 @@ public class AiChatServiceImpl implements AiChatService { | @@ -216,16 +217,22 @@ public class AiChatServiceImpl implements AiChatService { | ||
| 216 | // 获取对话所有 message | 217 | // 获取对话所有 message |
| 217 | List<AiChatMessageDO> aiChatMessageDOList = chatMessageMapper.selectByConversationId(conversationId); | 218 | List<AiChatMessageDO> aiChatMessageDOList = chatMessageMapper.selectByConversationId(conversationId); |
| 218 | // 获取模型信息 | 219 | // 获取模型信息 |
| 219 | - Set<Long> modalIds = aiChatMessageDOList.stream().map(AiChatMessageDO::getModelId).collect(Collectors.toSet()); | ||
| 220 | - List<AiChatModelDO> modalList = chatModalService.getModalByIds(modalIds); | ||
| 221 | - Map<Long, AiChatModelDO> modalIdMap = modalList.stream().collect(Collectors.toMap(AiChatModelDO::getId, o -> o)); | 220 | + Set<Long> roleIds = aiChatMessageDOList.stream().map(AiChatMessageDO::getRoleId).collect(Collectors.toSet()); |
| 221 | + List<AiChatRoleDO> roleList; | ||
| 222 | + if (!CollUtil.isEmpty(roleIds)) { | ||
| 223 | + roleList = chatRoleService.getChatRoles(roleIds); | ||
| 224 | + } else { | ||
| 225 | + roleList = Collections.emptyList(); | ||
| 226 | + } | ||
| 227 | + Map<Long, AiChatRoleDO> roleMap = roleList.stream().collect(Collectors.toMap(AiChatRoleDO::getId, o -> o)); | ||
| 222 | // 转换 AiChatMessageRespVO | 228 | // 转换 AiChatMessageRespVO |
| 223 | List<AiChatMessageRespVO> aiChatMessageRespList = AiChatMessageConvert.INSTANCE.convertAiChatMessageRespVOList(aiChatMessageDOList); | 229 | List<AiChatMessageRespVO> aiChatMessageRespList = AiChatMessageConvert.INSTANCE.convertAiChatMessageRespVOList(aiChatMessageDOList); |
| 224 | - // 设置用户头像 和 模型头像 todo @芋艿 这里需要转换 用户头像、模型头像 | 230 | + // 设置用户头像 和 模型头像 |
| 225 | return aiChatMessageRespList.stream().map(item -> { | 231 | return aiChatMessageRespList.stream().map(item -> { |
| 226 | - if (modalIdMap.containsKey(item.getModelId())) { | ||
| 227 | -// modalIdMap.get(item.getModelId()); | ||
| 228 | -// item.setModelImage() | 232 | + // 设置 role 头像 |
| 233 | + if (roleMap.containsKey(item.getRoleId())) { | ||
| 234 | + AiChatRoleDO role = roleMap.get(item.getRoleId()); | ||
| 235 | + item.setRoleAvatar(role.getAvatar()); | ||
| 229 | } | 236 | } |
| 230 | return item; | 237 | return item; |
| 231 | }).collect(Collectors.toList()); | 238 | }).collect(Collectors.toList()); |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/model/AiChatRoleService.java
| @@ -9,6 +9,7 @@ import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatRoleDO; | @@ -9,6 +9,7 @@ import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatRoleDO; | ||
| 9 | import jakarta.validation.Valid; | 9 | import jakarta.validation.Valid; |
| 10 | 10 | ||
| 11 | import java.util.List; | 11 | import java.util.List; |
| 12 | +import java.util.Set; | ||
| 12 | 13 | ||
| 13 | /** | 14 | /** |
| 14 | * AI 聊天角色 Service 接口 | 15 | * AI 聊天角色 Service 接口 |
| @@ -73,6 +74,14 @@ public interface AiChatRoleService { | @@ -73,6 +74,14 @@ public interface AiChatRoleService { | ||
| 73 | AiChatRoleDO getChatRole(Long id); | 74 | AiChatRoleDO getChatRole(Long id); |
| 74 | 75 | ||
| 75 | /** | 76 | /** |
| 77 | + * 获得聊天角色 - 根据 ids | ||
| 78 | + * | ||
| 79 | + * @param roleIds | ||
| 80 | + * @return | ||
| 81 | + */ | ||
| 82 | + List<AiChatRoleDO> getChatRoles(Set<Long> roleIds); | ||
| 83 | + | ||
| 84 | + /** | ||
| 76 | * 校验聊天角色是否合法 | 85 | * 校验聊天角色是否合法 |
| 77 | * | 86 | * |
| 78 | * @param id 角色编号 | 87 | * @param id 角色编号 |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/model/AiChatRoleServiceImpl.java
| @@ -16,6 +16,7 @@ import org.springframework.stereotype.Service; | @@ -16,6 +16,7 @@ import org.springframework.stereotype.Service; | ||
| 16 | 16 | ||
| 17 | import java.util.List; | 17 | import java.util.List; |
| 18 | import java.util.Objects; | 18 | import java.util.Objects; |
| 19 | +import java.util.Set; | ||
| 19 | import java.util.function.Function; | 20 | import java.util.function.Function; |
| 20 | import java.util.function.Predicate; | 21 | import java.util.function.Predicate; |
| 21 | import java.util.stream.Collectors; | 22 | import java.util.stream.Collectors; |
| @@ -106,6 +107,11 @@ public class AiChatRoleServiceImpl implements AiChatRoleService { | @@ -106,6 +107,11 @@ public class AiChatRoleServiceImpl implements AiChatRoleService { | ||
| 106 | } | 107 | } |
| 107 | 108 | ||
| 108 | @Override | 109 | @Override |
| 110 | + public List<AiChatRoleDO> getChatRoles(Set<Long> roleIds) { | ||
| 111 | + return chatRoleMapper.selectBatchIds(roleIds); | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + @Override | ||
| 109 | public AiChatRoleDO validateChatRole(Long id) { | 115 | public AiChatRoleDO validateChatRole(Long id) { |
| 110 | AiChatRoleDO chatRole = validateChatRoleExists(id); | 116 | AiChatRoleDO chatRole = validateChatRoleExists(id); |
| 111 | if (CommonStatusEnum.isDisable(chatRole.getStatus())) { | 117 | if (CommonStatusEnum.isDisable(chatRole.getStatus())) { |