由于训练数据的时效性限制,大模型无法准确回答如股票价格、明日天气等实时问题,启用联网搜索功能后,模型将基于实时检索数据回复。
使用方式
联网搜索功能支持三种调用方式,启用参数有所不同:
OpenAI 兼容-Responses API
通过 tools 参数启用网页抓取功能,需添加 web_search 工具。
Responses API 仅支持Qwen3.5、qwen3-max、qwen3-max-2026-01-23。
为了获得最佳回复效果,建议同时开启web_search、web_extractor和code_interpreter工具。
# 导入依赖与创建客户端...
response = client.responses.create(
model="qwen3-max-2026-01-23",
input="杭州天气",
tools=[
{"type": "web_search"},
{"type": "web_extractor"},
{"type": "code_interpreter"}
],
extra_body={"enable_thinking": True}
)OpenAI 兼容-Chat Completions API
传递 enable_search: true 参数可启用联网搜索功能。
# 导入依赖与创建客户端...
completion = client.chat.completions.create(
# 需使用支持联网搜索的模型
model="qwen3-max",
messages=[{"role": "user", "content": "杭州明天天气如何"}],
# 由于 enable_search 非 OpenAI 标准参数,使用 Python SDK 需要通过 extra_body 传入(使用Node.js SDK 需作为顶层参数传入)
extra_body={"enable_search": True}
)DashScope
传递 enable_search: true 参数可启用联网搜索功能。
# 导入依赖...
response = dashscope.Generation.call(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 需使用支持联网搜索的模型
model="qwen3-max",
messages=[{"role": "user", "content": "杭州明天天气如何"}],
# 通过 enable_search 参数开启联网搜索
enable_search=True,
result_format="message"
)支持的模型
国际
千问Plus:qwen3.5-plus、qwen3.5-plus-2026-02-15及之后的快照版本
思考模型和非思考模式下均支持
agent和agent_max的搜索策略。千问Flash:qwen3.5-flash、qwen3.5-flash-2026-02-23及之后的快照版本
思考模型和非思考模式下均支持
agent和agent_max的搜索策略。千问max:
qwen3-max和qwen3-max-2026-01-23:
非思考模式:搜索策略需设为
agent。思考模式:搜索策略需设为
agent或agent_max(在agent基础上支持网页抓取)。
qwen3-max-2025-09-23:搜索策略需设为
agent。
千问Omni: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.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角色扮演:qwen-plus-character、qwen-flash-character
全球
千问Plus:qwen3.5-plus、qwen3.5-plus-2026-02-15及之后的快照版本
千问Flash:qwen3.5-flash、qwen3.5-flash-2026-02-23及之后的快照版本
以上模型在思考模型和非思考模式下均支持agent 和agent_max的搜索策略。
中国内地
千问max:
qwen3-max和qwen3-max-2026-01-23:
非思考模式:搜索策略需设为
agent思考模式:搜索策略需设为
agent或agent_max(在agent基础上支持网页抓取)
qwen3-max-2025-09-23:搜索策略需设为
agent
千问Plus:qwen3.5-plus、qwen3.5-plus-2026-02-15及之后的快照版本
思考模型和非思考模式下均支持
agent和agent_max的搜索策略千问Flash:qwen3.5-flash、qwen3.5-flash-2026-02-23及之后的快照版本
思考模型和非思考模式下均支持
agent和agent_max的搜索策略千问Omni: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.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角色扮演:qwen-plus-character
快速开始
运行以下代码快速通过联网搜索服务查询股票信息。
OpenAI 兼容
OpenAI 兼容协议不支持在响应中返回搜索来源。
Python
import os
from openai import OpenAI
client = OpenAI(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen3-max",
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 港元**。
请注意,股价会实时波动,以上信息仅供参考。Node.js
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
});
async function main() {
const completion = await openai.chat.completions.create({
model: "qwen3-max",
messages: [
{ role: "user", content: "阿里巴巴股价如何" }
],
enable_search: true,
search_options: {
// 联网搜索策略,仅支持配置为 agent
search_strategy: "agent"
}
});
console.log(completion.choices[0].message.content);
}
main();响应示例
根据最新的市场数据,阿里巴巴的股价在不同市场表现如下:
* **美股 (BABA)**:最新股价约为 **159.84 美元**。
* **港股 (09988.HK)**:最新股价约为 **158.00 港元**。
请注意,股价会实时波动,以上信息仅供参考。根据最新的市场数据,阿里巴巴的股价在不同市场表现如下:
* **美股 (BABA)**:最新股价约为 **159.84 美元**。
* **港股 (09988.HK)**:最新股价约为 **158.00 港元**。
请注意,股价会实时波动,以上信息仅供参考。curl
curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-max",
"messages": [
{
"role": "user",
"content": "阿里巴巴股价如何"
}
],
"enable_search": true,
"search_options": {
"search_strategy": "agent"
}
}'DashScope
DashScope 协议支持设置enable_source为true,使返回数据中包含搜索来源。
Python
import os
import dashscope
dashscope.base_http_api_url = "https://dashscope-intl.aliyuncs.com/api/v1"
response = dashscope.Generation.call(
api_key=os.getenv("DASHSCOPE_API_KEY"),
model="qwen3-max",
messages=[{"role": "user", "content": "阿里巴巴股价"}],
enable_search=True,
search_options={
# 联网搜索策略,目前仅支持 agent 策略:可多次调用联网搜索工具与大模型,实现多轮信息检索与内容整合
"search_strategy": "agent",
"enable_source": True # 是否返回搜索来源
},
result_format="message",
)
print("="*20 + "搜索结果" + "="*20)
for web in response.output.search_info["search_results"]:
print(f"[{web['index']}]: [{web['title']}]({web['url']})")
print("="*20 + "回复内容" + "="*20)
print(response.output.choices[0].message.content)响应示例
====================搜索结果====================
[1]: [阿里巴巴(BABA)股票价格_行情_走势图 - 东方财富](https://wap.eastmoney.com/quote/stock/106.BABA.html)
[2]: [阿里巴巴(BABA)_美股行情_今日股价与走势图_新浪财经](https://gu.sina.cn/quotes/us/BABA)
[3]: [阿里巴巴(BABA)股票最新价格行情,实时走势图,股价分析预测](https://cn.investing.com/equities/alibaba)
[4]: [阿里巴巴-W (9988.HK) 股價、新聞、報價和記錄 - Yahoo 財經](https://hk.finance.yahoo.com/quote/9988.HK/)
[5]: [阿里巴巴(BABA)股票股价_股价行情_讨论 - 雪球](https://xueqiu.com/S/BABA)
[6]: [阿里巴巴(BABA)股票股价, 市值, 实时行情, 走势图, 财报- Moomoo](https://www.moomoo.com/hans/stock/BABA-US)
[7]: [Alibaba Group Holding Limited (BABA) Stock Price, News, Quote ...](https://finance.yahoo.com/quote/BABA/)
[8]: [阿里巴巴 - 腾讯证券](https://gu.qq.com/usBABA.N)
[9]: [W(09988)股票股价, 市值, 实时行情, 走势图, 财报- 阿里巴巴 - Moomoo](https://www.moomoo.com/hans/stock/09988-HK)
====================回复内容====================
根据最新的市场数据,阿里巴巴的股价信息如下:
* **美股 (BABA)**:
* 今日开盘价:160.98美元
* 昨日收盘价:160.80美元
* 今日最高价:161.19美元
* 今日最低价:156.20美元
* **港股 (09988.HK)**:
* 最新报价约为:158.00 - 158.10港元
* 今日开盘价:156.50港元
* 前一交易日收盘价:162.00港元
* 今日波动范围:156.30 - 158.40港元Java
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.SearchOptions;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.dashscope.common.Role;
import java.util.Arrays;
public class Main {
static {Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";}
public static void main(String[] args) {
Generation gen = new Generation();
Message userMsg = Message.builder()
.role(Role.USER.getValue())
.content("阿里巴巴的股价")
.build();
SearchOptions searchOptions = SearchOptions.builder()
// 联网搜索策略,仅支持配置为 agent
.searchStrategy("agent")
// 返回搜索来源
.enableSource(true)
.build();
GenerationParam param = GenerationParam.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen3-max")
.messages(Arrays.asList(userMsg))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.enableSearch(true)
.searchOptions(searchOptions)
.build();
try {
GenerationResult result = gen.call(param);
System.out.println("=".repeat(20)+"搜索结果"+"=".repeat(20));
System.out.println(result.getOutput().getSearchInfo().getSearchResults());
System.out.println("=".repeat(20)+"回复内容"+"=".repeat(20));
System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}响应示例
====================搜索结果====================
[SearchInfo.SearchResult(siteName=null, icon=null, index=1, title=阿里巴巴(BABA)股票价格_行情_走势图 - 东方财富, url=https://wap.eastmoney.com/quote/stock/106.BABA.html), SearchInfo.SearchResult(siteName=null, icon=null, index=2, title=阿里巴巴(BABA)_美股行情_今日股价与走势图_新浪财经, url=https://gu.sina.cn/quotes/us/BABA), SearchInfo.SearchResult(siteName=null, icon=null, index=3, title=阿里巴巴(BABA)股票最新价格行情,实时走势图,股价分析预测, url=https://cn.investing.com/equities/alibaba), SearchInfo.SearchResult(siteName=null, icon=null, index=4, title=阿里巴巴(BABA)股票股价_股价行情_讨论 - 雪球, url=https://xueqiu.com/S/BABA), SearchInfo.SearchResult(siteName=null, icon=null, index=5, title=阿里巴巴-W (9988.HK) 股價、新聞、報價和記錄 - Yahoo 財經, url=https://hk.finance.yahoo.com/quote/9988.HK/), SearchInfo.SearchResult(siteName=null, icon=null, index=6, title=阿里巴巴(BABA)股票股价, 市值, 实时行情, 走势图, 财报- Moomoo, url=https://www.moomoo.com/hans/stock/BABA-US), SearchInfo.SearchResult(siteName=null, icon=null, index=7, title=Alibaba Group Holding Limited (BABA) - Yahoo Finance, url=https://finance.yahoo.com/quote/BABA/), SearchInfo.SearchResult(siteName=null, icon=null, index=8, title=阿里巴巴 - 腾讯证券, url=https://gu.qq.com/usBABA.N), SearchInfo.SearchResult(siteName=null, icon=null, index=9, title=W(09988)股票股价, 市值, 实时行情, 走势图, 财报- 阿里巴巴 - Moomoo, url=https://www.moomoo.com/hans/stock/09988-HK)]
====================回复内容====================
根据最新的市场数据,阿里巴巴的股价如下:
* **美股 (BABA)**:最新股价约为 **159.84 美元**。
* **港股 (09988.HK)**:最新股价约为 **158.00 港元**。
请注意,股价会随市场交易实时波动,以上信息仅供参考。curl
curl -X POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-max",
"input":{
"messages":[
{
"role": "user",
"content": "阿里巴巴的股价"
}
]
},
"parameters": {
"enable_search": true,
"search_options": {
"search_strategy": "agent",
"enable_source": true
},
"result_format": "message"
}
}'响应示例
{
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"content": "根据最新的市场数据,阿里巴巴的股价因其在美股和港股同时上市而有所不同:\n\n* **美股 (BABA)**:最新股价约为 **160.40 美元**。\n * 今日开盘价:160.98 美元\n * 今日波动范围:156.20 - 161.19 美元\n\n* **港股 (09988.HK)**:最新股价约为 **158.10 港元**。\n * 今日开盘价:156.50 港元\n * 今日波动范围:156.30 - 158.40 港元\n\n请注意,股价会随市场交易实时变动,以上信息仅供参考。",
"role": "assistant"
}
}
],
"search_info": {
"search_results": [
{
"index": 1,
"title": "阿里巴巴(BABA)股票价格_行情_走势图 - 东方财富",
"url": "https://wap.eastmoney.com/quote/stock/106.BABA.html"
},
{
"index": 2,
"title": "阿里巴巴(BABA)_美股行情_今日股价与走势图_新浪财经",
"url": "https://gu.sina.cn/quotes/us/BABA"
},
{
"index": 3,
"title": "阿里巴巴-W (9988.HK) 股價、新聞、報價和記錄 - Yahoo 財經",
"url": "https://hk.finance.yahoo.com/quote/9988.HK/"
},
{
"index": 4,
"title": "阿里巴巴(BABA)股票最新价格行情,实时走势图,股价分析预测",
"url": "https://cn.investing.com/equities/alibaba"
},
{
"index": 5,
"title": "阿里巴巴(BABA)股票股价_股价行情_讨论 - 雪球",
"url": "https://xueqiu.com/S/BABA"
},
{
"index": 6,
"title": "阿里巴巴(BABA)股票股价, 市值, 实时行情, 走势图, 财报- Moomoo",
"url": "https://www.moomoo.com/hans/stock/BABA-US"
},
{
"index": 7,
"title": "W(09988)股票股价, 市值, 实时行情, 走势图, 财报- 阿里巴巴 - Moomoo",
"url": "https://www.moomoo.com/hans/stock/09988-HK"
},
{
"index": 8,
"title": "Alibaba Group Holding Limited (BABA) 股價、新聞、報價和記錄",
"url": "https://hk.finance.yahoo.com/quote/BABA/"
},
{
"index": 9,
"title": "阿里巴巴 - 腾讯证券",
"url": "https://gu.qq.com/usBABA.N"
}
]
}
},
"usage": {
"input_tokens": 2004,
"output_tokens": 203,
"plugins": {
"search": {
"count": 1,
"strategy": "agent"
}
},
"prompt_tokens_details": {
"cached_tokens": 0
},
"total_tokens": 2207
},
"request_id": "45c231d2-811e-4e04-a361-f2c1909f1dd9"
}Responses API 的联网搜索
通过 tools 参数启用网页抓取功能,需在tools数组中添加 web_search 工具。
仅支持qwen3.5-plus、qwen3.5-plus-2026-02-15、qwen3.5-flash、qwen3.5-flash-2026-02-23;以及思考模式下的 qwen3-max、qwen3-max-2026-01-23。
为了获得最佳回复效果,建议同时开启web_search、web_extractor和code_interpreter工具。
from openai import OpenAI
import os
client = OpenAI(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
)
response = client.responses.create(
model="qwen3-max-2026-01-23",
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)}")
# 取消以下注释查看中间过程的输出
# for r in response.output:
# print(r.model_dump_json())import OpenAI from "openai";
const openai = new OpenAI({
// 若没有配置环境变量,请用百炼API Key将下行替换为:apiKey: "sk-xxx",
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});
async function main() {
const response = await openai.responses.create({
model: "qwen3-max-2026-01-23",
input: "新加坡天气",
tools: [
{ type: "web_search" },
{ type: "web_extractor" },
{ type: "code_interpreter" }
],
enable_thinking: true
});
console.log("====================回复内容====================");
console.log(response.output_text);
设置搜索量级策略
console.log("====================工具调用次数====================");
可根据对成本、效果和响应速度的要求,通过search_strategy设置搜索量级策略。
console.log(`联网搜索次数: ${response.usage.x_tools.web_search?.count || 0}`);
// console.log(JSON.stringify(response.output[0], null, 2));
}
main();curl -X POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-max-2026-01-23",
"input": "新加坡天气",
"tools": [
{"type": "web_search"},
{"type": "web_extractor"},
{"type": "code_interpreter"}
],
"enable_thinking": true
}'计费说明
计费涉及两个方面:
模型调用费用:联网搜索的网页内容会拼接到提示词中,增加模型的输入 Token,按照模型的标准价格计费。价格详情请参考模型列表。
搜索策略费用:
agent 策略:
每调用 1000 次的费用为:
中国内地和全球部署模式:$0.573411
国际部署模式 $10.00。
agent_max 策略(限时优惠):
包含联网搜索与网页抓取的费用。
联网搜索工具每 1000 次调用费用:
中国内地部署模式:$0.573411。
国际部署模式:$10.00。
网页抓取工具限时免费。
错误信息
如果执行报错,请参见错误信息进行解决。