本文详解大模型函数调用(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,并能用代码将大模型和业务衔接。

  • 大模型 AI 能干什么?
  • 大模型是怎样获得「智能」的?
  • 用好 AI 的核心心法
  • 大模型应用业务架构
  • 大模型应用技术架构
  • 代码示例:向 GPT-3.5 灌入新知识
  • 提示工程的意义和核心思想
  • Prompt 典型构成
  • 指令调优方法论
  • 思维链和思维树
  • Prompt 攻击和防范

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

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

  • 为什么要做 RAG
  • 搭建一个简单的 ChatPDF
  • 检索的基础概念
  • 什么是向量表示(Embeddings)
  • 向量数据库与向量检索
  • 基于向量检索的 RAG
  • 搭建 RAG 系统的扩展知识
  • 混合检索与 RAG-Fusion 简介
  • 向量模型本地部署

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

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

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

  • 为什么要做 RAG
  • 什么是模型
  • 什么是模型训练
  • 求解器 & 损失函数简介
  • 小实验2:手写一个简单的神经网络并训练它
  • 什么是训练/预训练/微调/轻量化微调
  • Transformer结构简介
  • 轻量化微调
  • 实验数据集的构建

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

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

  • 硬件选型
  • 带你了解全球大模型
  • 使用国产大模型服务
  • 搭建 OpenAI 代理
  • 热身:基于阿里云 PAI 部署 Stable Diffusion
  • 在本地计算机运行大模型
  • 大模型的私有化部署
  • 基于 vLLM 部署大模型
  • 案例:如何优雅地在阿里云私有部署开源大模型
  • 部署一套开源 LLM 项目
  • 内容安全
  • 互联网信息服务算法备案

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

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

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

在这里插入图片描述

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐