联网搜索(Web Search)
DeepSeek 联网搜索
通过 Responses 接口的 web_search 工具,让 DeepSeek V4.1 Flash 联网搜索实时信息并给出来源。
📝 简介
deepseek-v4.1-flash 支持联网搜索:模型会根据问题自动决定是否搜索、搜什么关键词,再结合搜索结果作答,并在回答中附上来源链接。适合查询新闻、天气、行情、赛事结果等时效性强的信息。
联网搜索通过 Responses 接口(POST /v1/responses)的 web_search 工具开启。
使用前提
- 令牌分组选择 Deepseek官转
- 模型:
deepseek-v4.1-flash - 接口:
POST https://api.88api.ai/v1/responses
💡 请求示例
基础用法 ✅
curl https://api.88api.ai/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"model": "deepseek-v4.1-flash",
"tools": [{"type": "web_search"}],
"input": "今天杭州天气怎么样?"
}'Python(OpenAI SDK) ✅
from openai import OpenAI
client = OpenAI(base_url="https://api.88api.ai/v1", api_key="sk-你的令牌")
resp = client.responses.create(
model="deepseek-v4.1-flash",
tools=[{"type": "web_search"}],
input="今天杭州天气怎么样?",
)
print(resp.output_text)限制关键词数和结果数 ✅
curl https://api.88api.ai/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"model": "deepseek-v4.1-flash",
"max_tool_calls": 2,
"tools": [{
"type": "web_search",
"max_keyword": 2,
"limit": 5,
"user_location": {"type": "approximate", "country": "中国", "region": "浙江", "city": "杭州"}
}],
"input": "今天杭州天气怎么样?"
}'流式输出 ✅
在请求体中加上 "stream": true 即可。搜索过程会依次返回以下事件,之后才开始输出回答文字:
| 事件 | 含义 |
|---|---|
response.web_search_call.in_progress | 开始一次搜索 |
response.web_search_call.searching | 正在搜索 |
response.web_search_call.completed | 本次搜索完成 |
response.output_text.delta | 回答文字(逐段输出) |
response.completed | 全部完成,包含 usage |
📬 响应示例
以下为节选,省略了思考过程(reasoning)等内容:
{
"object": "response",
"status": "completed",
"output": [
{
"type": "web_search_call",
"id": "ws_0217925902513450000...",
"status": "completed",
"action": {
"type": "search",
"query": "杭州今天天气"
}
},
{
"type": "message",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "杭州今天上午雨停,转为阴到多云,最高气温约 21–24℃……",
"annotations": [
{
"type": "url_citation",
"title": "中国气象局-天气预报-城市预报",
"url": "https://weather.cma.cn/web/text/HD/AZJ.html",
"site_name": "中国气象局",
"publish_time": "2026年10月21日 08:00:00(CST) 星期三"
}
]
}
]
}
],
"usage": {
"input_tokens": 4320,
"output_tokens": 418,
"total_tokens": 4738,
"tool_usage": {"web_search": 1},
"tool_usage_details": {"web_search": {"search_engine": 1}}
}
}| 字段 | 说明 |
|---|---|
output[].type = "web_search_call" | 模型发起的一次搜索,action.query 是所用关键词;一轮搜索有多个关键词时用 ; 分隔 |
output[].type = "message" | 最终回答,output_text.text 为正文 |
annotations[].type = "url_citation" | 回答引用的来源:标题、链接、网站名、发布时间 |
usage.tool_usage.web_search | 本次请求的搜索次数(按关键词计) |
⚙️ 参数说明
tools 中的 web_search
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
type | string | 是 | 固定为 web_search |
max_keyword | integer | 否 | 每轮搜索最多使用几个关键词,取值 1–50 |
limit | integer | 否 | 每次搜索最多返回几条结果,取值 1–50,默认 10 |
user_location | object | 否 | 用户大致位置,提升天气、本地生活等问题的准确度。格式:{"type": "approximate", "country": "中国", "region": "浙江", "city": "杭州"} |
请求级参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
max_tool_calls | integer | 否 | 一次回答中最多搜索几轮,取值 1–10,默认 3 |
stream | boolean | 否 | 是否流式输出 |
其余参数(如 input、instructions、max_output_tokens)与 OpenAI 响应格式(Responses) 相同。
💰 计费说明
费用 = 模型 token 费用 + 搜索费用:
- 搜索费用按关键词次数计:一轮搜索用了几个关键词就计几次,以返回结果中的
usage.tool_usage.web_search为准。模型判断不需要搜索时,不产生搜索费用。 - token 费用:搜索结果会作为输入交给模型阅读,所以输入 token 会明显增加(一次搜索通常增加几千 token),按模型价格计费。
- 想控制费用,可以用
max_keyword限制每轮关键词数、用max_tool_calls限制搜索轮数。
❓ 常见问题
用 /v1/chat/completions 可以联网搜索吗?
不可以。联网搜索只在 /v1/responses 接口生效;在 Chat Completions 接口传 web_search 工具不会搜索,也不会产生搜索费用。
为什么有时没有搜索? 是否搜索由模型根据问题判断。如果问题不需要实时信息(例如数学计算、通用知识),模型会直接回答。需要强制联网时,可以在问题里明确要求「请联网搜索后回答」。
其他分组可以用吗?
目前仅 Deepseek官转 分组的 deepseek-v4.1-flash 支持联网搜索。