聯網搜尋(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 支援聯網搜尋。