原生网页搜索
AI 网页搜索 API - 让 GPT-4、Claude、Gemini 实时搜索网络。获取当前信息、新闻和事实,实现有依据的 AI 响应。
原生网页搜索
UniterRouter 支持原生网页搜索功能,允许模型访问互联网上的实时信息。此功能对于回答关于时事、最新新闻、实时数据和其他可能不在模型训练数据中的时效性信息非常有用。
工作原理
当你在请求中包含 web_search 工具时,模型可以在生成响应之前搜索网页以收集相关信息:
- 你发送启用
web_search工具的请求 - 模型根据查询判断是否需要网页搜索
- 如果需要,模型执行网页搜索以收集当前信息
- 模型综合搜索结果并生成响应
- 响应中包含引用以显示信息来源
支持的提供商
原生网页搜索在部分模型上可用。在我们的模型页面查看所有支持原生网页搜索的模型。
基本用法
要启用网页搜索,在请求中添加 web_search 工具:
curl -X POST "https://api.uniterouter.ai/v1/chat/completions" \
-H "Authorization: Bearer $UNITEROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-5.2",
"messages": [
{
"role": "user",
"content": "旧金山现在的天气怎么样?"
}
],
"tools": [
{
"type": "web_search"
}
]
}'示例响应
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1234567890,
"model": "openai/gpt-5.2",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "旧金山当前天气为 57°F (14°C),多云...",
"annotations": [
{
"type": "url_citation",
"url": "https://weather.com/...",
"title": "旧金山天气"
}
]
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 150,
"total_tokens": 165,
"cost_usd_total": 0.0315
}
}网页搜索选项
web_search 工具接受可选的配置参数:
用户位置
提供位置上下文以获得更相关的本地搜索结果:
{
"type": "web_search",
"user_location": {
"city": "上海",
"region": "上海",
"country": "CN",
"timezone": "Asia/Shanghai"
}
}搜索上下文大小
控制检索的网页内容量(仅限 OpenAI):
{
"type": "web_search",
"search_context_size": "medium"
}可用值:
low- 最少搜索上下文,响应更快medium- 平衡的上下文(默认)high- 最大搜索上下文,更全面
最大使用次数
限制每个请求的搜索次数(取决于提供商):
{
"type": "web_search",
"max_uses": 3
}使用 SDK
OpenAI SDK (Python)
from openai import OpenAI
client = OpenAI(
base_url="https://api.uniterouter.ai/v1",
api_key="your-api-key"
)
response = client.chat.completions.create(
model="openai/gpt-5.2",
messages=[
{"role": "user", "content": "今天的最新新闻头条是什么?"}
],
tools=[{"type": "web_search"}]
)
print(response.choices[0].message.content)OpenAI SDK (TypeScript)
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.uniterouter.ai/v1",
apiKey: "your-api-key",
});
const response = await client.chat.completions.create({
model: "openai/gpt-5.2",
messages: [{ role: "user", content: "最新的科技新闻是什么?" }],
tools: [{ type: "web_search" }],
});
console.log(response.choices[0].message.content);流式传输
网页搜索支持流式响应。引用包含在最终块中:
curl -X POST "https://api.uniterouter.ai/v1/chat/completions" \
-H "Authorization: Bearer $UNITEROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-5.2",
"messages": [
{"role": "user", "content": "苹果公司当前的股价是多少?"}
],
"tools": [{"type": "web_search"}],
"stream": true
}'引用和来源
网页搜索响应包含引用以显示信息来源。这些出现在消息的 annotations 字段中:
{
"annotations": [
{
"type": "url_citation",
"url": "https://example.com/article",
"title": "文章标题",
"start_index": 0,
"end_index": 50
}
]
}引用格式可能因提供商略有不同,但 UniterRouter 会将它们标准化为一致的结构。
成本跟踪
网页搜索成本在使用对象中与 token 成本分开跟踪:
{
"usage": {
"prompt_tokens": 15,
"completion_tokens": 150,
"total_tokens": 165,
"cost_usd_total": 0.0125,
"cost_usd_input": 0.0015,
"cost_usd_output": 0.01,
"cost_usd_web_search": 0.01
}
}cost_usd_web_search 字段显示专门用于网页搜索查询的成本。网页搜索对推理模型(GPT-5、o 系列)按每次搜索调用 $0.01 计费,对非推理模型按每次调用 $0.025 计费。
与函数工具结合使用
你可以将网页搜索与常规函数工具一起使用:
{
"tools": [
{ "type": "web_search" },
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取某地天气",
"parameters": {
"type": "object",
"properties": {
"location": { "type": "string" }
}
}
}
}
]
}某些专用搜索模型仅支持网页搜索,不支持额外的函数工具。如果需要同时使用网页搜索和函数工具,请使用 gpt-5.2 或其他 GPT-5 系列模型。
使用场景
时事和新闻
{
"messages": [
{ "role": "user", "content": "今天的主要新闻有哪些?" }
],
"tools": [{ "type": "web_search" }]
}实时数据
{
"messages": [
{ "role": "user", "content": "比特币当前价格是多少?" }
],
"tools": [{ "type": "web_search" }]
}研究和事实核查
{
"messages": [
{
"role": "user",
"content": "关于气候变化的最新研究发现是什么?"
}
],
"tools": [{ "type": "web_search" }]
}本地信息
{
"messages": [
{
"role": "user",
"content": "我附近现在有哪些餐厅在营业?"
}
],
"tools": [
{
"type": "web_search",
"user_location": {
"city": "北京",
"country": "CN"
}
}
]
}最佳实践
- 使用 GPT-5.2:为获得最佳网页搜索体验和完整工具支持,请使用
openai/gpt-5.2 - 提供位置上下文:当查询依赖位置时,包含
user_location以获得更相关的结果 - 监控成本:网页搜索除了 token 成本外还会产生每次查询的成本
- 检查引用:始终查看响应中的引用以验证信息来源
- 使用流式传输:对于面向用户的应用,启用流式传输以在生成时显示响应
错误处理
如果你尝试在不支持的模型上使用网页搜索:
{
"error": {
"message": "模型 gpt-4o 不支持原生网页搜索。请移除 web_search 工具或使用支持它的模型。参见 https://uniterouter.ai/models?features=webSearch 了解支持的模型。",
"type": "invalid_request_error"
}
}为避免此错误,仅在启用原生网页搜索的模型上使用 web_search 工具。
How is this guide?