From cad1ce4852f449785fee1b3d2fc9d7bc3acc42e1 Mon Sep 17 00:00:00 2001 From: cherishsince Date: Wed, 22 May 2024 10:30:37 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=A2=9E=E5=8A=A0=E3=80=91AI=20messag?= =?UTF-8?q?e=20=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7=E5=A4=B4=E5=83=8F?= =?UTF-8?q?=E3=80=81role=E5=A4=B4=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/vo/message/AiChatMessageRespVO.java | 7 +++++++ .../ai/service/impl/AiChatServiceImpl.java | 21 ++++++++++++------- .../ai/service/model/AiChatRoleService.java | 9 ++++++++ .../service/model/AiChatRoleServiceImpl.java | 6 ++++++ 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/vo/message/AiChatMessageRespVO.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/vo/message/AiChatMessageRespVO.java index 49c7ee54b..e4e878e40 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/vo/message/AiChatMessageRespVO.java +++ b/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 { @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "2024-05-12 12:51") private LocalDateTime createTime; + // ========= 扩展字段 + + @Schema(description = "用户头像", requiredMode = Schema.RequiredMode.REQUIRED, example = "http://xxx") + private String userAvatar; + + @Schema(description = "角色头像", requiredMode = Schema.RequiredMode.REQUIRED, example = "http://xxx") + private String roleAvatar; } diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java index 0a52681c6..c603f43e8 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java +++ b/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; import cn.iocoder.yudao.module.ai.dal.dataobject.chat.AiChatConversationDO; import cn.iocoder.yudao.module.ai.dal.dataobject.chat.AiChatMessageDO; import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModelDO; +import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatRoleDO; import cn.iocoder.yudao.module.ai.dal.mysql.AiChatMessageMapper; import cn.iocoder.yudao.module.ai.service.AiChatService; import cn.iocoder.yudao.module.ai.service.chat.AiChatConversationService; @@ -216,16 +217,22 @@ public class AiChatServiceImpl implements AiChatService { // 获取对话所有 message List aiChatMessageDOList = chatMessageMapper.selectByConversationId(conversationId); // 获取模型信息 - Set modalIds = aiChatMessageDOList.stream().map(AiChatMessageDO::getModelId).collect(Collectors.toSet()); - List modalList = chatModalService.getModalByIds(modalIds); - Map modalIdMap = modalList.stream().collect(Collectors.toMap(AiChatModelDO::getId, o -> o)); + Set roleIds = aiChatMessageDOList.stream().map(AiChatMessageDO::getRoleId).collect(Collectors.toSet()); + List roleList; + if (!CollUtil.isEmpty(roleIds)) { + roleList = chatRoleService.getChatRoles(roleIds); + } else { + roleList = Collections.emptyList(); + } + Map roleMap = roleList.stream().collect(Collectors.toMap(AiChatRoleDO::getId, o -> o)); // 转换 AiChatMessageRespVO List aiChatMessageRespList = AiChatMessageConvert.INSTANCE.convertAiChatMessageRespVOList(aiChatMessageDOList); - // 设置用户头像 和 模型头像 todo @芋艿 这里需要转换 用户头像、模型头像 + // 设置用户头像 和 模型头像 return aiChatMessageRespList.stream().map(item -> { - if (modalIdMap.containsKey(item.getModelId())) { -// modalIdMap.get(item.getModelId()); -// item.setModelImage() + // 设置 role 头像 + if (roleMap.containsKey(item.getRoleId())) { + AiChatRoleDO role = roleMap.get(item.getRoleId()); + item.setRoleAvatar(role.getAvatar()); } return item; }).collect(Collectors.toList()); diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/model/AiChatRoleService.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/model/AiChatRoleService.java index 7206fd104..228b1ee6e 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/model/AiChatRoleService.java +++ b/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; import jakarta.validation.Valid; import java.util.List; +import java.util.Set; /** * AI 聊天角色 Service 接口 @@ -72,6 +73,14 @@ public interface AiChatRoleService { */ AiChatRoleDO getChatRole(Long id); + /** + * 获得聊天角色 - 根据 ids + * + * @param roleIds + * @return + */ + List getChatRoles(Set roleIds); + /** * 校验聊天角色是否合法 * diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/model/AiChatRoleServiceImpl.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/model/AiChatRoleServiceImpl.java index e0192ffe4..1413dd8ce 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/model/AiChatRoleServiceImpl.java +++ b/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; import java.util.List; import java.util.Objects; +import java.util.Set; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -105,6 +106,11 @@ public class AiChatRoleServiceImpl implements AiChatRoleService { return chatRoleMapper.selectById(id); } + @Override + public List getChatRoles(Set roleIds) { + return chatRoleMapper.selectBatchIds(roleIds); + } + @Override public AiChatRoleDO validateChatRole(Long id) { AiChatRoleDO chatRole = validateChatRoleExists(id);