在生成模型回复或构建智能体时,您可以使用内置工具、函数调用、程序化工具调用、工具搜索和远程 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-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-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 = {
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-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 = [
{
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-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-astra",
"tools": [
{
"type": "mcp",
"server_label": "dmcp",
"server_description": "A Dungeons and Dragons MCP server to assist with dice rolling.",
"server_url": "https://dmcp-server.deno.dev/mcp",
"require_approval": "never"
}
],
"input": "Roll 2d4+1"
}'可用工具
以下是 OpenAI 平台上可用工具的概览。选择其中一项,了解详细的使用指南。
调用自定义代码,让模型能够访问更多数据和能力。
在生成模型回复时使用互联网上的数据。
通过 Model Context Protocol (MCP) 服务器,让模型能够使用新的能力。
在托管式 Shell 环境中上传和复用带有版本的技能包。
在托管容器或您自己的本地运行时中运行 Shell 命令。
创建智能体工作流,让模型能够操作计算机界面。
使用 GPT Image 生成或编辑图像。
在生成回复时搜索已上传文件的内容,获取上下文。
将相关工具动态加载到模型的上下文中,优化 Token 用量。
让模型编写并运行 JavaScript 来编排工具调用。
在 API 中使用
发送生成模型回复的请求时,您通常可以在 tools 参数中指定配置,以启用工具访问。每种工具都有各自的配置要求,详细说明请参阅可用工具部分。
模型会根据提供的提示,自动决定是否使用已配置的工具。例如,如果您的提示要求获取模型训练截止日期之后的信息,且已启用网页搜索,模型通常会调用网页搜索工具来检索相关的最新信息。
一些高级工作流还可以在交互过程中加载更多工具定义。例如,工具搜索可以推迟加载函数定义,直到模型判断需要使用这些函数时再加载。
您可以在 API 请求中设置 tool_choice 参数,明确控制或引导这一行为。
Agents API
Agents API 为您运行智能体循环。请在 agent.tools 中配置工具,在您的应用程序中处理函数调用,并在工具需要执行环境时连接沙盒。
如需调用应用程序代码,请参阅函数;如需连接工具服务器,请参阅 MCP 连接;对于需要执行环境的工具,请参阅沙盒配置。程序化工具调用默认启用。系统通过沙盒的能力目录发现技能。
在 Agents SDK 中使用
在 Agents SDK 中,工具的语义保持不变,但工具的接入配置从单次 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 时,请参阅集成与可观测性。