调用工具:联网搜索
This commit is contained in:
parent
301cd1a6cc
commit
a00013395a
@ -1,5 +1,7 @@
|
|||||||
package com.huangge1199.aiagent.Service;
|
package com.huangge1199.aiagent.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ToolsService
|
* ToolsService
|
||||||
*
|
*
|
||||||
@ -14,4 +16,6 @@ public interface ToolsService {
|
|||||||
String readFileTest(String name);
|
String readFileTest(String name);
|
||||||
|
|
||||||
String aiWriteFile(String question);
|
String aiWriteFile(String question);
|
||||||
|
|
||||||
|
List<String> webSearch(String question);
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,16 @@ import com.huangge1199.aiagent.Service.ToolsService;
|
|||||||
import com.huangge1199.aiagent.config.MyLoggerAdvisor;
|
import com.huangge1199.aiagent.config.MyLoggerAdvisor;
|
||||||
import com.huangge1199.aiagent.tools.FileTool;
|
import com.huangge1199.aiagent.tools.FileTool;
|
||||||
import com.huangge1199.aiagent.tools.WeatherTool;
|
import com.huangge1199.aiagent.tools.WeatherTool;
|
||||||
|
import com.huangge1199.aiagent.tools.WebSearchTool;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.ai.chat.client.ChatClient;
|
import org.springframework.ai.chat.client.ChatClient;
|
||||||
import org.springframework.ai.ollama.OllamaChatModel;
|
import org.springframework.ai.ollama.OllamaChatModel;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ToolsServiceImpl
|
* ToolsServiceImpl
|
||||||
*
|
*
|
||||||
@ -21,6 +26,9 @@ public class ToolsServiceImpl implements ToolsService {
|
|||||||
@Resource
|
@Resource
|
||||||
private OllamaChatModel ollamaChatModel;
|
private OllamaChatModel ollamaChatModel;
|
||||||
|
|
||||||
|
@Value("${search-api.api-key}")
|
||||||
|
private String searchApiKey;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getWeather(String question) {
|
public String getWeather(String question) {
|
||||||
return ChatClient.create(ollamaChatModel)
|
return ChatClient.create(ollamaChatModel)
|
||||||
@ -50,4 +58,10 @@ public class ToolsServiceImpl implements ToolsService {
|
|||||||
.tools(new FileTool())
|
.tools(new FileTool())
|
||||||
.call().content();
|
.call().content();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> webSearch(String question) {
|
||||||
|
WebSearchTool webSearchTool = new WebSearchTool(searchApiKey);
|
||||||
|
return List.of(webSearchTool.searchWeb(question).split(","));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
package com.huangge1199.aiagent.controller;
|
package com.huangge1199.aiagent.controller;
|
||||||
|
|
||||||
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONObject;
|
||||||
import com.alibaba.cloud.nacos.utils.StringUtils;
|
|
||||||
import com.huangge1199.aiagent.Service.ToolsService;
|
import com.huangge1199.aiagent.Service.ToolsService;
|
||||||
import com.huangge1199.aiagent.common.R;
|
import com.huangge1199.aiagent.common.R;
|
||||||
import com.huangge1199.aiagent.util.CheckUtils;
|
import com.huangge1199.aiagent.util.CheckUtils;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.ai.document.Document;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@ -64,4 +62,12 @@ public class ToolController {
|
|||||||
String result = toolsService.aiWriteFile(question);
|
String result = toolsService.aiWriteFile(question);
|
||||||
return R.ok(result);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user