跳到主要内容

Tool Calling 使用指南

Spring AI Alibaba 提供了丰富的 Tool Calling 扩展实现,允许 AI 模型与外部 API 和工具进行交互,增强模型的能力。本文档将详细介绍 Tool Calling 的使用方法,并列出所有支持的扩展实现。

目录

概述

Tool Calling(工具调用,也称为 Function Calling)是 AI 应用中的常见模式,允许模型与一组 API 或工具进行交互,增强模型的能力。

工具主要用于:

  • 信息检索:从外部数据源检索信息,如数据库、Web 服务、文件系统或 Web 搜索引擎。例如,获取当前天气、检索最新新闻、查询数据库等。
  • 执行操作:在软件系统中执行特定操作,如发送电子邮件、在数据库中创建新记录、提交表单或触发工作流。例如,预订航班、填写表单、生成代码等。

Spring AI Alibaba 扩展了 Spring AI 的 Tool Calling 功能,提供了多种预构建的工具实现,包括:

  • 搜索引擎(百度搜索、Google Scholar、Tavily Search 等)
  • 翻译服务(阿里翻译、百度翻译、Google 翻译等)
  • 地图服务(高德地图、百度地图、腾讯地图等)
  • 数据服务(天气、快递、新闻等)
  • 开发工具(GitHub、JSON 处理、正则表达式等)
  • 其他工具(时间、Python 执行、敏感词过滤等)

详细使用说明

Python Tool 示例

PythonTool 使用 GraalVM polyglot 在沙箱环境中执行 Python 代码。这个工具允许 AI 代理执行 Python 代码片段并获取结果。

依赖配置

使用 Maven 添加依赖:

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-tool-calling-python</artifactId>
<version>${version}</version>
</dependency>

注意:Python Tool 需要 GraalVM polyglot 依赖,这些依赖已作为可选依赖包含。如果需要使用,请确保 GraalVM polyglot 在 classpath 中。

自动配置

Python Tool 默认启用,会自动注册为 ToolCallback 并可供 AI 代理使用。如果需要禁用,可以在配置文件中设置:

spring:
ai:
alibaba:
python:
tool:
enabled: false

基本使用

示例 1:在 ChatClient 中使用
import com.alibaba.cloud.ai.agent.python.tool.PythonTool;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PythonToolService {

@Autowired
private ChatModel chatModel;

@Autowired
private PythonTool pythonTool;

public String executeCalculation(String question) {
// 创建 ChatClient 并添加 Python Tool
ChatClient chatClient = ChatClient.builder(chatModel)
.tools(pythonTool.createPythonToolCallback(PythonTool.DESCRIPTION))
.build();

// 使用工具进行对话
String response = chatClient.prompt(question)
.call()
.content();

return response;
}
}
示例 2:直接使用 PythonTool
import com.alibaba.cloud.ai.agent.python.tool.PythonTool;
import org.springframework.ai.chat.model.ToolContext;

public class DirectPythonToolUsage {

public void executePythonCode() {
PythonTool pythonTool = new PythonTool();

// 创建请求
PythonTool.PythonRequest request = new PythonTool.PythonRequest("2 + 2");

// 执行 Python 代码
String result = pythonTool.apply(request, new ToolContext());

System.out.println("Result: " + result); // 输出: Result: 4
}
}
示例 3:执行复杂计算
import com.alibaba.cloud.ai.agent.python.tool.PythonTool;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;

public class ComplexCalculationExample {

private final ChatClient chatClient;

public ComplexCalculationExample(ChatModel chatModel, PythonTool pythonTool) {
this.chatClient = ChatClient.builder(chatModel)
.tools(pythonTool.createPythonToolCallback(PythonTool.DESCRIPTION))
.build();
}

public String calculateStatistics(String data) {
String prompt = String.format("""
请使用 Python 工具计算以下数据的统计信息:
%s

请计算平均值、最大值、最小值和总和。
""", data);

return chatClient.prompt(prompt)
.call()
.content();
}
}
示例 4:字符串处理
import com.alibaba.cloud.ai.agent.python.tool.PythonTool;
import org.springframework.ai.chat.model.ToolContext;

public class StringProcessingExample {

public void processStrings() {
PythonTool pythonTool = new PythonTool();
ToolContext toolContext = new ToolContext();

// 字符串连接
PythonTool.PythonRequest request1 = new PythonTool.PythonRequest(
"'Hello, ' + 'World'"
);
String result1 = pythonTool.apply(request1, toolContext);
System.out.println(result1); // 输出: Hello, World

// 列表操作
PythonTool.PythonRequest request2 = new PythonTool.PythonRequest(
"[1, 2, 3, 4, 5][:3]"
);
String result2 = pythonTool.apply(request2, toolContext);
System.out.println(result2); // 输出: [1, 2, 3]

// 字典操作
PythonTool.PythonRequest request3 = new PythonTool.PythonRequest(
"{'name': 'Alice', 'age': 30}.get('name')"
);
String result3 = pythonTool.apply(request3, toolContext);
System.out.println(result3); // 输出: Alice
}
}
示例 5:在 Spring Boot 应用中使用
import com.alibaba.cloud.ai.agent.python.tool.PythonTool;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PythonToolAgent {

private final ChatClient chatClient;

@Autowired
public PythonToolAgent(ChatModel chatModel, PythonTool pythonTool) {
this.chatClient = ChatClient.builder(chatModel)
.tools(pythonTool.createPythonToolCallback(PythonTool.DESCRIPTION))
.build();
}

public String askWithPython(String question) {
return chatClient.prompt(question)
.call()
.content();
}
}
示例 6:自定义 ToolCallback
import com.alibaba.cloud.ai.agent.python.tool.PythonTool;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PythonToolConfiguration {

@Bean
public PythonTool pythonTool() {
return new PythonTool();
}

@Bean
public ToolCallback pythonToolCallback(PythonTool pythonTool) {
return PythonTool.createPythonToolCallback(
"执行 Python 代码并返回结果。支持数学计算、字符串处理、列表操作等。"
);
}
}
示例 7:多工具组合使用
import com.alibaba.cloud.ai.agent.python.tool.PythonTool;
import com.alibaba.cloud.ai.agent.time.tool.TimeTool;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;

public class MultiToolExample {

public ChatClient createChatClient(ChatModel chatModel) {
PythonTool pythonTool = new PythonTool();
TimeTool timeTool = new TimeTool();

return ChatClient.builder(chatModel)
.tools(
pythonTool.createPythonToolCallback(PythonTool.DESCRIPTION),
timeTool.createTimeToolCallback(TimeTool.DESCRIPTION)
)
.build();
}

public String complexQuery(String question) {
ChatClient chatClient = createChatClient(chatModel);

return chatClient.prompt(question)
.call()
.content();
}
}

安全特性

Python Tool 在沙箱环境中运行,具有以下安全限制:

  • 文件 I/O 已禁用:无法读取或写入文件
  • 本地访问已禁用:无法访问本地系统资源
  • 进程创建已禁用:无法创建新进程
  • 默认限制所有访问:所有访问默认受限

这些限制确保了 Python 代码的执行安全性,防止恶意代码对系统造成损害。

支持的数据类型

Python Tool 支持以下 Python 数据类型的返回:

  • 字符串:直接返回字符串值
  • 数字:转换为字符串返回
  • 布尔值:转换为字符串返回
  • 数组/列表:转换为字符串表示形式(如 [1, 2, 3]
  • 其他类型:使用 toString() 方法转换

注意事项

  1. GraalVM 依赖:确保 GraalVM polyglot 依赖在 classpath 中
  2. 性能考虑:每次执行都会创建新的 Context,对于频繁调用可能需要优化
  3. 错误处理:代码执行错误会被捕获并返回错误消息
  4. 代码限制:由于安全限制,某些 Python 功能可能不可用
  5. 资源管理:Context 会自动关闭,无需手动管理

完整示例:AI 代理使用 Python Tool

import com.alibaba.cloud.ai.agent.python.tool.PythonTool;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.stereotype.Service;

@Service
public class PythonToolAgentService {

private final ChatClient chatClient;

public PythonToolAgentService(ChatModel chatModel, PythonTool pythonTool) {
this.chatClient = ChatClient.builder(chatModel)
.tools(pythonTool.createPythonToolCallback(PythonTool.DESCRIPTION))
.build();
}

public String solveMathProblem(String problem) {
String prompt = String.format("""
请使用 Python 工具解决以下数学问题:
%s

请展示计算过程。
""", problem);

return chatClient.prompt(prompt)
.call()
.content();
}

public String analyzeData(String dataDescription) {
String prompt = String.format("""
请使用 Python 工具分析以下数据:
%s

请计算基本统计信息(平均值、中位数、标准差等)。
""", dataDescription);

return chatClient.prompt(prompt)
.call()
.content();
}
}

支持的扩展实现

下表列出了 Spring AI Alibaba 提供的所有 Tool Calling 扩展实现:

模块名称ArtifactId功能说明主要用途
阿里翻译spring-ai-alibaba-starter-tool-calling-alitranslate阿里云翻译服务文本翻译
阿里云 AI 搜索spring-ai-alibaba-starter-tool-calling-aliyunaisearch阿里云 AI 搜索智能搜索
高德地图spring-ai-alibaba-starter-tool-calling-amap高德地图 API地理位置服务
百度地图spring-ai-alibaba-starter-tool-calling-baidumap百度地图 API地理位置服务
百度搜索spring-ai-alibaba-starter-tool-calling-baidusearch百度搜索 APIWeb 搜索
百度翻译spring-ai-alibaba-starter-tool-calling-baidutranslate百度翻译 API文本翻译
Brave Searchspring-ai-alibaba-starter-tool-calling-bravesearchBrave 搜索引擎Web 搜索
Commonspring-ai-alibaba-starter-tool-calling-common通用工具基础工具基础类
钉钉spring-ai-alibaba-starter-tool-calling-dingtalk钉钉 API企业通讯
DuckDuckGospring-ai-alibaba-starter-tool-calling-duckduckgoDuckDuckGo 搜索Web 搜索
Firecrawlspring-ai-alibaba-starter-tool-calling-firecrawlFirecrawl 网页抓取网页内容提取
GitHub Toolkitspring-ai-alibaba-starter-tool-calling-githubtoolkitGitHub API代码仓库操作
Google Scholarspring-ai-alibaba-starter-tool-calling-googlescholarGoogle Scholar学术搜索
Google 翻译spring-ai-alibaba-starter-tool-calling-googletranslateGoogle 翻译 API文本翻译
Google Trendsspring-ai-alibaba-starter-tool-calling-googletrendsGoogle Trends趋势分析
Jina Crawlerspring-ai-alibaba-starter-tool-calling-jinacrawlerJina 爬虫网页爬取
JSON 处理spring-ai-alibaba-starter-tool-calling-jsonprocessorJSON 数据处理JSON 操作
快递 100spring-ai-alibaba-starter-tool-calling-kuaidi100快递 100 API快递查询
飞书spring-ai-alibaba-starter-tool-calling-larksuite飞书 API企业协作
Memcachedspring-ai-alibaba-starter-tool-calling-memcachedMemcached缓存操作
Metasospring-ai-alibaba-starter-tool-calling-metasoMetaso 搜索Web 搜索
Microsoft 翻译spring-ai-alibaba-starter-tool-calling-microsofttranslateMicrosoft 翻译文本翻译
MinIOspring-ai-alibaba-starter-tool-calling-minioMinIO 对象存储文件存储
Ollama Search Modelspring-ai-alibaba-starter-tool-calling-ollamasearchmodelOllama 搜索模型本地模型搜索
OpenAlexspring-ai-alibaba-starter-tool-calling-openalexOpenAlex 学术学术数据
OpenTripMapspring-ai-alibaba-starter-tool-calling-opentripmapOpenTripMap旅游信息
Pythonspring-ai-alibaba-starter-tool-calling-pythonPython 代码执行代码执行
正则表达式spring-ai-alibaba-starter-tool-calling-regex正则表达式处理文本匹配
Searchesspring-ai-alibaba-starter-tool-calling-searches搜索工具集合多搜索引擎
敏感词过滤spring-ai-alibaba-starter-tool-calling-sensitivefilter敏感词过滤内容审核
SerpAPIspring-ai-alibaba-starter-tool-calling-serpapiSerpAPI搜索引擎结果
新浪新闻spring-ai-alibaba-starter-tool-calling-sinanews新浪新闻 API新闻检索
Tavily Searchspring-ai-alibaba-starter-tool-calling-tavilysearchTavily 搜索AI 搜索
腾讯地图spring-ai-alibaba-starter-tool-calling-tencentmap腾讯地图 API地理位置服务
时间工具spring-ai-alibaba-starter-tool-calling-time时间处理时间操作
头条新闻spring-ai-alibaba-starter-tool-calling-toutiaonews头条新闻 API新闻检索
TripAdvisorspring-ai-alibaba-starter-tool-calling-tripadvisorTripAdvisor API旅游信息
Tusharespring-ai-alibaba-starter-tool-calling-tushareTushare 金融数据金融数据
天气spring-ai-alibaba-starter-tool-calling-weather天气 API天气查询
Wikipediaspring-ai-alibaba-starter-tool-calling-wikipediaWikipedia API百科查询
世界银行数据spring-ai-alibaba-starter-tool-calling-worldbankdata世界银行数据经济数据
有道翻译spring-ai-alibaba-starter-tool-calling-youdaotranslate有道翻译 API文本翻译
语雀spring-ai-alibaba-starter-tool-calling-yuque语雀 API文档管理

使用说明

所有 Tool Calling 实现都遵循相同的使用模式:

  1. 添加依赖:在 pom.xmlbuild.gradle 中添加相应的依赖
  2. 自动配置:大多数工具都支持 Spring Boot 自动配置,会自动注册为 ToolCallback
  3. 在 ChatClient 中使用:通过 ChatClient.builder().tools(...) 添加工具
  4. AI 模型调用:AI 模型会根据对话内容自动决定是否调用工具

通用接口

所有 Tool 都实现了 BiFunction<Request, ToolContext, String> 接口,并通过 FunctionToolCallback 包装为 ToolCallback

public interface ToolCallback {
String getName();
String getDescription();
ToolResponse call(ToolRequest request);
}

配置说明

大多数工具都支持通过 Spring Boot 配置属性进行配置:

spring:
ai:
alibaba:
tool-name:
enabled: true # 启用/禁用工具
# 其他工具特定配置

认证和 API Key

许多工具需要 API Key 或认证信息:

  • 搜索引擎:通常需要 API Key
  • 翻译服务:需要服务提供商的 API Key
  • 地图服务:需要地图服务商的 Key
  • 其他服务:根据具体服务要求配置

请参考各工具的文档了解具体的配置方法。

最佳实践

  1. 工具选择:根据实际需求选择合适的工具,避免添加不必要的工具
  2. 错误处理:工具调用可能失败,确保有适当的错误处理机制
  3. 性能优化:对于频繁调用的工具,考虑缓存结果
  4. 安全性:确保 API Key 等敏感信息的安全存储
  5. 成本控制:某些工具(如搜索、翻译)可能产生费用,注意使用量

更多信息

  • 每个工具的具体使用方法和配置选项,请参考各模块的文档
  • 对于需要认证的工具,请确保正确配置访问凭证
  • 某些工具可能需要额外的依赖或运行时环境
  • 建议查看 Spring AI 官方文档了解 Tool Calling 的更多细节

注意:本文档基于当前可用的实现。随着项目的发展,可能会有新的 Tool 实现添加或现有实现的更新。建议定期查看项目文档以获取最新信息。

Spring AI Alibaba 开源项目基于 Spring AI 构建,是阿里云通义系列模型及服务在 Java AI 应用开发领域的最佳实践,提供高层次的 AI API 抽象与云原生基础设施集成方案,帮助开发者快速构建 AI 应用。