Skip to content

GPT 工具调用(Tools)

GPT 系列模型(如 gpt-4.1gpt-4ogpt-4o-mini 等)在本平台支持工具调用(Tools),常见用法包括:

  • 联网搜索:使用内置 web_search_preview 工具从互联网获取最新信息
  • 自定义函数:通过 Function Calling 让模型调用你的业务接口

推荐使用 OpenAI Responses 接口启用内置网络搜索工具,模型会在需要时自动触发搜索,并在回答里返回引用来源(url_citation)。

基础调用示例

curl https://api-cs-al.naci-tech.com/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "tool_choice": "auto",
    "tools": [{ "type": "web_search_preview" }],
    "input": "搜索并总结:2026 年苹果 WWDC 的主要发布内容(给出来源)。"
  }'

常用参数

  • tool_choice:建议使用 auto(让模型自行决定是否要搜索)
  • tools[].type:网络搜索工具为 web_search_preview(部分环境也可能支持带版本后缀的类型)
  • search_context_size:控制搜索上下文规模(low/medium/high

示例(限制上下文规模):

{
  "tools": [
    { "type": "web_search_preview", "search_context_size": "high" }
  ]
}

如何获取引用来源(URL Citation)

当模型使用网络搜索工具时,Responses 的输出通常会包含:

  • 一个 type: "web_search_call" 的输出项(表示已执行搜索)
  • 一个 type: "message" 的输出项,其中 content[].annotations[] 可能包含 url_citation

你可以在响应 JSON 里查找类似结构(字段位置以实际返回为准):

{
  "output": [
    { "type": "web_search_call", "status": "completed" },
    {
      "type": "message",
      "role": "assistant",
      "content": [
        {
          "type": "output_text",
          "text": "……",
          "annotations": [
            {
              "type": "url_citation",
              "url": "https://example.com/...",
              "title": "..."
            }
          ]
        }
      ]
    }
  ]
}

🧩 自定义函数(Function Calling)

当你需要接入内部系统(数据库、订单系统、知识库、业务 API 等),可以通过 OpenAI Chat Completionstools 参数定义函数,模型会返回 tool_calls,由你来执行函数并回传结果。

1)定义函数并让模型触发调用

curl https://api-cs-al.naci-tech.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
      { "role": "user", "content": "帮我查一下杭州今天的天气,并给出出行建议" }
    ],
    "tool_choice": "auto",
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_weather",
          "description": "查询指定城市的天气",
          "parameters": {
            "type": "object",
            "properties": {
              "city": { "type": "string", "description": "城市名,例如 杭州" }
            },
            "required": ["city"]
          }
        }
      }
    ]
  }'

如果模型决定调用函数,会返回 finish_reason: "tool_calls",并在 choices[].message.tool_calls[] 给出函数名与参数(arguments 是 JSON 字符串)。

2)回传函数执行结果,让模型生成最终回答

将你实际执行 get_weather 的结果回传给模型(关键是把 tool_call_id 对上第一步返回的 tool_calls[].id):

curl https://api-cs-al.naci-tech.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
      { "role": "user", "content": "帮我查一下杭州今天的天气,并给出出行建议" },
      {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "call_abc123",
            "type": "function",
            "function": {
              "name": "get_weather",
              "arguments": "{\"city\":\"杭州\"}"
            }
          }
        ]
      },
      {
        "role": "tool",
        "tool_call_id": "call_abc123",
        "content": "{\"temp_c\": 12, \"condition\": \"小雨\", \"wind\": \"东北风3级\"}"
      }
    ]
  }'

🔗 相关文档