|
@@ -7,6 +7,7 @@ import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.json.JSONUtil;
|
|
import cn.hutool.json.JSONUtil;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.backendsys.modules.ai.chat.entity.Chat;
|
|
import com.backendsys.modules.ai.chat.entity.Chat;
|
|
|
|
+import com.backendsys.modules.ai.chat.entity.ChatCompletionParam;
|
|
import com.backendsys.modules.ai.chat.entity.ChatResult;
|
|
import com.backendsys.modules.ai.chat.entity.ChatResult;
|
|
import com.backendsys.modules.ai.chat.entity.ChatSseMessage;
|
|
import com.backendsys.modules.ai.chat.entity.ChatSseMessage;
|
|
import com.backendsys.modules.common.config.redis.utils.RedisUtil;
|
|
import com.backendsys.modules.common.config.redis.utils.RedisUtil;
|
|
@@ -59,7 +60,15 @@ public class OllamaUtil {
|
|
/**
|
|
/**
|
|
* 流式对话
|
|
* 流式对话
|
|
*/
|
|
*/
|
|
- public ChatResult chatCompletion(Long user_id, String model, String prompt, String history_code, List<Chat> chatList) {
|
|
|
|
|
|
+ public ChatResult chatCompletion(ChatCompletionParam chatCompletionParam) {
|
|
|
|
+
|
|
|
|
+ // 参数化
|
|
|
|
+ Long user_id = chatCompletionParam.getUser_id();
|
|
|
|
+ String model = chatCompletionParam.getModel();
|
|
|
|
+ String prompt = chatCompletionParam.getPrompt();
|
|
|
|
+ String history_code = chatCompletionParam.getHistory_code();
|
|
|
|
+ List<Chat> chatList = chatCompletionParam.getChatList();
|
|
|
|
+ Boolean internet = chatCompletionParam.getInternet();
|
|
|
|
|
|
// 定义作用于全局的变量
|
|
// 定义作用于全局的变量
|
|
Long contentDuration = 0L;
|
|
Long contentDuration = 0L;
|
|
@@ -89,19 +98,22 @@ public class OllamaUtil {
|
|
Collections.reverse(messages);
|
|
Collections.reverse(messages);
|
|
}
|
|
}
|
|
|
|
|
|
- // 加 联网搜索 参数?
|
|
|
|
-
|
|
|
|
// -- [博查] Web Search API ----------------------------------------------
|
|
// -- [博查] Web Search API ----------------------------------------------
|
|
- if (false) {
|
|
|
|
|
|
+ if (internet) {
|
|
|
|
+
|
|
|
|
+ // 博查、统计接口时间、设置返回参数
|
|
|
|
+ long internetStartTime = System.currentTimeMillis();
|
|
JsonNode searchResult = bochaService.WebSearch(new BochaParam(prompt));
|
|
JsonNode searchResult = bochaService.WebSearch(new BochaParam(prompt));
|
|
String context = bochaService.WebSearchToString(searchResult);
|
|
String context = bochaService.WebSearchToString(searchResult);
|
|
|
|
+ long internetEndTime = System.currentTimeMillis();
|
|
|
|
+ chatResult.setInternet_duration(internetStartTime - internetEndTime);
|
|
|
|
+ chatResult.setInternet_content(context);
|
|
|
|
+
|
|
// 将搜索结果作为上下文添加到消息中
|
|
// 将搜索结果作为上下文添加到消息中
|
|
messages.add(new DSRequestMessage("system", context));
|
|
messages.add(new DSRequestMessage("system", context));
|
|
}
|
|
}
|
|
// -----------------------------------------------------------------------
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
// 新的对话内容
|
|
// 新的对话内容
|
|
messages.add(new DSRequestMessage("user", prompt));
|
|
messages.add(new DSRequestMessage("user", prompt));
|
|
|
|
|
|
@@ -134,6 +146,7 @@ public class OllamaUtil {
|
|
"content": "你是一个能够理解中文指令并帮助完成任务的智能助手。你的任务是根据用户的需求生成合适的分类任务或生成任务,并准确判断这些任务的类型。请确保你的回答简洁、准确且符合中英文语境。"
|
|
"content": "你是一个能够理解中文指令并帮助完成任务的智能助手。你的任务是根据用户的需求生成合适的分类任务或生成任务,并准确判断这些任务的类型。请确保你的回答简洁、准确且符合中英文语境。"
|
|
},
|
|
},
|
|
{
|
|
{
|
|
|
|
+ // "role": "assistant",
|
|
"role": "user",
|
|
"role": "user",
|
|
"content": "写一个简单的 Python 函数,用于计算两个数的和"
|
|
"content": "写一个简单的 Python 函数,用于计算两个数的和"
|
|
}
|
|
}
|
|
@@ -230,12 +243,6 @@ public class OllamaUtil {
|
|
// String content = resJson.getString("response");
|
|
// String content = resJson.getString("response");
|
|
// --------------------------------------------------------------
|
|
// --------------------------------------------------------------
|
|
|
|
|
|
-
|
|
|
|
- // System.out.println("content: " + content);
|
|
|
|
- // content: \n\n
|
|
|
|
- // content: <think>
|
|
|
|
- // content: </think>
|
|
|
|
-
|
|
|
|
// 开始思考
|
|
// 开始思考
|
|
if (content.contains("<think>")) {
|
|
if (content.contains("<think>")) {
|
|
isThinking = true;
|
|
isThinking = true;
|
|
@@ -296,12 +303,7 @@ public class OllamaUtil {
|
|
System.out.println("全部推理: " + allThinkContent);
|
|
System.out.println("全部推理: " + allThinkContent);
|
|
System.out.println("全部回答: " + allReplyContent);
|
|
System.out.println("全部回答: " + allReplyContent);
|
|
System.out.println("总输出耗时: " + contentDuration + " 毫秒");
|
|
System.out.println("总输出耗时: " + contentDuration + " 毫秒");
|
|
-
|
|
|
|
- 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("-----");
|
|
|
|
|
|
+ System.out.println("---------------------------------------------------");
|
|
|
|
|
|
if (StrUtil.isNotEmpty(allThinkContent.toString())) {
|
|
if (StrUtil.isNotEmpty(allThinkContent.toString())) {
|
|
chatResult.setReasoning_content(allThinkContent.toString());
|
|
chatResult.setReasoning_content(allThinkContent.toString());
|
|
@@ -362,23 +364,5 @@ public class OllamaUtil {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
-// 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());
|
|
|
|
-//
|
|
|
|
-// chatResult.setContent(e.getMessage());
|
|
|
|
-// return chatResult;
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-// } finally {
|
|
|
|
-// System.out.println("Finally.");
|
|
|
|
-// // [SSE] 发送消息
|
|
|
|
-// ChatSseMessage chatSseMessage = new ChatSseMessage("REPLY", "[DONE][REPLY]", contentDuration);
|
|
|
|
-// sseUtil.send(user_id, new SseResponse(SseResponseEnum.DEEPSEEK, chatSseMessage).toJsonStr());
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|