SpringAI之Tool Calling

06-01 1682阅读

SpringAI Tool Calling的使用

  • 一、概述
  • 二、FunctionCallback API (已弃用)
  • 三、Tool Calling API
    • 变更概述
    • 主要变化
    • 迁移示例
      • 1. 基本函数回调
      • 2. ChatClient 使用情况
      • 3. 基于方法的函数回调
      • 4. 选项配置
      • 5. ChatClient Builder 中的默认函数
      • 6. Spring Bean 配置
    • 带有 @Tool 的声明性规范
  • 四、具体示例

一、概述

工具调用(也称为函数调用)是 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()
SpringAI之Tool Calling
(图片来源网络,侵删)
SpringAI之Tool Calling
(图片来源网络,侵删)
SpringAI之Tool Calling
(图片来源网络,侵删)
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码