refactor(module-llm):优化数据处理逻辑

-增加对 "[DONE]" 响应的处理
- 改进 JSON 数据解析方式
- 添加对 <think> 标签的特殊处理
- 优化日志输出内容
This commit is contained in:
sunxiqing 2025-03-05 18:57:06 +08:00
parent 6006f3050d
commit 8288280a9d

View File

@ -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, "<think>")|| ObjectUtil.contains(contentType, "</think>")){
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());
}
}
// // 心跳检测