Skip to main content

联网搜索

大模型的训练数据存在知识截止日期,无法回答实时问题。启用联网搜索后,模型可从网络获取实时数据,准确回答股票价格、天气预报、最新新闻等时效性问题。

使用方式

联网搜索支持以下四种API调用方式,启用参数各有不同:
  • OpenAI 兼容-Responses API
  • OpenAI 兼容-Chat Completions API
  • DashScope
  • Anthropic 兼容
通过 tools 参数添加 web_search 工具即可启用联网搜索。
Responses API 仅支持部分模型,支持的模型请参见Responses API的联网搜索。
# 导入依赖与创建客户端...
response = client.responses.create(
    model="qwen3.8-max",
    input="杭州天气",
    tools=[
        {"type": "web_search"},
        {"type": "web_extractor"},
        {"type": "code_interpreter"}
    ],
    extra_body={"enable_thinking": True}
)

多模态模型的联网搜索

Qwen3.8 Max、Flash 与开源系列、Qwen3.7-Flash、Qwen3.7-Plus、Qwen3.6 系列、Qwen3.5 系列(含 qwen3.5-plus、qwen3.5-flash、qwen3.5-omni 系列与千问开源模型)等模型支持图片、视频等多模态输入,属于多模态模型。 搜索策略因系列而异:qwen3.5-omni 系列仅支持 agent 策略;qwen3.8-max、qwen3.8-max-0902、qwen3.8-flash、qwen3.8-2.4t-a95b 和 qwen3.8-27b 在多模态接口下不支持 agent 策略(使用默认的 turbo 或 max),如需对 Qwen3.8 使用 agent 式检索,请改用 Responses API 的联网搜索;本节列出的其余模型支持 turbo、max、agent 策略。这类模型需通过多模态接口(multimodal-generation 端点)调用:Python 与 Java 使用 MultiModalConversation,而不能使用面向纯文本模型的 Generation(text-generation 端点)。多模态模型的基础调用方式可参见《视觉推理》《图像与视频理解》文档。
若使用 Generation(text-generation 端点)调用上述多模态模型,会返回 400 url error, please check url,请改用 MultiModalConversation(multimodal-generation 端点)。Java SDK 的 MultiModalConversationParam 提供 enableSearch(true) 用于开启联网搜索,但未提供 searchOptions() 方法,需通过通用参数 parameter("search_options", ...) 注入搜索策略等配置;Python 的 MultiModalConversation.call 可直接传入 search_options。多模态模型开启联网搜索时需使用流式调用(Java 使用 streamCall,Python 设置 stream=True),否则会返回 Non-streaming mode does not support Web Search 报错。
Python
import os
import dashscope
from dashscope import MultiModalConversation
# 以下为新加坡地域配置,调用时请将 {WorkspaceId} 替换为真实的业务空间ID,各地域的配置不同。
dashscope.base_http_api_url = "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1"
responses = MultiModalConversation.call(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # 需使用支持联网搜索的多模态模型
    model="qwen3.5-plus",
    messages=[{"role": "user", "content": [{"text": "杭州今天天气如何"}]}],
    # 多模态接口可直接传入 enable_search 与 search_options
    enable_search=True,
    search_options={
        # qwen3.5-omni 系列需设为 agent;Qwen3.8 系列在多模态接口下不支持 agent,保持默认即可
        "search_strategy": "agent",
        "enable_source": True,
    },
    # 多模态模型开启联网搜索时需使用流式调用
    stream=True,
    incremental_output=True,
)
for response in responses:
    print(response.output.choices[0].message.content)

支持的模型

支持联网搜索的模型如下。支持多模态输入的模型(Qwen3.8 Max、Flash 与开源系列、Qwen3.7-Flash/Plus、Qwen3.6 系列、Qwen3.5 系列及千问开源模型等)需通过多模态接口调用,参见多模态模型的联网搜索。
  • 新加坡
  • 华北2(北京)
  • 全球
  • 千问
    • Qwen3.8 系列:qwen3.8-max、qwen3.8-max-0902、qwen3.8-flash、qwen3.8-2.4t-a95b、qwen3.8-27b(search_strategy 不支持 agent;如需 agent 式检索,请通过 Responses API 使用 web_search 工具)
    • Qwen3.7 系列:qwen3.7-max、qwen3.7-max-preview、qwen3.7-max-2026-05-17及之后的快照版本、qwen3.7-plus、qwen3.7-plus-2026-05-26及之后的快照版本、qwen3.7-flash、qwen3.7-flash-2026-07-15及之后的快照版本
    • Qwen3.6 系列:qwen3.6-plus、qwen3.6-plus-2026-04-02及之后的快照版本、qwen3.6-flash、qwen3.6-flash-2026-04-16及之后的快照版本、qwen3.6-27b、qwen3.6-35b-a3b
    • Qwen3.5 系列:qwen3.5-plus、qwen3.5-plus-2026-02-15及之后的快照版本、qwen3.5-flash、qwen3.5-flash-2026-02-23及之后的快照版本、qwen3.5-27b、qwen3.5-35b-a3b、qwen3.5-122b-a10b、qwen3.5-397b-a17b
    • 千问Omni:qwen3.8-omni-flash、qwen3.5-omni-plus、qwen3.5-omni-plus-2026-03-15、qwen3.5-omni-flash、qwen3.5-omni-flash-2026-03-15(搜索策略需设为 agent)
    • 千问Omni-Realtime:qwen3.8-omni-flash-realtime、qwen3.5-omni-plus-realtime、qwen3.5-omni-plus-realtime-2026-03-15、qwen3.5-omni-flash-realtime、qwen3.5-omni-flash-realtime-2026-03-15(搜索策略需设为 agent)
  • 第三方模型
    • DeepSeek:deepseek-v4-pro、deepseek-v4-pro-0813、deepseek-v4-flash-0731(其中 deepseek-v4 系列同时支持Responses API)
    • GLM:glm-5.2(仅支持通过Responses API联网搜索)
    • Kimi:kimi-k3(仅支持通过Responses API联网搜索)

快速开始

以下示例通过联网搜索查询股票信息。
  • OpenAI 兼容
  • DashScope
通过 enable_search=True 开启联网搜索。使用 qwen3.8-omni-flash 时,还需设置 search_options={"search_strategy": "agent"};如通过 Responses API 调用,则配置 tools=[{"type": "web_search"}],见Responses API 联网搜索。
OpenAI 兼容协议不支持在响应中返回搜索来源。
  • Python
  • Node.js
  • curl
import os
from openai import OpenAI

client = OpenAI(
    # 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # 以下为新加坡地域配置,调用时请将 {WorkspaceId} 替换为真实的业务空间ID,各地域的配置不同。
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
    model="qwen-plus",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "阿里巴巴股价如何"},
    ],
    extra_body={
        "enable_search": True,
        "search_options": {
            # 联网搜索策略,仅支持配置为 agent
            "search_strategy": "agent"
        }
    }
)
print(completion.choices[0].message.content)
响应示例
根据最新的市场数据,阿里巴巴的股价在不同市场表现如下:

*   **美股 (BABA)**:最新股价约为 **159.84 美元**。
*   **港股 (09988.HK)**:最新股价约为 **158.00 港元**。

请注意,股价会实时波动,以上信息仅供参考。根据最新的市场数据,阿里巴巴的股价在不同市场表现如下:

*   **美股 (BABA)**:最新股价约为 **159.84 美元**。
*   **港股 (09988.HK)**:最新股价约为 **158.00 港元**。

请注意,股价会实时波动,以上信息仅供参考。

Responses API的联网搜索

通过 tools 参数的tools数组中添加 web_search 工具即可启用联网搜索。
通过 web_search 工具启用时,模型以 agent 式多轮检索 运行——根据问题自主发起多次 web_search_call(必要时配合 web_extractor_call),最终整合结果作答。
支持以下模型:Qwen3.8 Max、Flash 与开源系列、qwen3.8-omni-flash;Qwen3.7、Qwen3.5 系列模型(含开源模型,不含Omni)、Qwen3.6-Plus、Qwen3.6-Flash 系列及 qwen3.6-35b-a3b;deepseek-v4-flash-0731、deepseek-v4-pro、deepseek-v4-pro-0813;glm-5.2;kimi-k3。
Qwen3.8 系列的 agent 式检索:Qwen3.8 系列(qwen3.8-max、qwen3.8-max-0902、qwen3.8-flash、qwen3.8-2.4t-a95b、qwen3.8-27b)在 Chat Completions/DashScope 协议下不支持 search_strategy: agent,但通过本节的 Responses API web_search 工具即可实现同等的 agent 式多轮检索能力。
Responses API 不使用 search_strategy 参数。是否发起联网搜索、以及是否进行多轮检索,均由是否挂载 web_search 工具及模型自主决策决定;传入 search_options.search_strategy 会被静默忽略,不会触发搜索。
对于支持以下工具的模型,建议同时开启 web_search、web_extractor 和 code_interpreter。qwen3.8-omni-flash 的内置工具仅支持 web_search。
关于Responses API的使用说明、代码示例和迁移指南,请参见 OpenAI兼容-Responses 。
from openai import OpenAI
import os

client = OpenAI(
    # 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # 以下为新加坡地域配置,调用时请将 {WorkspaceId} 替换为真实的业务空间ID,各地域的配置不同。
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)

response = client.responses.create(
    # Qwen3.8 系列通过 Responses API 的 web_search 工具即可使用 agent 式多轮检索
    model="qwen3.8-max",
    input="新加坡明天天气怎么样?",
    tools=[
        {"type": "web_search"},
        {"type": "web_extractor"},
        {"type": "code_interpreter"}
    ],
    extra_body={"enable_thinking": True}
)

print("="*20 + "回复内容" + "="*20)
print(response.output_text)

print("="*20 + "工具调用次数" + "="*20)
usage = response.usage
if hasattr(usage, 'x_tools') and usage.x_tools:
    print(f"联网搜索次数: {usage.x_tools.get('web_search', {}).get('count', 0)}")
    print(f"网页抓取次数: {usage.x_tools.get('web_extractor', {}).get('count', 0)}")

# 打印每一轮 web_search_call 的搜索来源,观察 agent 式多轮检索过程
print("="*20 + "搜索来源" + "="*20)
for item in response.output:
    if item.type == "web_search_call":
        for i, source in enumerate(item.action.sources, start=1):
            print(f"[{i}] {source.url}")
以 qwen3.8-max 请求 新加坡明天天气怎么样? 为例,output 数组通常包含多次 web_search_call 与 web_extractor_call,usage.x_tools.web_search.count 会大于 1,体现 agent 式多轮检索:
{
  "output": [
    { "type": "reasoning" },
    { "type": "web_search_call", "action": { "sources": [ "..." ] } },
    { "type": "web_extractor_call" },
    { "type": "reasoning" },
    { "type": "web_search_call", "action": { "sources": [ "..." ] } },
    { "type": "message", "content": [ "..." ] }
  ],
  "usage": {
    "x_tools": {
      "web_search":    { "count": 2 },
      "web_extractor": { "count": 2 }
    }
  }
}

获取搜索来源

执行联网搜索后,搜索来源会在响应的 output 数组中 type 为 web_search_call 的元素内返回,其 action.sources 字段为搜索来源链接列表。可在上述示例的 response 基础上按如下方式提取:
Responses API 暂不支持 enable_source、enable_citation、citation_format 参数,不会在回复内容中自动插入 [1] 角标。如需角标标注,请使用 DashScope 调用方式。
# 在上述 response 的基础上提取搜索来源
print("=" * 20 + "搜索来源" + "=" * 20)
for item in response.output:
    if item.type == "web_search_call":
        for i, source in enumerate(item.action.sources, start=1):
            print(f"[{i}] {source.url}")

计费说明

本文所述“联网搜索”为模型内置的联网搜索功能,其计费如下方所示,本身不提供免费调用额度。它与百炼 MCP 广场提供的“联网搜索 MCP”服务是相互独立的两个功能,计费也相互独立:联网搜索 MCP 全部用户前 2000 次调用免费,免费额度用尽后按 29 元/千次计费,详情请参见添加联网搜索MCP。
联网搜索的费用包含两部分:
  • 模型调用费用:联网搜索的网页内容会拼接到提示词中,增加模型的输入 Token,按照模型的标准价格计费。价格详情请参考百炼控制台。使用 Responses API 或 Anthropic 兼容方式时,联网搜索工具的计费与 agent 策略相同。
  • 搜索策略费用:
    • agent 策略:
      • 每调用 1000 次的费用为:
        • 华北2(北京)、美国(弗吉尼亚)、中国香港、日本(东京)、德国(法兰克福)地域:$0.573411
        • 新加坡地域 $10.00。
    • agent_max 策略(限时优惠): 包含联网搜索与网页抓取的费用。
      • 联网搜索工具每 1000 次调用费用:
        • 华北2(北京)、美国(弗吉尼亚)、中国香港、日本(东京)、德国(法兰克福)地域:$0.573411。
        • 新加坡地域:$10.00。
      • 网页抓取工具限时免费。

Q:联网搜索后模型返回“无法回答”或无响应?

A:联网搜索结果可能包含管控信息,触发内容安全规则,导致模型返回 DataInspectionFailed 错误 (HTTP 400),响应内容为“抱歉,我无法回答这个问题”。排查方法:关闭联网搜索后重新发送相同查询,若模型正常回复,则确认是搜索返回的内容触发了拦截。内容安全拦截为非确定性行为,取决于搜索返回的具体内容,并非所有敏感话题查询都会触发。

Q:账单的联网搜索次数与 API 返回的 count 字段不一致,如何对账?

A:两者统计口径不同,数值存在差异属于预期情况:
  • 账单计量:以搜索服务实际完成检索并正常返回结果为计费依据。
  • count 字段(DashScope 协议为 usage.plugins.search.count,OpenAI 兼容协议为 usage.x_tools.search.count,两者取值一致):表示本轮对话经意图判定需要联网搜索,在意图判定阶段即被置位,不代表检索已实际完成。即使触发限流或检索未返回结果,该字段仍会返回数值,不会置 0 或省略。
因此 count 不作为计费或对账的依据,对账请以账单出账数据为准。

错误信息

如果执行报错,请参见错误码进行解决。
Token Plan
模型体验
用量统计与性能监控
资产中心
服务支持