產生模型回應或建構智慧體時,你可以透過內建工具、函式呼叫、程式化工具呼叫、工具搜尋和遠端 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: 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-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-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": "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 API 會從沙盒的能力目錄中找出可用的技能。
在 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 提供,請參閱整合與可觀測性。