75 lines
2.2 KiB
Java
75 lines
2.2 KiB
Java
package com.huangge1199.aiagent.Service.impl;
|
|
|
|
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.WebScrapTool;
|
|
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
|
|
*
|
|
* @author huangge1199
|
|
* @since 2025/5/27 15:07:06
|
|
*/
|
|
@Service
|
|
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)
|
|
.prompt(question)
|
|
.advisors(new MyLoggerAdvisor())
|
|
.tools(new WeatherTool())
|
|
.call().content();
|
|
}
|
|
|
|
@Override
|
|
public String writeFileTest(String context, String name) {
|
|
FileTool fileTool = new FileTool();
|
|
return fileTool.writeFile(name,context);
|
|
}
|
|
|
|
@Override
|
|
public String readFileTest(String name) {
|
|
FileTool fileTool = new FileTool();
|
|
return fileTool.readFile(name);
|
|
}
|
|
|
|
@Override
|
|
public String aiWriteFile(String question) {
|
|
return ChatClient.create(ollamaChatModel)
|
|
.prompt(question)
|
|
.advisors(new MyLoggerAdvisor())
|
|
.tools(new FileTool())
|
|
.call().content();
|
|
}
|
|
|
|
@Override
|
|
public List<String> webSearch(String question) {
|
|
WebSearchTool webSearchTool = new WebSearchTool(searchApiKey);
|
|
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(","));
|
|
}
|
|
}
|