此版本仍在开发中,尚未被视为稳定版本。最新的快照版本请使用 Spring AI 1.0.0-SNAPSHOT!spring-doc.cadn.net.cn

从 FunctionCallback 迁移到 ToolCallback API

本指南可帮助您从已弃用的FunctionCallbackAPI 到新的ToolCallbackAPI 中的 API 中。有关新 API 的更多信息,请查看 Tools Calling 文档。spring-doc.cadn.net.cn

变更概述

这些更改是改进和扩展 Spring AI 中的工具调用功能的更广泛努力的一部分。除其他外,新 API 从 “functions” 改为 “tools” 术语,以更好地与行业惯例保持一致。这涉及多项 API 更改,同时通过已弃用的方法保持向后兼容性。spring-doc.cadn.net.cn

主要变化

  1. FunctionCallbackToolCallbackspring-doc.cadn.net.cn

  2. FunctionCallback.builder().function()FunctionToolCallback.builder()spring-doc.cadn.net.cn

  3. FunctionCallback.builder().method()MethodToolCallback.builder()spring-doc.cadn.net.cn

  4. FunctionCallingOptionsToolCallingChatOptionsspring-doc.cadn.net.cn

  5. ChatClient.builder().defaultFunctions()ChatClient.builder().defaultTools()spring-doc.cadn.net.cn

  6. ChatClient.functions()ChatClient.tools()spring-doc.cadn.net.cn

  7. FunctionCallingOptions.builder().functions()ToolCallingChatOptions.builder().toolNames()spring-doc.cadn.net.cn

  8. FunctionCallingOptions.builder().functionCallbacks()ToolCallingChatOptions.builder().toolCallbacks()spring-doc.cadn.net.cn

迁移示例

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()

或者使用声明式方法:spring-doc.cadn.net.cn

class WeatherTools {

    @Tool(description = "Get the weather in location")
    public void getWeatherInLocation(String location, Unit unit) {
        // ...
    }

}

您可以使用相同的ChatClient#tools()用于注册基于方法的工具回调的 API:spring-doc.cadn.net.cn

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())
    .call()
    .content();

或者使用声明式方法:spring-doc.cadn.net.cn

String response = ChatClient.create(chatModel)
    .prompt()
    .user("What's the weather like in San Francisco?")
    .tools(new WeatherTools())
    .call()
    .content();

4. 选项配置

FunctionCallingOptions.builder()
    .model(modelName)
    .function("weatherFunction")
    .build()
ToolCallingChatOptions.builder()
    .model(modelName)
    .toolNames("weatherFunction")
    .build()

5. ChatClient Builder 中的默认函数

ChatClient.builder(chatModel)
    .defaultFunctions(FunctionCallback.builder()
        .function("getCurrentWeather", new MockWeatherService())
        .description("Get the weather in location")
        .inputType(MockWeatherService.Request.class)
        .build())
    .build()
ChatClient.builder(chatModel)
    .defaultTools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
        .description("Get the weather in location")
        .inputType(MockWeatherService.Request.class)
        .build())
    .build()

6. Spring Bean 配置

@Bean
public FunctionCallback weatherFunctionInfo() {
    return FunctionCallback.builder()
        .function("WeatherInfo", new MockWeatherService())
        .description("Get the current weather")
        .inputType(MockWeatherService.Request.class)
        .build();
}
@Bean
public ToolCallback weatherFunctionInfo() {
    return FunctionToolCallback.builder("WeatherInfo", new MockWeatherService())
        .description("Get the current weather")
        .inputType(MockWeatherService.Request.class)
        .build();
}

重大更改

  1. method()函数回调中的配置已替换为更显式的方法工具配置,使用ToolDefinitionMethodToolCallback.spring-doc.cadn.net.cn

  2. 当使用基于方法的回调时,你现在需要显式地使用ReflectionUtils并将其提供给构建器。或者,您可以将声明式方法与@Tool注解。spring-doc.cadn.net.cn

  3. 对于非静态方法,您现在必须同时提供 method 和 target 对象:spring-doc.cadn.net.cn

MethodToolCallback.builder()
    .toolDefinition(ToolDefinition.builder(toolMethod)
        .description("Description")
        .build())
    .toolMethod(toolMethod)
    .toolObject(targetObject)
    .build()

已弃用的方法

以下方法已弃用,并将在未来发行版中删除:spring-doc.cadn.net.cn

使用他们的tools相反。spring-doc.cadn.net.cn

带有 @Tool 的声明性规范

现在,您可以使用方法级注解 (@Tool) 向 Spring AI 注册工具:spring-doc.cadn.net.cn

class Home {

    @Tool(description = "Turn light On or Off in a room.")
    void turnLight(String roomName, boolean on) {
        // ...
        logger.info("Turn light in room: {} to: {}", roomName, on);
    }
}

String response = ChatClient.create(this.chatModel).prompt()
        .user("Turn the light in the living room On.")
        .tools(new Home())
        .call()
        .content();

其他说明

  1. 新的 API 在工具定义和实现之间提供了更好的分离。spring-doc.cadn.net.cn

  2. 工具定义可以在不同的实现中重复使用。spring-doc.cadn.net.cn

  3. 针对常见使用案例,简化了构建器模式。spring-doc.cadn.net.cn

  4. 更好地支持基于方法的工具,并改进了错误处理。spring-doc.cadn.net.cn

时间线

已弃用的方法将在当前里程碑版本中保留以实现向后兼容性,但将在下一个里程碑版本中删除。建议尽快迁移到新的 API。spring-doc.cadn.net.cn