网络搜索
本文档介绍如何在 ShuYou 平台中使用 Web Search 功能。ShuYou 支持通过多种兼容协议调用 Web Search,包括 Chat Completions、Messages、Responses 以及 Vertex AI。概述
Web Search 功能允许 AI 模型在生成回答时访问实时网络信息,从而提供更准确、更及时的答案。该功能特别适用于:- 查询实时新闻和事件
- 获取最新的产品信息和价格
- 查询天气、股票等动态数据
- 获取最新的技术文档和资料
支持的协议
| 协议 | 端点 | Web Search 参数 |
|---|---|---|
| Chat Completions (OpenAI 兼容) | /v1/chat/completions | web_search_options |
| Messages (Anthropic 兼容) | /v1/messages | tools 中的 web_search_20250305 |
| Responses (OpenAI Responses) | /v1/responses | tools 中的 web_search 系列 |
| Vertex AI (Google 兼容) | /api/vertex-ai/v1/... | tools 中的 googleSearch |
1. Chat Completions API
Chat Completions API 使用web_search_options 参数启用 Web Search 功能。
参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
web_search_options | object | 否 | Web 搜索配置 |
web_search_options.search_context_size | string | 否 | 搜索上下文大小:low / medium / high |
web_search_options.user_location | object | 否 | 用户位置信息,用于本地化搜索结果 |
web_search_options.user_location.type | string | 是 | 位置类型,固定为 approximate |
web_search_options.user_location.city | string | 否 | 城市名称 |
web_search_options.user_location.country | string | 否 | 国家代码(两位 ISO,如 CN、US) |
web_search_options.user_location.region | string | 否 | 地区/省份 |
web_search_options.user_location.timezone | string | 否 | 时区(IANA 格式,如 Asia/Shanghai) |
示例
2. Messages API (Anthropic 兼容)
Messages API 使用tools 参数中的 web_search_20250305 类型启用 Web Search 功能。
参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tools[].type | string | 是 | 工具类型,固定为 web_search_20250305 |
tools[].name | string | 是 | 工具名称,固定为 web_search |
tools[].allowed_domains | array | 否 | 允许搜索的域名白名单 |
tools[].blocked_domains | array | 否 | 禁止搜索的域名黑名单 |
tools[].max_uses | number | 否 | 单次请求中最大搜索次数 |
tools[].user_location | object | 否 | 用户位置信息 |
tools[].user_location.type | string | 是 | 位置类型,固定为 approximate |
tools[].user_location.city | string | 否 | 城市名称 |
tools[].user_location.country | string | 否 | 国家代码(ISO 3166-1 alpha-2) |
tools[].user_location.region | string | 否 | 地区 |
tools[].user_location.timezone | string | 否 | 时区(IANA 格式) |
示例
3. Responses API (OpenAI Responses)
Responses API 使用tools 参数中的 web_search 系列类型启用 Web Search 功能。
支持的 Web Search 类型
| 类型 | 说明 |
|---|---|
web_search | 网页搜索(一般可用版本) |
web_search_2025_08_26 | 网页搜索 2025 版本 |
web_search_preview | 网页搜索预览版 |
web_search_preview_2025_03_11 | 网页搜索预览版 2025 |
参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tools[].type | string | 是 | Web Search 类型 |
tools[].search_context_size | string | 否 | 搜索上下文大小:low / medium / high |
tools[].filters | object | 否 | 搜索过滤器(仅 web_search 类型) |
tools[].filters.allowed_domains | array | 否 | 允许的域名白名单 |
tools[].user_location | object | 否 | 用户位置信息 |
tools[].user_location.type | string | 是 | 位置类型,固定为 approximate |
tools[].user_location.city | string | 否 | 城市名称 |
tools[].user_location.country | string | 否 | 国家代码(两位 ISO) |
tools[].user_location.region | string | 否 | 地区/州代码 |
tools[].user_location.timezone | string | 否 | 时区(IANA 格式) |
示例
4. Vertex AI API (Google 兼容)
Vertex AI API 使用tools 参数中的 googleSearch 启用 Google Search Grounding 功能。
参数说明
在 Vertex AI 中,Web Search 通过googleSearch 工具启用,并通过响应中的 groundingMetadata 返回搜索来源信息。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tools[].googleSearch | object | 是 | Google Search 配置(空对象即可启用) |
响应中的 Grounding 信息
| 字段 | 类型 | 说明 |
|---|---|---|
groundingMetadata.webSearchQueries | array | 执行的搜索查询 |
groundingMetadata.groundingChunks | array | 证据片段 |
groundingMetadata.groundingChunks[].web.uri | string | 来源 URL |
groundingMetadata.groundingChunks[].web.title | string | 来源标题 |
groundingMetadata.groundingChunks[].web.domain | string | 来源域名 |
示例
响应格式对比
Chat Completions 响应
Messages 响应
Responses 响应
Vertex AI 响应
流式事件 (Responses API)
使用 Responses API 的流式模式时,会收到以下 Web Search 相关事件:| 事件类型 | 说明 |
|---|---|
response.web_search_call.in_progress | Web Search 调用开始 |
response.web_search_call.searching | 正在执行搜索 |
response.web_search_call.completed | 搜索完成 |
最佳实践
1. 选择合适的搜索上下文大小
low:适用于简单查询,响应更快,成本更低medium:平衡选择,适用于大多数场景high:适用于需要深入研究的复杂问题
2. 使用用户位置信息
为获得更相关的本地化结果,建议提供用户位置信息:3. 合理使用域名过滤
在 Messages API 中,可以使用allowed_domains 或 blocked_domains 来控制搜索范围:
4. 限制搜索次数
在 Messages API 中,使用max_uses 控制单次请求的最大搜索次数,以控制成本:
5. 处理引用信息
始终检查并展示响应中的引用信息,这有助于用户验证信息来源的可靠性。注意事项
- 计费:Web Search 功能会产生额外费用,具体请参考定价文档
- 延迟:启用 Web Search 会增加响应延迟,因为需要执行实时搜索
- 可用性:并非所有模型都支持 Web Search,请确认目标模型的支持情况
- 结果准确性:Web Search 结果来自实时网络,信息准确性取决于搜索引擎和来源网站
常见问题
Q: 如何知道模型是否执行了 Web Search?
A: 可以通过以下方式判断:- Chat Completions:检查
message.annotations中的url_citation - Messages:查看
usage.server_tool_use.web_search_requests - Responses:查看
output中的web_search_call项 - Vertex AI:检查
groundingMetadata是否存在
Q: 为什么有时候没有返回搜索结果?
A: 可能的原因包括:- 问题不需要实时信息,模型判断无需搜索
- 搜索结果与问题不相关,被模型过滤
- 网络问题导致搜索失败
Q: 如何优化搜索效果?
A: 建议:- 提供清晰、具体的问题
- 使用适当的搜索上下文大小
- 提供用户位置信息以获取本地化结果
- 在 Messages API 中使用域名过滤聚焦搜索范围