Эх сурвалжийг харах

【测试】chat 聊天(send、send-stream)

cherishsince 11 сар өмнө
parent
commit
708d66e8cf

+ 2 - 2
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<AiChatMessageRespVO> sendMessage(@Validated @ModelAttribute AiChatMessageSendReqVO sendReqVO) {
+    public CommonResult<AiChatMessageRespVO> 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<AiChatMessageRespVO>
     @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;

+ 1 - 1
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);
         }
     }

+ 5 - 8
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<ChatResponse> 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());
 
                 }

+ 23 - 2
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}}

+ 0 - 30
yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/chat.http

@@ -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}}
-
-
-
-