From 8288280a9d0f0ba186a3d8f977590156f5bff698 Mon Sep 17 00:00:00 2001 From: sunxiqing <2240398334@qq.com> Date: Wed, 5 Mar 2025 18:57:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor(module-llm):=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -增加对 "[DONE]" 响应的处理 - 改进 JSON 数据解析方式 - 添加对 标签的特殊处理 - 优化日志输出内容 --- .../module/llm/service/http/ModelService.java | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) 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 03900dd15..991461464 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 @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.llm.service.http; +import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.util.http.HttpUtils; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.module.llm.dal.dataobject.servername.ServerNameDO; @@ -284,18 +285,35 @@ public class ModelService { } log.info("接收到的响应行数据: {}", line); - line = line.replaceAll("\n", " "); - String content = parseStreamLine(line, uuid, groupId); - if (content != null) { - emitter.send( - SseEmitter.event() - .data(content, MediaType.TEXT_EVENT_STREAM) - ); - log.info("已发送数据:{}", content); + int i = 0; + if (!"data: [DONE]".equals(line)) { + String jsonData = line.substring(line.indexOf("{")); + JSONObject jsonObject = JSON.parseObject(jsonData); + log.info("处理后数据: {}", jsonData); + JSONObject content1 = jsonObject.getJSONArray("choices").getJSONObject(0); + // 获取delta对象 + JSONObject delta = content1.getJSONObject("delta"); + // 获取content字段 + String contentType = delta.getString("content"); + log.info("返回字符串: {}", contentType); + if (ObjectUtil.contains(contentType, "")|| ObjectUtil.contains(contentType, "")){ + i++; + } } - ChatReqVO chatReqVO = JSONObject.parseObject(content, ChatReqVO.class); - if (content != null) { - result.append(chatReqVO.getContent()); + if (i!=1) { + line = line.replaceAll("\n", " "); + String content = parseStreamLine(line, uuid,groupId); + if (content != null) { + emitter.send( + SseEmitter.event() + .data(content, MediaType.TEXT_EVENT_STREAM) + ); + log.info("已发送数据:{}", content); + } + ChatReqVO chatReqVO = JSONObject.parseObject(content, ChatReqVO.class); + if (content != null) { + result.append(chatReqVO.getContent()); + } } // // 心跳检测