嘉义市网站建设_网站建设公司_Banner设计_seo优化
2026/1/16 6:35:04 网站建设 项目流程

本文详解大模型函数调用(Function Calling)技术,包括核心概念、与ReACT的区别、工具定义格式及应用场景。通过Python代码示例展示如何让大模型执行计算任务,获取更准确结果。Function Calling使大模型能与外部服务交互,适用于API调用、数据库查询等场景,是提升AI应用能力的重要技术。


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}

如何系统的学习大模型 AI ?

由于新岗位的生产效率,要优于被取代岗位的生产效率,所以实际上整个社会的生产效率是提升的。

但是具体到个人,只能说是:

“最先掌握AI的人,将会比较晚掌握AI的人有竞争优势”。

这句话,放在计算机、互联网、移动互联网的开局时期,都是一样的道理。

我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。

我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

一直在更新,更多的大模型学习和面试资料已经上传带到CSDN的官方了,有需要的朋友可以扫描下方二维码免费领取【保证100%免费】👇👇

01.大模型风口已至:月薪30K+的AI岗正在批量诞生

2025年大模型应用呈现爆发式增长,根据工信部最新数据:

国内大模型相关岗位缺口达47万

初级工程师平均薪资28K(数据来源:BOSS直聘报告)

70%企业存在"能用模型不会调优"的痛点

真实案例:某二本机械专业学员,通过4个月系统学习,成功拿到某AI医疗公司大模型优化岗offer,薪资直接翻3倍!

02.大模型 AI 学习和面试资料

1️⃣ 提示词工程:把ChatGPT从玩具变成生产工具
2️⃣ RAG系统:让大模型精准输出行业知识
3️⃣ 智能体开发:用AutoGPT打造24小时数字员工

📦熬了三个大夜整理的《AI进化工具包》送你:
✔️ 大厂内部LLM落地手册(含58个真实案例)
✔️ 提示词设计模板库(覆盖12大应用场景)
✔️ 私藏学习路径图(0基础到项目实战仅需90天)





第一阶段(10天):初阶应用

该阶段让大家对大模型 AI有一个最前沿的认识,对大模型 AI 的理解超过 95% 的人,可以在相关讨论时发表高级、不跟风、又接地气的见解,别人只会和 AI 聊天,而你能调教 AI,并能用代码将大模型和业务衔接。

第二阶段(30天):高阶应用

该阶段我们正式进入大模型 AI 进阶实战学习,学会构造私有知识库,扩展 AI 的能力。快速开发一个完整的基于 agent 对话机器人。掌握功能最强的大模型开发框架,抓住最新的技术进展,适合 Python 和 JavaScript 程序员。

第三阶段(30天):模型训练

恭喜你,如果学到这里,你基本可以找到一份大模型 AI相关的工作,自己也能训练 GPT 了!通过微调,训练自己的垂直大模型,能独立训练开源多模态大模型,掌握更多技术方案。

到此为止,大概2个月的时间。你已经成为了一名“AI小子”。那么你还想往下探索吗?

第四阶段(20天):商业闭环

对全球大模型从性能、吞吐量、成本等方面有一定的认知,可以在云端和本地等多种环境下部署大模型,找到适合自己的项目/创业方向,做一名被 AI 武装的产品经理。

学习是一个过程,只要学习就会有挑战。天道酬勤,你越努力,就会成为越优秀的自己。

如果你能在15天内完成所有的任务,那你堪称天才。然而,如果你能完成 60-70% 的内容,你就已经开始具备成为一名大模型 AI 的正确特征了。

这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询