Forráskód Böngészése

Dev接入博查(联网搜索)

tsurumure 5 hónapja
szülő
commit
be1090fd82

+ 2 - 2
src/main/java/com/backendsys/modules/sdk/bocha/entity/BochaResult.java

@@ -1,14 +1,14 @@
 package com.backendsys.modules.sdk.bocha.entity;
 
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonProperty;
 import lombok.Data;
 
 @Data
 public class BochaResult {
-
     private String name;
     private String url;
     private String summary;
     private String siteIcon;
     private String siteName;
-
 }

+ 6 - 1
src/main/java/com/backendsys/modules/sdk/bocha/service/BochaService.java

@@ -2,8 +2,10 @@ package com.backendsys.modules.sdk.bocha.service;
 
 import cn.hutool.json.JSONObject;
 import com.backendsys.modules.sdk.bocha.entity.BochaParam;
+import com.backendsys.modules.sdk.bocha.entity.BochaResult;
 import com.fasterxml.jackson.databind.JsonNode;
 
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -13,5 +15,8 @@ public interface BochaService {
 
     // [博查] Web Search API
     JsonNode WebSearch(BochaParam bochaParam);
-
+    // [格式化] Web Search API 格式化 (List)
+    List<BochaResult> WebSearchToList(JsonNode result);
+    // [格式化] Web Search API 格式化 (String)
+    String WebSearchToString(JsonNode result);
 }

+ 47 - 0
src/main/java/com/backendsys/modules/sdk/bocha/service/impl/BochaServiceImpl.java

@@ -6,7 +6,9 @@ import cn.hutool.json.JSONNull;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import com.backendsys.modules.sdk.bocha.entity.BochaParam;
+import com.backendsys.modules.sdk.bocha.entity.BochaResult;
 import com.backendsys.modules.sdk.bocha.service.BochaService;
+import com.backendsys.modules.sdk.deepseek.entity.DSRequestMessage;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
@@ -21,6 +23,8 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
 
 @Service
 public class BochaServiceImpl implements BochaService {
@@ -100,4 +104,47 @@ public class BochaServiceImpl implements BochaService {
         }
 
     }
+
+
+    /**
+     * [格式化] Web Search API 格式化
+     */
+    @Override
+    public List<BochaResult> WebSearchToList(JsonNode resultList) {
+        if (resultList.isArray()) {
+            List<BochaResult> bochaResultList = new ArrayList<>();
+            for (JsonNode node : resultList) {
+                BochaResult bochaResult = new BochaResult();
+                bochaResult.setName(node.path("title").asText()); // 根据 JSON 字段名获取值
+                bochaResult.setUrl(node.path("url").asText());
+                bochaResult.setSummary(node.path("summary").asText());
+                bochaResult.setSiteIcon(node.path("siteIcon").asText());
+                bochaResult.setSiteName(node.path("siteName").asText());
+                bochaResultList.add(bochaResult);
+            }
+            return bochaResultList;
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * [格式化] Web Search API 格式化 (String)
+     */
+    @Override
+    public String WebSearchToString(JsonNode result) {
+        List<BochaResult> bochaResultList = WebSearchToList(result);
+        if (!bochaResultList.isEmpty()) {
+            StringBuilder context = new StringBuilder();
+            for (BochaResult item : bochaResultList) {
+                context.append("标题: ").append(item.getName()).append("\n");
+                context.append("摘要: ").append(item.getSummary()).append("\n");
+                context.append("链接: ").append(item.getUrl()).append("\n\n");
+            }
+            // 将搜索结果作为上下文添加到消息中
+            return context.toString();
+        }
+        return null;
+    }
+
 }

+ 13 - 22
src/main/java/com/backendsys/modules/sdk/deepseek/utils/OllamaUtil.java

@@ -89,6 +89,19 @@ public class OllamaUtil {
                 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));
 
@@ -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] 构建请求体
                 HttpPost request = new HttpPost(DOMAIN + "/api/chat");
@@ -171,7 +164,6 @@ public class OllamaUtil {
 
                 request.setEntity(new StringEntity(requestBody, StandardCharsets.UTF_8));
 
-
                 try (CloseableHttpResponse response = client.execute(request);
                      BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8))) {
 
@@ -180,7 +172,6 @@ public class OllamaUtil {
                     long thinkStartTime = 0L;                            // 开始思考时间
                     long thinkDuration = 0L;                             // 思考耗时
 
-
                     System.out.println("API 调用耗时: " + apiDuration + " 毫秒");
                     System.out.println("---- 开始流式回答: ------------------------------------");