调用工具:网页抓取

This commit is contained in:
huangge1199 2025-05-28 15:32:25 +08:00
parent a00013395a
commit 642edae0be
5 changed files with 50 additions and 1 deletions

View File

@ -120,7 +120,12 @@
<artifactId>spring-ai-pgvector-store</artifactId> <artifactId>spring-ai-pgvector-store</artifactId>
<version>1.0.0-M6</version> <version>1.0.0-M6</version>
</dependency> </dependency>
<!-- 网页抓取 https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.20.1</version>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>

View File

@ -18,4 +18,6 @@ public interface ToolsService {
String aiWriteFile(String question); String aiWriteFile(String question);
List<String> webSearch(String question); List<String> webSearch(String question);
List<String> webScrap(String question);
} }

View File

@ -4,6 +4,7 @@ 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.WebScrapTool;
import com.huangge1199.aiagent.tools.WebSearchTool; 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;
@ -64,4 +65,10 @@ public class ToolsServiceImpl implements ToolsService {
WebSearchTool webSearchTool = new WebSearchTool(searchApiKey); WebSearchTool webSearchTool = new WebSearchTool(searchApiKey);
return List.of(webSearchTool.searchWeb(question).split(",")); return List.of(webSearchTool.searchWeb(question).split(","));
} }
@Override
public List<String> webScrap(String url) {
WebScrapTool webScrapTool = new WebScrapTool();
return List.of(webScrapTool.scrapeWebPage(url).split(","));
}
} }

View File

@ -70,4 +70,12 @@ public class ToolController {
List<String> result = toolsService.webSearch(question); List<String> result = toolsService.webSearch(question);
return R.ok(result); return R.ok(result);
} }
@PostMapping("/webScrap")
@Operation(summary = "网页抓取")
public R<List<String>> webScrap(@RequestBody String question) {
CheckUtils.checkEmpty(question, "问题");
List<String> result = toolsService.webScrap(question);
return R.ok(result);
}
} }

View File

@ -0,0 +1,27 @@
package com.huangge1199.aiagent.tools;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import java.io.IOException;
/**
* WebScrapTool
*
* @author huangge1199
* @since 2025/5/28 15:10:11
*/
public class WebScrapTool {
@Tool(description = "Scrape the content of a web page")
public String scrapeWebPage(@ToolParam(description = "URL of the web page to scrape") String url) {
try {
Document doc = Jsoup.connect(url).get();
return doc.html();
} catch (IOException e) {
return "Error scraping web page: " + e.getMessage();
}
}
}