网页搜索让模型能够获取互联网上的最新信息,并在回答中引用信息来源。要启用此功能,请使用 Responses API 中的网页搜索工具;在某些情况下,也可以使用 Chat Completions。
OpenAI 模型主要支持三种网页搜索方式:
非推理网页搜索:非推理模型将用户的查询发送给网页搜索工具,工具根据排名靠前的结果返回回复。模型不进行内部规划,只是转达搜索工具的回复。这种方式速度快,非常适合快速查询。
使用推理模型进行智能体搜索时,模型会主动管理搜索过程。它可以在思维链中执行网页搜索、分析结果,并决定是否继续搜索。这种灵活性让智能体搜索非常适合复杂的工作流,但也意味着搜索耗时会比快速查询更长。例如,您可以调整 gpt-5.5 等模型的推理级别,同时改变搜索的深度和延迟。
深度研究是一种由智能体驱动的专门方法,供推理模型开展深入、长时间的调查。模型在思维链中执行网页搜索,通常会查阅数百个来源。深度研究可能运行数分钟,最好配合后台模式使用。请使用 gpt-5.5,并将推理设置为 high 或 xhigh。
使用场景 推荐方案 说明 新建网页搜索集成 在 Responses API 中使用 web_search 和 gpt-5.5 支持托管式网页搜索的控制功能,包括筛选、来源、实时访问控制,以及更长时间的研究任务 现有的 Chat Completions 搜索集成 在 Chat Completions 中使用 gpt-5-search-api 仅在需要保留 Chat Completions 集成时使用此方案 多步骤研究或长时间运行的报告生成任务 使用 gpt-5.5,并将推理设置为 high 或 xhigh 对于可能需要数分钟才能生成的报告,请使用后台模式
使用 Responses API 时,您可以在内容生成 API 请求的 tools 数组中配置网页搜索以启用此功能。与其他工具一样,模型可以根据输入提示的内容,选择是否搜索网页。
对于新建的 Responses API 集成,请使用 { "type": "web_search" }。早期的 web_search_preview 工具仍可供旧版集成使用,但不支持 filters、external_web_access 和 return_token_budget 等较新的控制参数。
1
2
3
4
5
6
7
8
9
10 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); 1
2
3
4
5
6
7
8
9
10
11 from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-6-astra",
tools=[{"type": "web_search"}],
input="What was a positive news story from today?",
)
print(response.output_text) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Tools: []responses.ToolUnionParam{
responses.ToolParamOfWebSearch(responses.WebSearchToolTypeWebSearch),
},
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("What was a positive news story from today?")},
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.WebSearchTool;
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.input("What was a positive news story from today?")
.addTool(WebSearchTool.builder().type(WebSearchTool.Type.WEB_SEARCH).build())
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text())); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
CreateResponseOptions options = new() { Model = "gpt-6-astra" };
options.Tools.Add(ResponseTool.CreateWebSearchTool());
options.InputItems.Add(
ResponseItem.CreateUserMessageItem("What was a positive news story from today?")
);
ResponseResult response = await client.CreateResponseAsync(options);
Console.WriteLine(response.GetOutputText()); 1
2
3
4
5
6
7
8
9
10
11 require "openai"
openai = OpenAI::Client.new
response = openai.responses.create(
model: "gpt-6-astra",
tools: [{ type: "web_search" }],
input: "What was a positive news story from today?"
)
puts(response.output_text) 1
2
3
4
5
6
7
8 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"}],
"input": "what was a positive news story from today?"
}' 1
2
3
4
5
6
7
8 openai responses create \
--model gpt-6-astra \
--raw-output \
--transform 'output.#(type=="message").content.0.text' <<'YAML'
tools:
- type: web_search
input: What was a positive news story from today?
YAML 使用网页搜索工具的模型回复将包含两部分:
一个 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、标题和位置。
向最终用户展示网页结果或其中的信息时,您必须确保内嵌引用在用户界面中清晰可见且可点击。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [
{
"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..."
}
]
}
]
}
]
使用 Chat Completions API ,您可以直接访问 ChatGPT 搜索 所用的微调模型和工具。
使用 Chat Completions 时,模型始终会先从网页检索信息,再回答您的查询。要让模型自行决定是否搜索,请改用 Responses API 并配置 web_search 工具。
目前,在 Chat Completions 中进行网页搜索时,请使用以下模型:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import OpenAI from "openai" ;
const client = new OpenAI ();
const completion = await client.chat.completions. create ({
model: "gpt-5-search-api" ,
web_search_options: {},
messages: [
{
role: "user" ,
content: "What was a positive news story from today?" ,
},
],
});
console. log (completion.choices[ 0 ].message.content); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-5-search-api",
web_search_options={},
messages=[
{
"role": "user",
"content": "What was a positive news story from today?",
}
],
)
print(completion.choices[0].message.content) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
)
func main() {
client := openai.NewClient()
completion, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
Model: "gpt-5-search-api",
WebSearchOptions: openai.ChatCompletionNewParamsWebSearchOptions{},
Messages: []openai.ChatCompletionMessageParamUnion{
openai.UserMessage("What was a positive news story from today?"),
},
})
if err != nil {
panic(err)
}
fmt.Println(completion.Choices[0].Message.Content)
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
ChatCompletionCreateParams params =
ChatCompletionCreateParams.builder()
.model("gpt-5-search-api")
.addUserMessage("What was a positive news story today?")
.webSearchOptions(ChatCompletionCreateParams.WebSearchOptions.builder().build())
.build();
client.chat().completions().create(params).choices().stream()
.flatMap(choice -> choice.message().content().stream())
.forEach(System.out::println); 1
2
3
4
5
6
7
8
9
10
11
12
13 using OpenAI.Chat;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
string model = "gpt-5-search-api";
ChatClient client = new(model, key);
ChatCompletion completion = await client.CompleteChatAsync(
[new UserChatMessage("What was a positive news story today?")],
new ChatCompletionOptions { WebSearchOptions = new() }
);
Console.WriteLine(completion.Content[0].Text); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 require "openai"
client = OpenAI::Client.new
completion = client.chat.completions.create(
model: "gpt-5-search-api",
messages: [
{
role: :user,
content: "What was a positive news story today?"
}
],
web_search_options: {}
)
puts(completion.choices.fetch(0).message.content) 1
2
3
4
5
6
7
8
9
10
11 curl -X POST "https://api.openai.com/v1/chat/completions" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-type: application/json" \
-d '{
"model": "gpt-5-search-api",
"web_search_options": {},
"messages": [{
"role": "user",
"content": "What was a positive news story from today?"
}]
}' choices 数组中的 API 响应项将包含:
message.content,其中包含模型返回的文本结果及所有内嵌引用
annotations,其中包含所引用 URL 的列表
默认情况下,模型会在回复中以内嵌引用的形式标注网页搜索结果中的 URL。此外,url_citation 注释对象会包含所引用来源的 URL 和标题,以及模型回复中引用这些来源的文本的起始和结束字符索引。
向最终用户展示网页结果或其中的信息时,您必须确保内嵌引用在用户界面中清晰可见且可点击。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [
{
"index" : 0 ,
"message" : {
"role" : "assistant" ,
"content" : "the model response is here..." ,
"refusal" : null ,
"annotations" : [
{
"type" : "url_citation" ,
"url_citation" : {
"end_index" : 985 ,
"start_index" : 764 ,
"title" : "Page title..." ,
"url" : "https://..."
}
}
]
},
"finish_reason" : "stop"
}
]
如果您正在使用 推荐方案 说明 Responses 中的 web_search_preview 迁移到 web_search web_search 支持 filters、external_web_access 和 return_token_budget 等较新的控制参数gpt-4o-search-preview 或 gpt-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 数量,也不保证特定数量的来源或引用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14 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); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-6-astra",
tools=[
{
"type": "web_search",
"search_context_size": "low",
}
],
input="What movie won best picture in 2025?",
)
print(response.output_text) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
tool := responses.ToolParamOfWebSearch(responses.WebSearchToolTypeWebSearch)
tool.OfWebSearch.SearchContextSize = responses.WebSearchToolSearchContextSizeLow
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Tools: []responses.ToolUnionParam{tool},
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("What movie won best picture in 2025?")},
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.WebSearchTool;
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.input("What movie won best picture in 2025?")
.addTool(
WebSearchTool.builder()
.type(WebSearchTool.Type.WEB_SEARCH)
.searchContextSize(WebSearchTool.SearchContextSize.LOW)
.build())
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text())); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
CreateResponseOptions options = new() { Model = "gpt-6-astra" };
options.Tools.Add(
ResponseTool.CreateWebSearchTool(
searchContextSize: WebSearchToolContextSize.Low
)
);
options.InputItems.Add(
ResponseItem.CreateUserMessageItem("What movie won best picture in 2025?")
);
ResponseResult response = await client.CreateResponseAsync(options);
Console.WriteLine(response.GetOutputText()); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 require "openai"
client = OpenAI::Client.new
response = client.responses.create(
model: "gpt-6-astra",
input: "What movie won best picture in 2025?",
tools: [
{
type: :web_search,
search_context_size: :low
}
]
)
puts(response.output_text) 1
2
3
4
5
6
7
8
9
10
11 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",
"search_context_size": "low"
}],
"input": "What movie won best picture in 2025?"
}'
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。仅支持 default 和 unlimited 两个值;null、数字及其他字符串均会被拒绝。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
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.",
"",
"Do:",
"- Include specific figures, trends, statistics, and measurable outcomes.",
"- Prioritize reliable, up-to-date sources: peer-reviewed research, health organizations (e.g., WHO, CDC), regulatory agencies, or pharmaceutical earnings reports.",
"- Include inline citations and return all source metadata.",
"",
"Be analytical, avoid generalities, and ensure that each section supports data-backed reasoning that could inform healthcare policy or financial modeling.",
].join("\n"),
});
console.log(response.output_text); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 from openai import OpenAI
client = OpenAI()
response = client.responses.create(
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.
Do:
- Include specific figures, trends, statistics, and measurable outcomes.
- Prioritize reliable, up-to-date sources: peer-reviewed research, health organizations (e.g., WHO, CDC), regulatory agencies, or pharmaceutical earnings reports.
- Include inline citations and return all source metadata.
Be analytical, avoid generalities, and ensure that each section supports data-backed reasoning that could inform healthcare policy or financial modeling.""",
)
print(response.output_text) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 package main
import (
"context"
"fmt"
"strings"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
"github.com/openai/openai-go/v3/shared"
)
func main() {
client := openai.NewClient()
tool := responses.ToolParamOfWebSearch(responses.WebSearchToolTypeWebSearch)
tool.OfWebSearch.SetExtraFields(map[string]any{"return_token_budget": "unlimited"})
input := strings.Join([]string{
"Research the economic impact of semaglutide on global healthcare systems.",
"",
"Do:",
"- Include specific figures, trends, statistics, and measurable outcomes.",
"- Prioritize reliable, up-to-date sources: peer-reviewed research, health organizations, regulatory agencies, or pharmaceutical earnings reports.",
"- Include inline citations and return all source metadata.",
"",
"Be analytical, avoid generalities, and ensure that each section supports data-backed reasoning that could inform healthcare policy or financial modeling.",
}, "\n")
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Reasoning: shared.ReasoningParam{Effort: shared.ReasoningEffortXhigh},
Tools: []responses.ToolUnionParam{tool},
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String(input)},
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.JsonValue;
import com.openai.models.Reasoning;
import com.openai.models.ReasoningEffort;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.WebSearchTool;
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.input(
"Research the economic impact of semaglutide on global healthcare systems. Include current figures and citations.")
.reasoning(Reasoning.builder().effort(ReasoningEffort.XHIGH).build())
.addTool(
WebSearchTool.builder()
.type(WebSearchTool.Type.WEB_SEARCH)
.putAdditionalProperty("return_token_budget", JsonValue.from("unlimited"))
.build())
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text())); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 require "openai"
client = OpenAI::Client.new
response = client.responses.create(
model: "gpt-6-astra",
input: "Research the economic impact of semaglutide on global healthcare systems. Include current figures and citations.",
reasoning: { effort: :xhigh },
tools: [
{
type: :web_search,
return_token_budget: :unlimited
}
]
)
puts(response.output_text) 1
2
3
4
5
6
7
8
9
10
11
12
13
14 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-sports、oai-weather 或 oai-finance。web_search 和 web_search_preview 工具均支持 sources 字段。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 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",
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.",
});
console.log(response.output_text); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 from openai import OpenAI
client = OpenAI()
response = client.responses.create(
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.",
)
print(response.output_text) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
"github.com/openai/openai-go/v3/shared"
)
func main() {
client := openai.NewClient()
tool := responses.ToolParamOfWebSearch(responses.WebSearchToolTypeWebSearch)
tool.OfWebSearch.Filters = responses.WebSearchToolFiltersParam{
AllowedDomains: []string{"pubmed.ncbi.nlm.nih.gov", "clinicaltrials.gov", "www.who.int", "www.cdc.gov", "www.fda.gov"},
}
tool.OfWebSearch.Filters.SetExtraFields(map[string]any{"blocked_domains": []string{"reddit.com", "quora.com", "wikipedia.org"}})
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Reasoning: shared.ReasoningParam{Effort: shared.ReasoningEffortLow},
Tools: []responses.ToolUnionParam{tool},
Include: []responses.ResponseIncludable{responses.ResponseIncludableWebSearchCallActionSources},
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Please perform a web search on how semaglutide is used in the treatment of diabetes.")},
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.JsonValue;
import com.openai.models.Reasoning;
import com.openai.models.ReasoningEffort;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseIncludable;
import com.openai.models.responses.WebSearchTool;
import java.util.List;
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.input("Search for how semaglutide is used in the treatment of diabetes.")
.reasoning(Reasoning.builder().effort(ReasoningEffort.LOW).build())
.addInclude(ResponseIncludable.of("web_search_call.action.sources"))
.addTool(
WebSearchTool.builder()
.type(WebSearchTool.Type.WEB_SEARCH)
.filters(
WebSearchTool.Filters.builder()
.allowedDomains(
List.of(
"pubmed.ncbi.nlm.nih.gov",
"clinicaltrials.gov",
"www.who.int",
"www.cdc.gov",
"www.fda.gov"))
.putAdditionalProperty(
"blocked_domains",
JsonValue.from(List.of("reddit.com", "quora.com", "wikipedia.org")))
.build())
.build())
.build();
var response = client.responses().create(params);
response.output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text()));
response.output().stream()
.flatMap(item -> item.webSearchCall().stream())
.flatMap(call -> call.action().search().stream())
.flatMap(action -> action.sources().stream())
.flatMap(List::stream)
.forEach(source -> System.out.println(source.url())); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 require "openai"
client = OpenAI::Client.new
response = client.responses.create(
model: "gpt-6-astra",
reasoning: { effort: :low },
input: "Search for how semaglutide is used in the treatment of diabetes.",
include: ["web_search_call.action.sources"],
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"]
}
}
]
)
puts(response.output_text)
response.output
.grep(OpenAI::Models::Responses::ResponseFunctionWebSearch)
.each do |search_call|
action = search_call.action
next unless action.is_a?(
OpenAI::Models::Responses::ResponseFunctionWebSearch::Action::Search
)
Array(action.sources).each { |source| puts(source.url) }
end 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 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 项。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 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); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 from openai import OpenAI
client = OpenAI()
response = 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.",
)
print(response.output) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
"github.com/openai/openai-go/v3/shared"
)
func main() {
client := openai.NewClient()
tool := responses.ToolParamOfWebSearch(responses.WebSearchToolTypeWebSearch)
tool.OfWebSearch.SetExtraFields(map[string]any{
"search_content_types": []string{"image", "text"},
"image_settings": map[string]any{"max_results": 3, "caption": true},
})
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Reasoning: shared.ReasoningParam{Effort: shared.ReasoningEffortLow},
Tools: []responses.ToolUnionParam{tool},
Include: []responses.ResponseIncludable{responses.ResponseIncludableWebSearchCallResults},
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Search for recent images and supporting text sources about the Golden Gate Bridge at sunset.")},
})
if err != nil {
panic(err)
}
fmt.Println(response.Output)
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.JsonValue;
import com.openai.models.Reasoning;
import com.openai.models.ReasoningEffort;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseIncludable;
import com.openai.models.responses.WebSearchTool;
import java.util.List;
import java.util.Map;
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.input(
"Search for recent images and supporting text sources about the Golden Gate Bridge at sunset.")
.reasoning(Reasoning.builder().effort(ReasoningEffort.LOW).build())
.addInclude(ResponseIncludable.of("web_search_call.results"))
.addTool(
WebSearchTool.builder()
.type(WebSearchTool.Type.WEB_SEARCH)
.putAdditionalProperty(
"search_content_types", JsonValue.from(List.of("image", "text")))
.putAdditionalProperty(
"image_settings", JsonValue.from(Map.of("max_results", 3, "caption", true)))
.build())
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.webSearchCall().stream())
.map(call -> call._additionalProperties().get("results"))
.filter(java.util.Objects::nonNull)
.forEach(System.out::println); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 require "openai"
client = OpenAI::Client.new
response = client.responses.create(
model: "gpt-6-astra",
reasoning: { effort: :low },
input: "Search for recent images and supporting text sources about the Golden Gate Bridge at sunset.",
include: ["web_search_call.results"],
tools: [
{
type: :web_search,
search_content_types: ["image", "text"],
image_settings: {
max_results: 3,
caption: true
}
}
]
)
puts(response.output) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 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",
"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."
}' 每个 image_result 包括:
image_url:该结果的规范图像 URL。
source_website_url:发现该图像的网页。
thumbnail_url:缩略图 URL(如果可用)。
caption:简短的图注或描述(如果可用)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 {
"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"
}
]
}
]
}
要根据地理位置优化搜索结果,您可以使用国家、城市、地区和/或时区来指定用户的大致位置。
city 和 region 字段为自由文本字符串,例如分别填写 Minneapolis 和 Minnesota。
country 字段为两字母的 ISO 国家代码 ,例如 US。
timezone 字段为 IANA 时区 ,例如 America/Chicago。
请注意,深度研究模型在使用网页搜索时不支持用户位置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 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); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 from openai import OpenAI
client = OpenAI()
response = client.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?",
)
print(response.output_text) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
tool := responses.ToolParamOfWebSearch(responses.WebSearchToolTypeWebSearch)
tool.OfWebSearch.UserLocation = responses.WebSearchToolUserLocationParam{
Type: "approximate",
Country: openai.String("GB"),
City: openai.String("London"),
Region: openai.String("London"),
}
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Tools: []responses.ToolUnionParam{tool},
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("What are the best restaurants near me?")},
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.WebSearchTool;
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.input("What are the best restaurants near me?")
.addTool(
WebSearchTool.builder()
.type(WebSearchTool.Type.WEB_SEARCH)
.userLocation(
WebSearchTool.UserLocation.builder()
.type(WebSearchTool.UserLocation.Type.APPROXIMATE)
.city("London")
.country("GB")
.region("London")
.build())
.build())
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text())); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
CreateResponseOptions options = new() { Model = "gpt-6-astra" };
options.Tools.Add(
ResponseTool.CreateWebSearchTool(
userLocation: WebSearchToolLocation.CreateApproximateLocation(
country: "GB",
city: "London",
region: "London"
)
)
);
options.InputItems.Add(
ResponseItem.CreateUserMessageItem("What are the best restaurants near me?")
);
ResponseResult response = await client.CreateResponseAsync(options);
Console.WriteLine(response.GetOutputText()); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 require "openai"
client = OpenAI::Client.new
response = client.responses.create(
model: "gpt-6-astra",
input: "What are the best restaurants near me?",
tools: [
{
type: :web_search,
user_location: {
type: :approximate,
country: "GB",
city: "London",
region: "London"
}
}
]
)
puts(response.output_text) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 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",
"user_location": {
"type": "approximate",
"country": "GB",
"city": "London",
"region": "London"
}
}],
"input": "What are the best restaurants near me?"
}'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import OpenAI from "openai" ;
const client = new OpenAI ();
const completion = await client.chat.completions. create ({
model: "gpt-5-search-api" ,
web_search_options: {
user_location: {
type: "approximate" ,
approximate: {
country: "GB" ,
city: "London" ,
region: "London" ,
},
},
},
messages: [
{
role: "user" ,
content: "What are the best restaurants near me?" ,
},
],
});
console. log (completion.choices[ 0 ].message.content); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-5-search-api",
web_search_options={
"user_location": {
"type": "approximate",
"approximate": {
"country": "GB",
"city": "London",
"region": "London",
},
},
},
messages=[
{
"role": "user",
"content": "What are the best restaurants near me?",
}
],
)
print(completion.choices[0].message.content) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
)
func main() {
client := openai.NewClient()
completion, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
Model: "gpt-5-search-api",
WebSearchOptions: openai.ChatCompletionNewParamsWebSearchOptions{
UserLocation: openai.ChatCompletionNewParamsWebSearchOptionsUserLocation{
Approximate: openai.ChatCompletionNewParamsWebSearchOptionsUserLocationApproximate{
Country: openai.String("GB"),
City: openai.String("London"),
Region: openai.String("London"),
},
},
},
Messages: []openai.ChatCompletionMessageParamUnion{openai.UserMessage("What are the best restaurants near me?")},
})
if err != nil {
panic(err)
}
fmt.Println(completion.Choices[0].Message.Content)
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
ChatCompletionCreateParams params =
ChatCompletionCreateParams.builder()
.model("gpt-5-search-api")
.addUserMessage("What are the best restaurants near me?")
.webSearchOptions(
ChatCompletionCreateParams.WebSearchOptions.builder()
.userLocation(
ChatCompletionCreateParams.WebSearchOptions.UserLocation.builder()
.approximate(
ChatCompletionCreateParams.WebSearchOptions.UserLocation.Approximate
.builder()
.country("GB")
.city("London")
.region("London")
.build())
.build())
.build())
.build();
client.chat().completions().create(params).choices().stream()
.flatMap(choice -> choice.message().content().stream())
.forEach(System.out::println); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 require "openai"
client = OpenAI::Client.new
completion = client.chat.completions.create(
model: "gpt-5-search-api",
messages: [
{
role: :user,
content: "What are the best restaurants near me?"
}
],
web_search_options: {
user_location: {
type: :approximate,
approximate: {
country: "GB",
city: "London",
region: "London"
}
}
}
)
puts(completion.choices.fetch(0).message.content) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 curl -X POST "https://api.openai.com/v1/chat/completions" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-type: application/json" \
-d '{
"model": "gpt-5-search-api",
"web_search_options": {
"user_location": {
"type": "approximate",
"approximate": {
"country": "GB",
"city": "London",
"region": "London"
}
}
},
"messages": [{
"role": "user",
"content": "What are the best restaurants near me?"
}]
}'
在 Responses API 中,控制网页搜索工具是获取实时内容,还是仅使用已缓存或已索引的结果。
在 web_search 工具上设置 external_web_access: false,即可在离线模式下运行,仅使用缓存。
如果您不设置此参数,默认值为 true(实时访问)。
预览版本(web_search_preview)会忽略此参数,其行为等同于将 external_web_access 设为 true。
1
2
3
4
5
6
7
8
9
10
11 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."
}' 1
2
3
4
5
6
7
8
9
10
11 import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
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.",
});
console.log(response.output_text); 1
2
3
4
5
6
7
8
9
10
11 from openai import OpenAI
client = OpenAI()
resp = client.responses.create(
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.",
)
print(resp.output_text) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
tool := responses.ToolParamOfWebSearch(responses.WebSearchToolTypeWebSearch)
tool.OfWebSearch.SetExtraFields(map[string]any{"external_web_access": false})
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Tools: []responses.ToolUnionParam{tool},
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Find when the Eiffel Tower opened to the public and cite the source.")},
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.JsonValue;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.WebSearchTool;
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.input("Find when the Eiffel Tower opened to the public and cite the source.")
.addTool(
WebSearchTool.builder()
.type(WebSearchTool.Type.WEB_SEARCH)
.putAdditionalProperty("external_web_access", JsonValue.from(false))
.build())
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text())); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 require "openai"
client = OpenAI::Client.new
response = client.responses.create(
model: "gpt-6-astra",
input: "Find when the Eiffel Tower opened to the public and cite the source.",
tools: [
{
type: :web_search,
external_web_access: false
}
]
)
puts(response.output_text)
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
请使用托管的 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。
网页搜索不支持推理级别为 minimal 的 gpt-5 。
将 gpt-5.4 的推理强度设为 none 时,结果质量可能较低。
Responses API 网页搜索采用底层模型的分层速率限制。
web_search_preview 不支持 filters 或 return_token_budget,并且会忽略 external_web_access。
使用 tool_choice: "auto" 时,搜索是可选的。如果必须执行搜索,请使用 tool_choice: "required" 或明确指定网页搜索工具。