主导航

遗留 API

使用工具

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

在生成模型响应或构建智能体(Agent)时,您可以使用内置工具、函数调用、工具搜索和远程 MCP 服务器来扩展其能力。这些功能使模型能够搜索网络、检索文件内容、在运行时加载延迟的工具定义、调用您自己的函数或访问第三方服务。仅 gpt-5.4 及更高版本的模型支持 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 平台上可用工具的概览——选择其中一个以获取进一步的使用指南。

函数调用 (Function calling)

调用自定义代码,让模型能够访问额外的数据和能力。

网络搜索 (Web search)

在模型响应生成中包含来自互联网的数据。

远程 MCP 服务器 (Remote MCP servers)

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

技能 (Skills)

在托管的 shell 环境中上传并重复使用带版本的技能包。

Shell

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

计算机使用 (Computer use)

创建智能体工作流,使模型能够控制计算机界面。

图像生成 (Image generation)

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

文件搜索 (File search)

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

工具搜索 (Tool search)

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

API 中的使用

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

模型会根据提供的 提示词 (prompt) 自动决定是否使用已配置的工具。例如,如果您的提示词要求获取超出模型训练截止日期后的信息,且已启用网络搜索,模型通常会调用网络搜索工具来检索相关且最新的信息。

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

您可以通过在 API 请求 中设置 tool_choice 参数来显式控制或引导此行为。

Agents SDK 中的使用

在 Agents SDK 中,工具语义保持不变,但其配置逻辑从单一的 Responses API 请求移到了智能体定义和工作流设计中。

  • 当特定专家需要自行调用工具时,可将托管工具、函数工具或托管 MCP 工具直接附加在该专家身上。
  • 当经理(Manager)需要保留对面向用户的回复的控制权时,可将专家暴露为一种工具。
  • 即使 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 时使用 集成与可观测性

© . This website operates independently and is not affiliated with or endorsed by OpenAI, Inc. All brand names, logos, and trademarks are the property of their respective owners.