由於訓練資料的時效性限制,大模型無法準確回答如股票價格、明日天氣等即時問題,啟用連網搜尋功能後,模型將基於即時檢索資料回複。
使用方式
調用模型時,傳遞 enable_search: true 參數可啟用連網搜尋功能。啟用後,模型將判斷使用者問題是否需要連網查詢:若需要,則結合搜尋結果回答;若不需要,則直接使用模型自身知識回答。
以 OpenAI 與 DashScope 的 Python SDK 核心調用代碼為例,介紹如何啟用連網搜尋功能。
OpenAI 相容
# 匯入依賴與建立用戶端...
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
# 匯入依賴...
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"
)支援的模型
國際(新加坡)
僅支援 qwen3-max、qwen3-max-2025-09-23 模型,且搜尋策略需設為agent。
快速開始
運行以下代碼快速通過連網搜尋服務查詢股票資訊。
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"
}計費說明
計費涉及兩個方面:
模型調用費用:連網搜尋的網頁內容會拼接到提示詞中,增加模型的輸入 Token,按照模型的標準價格計費。價格詳情請參考模型列表。
搜尋策略費用:
agent策略每調用 1000 次的費用:中國大陸(北京)為$0.57341;國際(新加坡)為$10.00。
錯誤資訊
如果執行報錯,請參見錯誤資訊進行解決。