在生成模型回應或建構智慧體時,你可以透過內建工具、函式呼叫、以程式呼叫工具、工具搜尋和遠端 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 時,請參閱整合與可觀測性。