From 48c3210f5f0a2e95b006b609d0880deb93dc8eff Mon Sep 17 00:00:00 2001 From: YunaiV Date: Fri, 24 May 2024 15:38:56 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91AI=EF=BC=9A?= =?UTF-8?q?=E6=96=B0=E5=BB=BA=E5=AF=B9=E8=AF=9D=E6=97=B6=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E9=BB=98=E8=AE=A4=E8=A7=92=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/ai/dal/mysql/AiChatRoleMapper.java | 8 -------- .../chat/AiChatConversationServiceImpl.java | 17 +++++++++-------- .../service/chat/AiChatMessageServiceImpl.java | 4 +++- .../ai/service/model/AiChatRoleService.java | 9 --------- .../ai/service/model/AiChatRoleServiceImpl.java | 10 ---------- 5 files changed, 12 insertions(+), 36 deletions(-) diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/mysql/AiChatRoleMapper.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/mysql/AiChatRoleMapper.java index c2a9631dc..42a3cb195 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/mysql/AiChatRoleMapper.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/mysql/AiChatRoleMapper.java @@ -20,14 +20,6 @@ import java.util.List; @Mapper public interface AiChatRoleMapper extends BaseMapperX { - default AiChatRoleDO selectFirstByPublicStatusAndStatus(Boolean publicStatus, Integer status) { - return selectOne(new QueryWrapperX() - .eq("status", status) - .eq("public_status", publicStatus) - .limitN(1) - .orderByAsc("sort")); - } - default PageResult selectPage(AiChatRolePageReqVO reqVO) { return selectPage(reqVO, new LambdaQueryWrapperX() .likeIfPresent(AiChatRoleDO::getName, reqVO.getName()) diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/chat/AiChatConversationServiceImpl.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/chat/AiChatConversationServiceImpl.java index 1e3bd52ce..3a3b16aea 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/chat/AiChatConversationServiceImpl.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/chat/AiChatConversationServiceImpl.java @@ -46,21 +46,22 @@ public class AiChatConversationServiceImpl implements AiChatConversationService @Override public Long createChatConversationMy(AiChatConversationCreateMyReqVO createReqVO, Long userId) { // 1.1 获得 AiChatRoleDO 聊天角色 - AiChatRoleDO role = createReqVO.getRoleId() != null ? chatRoleService.validateChatRole(createReqVO.getRoleId()) - : chatRoleService.getRequiredDefaultChatRole(); - Assert.notNull(role, "必须找到聊天角色"); + AiChatRoleDO role = createReqVO.getRoleId() != null ? chatRoleService.validateChatRole(createReqVO.getRoleId()) : null; // 1.2 获得 AiChatModelDO 聊天模型 - AiChatModelDO model = role.getModelId() != null ? chatModalService.validateChatModel(role.getModelId()) + AiChatModelDO model = role != null && role.getModelId() != null ? chatModalService.validateChatModel(role.getModelId()) : chatModalService.getRequiredDefaultChatModel(); Assert.notNull(model, "必须找到默认模型"); validateChatModel(model); // 2. 创建 AiChatConversationDO 聊天对话 - String title = createReqVO.getRoleId() == null ? AiChatConversationDO.TITLE_DEFAULT : role.getName(); - AiChatConversationDO conversation = new AiChatConversationDO() - .setUserId(userId).setTitle(title).setPinned(false) - .setRoleId(role.getId()).setModelId(model.getId()).setModel(model.getModel()).setSystemMessage(role.getSystemMessage()) + AiChatConversationDO conversation = new AiChatConversationDO().setUserId(userId).setPinned(false) + .setModelId(model.getId()).setModel(model.getModel()) .setTemperature(model.getTemperature()).setMaxTokens(model.getMaxTokens()).setMaxContexts(model.getMaxContexts()); + if (role != null) { + conversation.setTitle(role.getName()).setRoleId(role.getId()).setSystemMessage(role.getSystemMessage()); + } else { + conversation.setTitle(AiChatConversationDO.TITLE_DEFAULT); + } chatConversationMapper.insert(conversation); return conversation.getId(); } diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/chat/AiChatMessageServiceImpl.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/chat/AiChatMessageServiceImpl.java index edc11995d..a6861243a 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/chat/AiChatMessageServiceImpl.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/chat/AiChatMessageServiceImpl.java @@ -165,7 +165,9 @@ public class AiChatMessageServiceImpl implements AiChatMessageService { // 1. 构建 Prompt Message 列表 List chatMessages = new ArrayList<>(); // 1.1 system context 角色设定 - chatMessages.add(new SystemMessage(conversation.getSystemMessage())); + if (StrUtil.isNotBlank(conversation.getSystemMessage())) { + chatMessages.add(new SystemMessage(conversation.getSystemMessage())); + } // 1.2 history message 历史消息 List contextMessages = filterContextMessages(messages, conversation, sendReqVO); contextMessages.forEach(message -> chatMessages.add(new ChatMessage(message.getType().toUpperCase(), message.getContent()))); 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 a7bc2501a..a602d6537 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 @@ -94,15 +94,6 @@ public interface AiChatRoleService { */ AiChatRoleDO validateChatRole(Long id); - /** - * 获得默认的聊天角色 - * - * 如果获取不到,则抛出 {@link cn.iocoder.yudao.framework.common.exception.ServiceException} 业务异常 - * - * @return 聊天角色 - */ - AiChatRoleDO getRequiredDefaultChatRole(); - /** * 获得聊天角色分页 * 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 d3083ec9f..8313e12a1 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 @@ -121,16 +121,6 @@ public class AiChatRoleServiceImpl implements AiChatRoleService { return chatRole; } - @Override - public AiChatRoleDO getRequiredDefaultChatRole() { - AiChatRoleDO chatRole = chatRoleMapper.selectFirstByPublicStatusAndStatus( - true, CommonStatusEnum.ENABLE.getStatus()); - if (chatRole == null) { - throw exception(CHAT_ROLE_DEFAULT_NOT_EXISTS); - } - return chatRole; - } - @Override public PageResult getChatRolePage(AiChatRolePageReqVO pageReqVO) { return chatRoleMapper.selectPage(pageReqVO);