Commit 25523fd53ee0d9cd9657c14f85f7f298a6094728
1 parent
97d4e56c
调整 chat 和 steamChat 获取对话逻辑
Showing
1 changed file
with
11 additions
and
12 deletions
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/ChatServiceImpl.java
| ... | ... | @@ -18,7 +18,9 @@ import cn.iocoder.yudao.module.ai.enums.ChatTypeEnum; |
| 18 | 18 | import cn.iocoder.yudao.module.ai.mapper.AiChatConversationMapper; |
| 19 | 19 | import cn.iocoder.yudao.module.ai.mapper.AiChatMessageMapper; |
| 20 | 20 | import cn.iocoder.yudao.module.ai.mapper.AiChatRoleMapper; |
| 21 | +import cn.iocoder.yudao.module.ai.service.ChatConversationService; | |
| 21 | 22 | import cn.iocoder.yudao.module.ai.service.ChatService; |
| 23 | +import cn.iocoder.yudao.module.ai.vo.ChatConversationRes; | |
| 22 | 24 | import cn.iocoder.yudao.module.ai.vo.ChatReq; |
| 23 | 25 | import lombok.AllArgsConstructor; |
| 24 | 26 | import lombok.extern.slf4j.Slf4j; |
| ... | ... | @@ -46,6 +48,7 @@ public class ChatServiceImpl implements ChatService { |
| 46 | 48 | private final AiChatRoleMapper aiChatRoleMapper; |
| 47 | 49 | private final AiChatMessageMapper aiChatMessageMapper; |
| 48 | 50 | private final AiChatConversationMapper aiChatConversationMapper; |
| 51 | + private final ChatConversationService chatConversationService; | |
| 49 | 52 | |
| 50 | 53 | |
| 51 | 54 | /** |
| ... | ... | @@ -59,13 +62,10 @@ public class ChatServiceImpl implements ChatService { |
| 59 | 62 | Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); |
| 60 | 63 | // 获取 client 类型 |
| 61 | 64 | AiClientNameEnum clientNameEnum = AiClientNameEnum.valueOfName(req.getModal()); |
| 62 | - // 获取 对话类型(新建还是继续) | |
| 63 | - ChatConversationTypeEnum chatConversationTypeEnum = ChatConversationTypeEnum.valueOfType(req.getConversationType()); | |
| 64 | - AiChatConversationDO aiChatConversationDO = getChatConversationNoExistToCreate(req, chatConversationTypeEnum, loginUserId); | |
| 65 | - | |
| 65 | + // 获取对话信息 | |
| 66 | + ChatConversationRes conversationRes = chatConversationService.getConversation(req.getConversationId()); | |
| 66 | 67 | // 保存 chat message |
| 67 | - saveChatMessage(req, aiChatConversationDO.getId(), loginUserId); | |
| 68 | - | |
| 68 | + saveChatMessage(req, conversationRes.getId(), loginUserId); | |
| 69 | 69 | String content = null; |
| 70 | 70 | try { |
| 71 | 71 | // 创建 chat 需要的 Prompt |
| ... | ... | @@ -80,7 +80,7 @@ public class ChatServiceImpl implements ChatService { |
| 80 | 80 | content = ExceptionUtil.getMessage(e); |
| 81 | 81 | } finally { |
| 82 | 82 | // 保存 chat message |
| 83 | - saveSystemChatMessage(req, aiChatConversationDO.getId(), loginUserId, content); | |
| 83 | + saveSystemChatMessage(req, conversationRes.getId(), loginUserId, content); | |
| 84 | 84 | } |
| 85 | 85 | return content; |
| 86 | 86 | } |
| ... | ... | @@ -176,16 +176,15 @@ public class ChatServiceImpl implements ChatService { |
| 176 | 176 | Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); |
| 177 | 177 | // 获取 client 类型 |
| 178 | 178 | AiClientNameEnum clientNameEnum = AiClientNameEnum.valueOfName(req.getModal()); |
| 179 | - // 获取 对话类型(新建还是继续) | |
| 180 | - ChatConversationTypeEnum chatConversationTypeEnum = ChatConversationTypeEnum.valueOfType(req.getConversationType()); | |
| 181 | - AiChatConversationDO aiChatConversationDO = getChatConversationNoExistToCreate(req, chatConversationTypeEnum, loginUserId); | |
| 179 | + // 获取对话信息 | |
| 180 | + ChatConversationRes conversationRes = chatConversationService.getConversation(req.getConversationId()); | |
| 182 | 181 | // 创建 chat 需要的 Prompt |
| 183 | 182 | Prompt prompt = new Prompt(req.getPrompt()); |
| 184 | 183 | req.setTopK(req.getTopK()); |
| 185 | 184 | req.setTopP(req.getTopP()); |
| 186 | 185 | req.setTemperature(req.getTemperature()); |
| 187 | 186 | // 保存 chat message |
| 188 | - saveChatMessage(req, aiChatConversationDO.getId(), loginUserId); | |
| 187 | + saveChatMessage(req, conversationRes.getId(), loginUserId); | |
| 189 | 188 | Flux<ChatResponse> streamResponse = aiClient.stream(prompt, clientNameEnum.getName()); |
| 190 | 189 | |
| 191 | 190 | StringBuffer contentBuffer = new StringBuffer(); |
| ... | ... | @@ -212,7 +211,7 @@ public class ChatServiceImpl implements ChatService { |
| 212 | 211 | log.info("发送完成!"); |
| 213 | 212 | sseEmitter.complete(); |
| 214 | 213 | // 保存 chat message |
| 215 | - saveSystemChatMessage(req, aiChatConversationDO.getId(), loginUserId, contentBuffer.toString()); | |
| 214 | + saveSystemChatMessage(req, conversationRes.getId(), loginUserId, contentBuffer.toString()); | |
| 216 | 215 | } |
| 217 | 216 | ); |
| 218 | 217 | } | ... | ... |