From 708d66e8cf0647cb4c7afa2743794a478c8b9bce Mon Sep 17 00:00:00 2001 From: cherishsince Date: Tue, 7 May 2024 16:47:25 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=B5=8B=E8=AF=95=E3=80=91chat=20?= =?UTF-8?q?=E8=81=8A=E5=A4=A9(send=E3=80=81send-stream)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/chat/AiChatMessageController.java | 4 +-- .../service/impl/AiChatModalServiceImpl.java | 2 +- .../ai/service/impl/AiChatServiceImpl.java | 13 ++++---- .../src/main/resources/http/chat-message.http | 25 ++++++++++++++-- .../src/main/resources/http/chat.http | 30 ------------------- 5 files changed, 31 insertions(+), 43 deletions(-) delete mode 100644 yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/chat.http diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/AiChatMessageController.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/AiChatMessageController.java index 285611cbe..a36f871a6 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/AiChatMessageController.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/AiChatMessageController.java @@ -30,7 +30,7 @@ public class AiChatMessageController { @Operation(summary = "发送消息(段式)", description = "一次性返回,响应较慢") @PostMapping("/send") - public CommonResult sendMessage(@Validated @ModelAttribute AiChatMessageSendReqVO sendReqVO) { + public CommonResult sendMessage(@Validated @RequestBody AiChatMessageSendReqVO sendReqVO) { // TODO done @fan:使用 static import;这样就 success 就行了; return success(chatService.chat(sendReqVO)); } @@ -39,7 +39,7 @@ public class AiChatMessageController { // TODO @fan:要不要使用 Flux 来返回;可以使用 Flux @Operation(summary = "发送消息(流式)", description = "流式返回,响应较快") @PostMapping(value = "/send-stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) - public SseEmitter sendMessageStream(@Validated @ModelAttribute AiChatMessageSendReqVO sendReqVO) { + public SseEmitter sendMessageStream(@Validated @RequestBody AiChatMessageSendReqVO sendReqVO) { Utf8SseEmitter sseEmitter = new Utf8SseEmitter(); chatService.chatStream(sendReqVO, sseEmitter); return sseEmitter; diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatModalServiceImpl.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatModalServiceImpl.java index c831ab723..0aec7a94c 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatModalServiceImpl.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatModalServiceImpl.java @@ -97,7 +97,7 @@ public class AiChatModalServiceImpl implements AiChatModelService { @Override public void validateAvailable(AiChatModalRespVO chatModal) { // 对话模型是否可用 - if (CommonStatusEnum.ENABLE.getStatus().equals(chatModal.getStatus())) { + if (!CommonStatusEnum.ENABLE.getStatus().equals(chatModal.getStatus())) { throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_MODAL_DISABLE_NOT_USED); } } 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 3241a144d..ba1b4b679 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 @@ -19,7 +19,6 @@ import cn.iocoder.yudao.module.ai.dal.dataobject.chat.AiChatMessageDO; import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatRoleDO; import cn.iocoder.yudao.module.ai.dal.mysql.AiChatConversationMapper; import cn.iocoder.yudao.module.ai.dal.mysql.AiChatMessageMapper; -import cn.iocoder.yudao.module.ai.dal.mysql.AiChatRoleMapper; import cn.iocoder.yudao.module.ai.service.AiChatConversationService; import cn.iocoder.yudao.module.ai.service.AiChatModelService; import cn.iocoder.yudao.module.ai.service.AiChatRoleService; @@ -48,7 +47,6 @@ import java.util.function.Consumer; public class AiChatServiceImpl implements AiChatService { private final AiChatClientFactory aiChatClientFactory; - private final AiChatRoleMapper aiChatRoleMapper; private final AiChatMessageMapper aiChatMessageMapper; private final AiChatConversationMapper aiChatConversationMapper; private final AiChatConversationService chatConversationService; @@ -72,7 +70,7 @@ public class AiChatServiceImpl implements AiChatService { // 校验角色是否公开 aiChatRoleService.validateIsPublic(aiChatRoleDO); // 获取 client 类型 - AiPlatformEnum platformEnum = AiPlatformEnum.valueOfPlatform(chatModal.getModel()); + AiPlatformEnum platformEnum = AiPlatformEnum.valueOfPlatform(chatModal.getPlatform()); // 保存 chat message insertChatMessage(conversation.getId(), MessageType.USER, loginUserId, conversation.getRoleId(), chatModal.getModel(), chatModal.getId(), req.getContent(), @@ -90,13 +88,12 @@ public class AiChatServiceImpl implements AiChatService { ChatResponse call = chatClient.call(prompt); content = call.getResult().getOutput().getContent(); // 更新 conversation - } catch (Exception e) { content = ExceptionUtil.getMessage(e); } finally { // 保存 chat message insertChatMessage(conversation.getId(), MessageType.SYSTEM, loginUserId, conversation.getRoleId(), - chatModal.getModel(), chatModal.getId(), req.getContent(), + chatModal.getModel(), chatModal.getId(), content, null, conversation.getTemperature(), conversation.getMaxTokens(), conversation.getMaxContexts()); } return new AiChatMessageRespVO().setContent(content); @@ -154,7 +151,7 @@ public class AiChatServiceImpl implements AiChatService { null, conversation.getTemperature(), conversation.getMaxTokens(), conversation.getMaxContexts()); // 获取 client 类型 - AiPlatformEnum platformEnum = AiPlatformEnum.valueOfPlatform(chatModal.getModel()); + AiPlatformEnum platformEnum = AiPlatformEnum.valueOfPlatform(chatModal.getPlatform()); StreamingChatClient streamingChatClient = aiChatClientFactory.getStreamingChatClient(platformEnum); Flux streamResponse = streamingChatClient.stream(prompt); @@ -166,7 +163,7 @@ public class AiChatServiceImpl implements AiChatService { String content = chatResponse.getResults().get(0).getOutput().getContent(); try { contentBuffer.append(content); - sseEmitter.send(content, MediaType.APPLICATION_JSON); + sseEmitter.send(new AiChatMessageRespVO().setContent(content), MediaType.APPLICATION_JSON); } catch (IOException e) { log.error("发送异常{}", ExceptionUtil.getMessage(e)); // 如果不是因为关闭而抛出异常,则重新连接 @@ -183,7 +180,7 @@ public class AiChatServiceImpl implements AiChatService { sseEmitter.complete(); // 保存 chat message insertChatMessage(conversation.getId(), MessageType.SYSTEM, loginUserId, conversation.getRoleId(), - chatModal.getModel(), chatModal.getId(), req.getContent(), + chatModal.getModel(), chatModal.getId(), contentBuffer.toString(), null, conversation.getTemperature(), conversation.getMaxTokens(), conversation.getMaxContexts()); } diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/chat-message.http b/yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/chat-message.http index b39eda5ff..b357ab66e 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/chat-message.http +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/chat-message.http @@ -1,10 +1,31 @@ +### chat call +POST {{baseUrl}}/admin-api/ai/chat/message/send +Content-Type: application/json +Authorization: {{token}} + +{ + "conversationId": "1781604279872581649", + "content": "中国好看吗?" +} + + +### chat call +POST {{baseUrl}}/admin-api/ai/chat/message/send-stream +Content-Type: application/json +Authorization: {{token}} + +{ + "conversationId": "1781604279872581649", + "content": "苹果是什么颜色?" +} + ### message list -GET {{baseUrl}}/ai/chat/message/list?chatConversationId=1781604279872581644 +GET {{baseUrl}}/admin-api/ai/chat/message/list-by-conversation-id?conversationId=1781604279872581649 Authorization: {{token}} ### message delete -DELETE {{baseUrl}}/ai/chat/message/1781604279872581644/8 +DELETE {{baseUrl}}/admin-api/ai/chat/message/delete?id=50 Authorization: {{token}} diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/chat.http b/yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/chat.http deleted file mode 100644 index 829b74f45..000000000 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/chat.http +++ /dev/null @@ -1,30 +0,0 @@ - - -### 登录 - -POST {{baseUrl}}/admin-api/system/auth/login -Content-Type: application/json -tenant-id: 1 - -{ - "username": "admin", - "password": "123456", - "captchaVerification": "PfcH6mgr8tpXuMWFjvW6YVaqrswIuwmWI5dsVZSg7sGpWtDCUbHuDEXl3cFB1+VvCC/rAkSwK8Fad52FSuncVg==", - "socialCode": "1024", - "socialState": "9b2ffbc1-7425-4155-9894-9d5c08541d62", - "socialCodeValid": true -} - -### chat call -GET {{baseUrl}}/ai/chat?modal=qianwen&conversationId=1781604279872581644&prompt=中国好看吗? -Authorization: {{token}} - - - -### chat call -GET {{baseUrl}}/ai/chatStream?conversationId=1781604279872581644&prompt=苹果是什么颜色?&modal=yiYan3_5_8k -Authorization: {{token}} - - - -