Function Calling 是大语言模型从"对话"走向"行动"的关键桥梁——让模型能调用外部工具,而非仅生成文本。

核心概念 Link to heading

Function Calling(函数调用)是一种机制:模型不再只返回文本回复,而是返回结构化的工具调用请求,由客户端程序执行后将结果回传给模型,最终由模型生成面向用户的自然语言回复。

三个关键术语:

术语含义
Tool Definition工具定义,描述函数名、参数类型和用途
Tool Call模型返回的工具调用请求,包含函数名和参数值
Tool Result客户端执行函数后返回的结果

核心要点:模型不执行代码,只决定调用哪个工具、传入什么参数。 真正的函数调用发生在你的代码中。

为什么需要 Function Calling Link to heading

传统聊天 API 的本质是"基于训练数据的文本生成"。这意味着它有三个天然局限:

  1. 无法获取实时数据 —— 模型不知道今天的天气
  2. 无法操作外部系统 —— 模型无法帮你发邮件、查数据库
  3. 无法保证输出格式 —— 即使要求 JSON,模型也可能输出错误格式

对比:Prompt 硬编码 vs Function Calling Link to heading

以"用户问明天北京天气"为例。

Prompt 硬编码方式:在 prompt 中让模型输出 JSON,然后你解析。

请根据用户问题返回如下 JSON:
{"action": "get_weather", "city": "...", "date": "..."}

问题:模型可能输出格式错误的 JSON、编造不存在的参数、或者忽略指令直接回答。

Function Calling 方式:定义一个 get_weather 工具,模型返回结构化调用请求。

{
  "tool_calls": [
    {
      "name": "get_weather",
      "arguments": { "city": "北京", "date": "2026-04-20" }
    }
  ]
}

你的代码收到这个请求,调用真实天气 API,将结果回传模型,模型再生成最终回复。参数类型由 schema 保证,模型不会"编造"数据。

适用场景 Link to heading

  • 查询实时信息(天气、股票、库存)
  • 操作外部系统(发邮件、创建工单、更新数据库)
  • 调用第三方 API(翻译、搜索、支付)
  • 多步骤自动化流程

多平台对比 Link to heading

主流 LLM 平台都实现了 Function Calling 机制,名称略有不同。

OpenAI Function Calling Link to heading

{
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "查询指定城市的天气",
        "parameters": {
          "type": "object",
          "properties": {
            "city": { "type": "string" },
            "date": { "type": "string", "format": "date" }
          },
          "required": ["city"]
        }
      }
    }
  ]
}

Anthropic Tool Use Link to heading

{
  "tools": [
    {
      "name": "get_weather",
      "description": "查询指定城市的天气",
      "input_schema": {
        "type": "object",
        "properties": {
          "city": { "type": "string" },
          "date": { "type": "string", "format": "date" }
        },
        "required": ["city"]
      }
    }
  ]
}

Google Gemini Function Calling Link to heading

{
  "tools": [
    {
      "function_declarations": [
        {
          "name": "get_weather",
          "description": "查询指定城市的天气",
          "parameters": {
            "type": "object",
            "properties": {
              "city": { "type": "string" },
              "date": { "type": "string" }
            },
            "required": ["city"]
          }
        }
      ]
    }
  ]
}

共性与差异 Link to heading

特性OpenAIAnthropicGemini
参数名称toolstoolstools
Schema 格式JSON SchemaJSON Schema (略有差异)JSON Schema
工具调用返回tool_calls 数组tool_usefunction_calls
并行调用支持支持支持
强制调用tool_choice 控制tool_choice 控制默认自动选择

共性:定义工具 → 模型决定调用哪个 → 返回参数 → 客户端执行 → 结果回传。三家的核心流程完全一致。

使用流程 Link to heading

一次完整的 Function Calling 调用链路如下:

sequenceDiagram participant U as 用户 participant C as 客户端 participant M as 模型 U->>C: 发送问题 C->>M: 请求 + 工具定义 M-->>C: 返回 Tool Call C->>C: 执行函数 C->>M: 回传 Tool Result M-->>C: 生成最终回复 C-->>U: 返回答案

关键理解:整个流程是一个循环。模型可以在同一轮次调用多个工具(并行),也可以在收到工具结果后继续调用其他工具(链式)。客户端需要循环处理,直到模型不再返回 Tool Call 为止。

注意事项 Link to heading

安全性 Link to heading

模型生成的参数不可信,必须验证。就像处理用户输入一样,校验类型、范围、权限后再执行。

# 错误:直接信任模型参数
result = db.query(**tool_args)

# 正确:先验证
validate_args(tool_args, schema)
if not has_permission(tool_args["action"]):
    raise PermissionError

幂等性 Link to heading

只读操作(查询天气、搜索信息)可以安全重试。写操作(创建订单、删除数据)需要幂等设计或用户确认,防止重复调用造成副作用。

成本控制 Link to heading

每次工具调用都消耗额外的输入和输出 token。高频场景下需评估:是否可以用缓存减少调用、是否可以合并多个工具为一次调用。

官方链接 Link to heading

[1] https://platform.openai.com/docs/guides/function-calling

[2] https://docs.anthropic.com/en/docs/build-with-claude/tool-use

[3] https://ai.google.dev/gemini-api/docs/function-calling

Signature Link to heading

本文由 AI 生成,不保证正确,仅作参考