|
@@ -1,6 +1,7 @@
|
|
|
package com.backendsys.modules.sdk.deepseek.utils;
|
|
|
|
|
|
import cn.hutool.core.convert.Convert;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.backendsys.modules.ai.chat.entity.Chat;
|
|
|
import com.backendsys.modules.ai.chat.entity.ChatResult;
|
|
@@ -55,7 +56,12 @@ public class OllamaUtil {
|
|
|
try {
|
|
|
System.out.println("向模型: " + model + " 提问: " + prompt);
|
|
|
|
|
|
- // - 接口做最大问答限制 (1-字数限制, 2-次数限制)
|
|
|
+ // 记录请求开始时间
|
|
|
+ long allStartTime = System.currentTimeMillis();
|
|
|
+
|
|
|
+ // 加入上下文历史对话
|
|
|
+ System.out.println("---------------------- 历史对话: ----------------------");
|
|
|
+ System.out.println("-----------------------------------------------------");
|
|
|
|
|
|
|
|
|
|
|
@@ -71,15 +77,13 @@ public class OllamaUtil {
|
|
|
String requestBody = objectMapper.writeValueAsString(requestMap);
|
|
|
request.setEntity(new StringEntity(requestBody, StandardCharsets.UTF_8));
|
|
|
|
|
|
- // 记录请求开始时间
|
|
|
- long allStartTime = System.currentTimeMillis();
|
|
|
-
|
|
|
try (CloseableHttpResponse response = client.execute(request);
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8))) {
|
|
|
|
|
|
long apiDuration = System.currentTimeMillis() - allStartTime; // 接口耗时
|
|
|
- long thinkStartTime = 0L; // 开始思考
|
|
|
- long reasoningContentDuration = 0L; // 思考耗时
|
|
|
+
|
|
|
+ long thinkStartTime = 0L; // 开始思考时间
|
|
|
+ long thinkDuration = 0L; // 思考耗时
|
|
|
Boolean isThinking = false;
|
|
|
|
|
|
System.out.println("API 调用耗时: " + apiDuration + " 毫秒");
|
|
@@ -89,16 +93,13 @@ public class OllamaUtil {
|
|
|
ChatSseMessage chatLoadingSseMessage = new ChatSseMessage("LOADING", "正在思考");
|
|
|
sseUtil.send(user_id, new SseResponse(SseResponseEnum.DEEPSEEK, chatLoadingSseMessage).toJsonStr());
|
|
|
|
|
|
- StringBuilder allContent = new StringBuilder();
|
|
|
- StringBuilder allReasoningContent = new StringBuilder();
|
|
|
+ StringBuilder allReplyContent = new StringBuilder();
|
|
|
+ StringBuilder allThinkContent = new StringBuilder();
|
|
|
|
|
|
String line;
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- // System.out.println(line);
|
|
|
+ System.out.println(line);
|
|
|
/*
|
|
|
---------------------- line ----------------------
|
|
|
{"model":"deepseek-r1:1.5b","created_at":"2025-03-05T10:51:17.443189986Z","response":"\u003cthink\u003e","done":false}
|
|
@@ -107,18 +108,57 @@ public class OllamaUtil {
|
|
|
|
|
|
// 每行数据可以是一个JSON对象,根据实际情况处理
|
|
|
JSONObject resJson = JSONObject.parseObject(line);
|
|
|
- String responseContent = resJson.getString("response");
|
|
|
+ String content = resJson.getString("response");
|
|
|
|
|
|
- System.out.println("content: " + responseContent);
|
|
|
+ // System.out.println("content: " + content);
|
|
|
// content: <think>
|
|
|
// content: </think>
|
|
|
|
|
|
- String dataStr = (new SseResponse(SseResponseEnum.DEEPSEEK, responseContent)).toJsonStr();
|
|
|
- sseUtil.send(user_id, dataStr);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ // 开始思考
|
|
|
+ if (content.contains("<think>")) {
|
|
|
+ isThinking = true;
|
|
|
+ thinkStartTime = System.currentTimeMillis();
|
|
|
+ }
|
|
|
+ // 停止思考,并计算思考耗时
|
|
|
+ if (content.contains("</think>")) {
|
|
|
+ isThinking = false;
|
|
|
+ thinkDuration = thinkStartTime - allStartTime;
|
|
|
+ System.out.println("推理耗时: " + thinkDuration + "毫秒");
|
|
|
+ System.out.println("-----------------------------------------------");
|
|
|
+
|
|
|
+ // [SSE] 发送消息
|
|
|
+ ChatSseMessage chatSseMessage = new ChatSseMessage("THINK", "[DONE][THINK]", thinkDuration);
|
|
|
+ sseUtil.send(user_id, new SseResponse(SseResponseEnum.DEEPSEEK, chatSseMessage).toJsonStr());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // [思考] Think
|
|
|
+ if (isThinking) {
|
|
|
+ System.out.println("think: " + content);
|
|
|
+
|
|
|
+ if (!content.contains("<think>") && !content.contains("\n\n")) {
|
|
|
+ // [SSE] 发送消息
|
|
|
+ ChatSseMessage chatSseMessage = new ChatSseMessage("THINK", content);
|
|
|
+ sseUtil.send(user_id, new SseResponse(SseResponseEnum.DEEPSEEK, chatSseMessage).toJsonStr());
|
|
|
+
|
|
|
+ // 收集推理内容
|
|
|
+ allThinkContent.append(content);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // [回答] Reply
|
|
|
+ if (!isThinking) {
|
|
|
+ System.out.println("content: " + content);
|
|
|
+
|
|
|
+ if (!content.contains("</think>") && !content.contains("\n\n")) {
|
|
|
+ // [SSE] 发送消息
|
|
|
+ ChatSseMessage chatSseMessage = new ChatSseMessage("REPLY", content);
|
|
|
+ sseUtil.send(user_id, new SseResponse(SseResponseEnum.DEEPSEEK, chatSseMessage).toJsonStr());
|
|
|
+
|
|
|
+ // 收集回答内容
|
|
|
+ allReplyContent.append(content);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
@@ -126,27 +166,40 @@ public class OllamaUtil {
|
|
|
System.out.println("-------------------- 结束流式回答. --------------------");
|
|
|
contentDuration = System.currentTimeMillis() - allStartTime;
|
|
|
|
|
|
- System.out.println("全部推理: " + allReasoningContent);
|
|
|
- System.out.println("全部回答: " + allContent);
|
|
|
+ System.out.println("全部推理: " + allThinkContent);
|
|
|
+ System.out.println("全部回答: " + allReplyContent);
|
|
|
System.out.println("总输出耗时: " + contentDuration + " 毫秒");
|
|
|
|
|
|
- chatResult.setReasoning_content(allReasoningContent.toString());
|
|
|
- chatResult.setReasoning_duration(reasoningContentDuration);
|
|
|
- chatResult.setContent(allContent.toString());
|
|
|
+ System.out.println("-----");
|
|
|
+ System.out.println("Think content: " + allThinkContent.toString());
|
|
|
+ System.out.println("Think content length: " + allThinkContent.toString().length());
|
|
|
+ System.out.println("Think content is not empty: " + StrUtil.isNotEmpty(allThinkContent.toString()));
|
|
|
+ System.out.println("-----");
|
|
|
+
|
|
|
+ if (StrUtil.isNotEmpty(allThinkContent.toString())) {
|
|
|
+ chatResult.setReasoning_content(allThinkContent.toString());
|
|
|
+ chatResult.setReasoning_duration(thinkDuration);
|
|
|
+ }
|
|
|
+ chatResult.setContent(allReplyContent.toString());
|
|
|
chatResult.setContent_duration(contentDuration);
|
|
|
+
|
|
|
return chatResult;
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
+ System.out.println("Exception(1): " + e.getMessage());
|
|
|
+ String message = e.getMessage();
|
|
|
+ if (message.contains("failed to respond")) {
|
|
|
+ message = "系统繁忙,请稍后再试 (Failed to respond)";
|
|
|
+ }
|
|
|
// [SSE] 发送消息
|
|
|
- ChatSseMessage chatSseMessage = new ChatSseMessage("REPLY", e.getMessage(), contentDuration);
|
|
|
+ ChatSseMessage chatSseMessage = new ChatSseMessage("REPLY", message, contentDuration);
|
|
|
sseUtil.send(user_id, new SseResponse(SseResponseEnum.DEEPSEEK, chatSseMessage).toJsonStr());
|
|
|
|
|
|
chatResult.setContent(e.getMessage());
|
|
|
return chatResult;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
+ System.out.println("Exception(2): " + e.getMessage());
|
|
|
// [SSE] 发送消息
|
|
|
ChatSseMessage chatSseMessage = new ChatSseMessage("REPLY", e.getMessage(), contentDuration);
|
|
|
sseUtil.send(user_id, new SseResponse(SseResponseEnum.DEEPSEEK, chatSseMessage).toJsonStr());
|
|
@@ -157,7 +210,7 @@ public class OllamaUtil {
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
+ System.out.println("Exception(3): " + e.getMessage());
|
|
|
// [SSE] 发送消息
|
|
|
ChatSseMessage chatSseMessage = new ChatSseMessage("REPLY", e.getMessage(), contentDuration);
|
|
|
sseUtil.send(user_id, new SseResponse(SseResponseEnum.DEEPSEEK, chatSseMessage).toJsonStr());
|