For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
主要導覽

網頁搜尋

讓模型在產生回應前搜尋網頁,取得最新資訊。

網頁搜尋讓模型能從網際網路取得最新資訊,並提供附有來源引用的答案。若要啟用此功能,請使用 Responses API 中的網頁搜尋工具;在某些情況下,也可以使用 Chat Completions。

OpenAI 模型主要提供三種網頁搜尋方式:

  1. 非推理網頁搜尋:非推理模型會將使用者的查詢傳送至網頁搜尋工具,由工具根據排名靠前的結果傳回回應。模型不會進行內部規劃,只會轉傳搜尋工具的回應。這種方式速度快,適合快速查詢。
  2. 使用推理模型的智慧體式搜尋,由模型主動管理搜尋流程。模型可以在思路鏈中執行網頁搜尋、分析結果,並決定是否繼續搜尋。這種彈性讓智慧體式搜尋非常適合複雜的工作流程,但也表示搜尋所需時間比快速查詢更長。例如,你可以調整 gpt-5.5 等模型的推理程度,同時改變搜尋深度與延遲。
  3. 深度研究是一種由智慧體驅動的專門方法,讓推理模型進行深入且長時間的調查。模型會在思路鏈中執行網頁搜尋,通常會查閱數百個來源。深度研究可能需要執行數分鐘,最適合搭配背景模式使用。請使用 gpt-5.5,並將推理設定為 highxhigh

選擇整合方式

使用案例建議方式備註
新的網頁搜尋整合Responses API 搭配 web_searchgpt-5.5支援託管網頁搜尋的控制功能,例如篩選器、來源、即時存取控制,以及執行更長時間的研究
現有的 Chat Completions 搜尋整合Chat Completions 搭配 gpt-5-search-api僅在需要保留 Chat Completions 整合時使用
多步驟研究或需要長時間執行的報告生成使用 gpt-5.5,並將推理設定為 highxhigh對於可能需要數分鐘才能產生的報告,請使用背景模式

使用 Responses API 時,你可以在用於產生內容的 API 請求中,於 tools 陣列內設定網頁搜尋以啟用此功能。與其他工具一樣,模型可以根據輸入提示詞的內容,選擇是否搜尋網頁。

新的 Responses API 整合請使用 { "type": "web_search" }。較早的 web_search_preview 工具仍可供舊版整合使用,但不支援 filtersexternal_web_accessreturn_token_budget 等較新的控制功能。

網頁搜尋工具範例
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);

輸出與引用

使用網頁搜尋工具的模型回應會包含兩個部分:

  • 一個 web_search_call 輸出項目,包含搜尋呼叫的 ID,並在 web_search_call.action 中記錄執行的動作。動作為下列其中一種:
    • search,代表網頁搜尋。通常會包含用於搜尋的查詢 queries,但並非每次都會包含。搜尋動作會產生工具呼叫費用(請參閱定價)。
    • open_page,代表開啟頁面。推理模型支援此動作。
    • find_in_page,代表在頁面內搜尋。推理模型支援此動作。
  • 一個 message 輸出項目,包含:
    • message.content[0].text 中的文字結果
    • message.content[0].annotations 中所引用 URL 的註解

預設情況下,模型回應會針對網頁搜尋結果中的 URL 加入行內引用。此外,url_citation 註解物件會包含所引用來源的 URL、標題和位置。

向終端使用者顯示網頁搜尋結果或其中的資訊時,必須在使用者介面中清楚呈現行內引用,並讓使用者可以點選。

[
  {
    "type": "web_search_call",
    "id": "ws_67c9fa0502748190b7dd390736892e100be649c1a5ff9609",
    "status": "completed",
    "action": {
      "type": "search",
      "query": "latest news about AI"
    }
  },
  {
    "id": "msg_67c9fa077e288190af08fdffda2e34f20be649c1a5ff9609",
    "type": "message",
    "status": "completed",
    "role": "assistant",
    "content": [
      {
        "type": "output_text",
        "text": "On March 6, 2025, several news...",
        "annotations": [
          {
            "type": "url_citation",
            "start_index": 2606,
            "end_index": 2758,
            "url": "https://...",
            "title": "Title..."
          }
        ]
      }
    ]
  }
]
如果你使用的是建議方式備註
Responses 中的 web_search_preview遷移至 web_searchweb_search 支援 filtersexternal_web_accessreturn_token_budget 等較新的控制功能
gpt-4o-search-previewgpt-4o-mini-search-preview遷移至 Responses 的 web_search;如果必須繼續使用 Chat Completions,則改用 gpt-5-search-api預覽版搜尋模型已棄用,並於 2026-07-23 停止服務
Chat Completions 搜尋整合使用 gpt-5-search-api,或遷移至 Responses 的 web_search,以取得更多工具控制功能,並可選擇是否搜尋Chat Completions 搜尋模型一律會先搜尋再回應;Responses 的搜尋則是一項工具

搜尋上下文大小

search_context_size 控制模型在產生回應前,能取得多少來自網頁搜尋結果的上下文。簡單查詢請使用 low;若要採用兼顧各方面的預設設定,請使用 medium;當答案可能需要搜尋結果中的更多細節時,請使用 high。這項設定不會指定確切的 Token 數,也不保證特定數量的來源或引用。

設定搜尋上下文大小
import OpenAI from "openai";
const openai = new OpenAI();

const response = await openai.responses.create({
  model: "gpt-6-astra",
  tools: [
    {
      type: "web_search",
      search_context_size: "low",
    },
  ],
  input: "What movie won best picture in 2025?",
});
console.log(response.output_text);

執行更長時間的網頁研究

return_token_budget 控制在 Responses API 使用 GPT-5+ 推理模型執行搜尋時,工具可回傳多少網頁搜尋結果內容。大多數請求應保留預設值。只有在研究或評估需要投入大量推理、檢視許多頁面,且可能因標準回傳 Token 上限而提前停止時,才將其設為 unlimited

請視需要使用 unlimited,因為這可能增加延遲與成本。對於需要長時間執行多次搜尋的任務,請使用背景模式(background: true),讓請求以非同步方式持續執行,之後再擷取最終回應。

行為
default使用網頁搜尋結果的標準回傳 Token 額度。這與省略 return_token_budget 時的行為相同。
unlimited移除這次網頁搜尋執行的預設回傳 Token 額度限制。

此參數僅適用於透過 Responses API 託管的 web_search 工具,使用 GPT-5+ 進行推理式網頁搜尋的情況。它不會變更搜尋上下文視窗,也不適用於非推理式網頁搜尋、舊版 Search API 整合途徑、容器網頁搜尋、Chat Completions 搜尋模型或 web_search_preview。支援的值只有 defaultunlimitednull、數字及其他字串都會遭到拒絕。

執行更長時間的網頁搜尋
curl "https://api.openai.com/v1/responses" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-6-astra",
    "reasoning": { "effort": "xhigh" },
    "tools": [
      {
        "type": "web_search",
        "return_token_budget": "unlimited"
      }
    ],
    "input": "Research the economic impact of semaglutide on global healthcare systems.\n\nDo:\n- Include specific figures, trends, statistics, and measurable outcomes.\n- Prioritize reliable, up-to-date sources: peer-reviewed research, health organizations (e.g., WHO, CDC), regulatory agencies, or pharmaceutical earnings reports.\n- Include inline citations and return all source metadata.\n\nBe analytical, avoid generalities, and ensure that each section supports data-backed reasoning that could inform healthcare policy or financial modeling."
  }'

網域篩選

網頁搜尋的網域篩選功能可將結果限制在一組指定網域內。透過 filters 參數,你可以設定最多 100 個 allowed_domains 或最多 100 個 blocked_domains。填寫網域時,請省略 HTTP 或 HTTPS 前綴。例如,使用 openai.com,而非 https://openai.com/。搜尋範圍也會包含這些網域的子網域。請注意,網域篩選僅適用於 Responses API 的 web_search 工具。

來源

若要查看網頁搜尋期間擷取的所有 URL,請使用 sources 欄位。內文引用僅顯示最相關的參考資料,而 sources 會回傳模型在形成回應時參考過的完整 URL 清單。 來源數量通常多於引用數量。第三方即時資料來源也會顯示在這裡,並標示為 oai-sportsoai-weatheroai-financeweb_searchweb_search_preview 工具皆提供 sources 欄位。

列出來源
curl "https://api.openai.com/v1/responses" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-6-astra",
    "reasoning": { "effort": "low" },
    "tools": [
      {
        "type": "web_search",
        "filters": {
          "allowed_domains": [
            "pubmed.ncbi.nlm.nih.gov",
            "clinicaltrials.gov",
            "www.who.int",
            "www.cdc.gov",
            "www.fda.gov"
          ],
          "blocked_domains": [
            "reddit.com",
            "quora.com",
            "wikipedia.org"
          ]
        }
      }
    ],
    "tool_choice": "auto",
    "include": ["web_search_call.action.sources"],
    "input": "Please perform a web search on how semaglutide is used in the treatment of diabetes."
  }'

圖片搜尋結果

網頁搜尋除了回傳一般文字結果,也能回傳圖片結果。當應用程式需要最新或有網頁來源依據的視覺素材,例如產品照片、地標、地點、活動或視覺參考資料時,可使用圖片搜尋。

若要使用圖片搜尋,請將 search_content_types 設為包含 image。如果也需要輔助文字結果,協助模型摘要、排序或解釋擷取的圖片,請加入 text

使用 image_settings 控制圖片專屬行為:

  • max_results:要求回傳的圖片結果數量,須為正數。
  • caption:要求在有簡短圖片說明時一併提供。

若要檢視原始圖片結果,請在請求中包含 web_search_call.results,並從回應中讀取 web_search_call.results[]。圖片結果會與助理訊息分開回傳,因此當應用程式需要 URL 或中繼資料時,請直接解析 web_search_call 項目。

搜尋圖片
import OpenAI from "openai";
const client = new OpenAI();

const response = await client.responses.create({
  model: "gpt-6-astra",
  reasoning: { effort: "low" },
  tools: [
    {
      type: "web_search",
      search_content_types: ["image", "text"],
      image_settings: {
        max_results: 3,
        caption: true,
      },
    },
  ],
  include: ["web_search_call.results"],
  input:
    "Search for recent images and supporting text sources about the Golden Gate Bridge at sunset.",
});

console.log(response.output);

每個 image_result 都包含:

  • image_url:該結果的標準圖片 URL。
  • source_website_url:找到該圖片的頁面。
  • thumbnail_url:縮圖 URL(若有)。
  • caption:簡短圖說或描述(若有)。
{
  "output": [
    {
      "type": "web_search_call",
      "status": "completed",
      "results": [
        {
          "type": "image_result",
          "image_url": "https://cdn.example/golden-gate-sunset.jpg",
          "thumbnail_url": "https://cdn.example/golden-gate-sunset-thumb.jpg",
          "source_website_url": "https://example.com/source-page",
          "caption": "Golden Gate Bridge at sunset"
        }
      ]
    }
  ]
}

使用者位置

若要依地理位置調整搜尋結果,你可以透過國家、城市、地區及/或時區,指定使用者的大致位置。

  • cityregion 欄位是可自由填寫的文字字串,例如分別填入 MinneapolisMinnesota
  • country 欄位是由兩個字母組成的 ISO 國家代碼,例如 US
  • timezone 欄位是 IANA 時區,例如 America/Chicago

請注意,深度研究模型使用網頁搜尋時,不支援使用者位置。

自訂使用者位置
import OpenAI from "openai";
const openai = new OpenAI();

const response = await openai.responses.create({
  model: "gpt-6-astra",
  tools: [
    {
      type: "web_search",
      user_location: {
        type: "approximate",
        country: "GB",
        city: "London",
        region: "London",
      },
    },
  ],
  input: "What are the best restaurants near me?",
});
console.log(response.output_text);

即時網際網路存取

控制 Responses API 中的網頁搜尋工具要擷取即時內容,還是僅使用已快取或已建立索引的結果。

  • web_search 工具上設定 external_web_access: false,即可在離線/僅使用快取的模式下執行。
  • 若未設定,預設為 true(即時存取)。
  • 預覽版本(web_search_preview)會忽略此參數,其行為等同於將 external_web_access 設為 true
控制即時網際網路存取
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": "web_search", "external_web_access": false }
    ],
    "tool_choice": "auto",
    "input": "Find when the Eiffel Tower opened to the public and cite the source."
  }'

限制

Chat Completions API

Chat Completions API 僅支援使用專用搜尋模型進行網頁搜尋。這些模型不支援 Responses API 的 web_search 功能,例如網域篩選、完整來源清單、即時存取控制及回傳 Token 額度控制。

模型上下文視窗限制
gpt-5-search-api200k使用 Chat Completions 搜尋模型整合途徑
gpt-4o-search-preview128k使用 Chat Completions 搜尋模型方式;已棄用,於 2026-07-23 停用
gpt-4o-mini-search-preview128k使用 Chat Completions 搜尋模型方式;已棄用,於 2026-07-23 停用

Responses API

請使用託管的 web_search 工具。Responses API 仍接受舊有整合使用 web_search_preview,但新整合請使用 web_search

如需更大的模型上下文視窗,請使用 gpt-5.5。網頁搜尋的上下文視窗仍為 128k。

模型模型上下文視窗限制
gpt-4.11M搜尋上下文上限為 128k
gpt-4.1-mini1M搜尋上下文上限為 128k
o4-mini200k搜尋上下文上限為 128k;已棄用,於 2026-10-23 停用

使用 Responses API 網頁搜尋時,即使模型的上下文視窗更大,搜尋上下文視窗的上限仍為 128k。

  • 網頁搜尋不支援推理設為 minimalgpt-5
  • gpt-5.4 的推理強度設為 none 時,可能會產生品質較低的結果。
  • Responses API 網頁搜尋採用底層模型的分級速率限制。
  • web_search_preview 不支援 filtersreturn_token_budget,並會忽略 external_web_access
  • 使用 tool_choice: "auto" 時,不一定會執行搜尋。如需強制執行搜尋,請使用 tool_choice: "required" 或明確指定網頁搜尋工具。

使用注意事項

API 可用性 速率限制 備註

與搭配此工具使用的底層模型 的分級速率限制相同。

定價
ZDR 與資料駐留