四、使用工具调用方式实现简单计算器
==================================================================================
==================================================================================
参考资料:
==================================================================================
本合集《三、使用Spring AI实现工具调用(Tool Calling)》
==================================================================================
在第三篇中我们了解了怎么在SpringAI中实现工具调用,限制我们实操使用工具调用来实现一个简单计算器
1、使用 @Tool 注解定义工具方式实现
import org.springframework.ai.tool.annotation.Tool;public class CalculatorTools {@Tool(description = "将两个数相加")public double add(double x, double y) {System.out.println("CalculatorTools的add被调用了");return x + y;}@Tool(description = "将两个数相减")public double subtract(double x, double y) {System.out.println("CalculatorTools的subtract被调用了");return x - y;}@Tool(description = "将两个数相乘")public double multiply(double x, double y) {System.out.println("CalculatorTools的multiply被调用了");return x * y;}@Tool(description = "将两个数相除")public double divide(double x, double y) {if (y == 0) {throw new IllegalArgumentException("除数不能为零");}System.out.println("CalculatorTools的divide被调用了");return x / y;}@Tool(description = "计算平方根")public double sqrt(double x) {if (x < 0) {throw new IllegalArgumentException("不能对负数开平方根");}System.out.println("CalculatorTools的sqrt被调用了");return Math.sqrt(x);}
}
@RestController
public class ToolsController {@Autowiredprivate ChatModel chatModel;@GetMapping(value = "/tools/calculator")public String toolCalculator(@RequestParam("userInput") String userInput) {return ChatClient.create(chatModel).prompt().user(userInput).tools(new CalculatorTools()).call().content();}
}


2、使用函数式编程定义工具
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Component;import java.util.function.Function;@Component
@Description("执行数学计算:加法、减法、乘法、除法")
public class CalculatorService implements Function<CalculatorService.Request, CalculatorService.Response> {public record Request(@JsonPropertyDescription("第一个操作数") double num1,@JsonPropertyDescription("第二个操作数") double num2,@JsonPropertyDescription("运算类型:ADD/SUBTRACT/MULTIPLY/DIVIDE")String operation) {}public record Response(double result, String message) {}@Overridepublic Response apply(Request request) {double result;String message;switch (request.operation().toUpperCase()) {case "ADD":result = request.num1() + request.num2();message = String.format("%.2f + %.2f = %.2f", request.num1(), request.num2(), result);break;case "SUBTRACT":result = request.num1() - request.num2();message = String.format("%.2f - %.2f = %.2f", request.num1(), request.num2(), result);break;case "MULTIPLY":result = request.num1() * request.num2();message = String.format("%.2f × %.2f = %.2f", request.num1(), request.num2(), result);break;case "DIVIDE":if (request.num2() == 0) {throw new IllegalArgumentException("除数不能为零");}result = request.num1() / request.num2();message = String.format("%.2f ÷ %.2f = %.2f", request.num1(), request.num2(), result);break;default:throw new IllegalArgumentException("不支持的运算类型: " + request.operation());}return new Response(result, message);}
}
2.1、手动构建 FunctionToolCallback
@RestController
public class ToolsController {@Autowiredprivate ChatModel chatModel;@GetMapping(value = "/functions/calculator")public String functionCalculator(@RequestParam("userInput") String userInput) {FunctionToolCallback<CalculatorService.Request, CalculatorService.Response> callback =FunctionToolCallback.builder("加、减、乘、除、平方根运算计算器", new CalculatorService()).description("支持加、减、乘、除、平方根运算等操作,如果用户问的问题不支持请告知详情。").inputType(CalculatorService.Request.class).build();return ChatClient.create(chatModel).prompt().toolCallbacks(callback).user(userInput).call().content();}
}


2.2、通过 @Bean 注册工具
请读者自行完成吧