调用工具:联网搜索

This commit is contained in:
huangge1199 2025-05-28 15:12:30 +08:00
parent 301cd1a6cc
commit a00013395a
4 changed files with 82 additions and 2 deletions

View File

@ -1,5 +1,7 @@
package com.huangge1199.aiagent.Service;
import java.util.List;
/**
* ToolsService
*
@ -14,4 +16,6 @@ public interface ToolsService {
String readFileTest(String name);
String aiWriteFile(String question);
List<String> webSearch(String question);
}

View File

@ -4,11 +4,16 @@ import com.huangge1199.aiagent.Service.ToolsService;
import com.huangge1199.aiagent.config.MyLoggerAdvisor;
import com.huangge1199.aiagent.tools.FileTool;
import com.huangge1199.aiagent.tools.WeatherTool;
import com.huangge1199.aiagent.tools.WebSearchTool;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* ToolsServiceImpl
*
@ -21,6 +26,9 @@ public class ToolsServiceImpl implements ToolsService {
@Resource
private OllamaChatModel ollamaChatModel;
@Value("${search-api.api-key}")
private String searchApiKey;
@Override
public String getWeather(String question) {
return ChatClient.create(ollamaChatModel)
@ -50,4 +58,10 @@ public class ToolsServiceImpl implements ToolsService {
.tools(new FileTool())
.call().content();
}
@Override
public List<String> webSearch(String question) {
WebSearchTool webSearchTool = new WebSearchTool(searchApiKey);
return List.of(webSearchTool.searchWeb(question).split(","));
}
}

View File

@ -1,14 +1,12 @@
package com.huangge1199.aiagent.controller;
import cn.hutool.json.JSONObject;
import com.alibaba.cloud.nacos.utils.StringUtils;
import com.huangge1199.aiagent.Service.ToolsService;
import com.huangge1199.aiagent.common.R;
import com.huangge1199.aiagent.util.CheckUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@ -64,4 +62,12 @@ public class ToolController {
String result = toolsService.aiWriteFile(question);
return R.ok(result);
}
@PostMapping("/webSearch")
@Operation(summary = "联网搜索")
public R<List<String>> webSearch(@RequestBody String question) {
CheckUtils.checkEmpty(question, "问题");
List<String> result = toolsService.webSearch(question);
return R.ok(result);
}
}

View File

@ -0,0 +1,56 @@
package com.huangge1199.aiagent.tools;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* WebSearchTool
*
* @author huangge1199
* @since 2025/5/28 11:00:28
*/
public class WebSearchTool {
// SearchAPI 的搜索接口地址
private static final String SEARCH_API_URL = "https://www.searchapi.io/api/v1/search";
private final String apiKey;
public WebSearchTool(String apiKey) {
this.apiKey = apiKey;
}
@Tool(description = "Search for information from Baidu Search Engine")
public String searchWeb(
@ToolParam(description = "Search query keyword") String query) {
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("q", query);
paramMap.put("api_key", apiKey);
paramMap.put("engine", "bing");
try {
String response = HttpUtil.get(SEARCH_API_URL, paramMap);
// 取出返回结果的前 5
JSONObject jsonObject = JSONUtil.parseObj(response);
// 提取 organic_results 部分
JSONArray organicResults = jsonObject.getJSONArray("organic_results");
List<Object> objects = organicResults.subList(0, 5);
// 拼接搜索结果为字符串
return objects.stream().map(obj -> {
JSONObject tmpJsonObject = (JSONObject) obj;
return tmpJsonObject.get("title").toString();
}).collect(Collectors.joining(","));
} catch (Exception e) {
return "Error searching Baidu: " + e.getMessage();
}
}
}