From 8b4b896fa02ad44ebb3412634afc34292bb2818f Mon Sep 17 00:00:00 2001 From: Liuyang <2746366019@qq.com> Date: Sun, 2 Mar 2025 12:45:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(llm):=20=E5=AE=9E=E7=8E=B0=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E5=A4=84=E7=90=86=E5=92=8C=E6=B5=81=E5=BC=8F=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E7=9A=84=E8=81=8A=E5=A4=A9=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 ChatReqVO 类作为聊天请求的参数对象 - 在 ConversationController 中实现异步处理聊天请求的逻辑 - 在 ModelService 中添加流式响应相关的代码 - 优化了错误处理和资源释放的逻辑 --- .../conversation/ConversationController.java | 16 +++++++++++-- .../module/llm/service/http/ModelService.java | 7 ++++-- .../module/llm/service/http/vo/ChatReqVO.java | 24 +++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/vo/ChatReqVO.java diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/controller/admin/conversation/ConversationController.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/controller/admin/conversation/ConversationController.java index 8e6db4560..0638d98be 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/controller/admin/conversation/ConversationController.java +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/controller/admin/conversation/ConversationController.java @@ -25,6 +25,8 @@ import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import java.io.IOException; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; @@ -109,8 +111,17 @@ public class ConversationController { public SseEmitter streamChat (@Valid @RequestBody ChatReqVO chatReqVO, HttpServletResponse response) { log.info("收到对话推理请求,请求参数: {}", chatReqVO); SseEmitter emitter = new SseEmitter(60_000L); + ExecutorService executor = Executors.newSingleThreadExecutor(); try { - conversationService.chatStream(chatReqVO, emitter, response); + executor.execute(() -> { + try { + conversationService.chatStream(chatReqVO, emitter, response); + } catch (Exception e) { + emitter.completeWithError(e); + } finally { + executor.shutdown(); + } + }); } catch (Exception e) { log.error("处理对话推理请求时发生异常", e); try { @@ -120,10 +131,11 @@ public class ConversationController { } } log.info("返回 SseEmitter 对象,准备进行流式响应"); + emitter.onCompletion(executor::shutdown); + emitter.onTimeout(executor::shutdown); return emitter; } - @PostMapping("/text-to-image") @Operation(summary = "文字转图片接口") public CommonResult textToImage (@Valid @RequestBody TextToImageReqVo req) { diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/ModelService.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/ModelService.java index f65ff0cea..e017bfc57 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/ModelService.java +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/ModelService.java @@ -284,9 +284,12 @@ public class ModelService { line = line.replaceAll("\n", " "); String content = parseStreamLine(line, uuid); if (content != null) { - emitter.send(SseEmitter.event() - .data(content, MediaType.TEXT_EVENT_STREAM) + emitter.send( + SseEmitter.event() + .data(content, MediaType.TEXT_EVENT_STREAM) + .reconnectTime(5000) ); + log.info("已发送数据:{}", content); } ChatReqVO chatReqVO = JSONObject.parseObject(content, ChatReqVO.class); if (content!=null){ diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/vo/ChatReqVO.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/vo/ChatReqVO.java new file mode 100644 index 000000000..b9ffe88ba --- /dev/null +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/vo/ChatReqVO.java @@ -0,0 +1,24 @@ +package cn.iocoder.yudao.module.llm.service.http.vo; + +import lombok.Data; + +/** + * @Description + */ +@Data +public class ChatReqVO { + /** + * 对话内容 + */ + private String content; + + /** + * 对话的uuid + */ + private String uuid; + + /** + * 是否结束对话 + */ + private boolean finish_reason; +}