Skip to main content

連網搜尋

大模型的訓練資料存在知識到期日,無法回答即時問題。啟用連網搜尋後,模型可從網路擷取即時資料,準確回答股票價格、天氣預報、最新新聞等時效性問題。

使用方式

DashScope、Chat Completions 設定 enable_search: true,透過 search_options 配置 agent 或 agent_max;Responses、Anthropic 透過 web_search 工具啟用網路搜尋。

支援的模型

下表按模型和協議列出網路搜尋的支援情況。 Qwen 版本系列指該版本下的 Max、Plus、Flash 和開源模型。

千問模型

模型agent / agent_maxweb_search (Responses)web_search (Anthropic)
Qwen4、Qwen3.8不支援支援支援
Qwen3.7支援支援Max / Plus / Flash
Qwen3.8-Omni-Flash僅 agent支援不支援
Qwen3.5-Omni(Plus、Flash)
Qwen3.8-Omni-Flash-Realtime
Qwen3.5-Omni-Realtime(Plus、Flash)
僅 agent不支援不支援

第三方模型

模型agent / agent_maxweb_search (Responses)web_search (Anthropic)
deepseek-v4-pro、deepseek-v4-pro-0813
deepseek-v4-flash-0731、deepseek-v4.1-flash-1017
支援支援支援
glm-5.2、glm-5.3不支援支援支援
kimi-k3、kimi-k3.1-pro不支援支援支援
模型agent / agent_maxweb_search (Responses)web_search (Anthropic)
Qwen3.6(Plus、Flash)支援支援支援
qwen3.6-35b-a3b支援支援不支援
qwen3.6-27b支援不支援不支援
Qwen3.5(Plus、Flash)支援支援支援
qwen3.5-27b、qwen3.5-35b-a3b
qwen3.5-122b-a10b、qwen3.5-397b-a17b
支援支援不支援
Qwen-Plus、Qwen-Flash 支援 agent 策略。

呼叫範例

以下範例使用新加坡配置,查詢天氣及景區遊覽提醒。DashScope、Chat Completions 使用多模態模型 qwen3.7-plus 和 agent 策略;Responses、Anthropic 使用 qwen4-max 和 web_search 工具。 其他地域的接入域名和業務空間部署範圍,參見選擇地域、服務部署範圍和接入域名。 多模態模型使用多模態介面;DashScope 網路搜尋需流式呼叫(stream=True),Java 使用 MultiModalConversation.streamCall。文字模型使用文字生成介面。
  • DashScope
  • Chat Completions
  • Responses
  • Anthropic
import os
import dashscope

client = dashscope.Client(
    api_key=os.environ["DASHSCOPE_API_KEY"],
    workspace=os.environ["DASHSCOPE_WORKSPACE"],
    region="ap-southeast-1",
)
stream = client.multimodal.chat.create(
    model="qwen3.7-plus",
    messages=[{"role": "user", "content": [{"text": 'Search the web for current weather in Singapore and recent visitor notices for Gardens by the Bay. Give temperature, rain, a short route, clothing advice, and source URLs in at most 200 words.'}]}],
    stream=True,
    incremental_output=True,
    max_tokens=2500,
    enable_search=True,
    search_options={"search_strategy": "agent", "enable_source": True},
)
usage = None
sources_printed = False
for chunk in stream:
    if chunk.search_info and chunk.search_info.search_results and not sources_printed:
        print(chunk.search_info.model_dump_json(indent=2))
        sources_printed = True
    for choice in chunk.choices:
        print(choice.delta.content or "", end="", flush=True)
    if chunk.usage:
        usage = chunk.usage
print()
if usage:
    print(usage.model_dump_json(indent=2))
output.search_info.search_results 傳回搜尋來源,usage.plugins.search 傳回搜尋計數和策略。

檢視響應範例

以下響應省略了重複條目、內容分片和長文字。
id:1
event:result
:HTTP_STATUS/200
data: {"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","reasoning_content":"Thinking","content":[]}}],"search_info":{"search_results":[]}},"usage":{"input_tokens":370,"output_tokens":1,"total_tokens":371,"output_tokens_details":{"reasoning_tokens":1,"text_tokens":1},"prompt_tokens_details":{"cached_tokens":0},"input_tokens_details":{"text_tokens":370}},"request_id":"1e85c487-48dd-980f-853c-5bd164877bf9"}

... (repeated chunks omitted)

id:93
event:result
:HTTP_STATUS/200
data: {"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","reasoning_content":"","content":[]}}],"search_info":{"search_results":[{"title":"Singapore, Central Singapore, Singapore Weather Forecast","url":"https://www.accuweather.com/en/sg/singapore/300597/weather-forecast/300597","index":1},"... (remaining entries omitted)"]}},"usage":{"input_tokens":370,"output_tokens":357,"total_tokens":727,"output_tokens_details":{"reasoning_tokens":305,"text_tokens":357},"prompt_tokens_details":{"cached_tokens":0},"plugins":{"search":{"count":1,"strategy":"agent"}},"input_tokens_details":{"text_tokens":370}},"request_id":"1e85c487-48dd-980f-853c-5bd164877bf9"}

... (repeated chunks omitted)

id:372
event:result
:HTTP_STATUS/200
data: {"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":[{"text":"**"}],"reasoning_content":""}}],"search_info":{"search_results":[{"title":"Singapore, Central Singapore, Singapore Weather Forecast","url":"https://www.accuweather.com/en/sg/singapore/300597/weather-forecast/300597","index":1},"... (remaining entries omitted)"]}},"usage":{"input_tokens":2425,"output_tokens":1322,"total_tokens":3747,"output_tokens_details":{"reasoning_tokens":1266,"text_tokens":1322},"prompt_tokens_details":{"cached_tokens":0},"plugins":{"search":{"count":1,"strategy":"agent"}},"input_tokens_details":{"text_tokens":2425}},"request_id":"1e85c487-48dd-980f-853c-5bd164877bf9"}

... (repeated chunks omitted)

id:445
event:result
:HTTP_STATUS/200
data: {"output":{"choices":[{"finish_reason":"stop","message":{"role":"assistant","content":[],"reasoning_content":""}}],"search_info":{"search_results":[{"title":"Singapore, Central Singapore, Singapore Weather Forecast","url":"https://www.accuweather.com/en/sg/singapore/300597/weather-forecast/300597","index":1},"... (remaining entries omitted)"]}},"usage":{"input_tokens":2425,"output_tokens":1591,"total_tokens":4016,"output_tokens_details":{"reasoning_tokens":1266,"text_tokens":1591},"prompt_tokens_details":{"cached_tokens":0},"plugins":{"search":{"count":1,"strategy":"agent"}},"input_tokens_details":{"text_tokens":2425}},"request_id":"1e85c487-48dd-980f-853c-5bd164877bf9"}

計費說明

網路搜尋費用由模型呼叫費用和搜尋工具費用組成。
  • 模型呼叫費用:檢索到的網頁內容增加輸入 Token,按所用模型的標準價格計費,價格請檢視百炼控制台。
  • 搜尋工具費用:按下表計費,Responses 和 Anthropic 的 web_search 與 agent 策略價格相同。
搜尋策略或工具地域每 1000 次呼叫價格
agent、agent_max、web_search華北2(北京)、美國(弗吉尼亞)、中國香港、日本(東京)、德國(法蘭克福)$0.573411
agent、agent_max、web_search新加坡$10.00
agent_max 當前為限時優惠價格,包含網路搜尋與網頁抓取費用,其中網頁抓取工具限時免費。

常見問題

Q:網頁搜尋後,模型回覆「無法回答」或沒有回應?

A:網頁搜尋結果可能包含受管制的資訊,觸發內容安全規則,導致模型回傳 DataInspectionFailed 錯誤(HTTP 400),並回覆「抱歉,我無法回答這個問題」。排查方法:停用網頁搜尋並重新傳送相同查詢;若模型正常回覆,即可確認是搜尋回傳的內容觸發了攔截。內容安全攔截是非確定性行為,取決於搜尋實際回傳的具體內容;並非所有敏感主題的查詢都會觸發。

Q:帳單的網路搜尋次數與 API 傳回的搜尋次數不一致,如何對帳?

A:不同協定和模型傳回的搜尋計次欄位不同:
  • DashScope: usage.plugins.search.count.
  • Chat Completions: usage.x_tools.search.count.
  • Responses: usage.x_tools.web_search.count / tool_usage.web_search.num_requests.
  • Anthropic: usage.server_tool_use.web_search_requests.
其中,名稱為 count 的欄位表示本輪對話經意圖判定需要網路搜尋,不代表檢索已實際完成。即使觸發限流或檢索未傳回結果,仍可能傳回計數;num_requests 和 web_search_requests 表示 API 傳回的搜尋請求次數。 帳單以搜尋服務實際完成檢索並正常傳回結果為計費依據。API 傳回的計次欄位用於觀察呼叫情況,對帳請以帳單出帳資料為準。

錯誤資訊

如果執行報錯,請參見錯誤碼進行解決。