|
@@ -89,6 +89,19 @@ public class OllamaUtil {
|
|
Collections.reverse(messages);
|
|
Collections.reverse(messages);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 加 联网搜索 参数?
|
|
|
|
+
|
|
|
|
+ // -- [博查] Web Search API ----------------------------------------------
|
|
|
|
+ if (false) {
|
|
|
|
+ JsonNode searchResult = bochaService.WebSearch(new BochaParam(prompt));
|
|
|
|
+ String context = bochaService.WebSearchToString(searchResult);
|
|
|
|
+ // 将搜索结果作为上下文添加到消息中
|
|
|
|
+ messages.add(new DSRequestMessage("system", context));
|
|
|
|
+ }
|
|
|
|
+ // -----------------------------------------------------------------------
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
// 新的对话内容
|
|
// 新的对话内容
|
|
messages.add(new DSRequestMessage("user", prompt));
|
|
messages.add(new DSRequestMessage("user", prompt));
|
|
|
|
|
|
@@ -132,26 +145,6 @@ public class OllamaUtil {
|
|
}
|
|
}
|
|
*/
|
|
*/
|
|
|
|
|
|
- JsonNode bochaResult = bochaService.WebSearch(new BochaParam(prompt));
|
|
|
|
- List<BochaResult> searchResults = JSONUtil.toList(JSONUtil.parseArray(bochaResult), BochaResult.class);
|
|
|
|
-
|
|
|
|
- /*
|
|
|
|
- StringBuilder context = new StringBuilder();
|
|
|
|
- for (SearchResult result : searchResults) {
|
|
|
|
- context.append("标题: ").append(result.getTitle()).append("\n");
|
|
|
|
- context.append("摘要: ").append(result.getSnippet()).append("\n");
|
|
|
|
- context.append("链接: ").append(result.getUrl()).append("\n\n");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 将搜索结果作为上下文添加到消息中
|
|
|
|
- ChatRequest.Context contextMessage = new ChatRequest.Context("system", context.toString());
|
|
|
|
- request.getMessages().add(contextMessage);
|
|
|
|
-
|
|
|
|
- // 调用 Ollama 的 /api/chat 接口
|
|
|
|
- RestTemplate restTemplate = new RestTemplate();
|
|
|
|
- String ollamaUrl = "http://localhost:11434/api/chat";
|
|
|
|
- ChatResponse response = restTemplate.postForObject(ollamaUrl, request, ChatResponse.class);
|
|
|
|
- */
|
|
|
|
|
|
|
|
// [Chat] 构建请求体
|
|
// [Chat] 构建请求体
|
|
HttpPost request = new HttpPost(DOMAIN + "/api/chat");
|
|
HttpPost request = new HttpPost(DOMAIN + "/api/chat");
|
|
@@ -171,7 +164,6 @@ public class OllamaUtil {
|
|
|
|
|
|
request.setEntity(new StringEntity(requestBody, StandardCharsets.UTF_8));
|
|
request.setEntity(new StringEntity(requestBody, StandardCharsets.UTF_8));
|
|
|
|
|
|
-
|
|
|
|
try (CloseableHttpResponse response = client.execute(request);
|
|
try (CloseableHttpResponse response = client.execute(request);
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8))) {
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8))) {
|
|
|
|
|
|
@@ -180,7 +172,6 @@ public class OllamaUtil {
|
|
long thinkStartTime = 0L; // 开始思考时间
|
|
long thinkStartTime = 0L; // 开始思考时间
|
|
long thinkDuration = 0L; // 思考耗时
|
|
long thinkDuration = 0L; // 思考耗时
|
|
|
|
|
|
-
|
|
|
|
System.out.println("API 调用耗时: " + apiDuration + " 毫秒");
|
|
System.out.println("API 调用耗时: " + apiDuration + " 毫秒");
|
|
System.out.println("---- 开始流式回答: ------------------------------------");
|
|
System.out.println("---- 开始流式回答: ------------------------------------");
|
|
|
|
|