1. 什么是Function Calling(函数调用)?
当用户提出一个问题时,让AI能识别和理解这个问题并且输出一个指令,告诉程序该调用哪个函数、传入什么参数。
2. 为什么需要Function Calling?
因为AI的能力边界。

AI能做AI不能做
回答问题、写代码、翻译获取实时时间
解释概念、推理分析查询天气
生成文本、总结归纳访问数据库
理解意图、规划步骤发送邮件/短信

表格中,AI不能做的部分,就需要开发人员自己写函数,供AI调用实现。

3. tools的JSON Schema格式

3.1 基础结构

json
{
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "函数名",
        "description": "函数描述(AI据此判断何时调用)",
        "parameters": {
          "type": "object",
          "properties": {
            "参数名1": {
              "type": "参数类型",
              "description": "参数描述"
            }
          },
          "required": ["必填参数列表"]
        }
      }
    }
  ]
}

3.2 最简单的工具:get_current_time(无参数)

json
{
  "type": "function",
  "function": {
    "name": "get_current_time",
    "description": "获取当前的日期和时间",
    "parameters": {}   // 没有参数,空对象
  }
}

3.3 带参数的工具:get_weather(有参数)

json
{
  "type": "function",
  "function": {
    "name": "get_weather",
    "description": "查询指定城市的天气",
    "parameters": {
      "type": "object",
      "properties": {
        "city": {
          "type": "string",
          "description": "城市名称,如:北京、上海"
        }
      },
      "required": ["city"]
    }
  }
}

3.4 复杂参数的工具:calculator

json
{
  "type": "function",
  "function": {
    "name": "calculator",
    "description": "计算数学表达式的结果",
    "parameters": {
      "type": "object",
      "properties": {
        "expression": {
          "type": "string",
          "description": "数学表达式,如:1024*256、100+200"
        }
      },
      "required": ["expression"]
    }
  }
}

4. 响应中的tool_calls字段
4.1 响应结构
当AI决定调用工具时,返回的JSON结构如下:

json
{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": null,  // 工具调用时content通常为空
        "tool_calls": [
          {
            "id": "call_abc123",        // 工具调用的唯一ID
            "type": "function",          // 固定为function
            "function": {
              "name": "get_weather",     // 要调用的函数名
              "arguments": "{\"city\": \"北京\"}"  // 参数(JSON字符串)
            }
          }
        ]
      },
      "finish_reason": "tool_calls"      // 表示需要调用工具
    }
  ]
}

4.2 字段说明

字段说明用途
tool_calls[].id本次调用的唯一ID第2次请求时需要原样返回
function.nameAI想要调用的函数名你的代码根据名称执行对应方法
function.arguments参数的JSON字符串解析后传给C#方法
finish_reason“tool_calls”判断是否需要调用工具

5.Function Calling需要几次API请求?
2次,第一次让ai理解问题 ,然后返回需要调用哪个方法;调用方法后,第二次调用AI把方法返回的结果传给AI ,AI组织语言输出答案。即:
第一次:AI理解问题 → 决定调用哪个方法
第二次:把方法结果传给AI → AI组织语言输出答案
Function Calling 的本质就是:你需要提前把所有可能被调用的函数都实现好。

6. 怎么判断是否需要Function Calling?
简单判断标准:
AI大脑里有的知识 → 不需要实现函数
需要获取外部信息或执行操作 → 需要实现函数

7. 实际项目中怎么规划函数?
按需添加,不是一次性写完。80/20法则,先实现高频功能,按需添加。

8. Function Calling vs 直接写代码的区别

方式怎么做适用场景
直接写代码if (用户说"时间") { GetTime(); }命令明确、固定
Function CallingAI判断意图 →判断调用哪个函数意图多样、自然语言
// 直接写代码(要穷举所有说法)
if (用户说.Contains("时间") || 用户说.Contains("几点") || 用户说.Contains("现在"))
{
    GetTime();
}

// Function Calling(AI帮你理解意图)
用户可以说:
- "现在几点了?"
- "能告诉我当前时间吗?"
- "What time is it?"
- "几时了?"
AI都能识别 → 调用 get_current_time
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐