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

This commit is contained in:
cherishsince 2024-05-07 16:47:25 +08:00
parent bd3e65898b
commit 708d66e8cf
5 changed files with 31 additions and 43 deletions

View File

@ -30,7 +30,7 @@ public class AiChatMessageController {
@Operation(summary = "发送消息(段式)", description = "一次性返回,响应较慢") @Operation(summary = "发送消息(段式)", description = "一次性返回,响应较慢")
@PostMapping("/send") @PostMapping("/send")
public CommonResult<AiChatMessageRespVO> sendMessage(@Validated @ModelAttribute AiChatMessageSendReqVO sendReqVO) { public CommonResult<AiChatMessageRespVO> sendMessage(@Validated @RequestBody AiChatMessageSendReqVO sendReqVO) {
// TODO done @fan使用 static import这样就 success 就行了 // TODO done @fan使用 static import这样就 success 就行了
return success(chatService.chat(sendReqVO)); return success(chatService.chat(sendReqVO));
} }
@ -39,7 +39,7 @@ public class AiChatMessageController {
// TODO @fan要不要使用 Flux 来返回可以使用 Flux<AiChatMessageRespVO> // TODO @fan要不要使用 Flux 来返回可以使用 Flux<AiChatMessageRespVO>
@Operation(summary = "发送消息(流式)", description = "流式返回,响应较快") @Operation(summary = "发送消息(流式)", description = "流式返回,响应较快")
@PostMapping(value = "/send-stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) @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(); Utf8SseEmitter sseEmitter = new Utf8SseEmitter();
chatService.chatStream(sendReqVO, sseEmitter); chatService.chatStream(sendReqVO, sseEmitter);
return sseEmitter; return sseEmitter;

View File

@ -97,7 +97,7 @@ public class AiChatModalServiceImpl implements AiChatModelService {
@Override @Override
public void validateAvailable(AiChatModalRespVO chatModal) { 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); throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_MODAL_DISABLE_NOT_USED);
} }
} }

View File

@ -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.dataobject.model.AiChatRoleDO;
import cn.iocoder.yudao.module.ai.dal.mysql.AiChatConversationMapper; 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.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.AiChatConversationService;
import cn.iocoder.yudao.module.ai.service.AiChatModelService; import cn.iocoder.yudao.module.ai.service.AiChatModelService;
import cn.iocoder.yudao.module.ai.service.AiChatRoleService; import cn.iocoder.yudao.module.ai.service.AiChatRoleService;
@ -48,7 +47,6 @@ import java.util.function.Consumer;
public class AiChatServiceImpl implements AiChatService { public class AiChatServiceImpl implements AiChatService {
private final AiChatClientFactory aiChatClientFactory; private final AiChatClientFactory aiChatClientFactory;
private final AiChatRoleMapper aiChatRoleMapper;
private final AiChatMessageMapper aiChatMessageMapper; private final AiChatMessageMapper aiChatMessageMapper;
private final AiChatConversationMapper aiChatConversationMapper; private final AiChatConversationMapper aiChatConversationMapper;
private final AiChatConversationService chatConversationService; private final AiChatConversationService chatConversationService;
@ -72,7 +70,7 @@ public class AiChatServiceImpl implements AiChatService {
// 校验角色是否公开 // 校验角色是否公开
aiChatRoleService.validateIsPublic(aiChatRoleDO); aiChatRoleService.validateIsPublic(aiChatRoleDO);
// 获取 client 类型 // 获取 client 类型
AiPlatformEnum platformEnum = AiPlatformEnum.valueOfPlatform(chatModal.getModel()); AiPlatformEnum platformEnum = AiPlatformEnum.valueOfPlatform(chatModal.getPlatform());
// 保存 chat message // 保存 chat message
insertChatMessage(conversation.getId(), MessageType.USER, loginUserId, conversation.getRoleId(), insertChatMessage(conversation.getId(), MessageType.USER, loginUserId, conversation.getRoleId(),
chatModal.getModel(), chatModal.getId(), req.getContent(), chatModal.getModel(), chatModal.getId(), req.getContent(),
@ -90,13 +88,12 @@ public class AiChatServiceImpl implements AiChatService {
ChatResponse call = chatClient.call(prompt); ChatResponse call = chatClient.call(prompt);
content = call.getResult().getOutput().getContent(); content = call.getResult().getOutput().getContent();
// 更新 conversation // 更新 conversation
} catch (Exception e) { } catch (Exception e) {
content = ExceptionUtil.getMessage(e); content = ExceptionUtil.getMessage(e);
} finally { } finally {
// 保存 chat message // 保存 chat message
insertChatMessage(conversation.getId(), MessageType.SYSTEM, loginUserId, conversation.getRoleId(), 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()); null, conversation.getTemperature(), conversation.getMaxTokens(), conversation.getMaxContexts());
} }
return new AiChatMessageRespVO().setContent(content); return new AiChatMessageRespVO().setContent(content);
@ -154,7 +151,7 @@ public class AiChatServiceImpl implements AiChatService {
null, conversation.getTemperature(), conversation.getMaxTokens(), conversation.getMaxContexts()); null, conversation.getTemperature(), conversation.getMaxTokens(), conversation.getMaxContexts());
// 获取 client 类型 // 获取 client 类型
AiPlatformEnum platformEnum = AiPlatformEnum.valueOfPlatform(chatModal.getModel()); AiPlatformEnum platformEnum = AiPlatformEnum.valueOfPlatform(chatModal.getPlatform());
StreamingChatClient streamingChatClient = aiChatClientFactory.getStreamingChatClient(platformEnum); StreamingChatClient streamingChatClient = aiChatClientFactory.getStreamingChatClient(platformEnum);
Flux<ChatResponse> streamResponse = streamingChatClient.stream(prompt); Flux<ChatResponse> streamResponse = streamingChatClient.stream(prompt);
@ -166,7 +163,7 @@ public class AiChatServiceImpl implements AiChatService {
String content = chatResponse.getResults().get(0).getOutput().getContent(); String content = chatResponse.getResults().get(0).getOutput().getContent();
try { try {
contentBuffer.append(content); contentBuffer.append(content);
sseEmitter.send(content, MediaType.APPLICATION_JSON); sseEmitter.send(new AiChatMessageRespVO().setContent(content), MediaType.APPLICATION_JSON);
} catch (IOException e) { } catch (IOException e) {
log.error("发送异常{}", ExceptionUtil.getMessage(e)); log.error("发送异常{}", ExceptionUtil.getMessage(e));
// 如果不是因为关闭而抛出异常则重新连接 // 如果不是因为关闭而抛出异常则重新连接
@ -183,7 +180,7 @@ public class AiChatServiceImpl implements AiChatService {
sseEmitter.complete(); sseEmitter.complete();
// 保存 chat message // 保存 chat message
insertChatMessage(conversation.getId(), MessageType.SYSTEM, loginUserId, conversation.getRoleId(), 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()); null, conversation.getTemperature(), conversation.getMaxTokens(), conversation.getMaxContexts());
} }

View File

@ -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 ### 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}} Authorization: {{token}}
### message delete ### message delete
DELETE {{baseUrl}}/ai/chat/message/1781604279872581644/8 DELETE {{baseUrl}}/admin-api/ai/chat/message/delete?id=50
Authorization: {{token}} Authorization: {{token}}

View File

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