Web Search
Web search 让模型能够访问来自互联网的最新信息,并给出带有来源引用的回答。
定义 · 一图看懂 · 工作原理
定义
Web search 让模型能够访问来自互联网的最新信息,并给出带有来源引用的回答。Responses API 通过 web search 工具启用搜索;Chat Completions 也在特定情况下提供搜索。在 Responses API 中,模型可以根据输入提示的内容,选择是否搜索网络。
Chat Completions API 在网络搜索方面只支持专门的搜索模型。Chat Completions 的搜索模型总会先搜索再回复;Responses API 将搜索作为工具提供。
一图看懂
这里的两个节点表示使用 web search 工具时的响应组成项。默认情况下,模型的响应会为在网络搜索结果中找到的 URL 加入行内引用。
- web_search_call 输出项带有搜索调用的 ID,以及
web_search_call.action中记录的所采取的操作。 - message 输出项在
message.content[0].text 中给出文本结果,并在message.content[0].annotations 中给出所引用 URL 的注释。
原文没有明确这些要点的先后顺序,因此按要点列出,不画先后箭头。
工作原理
新的 Responses API 集成使用 web_search 工具类型。较早的 web_search_preview 工具仍可用于旧版集成,但不支持 filters、external_web_access 和 return_token_budget 等较新的控制项。使用该工具的响应包含 web_search_call 项,其操作可以是 search,表示一次网络搜索。
open_page 操作表示打开一个页面,在推理模型中受支持。find_in_page 操作表示在页面内搜索,在推理模型中受支持。url_citation 注释补充了引用来源的 URL、标题和位置。
使用 Chat Completions 时,模型总是在回复查询之前先从网络检索信息。若要让模型自行决定是否搜索,需改用带有 web_search 工具的 Responses API。当 tool_choice 设为 auto 时,搜索是可选的。
search_context_size 控制模型生成回复前可使用的网络搜索上下文数量。查询只需要少量细节时使用 low,需要均衡的默认选项时使用 medium,答案可能需要搜索结果中更多细节时使用 high。这一设置不指定确切的 token 数,也不保证来源或引用数量。
sources 字段用于查看网络搜索期间检索到的 URL。行内引用只显示最相关的引用;sources 则列出模型形成回复时查阅的完整 URL 列表。web search 中的域名过滤可以把结果限制在特定的一组域名内。
通过 filters 参数最多可配置 100 个 allowed_domains 或最多 100 个 blocked_domains。域名过滤只在带有 web_search 工具的 Responses API 中可用。在 Responses API 中,可以控制 web search 工具是获取实时内容,还是只使用缓存或索引的结果。
web_search_preview 变体会忽略 external_web_access,其行为等同于 external_web_access 为 true。即使底层模型的上下文窗口更大,Responses API 中 web search 的上下文窗口上限仍为 128k。
例子
下方的官方请求示例在 tools 数组中启用 web search;配套响应是用于说明输出结构的样例。响应的文本结果位于 message.content[0].text,所引用的 URL 的注释位于 message.content[0].annotations。文档说明,Web search 可以同时返回图像结果和常规文本结果。
要使用图像搜索,需将 search_content_types 设置为包含 image。图像结果与助手消息分开返回。需要按地理位置细化搜索时,可用 country、city、region 或 timezone 提供近似用户位置。
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);[
{
"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..."
}
]
}
]
}
]import OpenAI from "openai";
const client = new OpenAI();
const tools: OpenAI.Responses.Tool[] = [
{
type: "web_search",
// @ts-expect-error The published SDK omits the documented image-search options.
search_content_types: ["image", "text"],
image_settings: {
max_results: 3,
caption: true,
},
},
];
const response = await client.responses.create({
model: "gpt-6-astra",
reasoning: { effort: "low" },
tools,
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);{
"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"
}
]
}
]
}来源
本页内容整理自以下官方来源,点击可核对原文。
核验日期 ·
本页由 AI 依据上述官方来源整理,并经自动核对,未经本站实测。 发现错误?告诉我们。