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}说真的,这两年看着身边一个个搞Java、C++、前端、数据、架构的开始卷大模型,挺唏嘘的。大家最开始都是写接口、搞Spring Boot、连数据库、配Redis,稳稳当当过日子。
结果GPT、DeepSeek火了之后,整条线上的人都开始有点慌了,大家都在想:“我是不是要学大模型,不然这饭碗还能保多久?”
我先给出最直接的答案:一定要把现有的技术和大模型结合起来,而不是抛弃你们现有技术!掌握AI能力的Java工程师比纯Java岗要吃香的多。
即使现在裁员、降薪、团队解散的比比皆是……但后续的趋势一定是AI应用落地!大模型方向才是实现职业升级、提升薪资待遇的绝佳机遇!
这绝非空谈。数据说话
2025年的最后一个月,脉脉高聘发布了《2025年度人才迁徙报告》,披露了2025年前10个月的招聘市场现状。
AI领域的人才需求呈现出极为迫切的“井喷”态势
2025年前10个月,新发AI岗位量同比增长543%,9月单月同比增幅超11倍。同时,在薪资方面,AI领域也显著领先。其中,月薪排名前20的高薪岗位平均月薪均超过6万元,而这些席位大部分被AI研发岗占据。
与此相对应,市场为AI人才支付了显著的溢价:算法工程师中,专攻AIGC方向的岗位平均薪资较普通算法工程师高出近18%;产品经理岗位中,AI方向的产品经理薪资也领先约20%。
当你意识到“技术+AI”是个人突围的最佳路径时,整个就业市场的数据也印证了同一个事实:AI大模型正成为高薪机会的最大源头。
最后
我在一线科技企业深耕十二载,见证过太多因技术卡位而跃迁的案例。那些率先拥抱 AI 的同事,早已在效率与薪资上形成代际优势,我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在大模型的学习中的很多困惑。
我整理出这套 AI 大模型突围资料包【允许白嫖】:
✅从入门到精通的全套视频教程
✅AI大模型学习路线图(0基础到项目实战仅需90天)
✅大模型书籍与技术文档PDF
✅各大厂大模型面试题目详解
✅640套AI大模型报告合集
✅大模型入门实战训练
这份完整版的大模型 AI 学习和面试资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】
①从入门到精通的全套视频教程
包含提示词工程、RAG、Agent等技术点
② AI大模型学习路线图(0基础到项目实战仅需90天)
全过程AI大模型学习路线
③学习电子书籍和技术文档
市面上的大模型书籍确实太多了,这些是我精选出来的
④各大厂大模型面试题目详解
⑤640套AI大模型报告合集
⑥大模型入门实战训练
👉获取方式:
有需要的小伙伴,可以保存图片到wx扫描二v码免费领取【保证100%免费】🆓