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 中的文本结果
    • 所引用 URL 的注释 message.content[0].annotations

默认情况下,模型会在回复中以内嵌引用的形式标注网页搜索结果中的 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 预算限制。

此参数仅适用于使用 GPT-5+ 推理模型进行网页搜索的 Responses API 托管 web_search 工具。它不会改变搜索上下文窗口,也不适用于非推理网页搜索、旧版 Search API 接入方式、容器网页搜索、Chat Completions 搜索模型或 web_search_preview。仅支持 defaultunlimited 两个值;null、数字及其他字符串均会被拒绝。

运行更长时间的网页搜索
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."
  }'

图像搜索结果

网页搜索可以在返回常规文本结果的同时返回图像结果。当您的应用需要最新的图像或有网页来源依据的视觉素材时,例如产品照片、地标、地点、活动图像或视觉参考资料,请使用图像搜索。

要使用图像搜索,请将 image 纳入 search_content_types。如果您还需要辅助文本结果来帮助模型总结、排序或解释检索到的图像,请添加 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 和数据驻留