铸云 AI
进阶指南

函数调用

让模型请求执行特定操作,通过函数调用与外部系统集成

什么是函数调用

函数调用(Function Calling)允许模型在响应中请求执行特定操作。通过定义工具(tools)数组, 模型可以智能判断何时需要调用函数,并返回结构化的函数调用请求。您负责执行这些函数并将结果返回给模型, 从而实现与大模型的有效协作。

  • 模型可以请求调用一个或多个函数
  • 函数调用返回结构化的参数信息
  • 支持跨多个函数调用并行处理
  • 可与流式响应结合使用

基本用法

定义 tools 数组,模型会根据用户请求自动判断是否需要调用函数。

Python 示例
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "获取指定城市的天气",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {
                    "type": "string",
                    "description": "城市名称"
                }
            },
            "required": ["city"]
        }
    }
}]

response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "北京天气怎么样?"}],
    tools=tools
)

# 处理 tool_calls 返回
if response.choices[0].message.tool_calls:
    for tool_call in response.choices[0].message.tool_calls:
        print(f"函数: {tool_call.function.name}")
        print(f"参数: {tool_call.function.arguments}")

请求格式

tools 参数包含函数的完整定义,采用 JSON Schema 格式描述参数结构。

tools 参数结构

type

必须为 "function",表示这是一个函数工具定义。

function.name

函数名称,用于在 tool_calls 中标识被调用的函数。

function.description

函数描述,帮助模型理解何时应该调用此函数。

function.parameters

JSON Schema 格式的参数定义,包括参数类型、描述和是否必填。

响应格式

当模型需要调用函数时,响应中包含 tool_calls 数组。

响应示例
{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "tool_calls": [
          {
            "id": "call_abc123",
            "type": "function",
            "function": {
              "name": "get_weather",
              "arguments": "{"city": "北京"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ]
}
  • id - 工具调用的唯一标识符
  • type - 固定为 "function"
  • function.name - 被调用的函数名称
  • function.arguments - JSON 字符串格式的参数
  • finish_reason - "tool_calls" 表示模型请求调用工具

处理工具调用

解析工具调用请求,执行函数后,将结果返回给模型继续对话。

完整流程
# 1. 发送请求并获取响应
response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "北京天气怎么样?"}],
    tools=tools
)

# 2. 解析工具调用
tool_call = response.choices[0].message.tool_calls[0]
function_name = tool_call.function.name
arguments = json.loads(tool_call.function.arguments)

# 3. 执行函数
if function_name == "get_weather":
    weather_data = get_weather(arguments["city"])

# 4. 将结果返回给模型
messages = [
    {"role": "user", "content": "北京天气怎么样?"},
    {"role": "assistant", "tool_calls": response.choices[0].message.tool_calls},
    {"role": "tool", "tool_call_id": tool_call.id, "content": json.dumps(weather_data)}
]

# 5. 继续对话获取最终响应
final_response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=messages
)

多工具调用

模型可以同时请求调用多个工具,支持并行处理提高效率。

多工具示例
# 定义多个工具
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "获取城市天气信息",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {"type": "string"}
                },
                "required": ["city"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "get_time",
            "description": "获取指定城市当前时间",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {"type": "string"}
                },
                "required": ["city"]
            }
        }
    }
]

# 模型可能同时请求调用多个工具
response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "北京和上海现在的天气和时间分别是多少?"}],
    tools=tools
)

# 处理多个工具调用
tool_calls = response.choices[0].message.tool_calls
for tool_call in tool_calls:
    print(f"调用函数: {tool_call.function.name}")
    print(f"参数: {tool_call.function.arguments}")

工具选择策略

通过 tool_choice 参数控制模型选择工具的方式。

auto(默认)

模型自主决定是否调用工具以及调用哪个工具。

required

必须调用至少一个工具,模型选择调用哪个或哪些工具。

specific

强制调用指定函数,通过 { name: '函数名' } 指定。

# auto - 模型自主决定(默认)
response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=messages,
    tools=tools,
    tool_choice="auto"
)

# required - 必须调用工具
response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=messages,
    tools=tools,
    tool_choice="required"
)

# specific - 指定调用某个函数
response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=messages,
    tools=tools,
    tool_choice={"type": "function", "function": {"name": "get_weather"}}
)

下一步

流式响应

了解如何实时获取模型响应,提升用户体验。

结构化输出

让模型返回符合特定格式的标准化输出。