🚀 PIG AI 新版来袭:AI能力全面升级! 点击了解一下?
@Tool public class CalculatorTool { @ToolMethod public double add(double a, double b) { return a + b; } @ToolMethod public double subtract(double a, double b) { return a - b; } }
@Configuration public class ToolConfig { @Bean public ToolRegistry toolRegistry() { return new ToolRegistry() .register(new CalculatorTool()) .register(new WeatherTool()) .register(new DatabaseTool()); } }
@Service public class ChatService { private final ChatClient chatClient; private final ToolRegistry toolRegistry; public ChatService(ChatClient chatClient, ToolRegistry toolRegistry) { this.chatClient = chatClient; this.toolRegistry = toolRegistry; } public String chat(String message) { return chatClient.generate(message, toolRegistry); } }
@Tool public class MathTool { @ToolMethod public double calculate(String expression) { // 实现 } }
@Tool public class WeatherTool { @ToolMethod public Weather getWeather(String location) { // API 调用实现 } }
@Tool public class DatabaseTool { @ToolMethod public List<Record> query(String sql) { // 数据库查询实现 } }
spring.ai.tools.enabled=true spring.ai.tools.timeout=5000 spring.ai.tools.max-retries=3 spring.ai.tools.cache-enabled=true
@Component public class CustomTool implements Tool { @Override public String getName() { return "custom-tool"; } @Override public Object execute(Map<String, Object> parameters) { // 自定义实现 return result; } }
management.endpoints.web.exposure.include=tools management.endpoint.tools.enabled=true