English
主导航

旧版 API

使用工具

使用远程 MCP 服务器或网络搜索等工具来扩展模型的能力。

在生成模型响应或构建智能体时,您可以使用内置工具、函数调用、工具搜索和远程 MCP 服务器来扩展其能力。这些工具使模型能够搜索网络、从您的文件中进行检索、在运行时加载延迟的工具定义、调用您自己的函数或访问第三方服务。仅限 gpt-5.4 and later models support tool_search.

为模型响应加入网络搜索结果
1
2
3
4
5
6
7
8
9
10
11
12
import OpenAI from "openai";
const client = new OpenAI();

const response = await client.responses.create({
    model: "gpt-5.5",
    tools: [
        { type: "web_search" },
    ],
    input: "What was a positive news story from today?",
});

console.log(response.output_text);

可用工具

以下是 OpenAI 平台中可用工具的概览——选择其中一个以获取更多使用指南。

函数调用

调用自定义代码,赋予模型访问额外数据与功能的能力。

网络搜索

在模型响应生成过程中引入互联网数据。

远程 MCP 服务器

通过模型上下文协议 (MCP) 服务器为模型赋予新能力。

技能

在托管的 Shell 环境中上传并复用版本化的技能包。

Shell

在托管的容器或您自己的本地运行时中执行 Shell 命令。

计算机使用

创建支持模型控制计算机界面的智能体工作流。

图像生成

使用 GPT Image 生成或编辑图像。

文件搜索

在生成响应时搜索已上传文件的内容以提供上下文。

工具搜索

动态加载相关工具到模型上下文中,以优化 token 使用。

在 API 中的使用

当发起请求生成 模型响应,你通常通过在以下位置指定配置来启用工具访问: tools 参数。每个工具都有其独特的配置要求——详情请参阅 可用工具 部分以获取详细说明。

基于提供的 提示词,模型会自动决定是否使用已配置的工具。例如,如果你的提示请求超出模型训练截止日期的信息且已启用网络搜索,模型通常会调用网络搜索工具以检索相关的最新信息。

一些高级工作流也可以在交互过程中加载更多工具定义。例如, 工具搜索 可以延迟函数定义,直到模型确定需要它们时才加载。

你可以通过设置 tool_choice 参数 in the API request.

在 Agents SDK 中的使用

在 Agents SDK 中,工具语义保持不变,但连接逻辑转移到了 Agent 定义和工作流设计中,而不是单一的 Responses API 请求。

  • 当需要某个专家自行调用工具时,直接在 Agent 上附加托管工具、函数工具或托管 MCP 工具。
  • 当管理器需要控制面向用户的回复时,将专家作为工具暴露。
  • 即使 SDK 建模了工具决策,也要在运行时保留 shell、应用补丁和计算机使用工具链。
将本地逻辑包装为函数工具
1
2
3
4
5
6
7
8
9
10
11
import { tool } from "@openai/agents";
import { z } from "zod";

const getWeatherTool = tool({
  name: "get_weather",
  description: "Get the weather for a given city.",
  parameters: z.object({ city: z.string() }),
  async execute({ city }) {
    return `The weather in ${city} is sunny.`;
  },
});
将专家作为工具暴露
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { Agent } from "@openai/agents";

const summarizer = new Agent({
  name: "Summarizer",
  instructions: "Generate a concise summary of the supplied text.",
});

const mainAgent = new Agent({
  name: "Research assistant",
  tools: [
    summarizer.asTool({
      toolName: "summarize_text",
      toolDescription: "Generate a concise summary of the supplied text.",
    }),
  ],
});

使用 智能体定义 当你正在构建单个专家时, 编排与交接 当工具影响所有权时, 护栏与人工审查 当工具影响审批时,以及 集成与可观测性 当功能来自 MCP 时。