前置知识
阅读本文前,建议先了解:
- 01-Function_Call 的定义与作用 - 基本概念
- 02-Function_Call 的工作原理 - 工作流程
一、主流平台的Tool/Function Call支持
主流LLM平台都支持Tool调用,但具体名称和实现略有不同:
| 平台 | 功能名称 | 常见叫法 |
|---|---|---|
| OpenAI | Function Calling | tools, function_call |
| Anthropic | Tool Use | tools, tool_use |
| Function Calling | tools, function_calling | |
| 国内 | 函数调用 | tools, function_call |
二、实现示例:OpenAI API
2.1 定义函数(Tool Schema)
# 定义我们想让LLM调用的工具
tools = [
{
"name": "get_weather",
"description": "获取指定城市的天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称"
}
},
"required": ["city"]
}
}
]2.2 发送请求
# 把工具列表传给API的 functions 参数
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "user", "content": "北京今天天气怎么样?"}
],
functions=tools # ← functions是API参数名,tools是我们定义的变量名
)命名解释
tools或functions:我们定义的变量名(可以自己改)functions=:OpenAI API规定的参数名(不能改)
2.3 处理响应
# 检查是否需要调用函数
if response.choices[0].message.function_call:
# 获取函数名和参数
function_name = response.choices[0].message.function_call.name
arguments = response.choices[0].message.function_call.arguments
# 执行函数
if function_name == "get_weather":
result = get_weather(city=arguments["city"])
# 将结果返回给LLM,获取最终回答
final_response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "user", "content": "北京今天天气怎么样?"},
{"role": "assistant", "function_call": {...}},
{"role": "function", "name": "get_weather", "content": result}
]
)三、常见的Function Call应用场景
3.1 天气查询
# 函数定义
{
"name": "get_weather",
"parameters": {
"city": "string" // 城市
}
}
# 使用场景
用户:今天出门要带伞吗?
LLM:调用 get_weather → 返回有雨 → 建议带伞3.2 日历管理
# 函数定义
{
"name": "create_event",
"parameters": {
"title": "string", // 事件标题
"time": "string", // 时间
"duration": "number" // 时长(分钟)
}
}
# 使用场景
用户:帮我安排下周三下午2点的会议
LLM:调用 create_event → 创建会议 → 确认成功3.3 搜索与问答
# 函数定义
{
"name": "search_knowledge",
"parameters": {
"query": "string" // 搜索关键词
}
}
# 使用场景
用户:我们公司关于年假的规定是什么?
LLM:调用 search_knowledge → 搜索内部文档 → 返回规定3.4 数据处理
# 函数定义
{
"name": "calculate",
"parameters": {
"expression": "string" // 数学表达式
}
}
# 使用场景
用户:帮我算一下 123 * 456
LLM:调用 calculate → 返回 56088四、设计好的Function Schema
4.1 描述要清晰
❌ "查天气"
✅ "获取指定城市的天气信息,包括温度、湿度、风力等"
4.2 参数说明要详细
❌ "city: string"
✅ "city: string, 城市名称,如北京、上海,必须使用官方全称"
4.3 合理设置必填参数
只把必要的参数设为 required
可选参数不要设为 required,否则LLM可能无法调用
五、Function Call的局限性
5.1 需要预先定义函数
LLM只能调用你提供的函数,无法自己创造。
❌ 用户:帮我执行一个计算任务
→ 没有定义相关函数,无法执行
5.2 函数执行是外部的
LLM只生成参数,实际执行由外部代码完成。
LLM:get_weather(city="北京")
实际执行:你的Python代码调用天气API
5.3 可能产生无效调用
LLM可能生成不符合Schema的参数。
预期参数:city (string)
LLM生成:{"city": 123} // 数字而不是字符串
六、Function Call vs Agent
很多人会把Function Call和Agent混淆,它们有关系但不同:
| 对比 | Function Call | Agent |
|---|---|---|
| 定义 | 调用预定义函数 | 更复杂的自动化系统 |
| 复杂度 | 单个或少量函数调用 | 多步骤推理+工具选择 |
| 自主性 | 较低(需要预设) | 较高(可以规划) |
关系
Function Call是Agent的基础组件。Agent可以理解为:LLM + Function Call + 推理引擎 + 记忆系统
七、总结
- 主流平台都支持Tool/Function Call(OpenAI、Claude、 Gemini等)
- 实现流程:定义Tool → 调用API → 处理响应 → 执行Tool → 返回结果
- 设计好的Tool Schema很关键
- Tool/Function Call是Agent的基础
八、延伸阅读
- 01-Function_Call 的定义与作用 - 基础概念
- 02-Function_Call 的工作原理 - 工作流程