调用工具:网页抓取
This commit is contained in:
parent
a00013395a
commit
642edae0be
7
pom.xml
7
pom.xml
@ -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>
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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(","));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user