Function/Tool Calling
@openai: 函数调用为 OpenAI 模型提供了一种强大而灵活的方式,可以与您的代码或外部服务进行交互。通过函数调用,您可以向 Assistant 描述函数,并让其智能地返回需要调用的函数及其参数。@openai
@langchain: 我们将"工具调用"与"函数调用"互换使用。虽然"函数调用"有时指的是单个函数的调用,但我们将所有模型视为可以在每条消息中返回多个工具或函数调用。

工具调用与 ReACT 的区别
| 项目 | ReAct 提示词工程 | Function Calling |
|---|---|---|
| 核心思想 | 用提示词引导模型「推理 + 行动 + 观察反馈」循环 | 明确声明函数签名,模型调用结构化函数 |
| 技术机制 | Prompt 模板控制流程,如:Think -> Act -> Obs | 通过函数描述注册函数接口,模型自动生成调用参数 |
| 环境交互灵活性 | 高:适用于复杂多步任务、agent 类流程 | 中:适合明确定义的单步工具调用 |
| 开发复杂度 | 中:需要精心设计提示词模板与中间状态管理 | 低~中:注册函数较直接,但需要参数与返回值处理 |
| 典型应用场景 | 多步推理、多工具交替使用、信息抽取等复杂场景 | 明确 API 调用、数据库查询、插件系统等 |
| 与 Agent 框架集成度 | 高:常用于 LangChain、AutoGPT 中的推理行为生成 | 高:OpenAI、LangChain、Claude 支持直接注册使用 |
| 优缺点总结 | ✅ 灵活可控 | ✅ 自动结构化调用 |
| ❌ 难以规模化自动解析 | ❌ 不擅长多步控制与反馈处理 |
function calling 核心概念
-
tools: 函数列表
-
function: 单个函数定义
-
function.name: 函数名字
-
function.description: 函数作用描述
-
parameters: 函数形参说明
"tools": [
{
"type": "function",
"function": {
"name": "current_time",
"description": "A tool for getting the current time.",
"parameters": {
"properties": {},
"required": [],
"type": "object"
}
}
},
{
"type": "function",
"function": {
"name": "simple_code",
"description": "A tool for running code and getting the result back. Only native packages are allowed, network/IO operations are disabled. and you must use print() or console.log() to output the result or result will be empty.",
"parameters": {
"properties": {
"code": {
"description": "code to be executed, only native packages are allowed, network/IO operations are disabled.",
"type": "string"
},
"language": {
"description": "language of the code, only "python3" and "javascript" are supported",
"type": "string"
}
},
"required": ["language", "code"],
"type": "object"
}
}
}
]
工具调用的返回格式
-
tool_calls: 工具调用序列,单个或者多个
-
function 函数调用
-
function.name 函数名字
-
function.arguments 函数实参
"message": {
"role": "assistant",
"content": "",
"tool_calls": [
{
"function": {
"name": "simple_code",
"arguments": {
"code": "print(0.9111 ** 3)",
"language": "python3"
}
}
}
]
}
常用工具
-
代码解析器 python node
-
命令解析器 bash
-
文件管理 file directory
-
浏览器控制 browser use
-
外部接口 openapi swagger
-
大模型上下文协议 MCP playwright-mcp
python 工具调用示例
没有工具调用的对话模型

使用工具可以获得更加准确的结果



带有 function call 的请求
{
"model": "qwen3",
"stream": false,
"options": {
"temperature": 0
},
"messages": [
{
"role": "system",
"content": "/no_think\n"
},
{
"role": "user",
"content": "使用python计算 0.9111**3="
}
],
"tools": [
{
"type": "function",
"function": {
"name": "current_time",
"description": "A tool for getting the current time.",
"parameters": {
"properties": {},
"required": [],
"type": "object"
}
}
},
{
"type": "function",
"function": {
"name": "simple_code",
"description": "A tool for running code and getting the result back. Only native packages are allowed, network/IO operations are disabled. and you must use print() or console.log() to output the result or result will be empty.",
"parameters": {
"properties": {
"code": {
"description": "code to be executed, only native packages are allowed, network/IO operations are disabled.",
"type": "string"
},
"language": {
"description": "language of the code, only \"python3\" and \"javascript\" are supported",
"type": "string"
}
},
"required": ["language", "code"],
"type": "object"
}
}
}
]
}
大模型的响应
{
"model": "qwen3",
"created_at": "2025-05-05T16:57:36.016688Z",
"message": {
"role": "assistant",
"content": "",
"tool_calls": [
{
"function": {
"name": "simple_code",
"arguments": {
"code": "print(0.9111 ** 3)",
"language": "python3"
}
}
}
]
},
"done_reason": "stop",
"done": true,
"total_duration": 2037533417,
"load_duration": 34440000,
"prompt_eval_count": 275,
"prompt_eval_duration": 776227166,
"eval_count": 42,
"eval_duration": 1225024292
}
最终请求
{
"model": "qwen3",
"stream": false,
"options": {
"temperature": 0
},
"messages": [
{
"role": "system",
"content": "/no_think\n"
},
{
"role": "user",
"content": "使用python计算 0.9111**3="
},
{
"role": "assistant",
"content": ""
},
{
"role": "tool",
"content": "0.756307034631\n"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "current_time",
"description": "A tool for getting the current time.",
"parameters": {
"properties": {},
"required": [],
"type": "object"
}
}
},
{
"type": "function",
"function": {
"name": "simple_code",
"description": "A tool for running code and getting the result back. Only native packages are allowed, network/IO operations are disabled. and you must use print() or console.log() to output the result or result will be empty.",
"parameters": {
"properties": {
"code": {
"description": "code to be executed, only native packages are allowed, network/IO operations are disabled.",
"type": "string"
},
"language": {
"description": "language of the code, only \"python3\" and \"javascript\" are supported",
"type": "string"
}
},
"required": ["language", "code"],
"type": "object"
}
}
}
]
}
最终响应
{
"model": "qwen3",
"created_at": "2025-05-05T16:57:37.121945Z",
"message": {
"role": "assistant",
"content": "<think>\n\n</think>\n\n0.9111 raised to the power of 3 is approximately **0.7563**."
},
"done_reason": "stop",
"done": true,
"total_duration": 1030672292,
"load_duration": 11999667,
"prompt_eval_count": 303,
"prompt_eval_duration": 122764292,
"eval_count": 29,
"eval_duration": 892274041
}