SpringAI Tool Calling的使用
一、概述
工具调用(也称为函数调用)是 AI 应用程序中的一种常见模式,允许模型与一组 API 或工具进行交互,从而增强其功能。
工具主要用于:
信息检索。此类别中的工具可用于从外部源(如数据库、Web 服务、文件系统或 Web 搜索引擎)检索信息。目标是增强模型的知识,使其能够回答其他方式无法回答的问题。因此,它们可用于检索增强生成 (RAG) 方案。例如,工具可用于检索给定位置的当前天气、检索最新的新闻文章或查询数据库以获取特定记录。
采取行动。此类别中的工具可用于在软件系统中执行作,例如发送电子邮件、在数据库中创建新记录、提交表单或触发工作流。目标是自动执行原本需要人工干预或显式编程的任务。例如,可以使用工具为与聊天机器人交互的客户预订航班,在网页上填写表单,或在代码生成场景中实现基于自动测试 (TDD) 的 Java 类。
二、FunctionCallback API (已弃用)
在SpringAI 1.0.0-M6版本中,Function Calling已被弃用,新的API已迁移到Tools Calling,具体可以到 SpringAI官网 查看具体文档。
三、Tool Calling API
变更概述
这些更改是改进和扩展 Spring AI 中的工具调用功能的更广泛努力的一部分。除其他外,新 API 从 “functions” 改为 “tools” 术语,以更好地与行业惯例保持一致。这涉及多项 API 更改,同时通过已弃用的方法保持向后兼容性。
主要变化
1、FunctionCallback→ToolCallback
2、FunctionCallback.builder().function()→FunctionToolCallback.builder()
3、FunctionCallback.builder().method()→MethodToolCallback.builder()
4、FunctionCallingOptions→ToolCallingChatOptions
5、ChatClient.builder().defaultFunctions()→ChatClient.builder().defaultTools()
6、ChatClient.functions()→ChatClient.tools()
7、FunctionCallingOptions.builder().functions()→ToolCallingChatOptions.builder().toolNames()
8、FunctionCallingOptions.builder().functionCallbacks()→ToolCallingChatOptions.builder().toolCallbacks()
迁移示例
1. 基本函数回调
以前:
FunctionCallback.builder()
.function("getCurrentWeather", new MockWeatherService())
.description("Get the weather in location")
.inputType(MockWeatherService.Request.class)
.build()
后:
FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
.description("Get the weather in location")
.inputType(MockWeatherService.Request.class)
.build()
2. ChatClient 使用情况
以前:
String response = ChatClient.create(chatModel)
.prompt()
.user("What's the weather like in San Francisco?")
.functions(FunctionCallback.builder()
.function("getCurrentWeather", new MockWeatherService())
.description("Get the weather in location")
.inputType(MockWeatherService.Request.class)
.build())
.call()
.content();
后:
String response = ChatClient.create(chatModel)
.prompt()
.user("What's the weather like in San Francisco?")
.tools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
.description("Get the weather in location")
.inputType(MockWeatherService.Request.class)
.build())
.call()
.content();
3. 基于方法的函数回调
以前:
FunctionCallback.builder()
.method("getWeatherInLocation", String.class, Unit.class)
.description("Get the weather in location")
.targetClass(TestFunctionClass.class)
.build()
后:
var toolMethod = ReflectionUtils.findMethod(TestFunctionClass.class, "getWeatherInLocation");
MethodToolCallback.builder()
.toolDefinition(ToolDefinition.builder(toolMethod)
.description("Get the weather in location")
.build())
.toolMethod(toolMethod)
.build()
或者使用声明式方法:
class WeatherTools {
@Tool(description = "Get the weather in location")
public void getWeatherInLocation(String location, Unit unit) {
// ...
}
}
您可以使用相同的 API 来注册基于方法的工具回调:ChatClient#tools()
String response = ChatClient.create(chatModel)
.prompt()
.user("What's the weather like in San Francisco?")
.tools(MethodToolCallback.builder()
.toolDefinition(ToolDefinition.builder(toolMethod)
.description("Get the weather in location")
.build())
.toolMethod(toolMethod)
.build()