在生成模型响应或构建智能体时,您可以使用内置工具、函数调用、编程式工具调用、工具搜索和远程 MCP 服务器来扩展模型的能力。模型可以通过这些方式搜索网页、从您的文件中检索信息、在运行时按需加载工具定义、调用您自己的函数、使用 JavaScript 组合工具调用,或访问第三方服务。只有 gpt-5.4 及后续模型支持 tool_search。
请根据您的运行环境选择集成方式。您可以在 Responses API 请求、Agents API 智能体或 Agents SDK 定义中配置工具。可用工具、配置方式以及处理工具调用的方式都取决于所选的集成方式。以下示例使用 Responses API。
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-6.2-astra",
tools: [{ type: "web_search" }],
input: "What was a positive news story from today?",
});
console.log(response.output_text);import OpenAI from "openai";
const openai = new OpenAI();
const response = await openai.responses.create({
model: "gpt-6.2-astra",
input: "What is deep research by OpenAI?",
tools: [
{
type: "file_search",
vector_store_ids: ["<vector_store_id>"],
},
],
});
console.log(response);import OpenAI from "openai";
const client = new OpenAI();
const crmNamespace: OpenAI.Responses.NamespaceTool = {
type: "namespace",
name: "crm",
description: "CRM tools for customer lookup and order management.",
tools: [
{
type: "function",
name: "get_customer_profile",
description: "Fetch a customer profile by customer ID.",
parameters: {
type: "object",
properties: {
customer_id: { type: "string" },
},
required: ["customer_id"],
additionalProperties: false,
},
},
{
type: "function",
name: "list_open_orders",
description: "List open orders for a customer ID.",
defer_loading: true,
parameters: {
type: "object",
properties: {
customer_id: { type: "string" },
},
required: ["customer_id"],
additionalProperties: false,
},
},
],
};
const response = await client.responses.create({
model: "gpt-6.2-astra",
input: "List open orders for customer CUST-12345.",
tools: [crmNamespace, { type: "tool_search" }],
parallel_tool_calls: false,
});
console.log(response.output);import OpenAI from "openai";
const client = new OpenAI();
const tools: OpenAI.Responses.Tool[] = [
{
type: "function",
name: "get_weather",
description: "Get current temperature for a given location.",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "City and country e.g. Bogotá, Colombia",
},
},
required: ["location"],
additionalProperties: false,
},
strict: true,
},
];
const response = await client.responses.create({
model: "gpt-6.2-astra",
input: [
{ role: "user", content: "What is the weather like in Paris today?" },
],
tools,
});
console.log(response.output[0]);curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-6.2-astra",
"tools": [
{
"type": "mcp",
"server_label": "openai_docs",
"server_description": "Search and read the public OpenAI documentation.",
"server_url": "https://developers.openai.com/mcp",
"require_approval": "never"
}
],
"input": "Search the OpenAI docs for Responses API streaming and return the relevant links."
}'可用工具
下列工具均可在 OpenAI 平台上使用。选择工具即可查看详细用法。
调用自定义代码,让模型能够访问更多数据并使用更多功能。
让模型在生成响应时使用互联网上的数据。
通过模型上下文协议(MCP)服务器,让模型能够使用新功能。
在托管式 Shell 环境中上传和复用带版本号的技能包。
在托管容器或您自己的本地运行环境中运行 Shell 命令。
创建智能体工作流,让模型能够操作计算机界面。
使用 GPT Image 生成或编辑图像。
在生成响应时搜索已上传的文件,从中获取相关上下文。
将相关工具动态加载到模型的上下文中,以优化 Token 用量。
让模型编写并运行 JavaScript 代码来编排工具调用。
在 API 中使用工具
发送请求以生成模型响应时,您通常需要在 tools 参数中指定工具配置,以便模型使用这些工具。每种工具的配置要求各不相同,详细说明请参阅可用工具一节。
模型会根据您提供的提示词自动决定是否使用已配置的工具。例如,如果您的提示词询问的是模型训练数据截止日期之后的信息,并且您已启用网页搜索,模型通常会调用网页搜索工具来检索相关的最新信息。
某些高级工作流还可以在交互过程中加载更多工具定义。例如,使用工具搜索可以推迟加载函数定义,直到模型判断需要这些函数时再加载。
要明确控制或引导模型选择工具,您可以设置 tool_choice 参数,该参数位于 API 请求中。
Agents API
Agents API 会为您运行智能体循环。请在 agent.tools 中配置工具,在您的应用中处理函数调用,并在工具需要执行环境时连接沙盒。
要调用应用代码,请参阅函数。要连接工具服务器,请参阅 MCP 连接。如果工具需要执行环境,请参阅沙盒配置。编程式工具调用默认启用。系统会在沙盒的能力目录中查找技能。
在 Agents SDK 中使用工具
在 Agents SDK 中,工具的行为与在 Responses API 中相同。区别在于,您需要在定义智能体和设计工作流时接入工具,而在 Responses API 中,您只需在单个请求中配置工具。
- 如果某个专用智能体需要自行调用托管工具、函数工具或托管 MCP 工具,请直接将这些工具添加到该智能体上。
- 如果需要由管理智能体负责向用户回复,请将专用智能体作为工具提供给管理智能体调用。
- 即使 SDK 对工具调用决策进行了建模,您仍需在自己的运行环境中保留 Shell、应用补丁和计算机使用工具的执行框架。
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.`;
},
});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 提供,请参阅集成与可观测性。