平台函数

平台函数是托管在服务端、由平台执行的内置工具,当前提供联网搜索 web_search。在请求里声明后,模型决定何时调用,平台执行并把结果回填给模型;你拿到的是调用记录和最终回答。按调用次数计费,可用函数以控制台「函数管理」页为准。

工作方式

  • 声明:请求的 tools 数组里带上平台函数;客户端不需要写执行逻辑,也拿不到函数的原始输出。
  • 发起:模型根据问题决定是否调用、用什么参数,不需要客户端干预。
  • 执行与回填:平台在服务端执行,把搜索结果回填给模型继续生成。
  • 回答:最终回答与调用记录一起返回;流式下先看到调用与进度事件,再看到正文。

托管工具在 Chat Completions 接口上不可用,请用 Responses 或 Messages 接口调用。

Responses 接口

请求

POST /v1/responses
{
  "model": "your-model",
  "input": "查一下杭州 2026年11月初的天气",
  "tools": [{ "type": "web_search" }]
}

返回(节选,output 顺序为推理 → 调用 → 回答)

{
  "id": "resp_…", "status": "completed",
  "output": [
    { "type": "reasoning", … },
    {
      "type": "web_search_call",
      "id": "fc_…",
      "status": "completed",
      "action": {
        "query": "杭州 2026年11月初 天气",
        "count": 10,
        "summary": true
      }
    },
    { "type": "message", "content": [ { "type": "output_text", "text": "…" } ] }
  ],
  "usage": { … }
}

流式(节选,进度事件按工具类型动态命名)

event: response.output_item.added
data: { "item": { "type": "web_search_call", "id": "fc_…", "status": "in_progress" } }

event: response.web_search_call.searching
data: { "item_id": "fc_…", "output_index": 1 }

进度事件用 item_id / output_index 与同一条 web_search_call 条目对应,结束时该条目状态转为 completed,随后照常收到 response.completed。平台扩展字段 max_tool_calls 可以限制单次请求的托管工具调用预算,见Responses 兼容说明。

Messages 接口

请求

POST /v1/messages
{
  "model": "your-model",
  "max_tokens": 3000,
  "messages": [{ "role": "user", "content": "查一下杭州 2026年11月初的天气" }],
  "tools": [{ "type": "web_search", "name": "web_search" }]
}

返回(节选,content 顺序为推理 → 调用 → 回答)

{
  "id": "msg_…", "stop_reason": "end_turn",
  "content": [
    { "type": "thinking", "thinking": "…" },
    {
      "type": "server_tool_use",
      "id": "srvtoolu_…",
      "name": "web_search",
      "input": { "query": "杭州 2026年11月初 天气", "count": 10, "summary": true }
    },
    { "type": "text", "text": "…" }
  ],
  "usage": { … }
}

thinking 块仅推理模型返回。平台不返回 web_search_tool_result 结果块——搜索结果只回填给模型;历史消息里的 web_search_tool_result 块在输入里被接受,便于把官方会话历史搬过来。流式按块推送 content_block_start / content_block_stop,与官方一致。

type 不带日期后缀

官方的写法是版本化 type(web_search_20250305)。平台不识别带日期后缀的写法——这样的工具声明会被丢弃,模型根本收不到,表现为模型回答「没有联网搜索工具」。type 必须写 web_search。

参数与行为

  • 调用参数由模型生成,不是客户端指定:query 是搜索词,count 是结果条数,summary 表示以摘要回填。一次请求可以发起多次调用,各自成条。
  • 声明里只写 type(Messages 另加 name);官方的 max_uses、allowed_domains 等配置字段平台不消费。
  • 个别模型上托管工具按普通函数模拟,模型看到的是 __web_search__ 这样的函数名,返回形状不变,见Responses 兼容说明。
  • 调用预算用请求顶层的 max_tool_calls 控制,Responses 与 Messages 同一种写法,请求样例见下。

请求(Responses,顶层带 max_tool_calls)

{
  "model": "your-model",
  "input": "查一下今天北京的天气",
  "tools": [{ "type": "web_search" }],
  "max_tool_calls": 5
}

请求(Messages,同一种写法)

{
  "model": "your-model",
  "messages": [{ "role": "user", "content": "查一下今天北京的天气" }],
  "tools": [{ "type": "web_search", "name": "web_search" }],
  "max_tool_calls": 5
}

两个接口都在请求顶层写 max_tool_calls。它是平台扩展字段,由平台自己消费,不发给模型;取值须为 ≥ 1 的整数,不写时默认 8。预算按已执行的调用次数计:一轮并行发起的多个调用整批执行,可能一次花掉甚至超出剩余预算;预算用尽后的下一轮由平台强制收尾作答,强制收尾仍发起新调用则请求失败。详细说明见Responses 与Messages 兼容说明。

计费与用量

  • 每次调用按函数价格计费,规则见计费规则;次数预算用 max_tool_calls 控制(见上文参数与行为)。
  • 函数按授权模型开放,配置在控制台完成。
  • 每次调用在函数用量里逐条记录:函数名、输入摘要、耗时与成败,见用量与账单。