|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
}
|