联网搜索
更新时间:2025-09-12
使用场景
对于需要获取实时信息或最新数据的场景,如新闻事件查询、文献检索、追踪政策变化。 基于联网搜索能力,模型能获取实时数据和信息,更精确的回答用户在特定场景下的问题。
使用方式
在请求body中添加如下web_search参数,即可实现联网搜索,参数描述如下:
参数名称 | 类型 | 必填 | 默认值 | 描述 |
---|---|---|---|---|
enable | boolean | 否 | 否 | 是否启用联网搜索功能 |
enable_trace | boolean | 否 | false | 是否返回溯源信息 |
enable_status | boolean | 否 | false | 是否在响应中返回搜索触发信号,如果触发搜索,首包返回『正在搜索』,通过delta_tag:search_status表示这一包是信号包 |
enable_citation | boolean | 否 | false | 是否在响应中包含引用来源角标 。单个角标格式示例:^[1]^ ,多个角标格式示例:^[1][2]^ |
search_mode | string | 否 | "auto" | 联网搜索模式: - auto :大模型基于query判断意图是否需要进行搜索。举例:用户query1+1等于几 ,默认不过联网搜索 - required :强制进行联网搜索,注意:ernie系列模型不支持强制搜索 |
search_number | integer | 否 | 10 | 检索的文献数量,范围在[1~28]之间 |
reference_number | integer | 否 | 10 | 用于给大模型总结的文献数量,范围在[1~28]之间(需≤search_number,若 reference_number > search_number 则将默认赋值 reference_number=search_number,如设置:search_number=6,reference_number=10,则search_number=10) |
入参示例:
JSON
1{
2 "web_search": {
3 "enable": true,
4 "enable_citation": true,
5 "enable_trace": true,
6 "enable_status": true,
7 "search_mode":"auto",
8 "search_number": 10,
9 "reference_number": 5
10 }
11}
支持模型列表
-
ERNIE X系列(含preview、日期版本)
- ernie-x1.1-preview
- ernie-x1-turbo-32k
-
ERNIE 4.5系列(含preview、日期版本)
- ernie-4.5-turbo-32k
- ernie-4.5-turbo-128k
- ernie-4.5-21b-a3b
-
DeepSeek系列(含日期版本)
- deepseek-v3
- deepseek-r1
- deepseek-v3.1-250821
- deepseek-v3.1-think-250821
-
Qwen3系列(含日期版本)
- qwen3-30b-a3b-instruct-2507
特别说明:
- ernie-x1-turbo-32k 只支持
web_search.enable
参数,其他参数不支持 - ERNIE系列不支持search_mode参数,只支持意图搜索,不支持强制搜索
使用示例
ERNIE-X1-Turbo-32K
- 只支持enable入参,不支持角标、搜索信号提前返回等能力。
- 可以触发多次多次搜索,比如下面的示例当中,第一次会搜索当前黄金价格,第二次会搜索过去24小时黄金价格涨跌幅,最后给出总结答案。最大支持触发10次搜索,将按照实际触发搜索的次数计费。
请求
Bash
1curl --location 'https://qianfan.bj.baidubce.com/v2/chat/completions' \
2--header 'Content-Type: application/json' \
3--header 'Authorization: Bearer your-key' \
4--data '{
5 "model": "ernie-x1-turbo-32k",
6 "messages": [
7 {
8 "role": "user",
9 "content": "当前黄金价格和过去24小时涨跌幅"
10 }
11 ],
12 "stream": true,
13 "web_search": {
14 "enable": true
15 }
16}'
返回
JSON
1data: {"id":"as-ypk4zzgdpt","object":"chat.completion.chunk","created":1750381709,"model":"ernie-x1-turbo-32k","choices":[{"index":0,"delta":{"content":null,"role":"assistant","reasoning_content":"用户"},"flag":0}]}
2
3data: {"id":"as-ypk4zzgdpt","object":"chat.completion.chunk","created":1750381709,"model":"ernie-x1-turbo-32k","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"希望"},"flag":0}]}
4
5......
6//第一次触发搜索
7data: {"id":"as-ypk4zzgdpt","object":"chat.completion.chunk","created":1750381709,"model":"ernie-x1-turbo-32k","choices":null,"search_results":[{"index":0,"url":"https://www.baidu.com/s?wd=%E5%BD%93%E5%89%8D%E9%BB%84%E9%87%91%E4%BB%B7%E6%A0%BC","title":"当前黄金价格"}]}
8......
9//第二次触发搜索
10data: {"id":"as-ypk4zzgdpt","object":"chat.completion.chunk","created":1750381709,"model":"ernie-x1-turbo-32k","choices":null,"search_results":[{"index":0,"url":"https://www.baidu.com/s?wd=%E8%BF%87%E5%8E%BB24%E5%B0%8F%E6%97%B6%E9%BB%84%E9%87%91%E4%BB%B7%E6%A0%BC%E6%B6%A8%E8%B7%8C%E5%B9%85","title":"过去24小时黄金价格涨跌幅"}]}
11......
12//最后一包汇总全量搜索记录
13data: {"id":"as-ypk4zzgdpt","object":"chat.completion.chunk","created":1750381709,"model":"ernie-x1-turbo-32k","choices":[{"index":0,"delta":{"content":""},"finish_reason":"stop","flag":0}],"usage":{"prompt_tokens":8,"completion_tokens":845,"total_tokens":853,"completion_tokens_details":{"reasoning_tokens":470}},"search_results":[{"index":0,"url":"https://www.baidu.com/s?wd=%E5%BD%93%E5%89%8D%E9%BB%84%E9%87%91%E4%BB%B7%E6%A0%BC","title":"当前黄金价格"},{"index":1,"url":"https://www.baidu.com/s?wd=%E8%BF%87%E5%8E%BB24%E5%B0%8F%E6%97%B6%E9%BB%84%E9%87%91%E4%BB%B7%E6%A0%BC%E6%B6%A8%E8%B7%8C%E5%B9%85","title":"过去24小时黄金价格涨跌幅"}]}
14
15data: [DONE]
ERNIE-4.5-Turbo-32k模型
- 只支持意图识别搜索,即是否触发搜索,由大模型意图识别判定。
请求
Bash
1curl --location 'https://qianfan.baidubce.com/v2/chat/completions' \
2--header 'Content-Type: application/json' \
3--header 'Authorization: Bearer your-key' \
4--data '{
5 "messages": [
6 {
7 "role": "user",
8 "content": "今日足球赛况"
9 }
10 ],
11 "stream": true,
12 "model": "ernie-4.5-turbo-32k",
13 "web_search": {
14 "enable": true,
15 "enable_trace": true,
16 "enable_status":true,
17 "enable_citation": true,
18 "search_number": 10,
19 "reference_number": 5
20 }
21}'
返回
JSON
1data: {"id":"as-yufms3fs7m","object":"chat.completion.chunk","created":1745507336,"model":"ernie-4.5-turbo-32k","choices":[{"index":0,"delta":{"content":"正在搜索","role":"assistant"},"delta_tag":"search_status"}]}
2
3data: {"id":"as-yufms3fs7m","object":"chat.completion.chunk","created":1745507336,"model":"ernie-4.5-turbo-32k","choices":[{"index":0,"delta":{"content":"今天是2","role":"assistant"},"flag":0}],"search_results":[{"index":1,"url":"https://live.nowscore.com/analysis/2607195.html","title":"2025年04月24日足球让球分析:都灵 VS 乌迪内斯 - 捷报比分网"},{"index":2,"url":"https://sports.163.com/world/?_=1498278879","title":"国际足球"},{"index":3,"url":"https://live.500.com/index.php图片","title":"足球比分直播_竞彩足球比分直播_足彩即时比分直播网-500彩票网"},{"index":4,"url":"https://sports.sohu.com/a/888368570_120693275","title":"4月24日足球赛前比分预测 进球数 半全场 二串一 竞彩足球赛事赛前分析预测"},{"index":5,"url":"https://m.titan007.com/indexnew.shtml\u0026wd=\u0026eqid=9761e11200059a7e0000000464709b01","title":"即时比分_比分直播_足球比分直播-新球体育-球探体育"},{"index":6,"url":"https://live.500.com/zucai.php","title":"【足彩比分直播】_足球彩票比分直播_胜负彩即时比分直播网-500彩票网"},{"index":7,"url":"https://m.sports.ydniu.com/news/detail/927441964","title":"2025年04月24日法女甲南特女足vs里昂女足历史战绩,交战记录,比分预测"},{"index":8,"url":"https://m.titan007.com/odds.htm","title":"即时比分_比分直播_足球比分直播-新球体育-球探体育"},{"index":9,"url":"https://www.vipc.cn/results/jczq?in=lotteryInfo","title":"竞彩足球开奖"},{"index":10,"url":"https://live.nowscore.com/analysis/2718294cn.html","title":"2025年04月24日足球让球分析:塞那乔其 VS 奥卢 - 捷报比分网"}]}
4
5data: {"id":"as-yufms3fs7m","object":"chat.completion.chunk","created":1745507336,"model":"ernie-4.5-turbo-32k","choices":[{"index":0,"delta":{"content":"02"},"flag":0}]}
6
7data: {"id":"as-yufms3fs7m","object":"chat.completion.chunk","created":1745507336,"model":"ernie-4.5-turbo-32k","choices":[{"index":0,"delta":{"content":"5年"},"flag":0}]}
8
9data: {"id":"as-yufms3fs7m","object":"chat.completion.chunk","created":1745507336,"model":"ernie-4.5-turbo-32k","choices":[{"index":0,"delta":{"content":"4月"},"flag":0}]}
10......
11
12data: {"id":"as-yufms3fs7m","object":"chat.completion.chunk","created":1745507336,"model":"ernie-4.5-turbo-32k","choices":[{"index":0,"delta":{"content":"。"},"flag":0}]}
13
14data: {"id":"as-yufms3fs7m","object":"chat.completion.chunk","created":1745507336,"model":"ernie-4.5-turbo-32k","choices":[{"index":0,"delta":{"content":"\n"},"flag":0}]}
15
16data: {"id":"as-yufms3fs7m","object":"chat.completion.chunk","created":1745507336,"model":"ernie-4.5-turbo-32k","choices":[{"index":0,"delta":{"content":"\n"},"flag":0}]}
17
18data: {"id":"as-yufms3fs7m","object":"chat.completion.chunk","created":1745507336,"model":"ernie-4.5-turbo-32k","choices":[{"index":0,"delta":{"content":"---"},"finish_reason":"stop","flag":0}],"search_results":[{"index":1,"url":"https://live.nowscore.com/analysis/2607195.html","title":"2025年04月24日足球让球分析:都灵 VS 乌迪内斯 - 捷报比分网"},{"index":2,"url":"https://sports.163.com/world/?_=1498278879","title":"国际足球"},{"index":3,"url":"https://live.500.com/index.php图片","title":"足球比分直播_竞彩足球比分直播_足彩即时比分直播网-500彩票网"},{"index":4,"url":"https://sports.sohu.com/a/888368570_120693275","title":"4月24日足球赛前比分预测 进球数 半全场 二串一 竞彩足球赛事赛前分析预测"},{"index":5,"url":"https://m.titan007.com/indexnew.shtml\u0026wd=\u0026eqid=9761e11200059a7e0000000464709b01","title":"即时比分_比分直播_足球比分直播-新球体育-球探体育"},{"index":6,"url":"https://live.500.com/zucai.php","title":"【足彩比分直播】_足球彩票比分直播_胜负彩即时比分直播网-500彩票网"},{"index":7,"url":"https://m.sports.ydniu.com/news/detail/927441964","title":"2025年04月24日法女甲南特女足vs里昂女足历史战绩,交战记录,比分预测"},{"index":8,"url":"https://m.titan007.com/odds.htm","title":"即时比分_比分直播_足球比分直播-新球体育-球探体育"},{"index":9,"url":"https://www.vipc.cn/results/jczq?in=lotteryInfo","title":"竞彩足球开奖"},{"index":10,"url":"https://live.nowscore.com/analysis/2718294cn.html","title":"2025年04月24日足球让球分析:塞那乔其 VS 奥卢 - 捷报比分网"}]}
19
20data: [DONE]
Deepseek系列模型
意图搜索-触发搜索
请求
Bash
1curl --location 'https://qianfan.baidubce.com/v2/chat/completions' \
2--header 'Content-Type: application/json' \
3--header 'Authorization: Bearer your-key' \
4--data '{
5 "messages": [
6 {
7 "role": "user",
8 "content": "2024年奥运会,中国奖牌多少枚"
9 }
10 ],
11 "stream": true,
12 "stream_options": {
13 "include_usage": true,
14 "chunk_include_usage": true
15 },
16 "model": "deepseek-v3",
17 "web_search": {
18 "search_mode": "auto",
19 "enable_status": true,
20 "enable_trace": true,
21 "enable_citation": true,
22 "search_number": 5,
23 "reference_number": 5,
24 "enable": true
25 }
26}'
返回
JSON
1data: {"id":"as-qwjdwhgn1s","object":"chat.completion.chunk","created":1755172035,"model":"deepseek-v3","choices":[{"index":0,"delta":{"content":"正在搜索","role":"assistant"},"delta_tag":"search_status"}]}
2
3data: {"id":"as-qwjdwhgn1s","object":"chat.completion.chunk","created":1755172035,"model":"deepseek-v3","choices":[{"index":0,"delta":{"content":"根据"},"flag":0}],"usage":{"prompt_tokens":8101,"completion_tokens":1,"total_tokens":8102,"prompt_tokens_details":{"cached_tokens":2}},"search_results":[{"index":1,"url":"https://baijiahao.baidu.com/s?id=1840065906790645210\u0026wfr=spider\u0026for=pc","title":"奥运会现在中国队拿了几块金牌了"},{"index":2,"url":"https://www.sport.gov.cn/n20001280/n20745751/c28131404/content.html","title":"从奥运金牌零的突破到创造境外参赛最好成绩 使命在肩 彰显奥运强国风采(上)"},{"index":3,"url":"https://baike.baidu.com/item/2024年我国运动员取得成绩报告/65253270","title":"2024年我国运动员取得成绩报告"},{"index":4,"url":"https://baijiahao.baidu.com/s?id=1807103733281103762\u0026wfr=spider\u0026for=pc","title":"2024奥运会中国体育代表团共夺得91枚奖牌:40金27银24铜"},{"index":5,"url":"https://www.sport.gov.cn/n4/n27894463/n27894386/c28002432/content.html","title":"续写中国竞技体育辉煌 展现新时代青年良好风貌——中国体育代表团实现巴黎奥运会参赛目标"}]}
4
5data: {"id":"as-qwjdwhgn1s","object":"chat.completion.chunk","created":1755172035,"model":"deepseek-v3","choices":[{"index":0,"delta":{"content":"2024"},"flag":0}],"usage":{"prompt_tokens":8101,"completion_tokens":3,"total_tokens":8104,"prompt_tokens_details":{"cached_tokens":2}}}
6
7data: {"id":"as-qwjdwhgn1s","object":"chat.completion.chunk","created":1755172035,"model":"deepseek-v3","choices":[{"index":0,"delta":{"content":"年巴黎"},"flag":0}],"usage":{"prompt_tokens":8101,"completion_tokens":5,"total_tokens":8106,"prompt_tokens_details":{"cached_tokens":2}}}
8
9......
10
11data: {"id":"as-qwjdwhgn1s","object":"chat.completion.chunk","created":1755172035,"model":"deepseek-v3","choices":[{"index":0,"delta":{"content":"坚实基础。"},"flag":0}],"usage":{"prompt_tokens":8101,"completion_tokens":386,"total_tokens":8487,"prompt_tokens_details":{"cached_tokens":2}}}
12
13data: {"id":"as-qwjdwhgn1s","object":"chat.completion.chunk","created":1755172035,"model":"deepseek-v3","choices":[{"index":0,"delta":{"content":""},"finish_reason":"stop","flag":0}],"usage":{"prompt_tokens":8101,"completion_tokens":386,"total_tokens":8487,"prompt_tokens_details":{"search_tokens":8088,"cached_tokens":2}},"search_results":[{"index":1,"url":"https://baijiahao.baidu.com/s?id=1840065906790645210\u0026wfr=spider\u0026for=pc","title":"奥运会现在中国队拿了几块金牌了"},{"index":2,"url":"https://www.sport.gov.cn/n20001280/n20745751/c28131404/content.html","title":"从奥运金牌零的突破到创造境外参赛最好成绩 使命在肩 彰显奥运强国风采(上)"},{"index":3,"url":"https://baike.baidu.com/item/2024年我国运动员取得成绩报告/65253270","title":"2024年我国运动员取得成绩报告"},{"index":4,"url":"https://baijiahao.baidu.com/s?id=1807103733281103762\u0026wfr=spider\u0026for=pc","title":"2024奥运会中国体育代表团共夺得91枚奖牌:40金27银24铜"},{"index":5,"url":"https://www.sport.gov.cn/n4/n27894463/n27894386/c28002432/content.html","title":"续写中国竞技体育辉煌 展现新时代青年良好风貌——中国体育代表团实现巴黎奥运会参赛目标"}]}
14
15data: [DONE]
意图搜索-不触发搜索
请求
Bash
1curl --location 'https://qianfan.baidubce.com/v2/chat/completions' \
2--header 'Content-Type: application/json' \
3--header 'Authorization: Bearer your-key' \
4--data '{
5 "messages": [
6 {
7 "role": "user",
8 "content": "1+1等于几"
9 }
10 ],
11 "stream": false,
12 "model": "deepseek-v3",
13 "web_search": {
14 "search_mode": "auto",
15 "enable_trace": true,
16 "enable_citation": true,
17 "search_number": 5,
18 "reference_number": 5,
19 "enable": true
20 }
21}'
返回
JSON
1{
2 "id": "as-vgg0dmt7vn",
3 "object": "chat.completion",
4 "created": 1755172220,
5 "model": "deepseek-v3",
6 "choices": [
7 {
8 "index": 0,
9 "message": {
10 "role": "assistant",
11 "content": "1 + 1 等于 **2**。 \n\n这是最基本的加法运算,也是数学中最基础的算术之一。 \n\n如果你有其他问题或想探讨更深的概念(比如二进制中的 1 + 1 = 10),欢迎继续提问! "
12 },
13 "finish_reason": "stop",
14 "flag": 0
15 }
16 ],
17 "usage": {
18 "prompt_tokens": 8,
19 "completion_tokens": 51,
20 "total_tokens": 59,
21 "prompt_tokens_details": {
22 "cached_tokens": 2
23 }
24 }
25}
强制搜索
请求
Bash
1curl --location 'https://qianfan.baidubce.com/v2/chat/completions' \
2--header 'Content-Type: application/json' \
3--header 'Authorization: Bearer your-key' \
4--data '{
5 "messages": [
6 {
7 "role": "user",
8 "content": "1+1等于几"
9 }
10 ],
11 "stream": false,
12 "model": "deepseek-v3",
13 "web_search": {
14 "search_mode": "required",
15 "enable_trace": true,
16 "enable_citation": true,
17 "search_number": 5,
18 "reference_number": 5,
19 "enable": true
20 }
21}'
返回
JSON
1{
2 "id": "as-36dm6ssbgd",
3 "object": "chat.completion",
4 "created": 1755172368,
5 "model": "deepseek-v3",
6 "choices": [
7 {
8 "index": 0,
9 "message": {
10 "role": "assistant",
11 "content": "根据数学运算的基本定义和不同领域的解释,1+1的结果存在多种可能性,具体如下:\n\n### 1. **数学标准答案:1+1=2** \n - 在初等数学和整数加法中,1+1=2是最基础的运算结果,符合单位量相加的确定性原则^[1][5]^。 \n - 高中数学也明确将1+1=2作为基础定义,强调对齐个位、逐位相加的步骤^[5]^。\n\n### 2. **生活场景中的特殊解释** \n - **1+1=1**:例如一堆沙子加另一堆沙子仍是一堆更大的沙子,或一群羊合并后仍为“一群”^[2][3]^。 \n - **1+1=3**:比喻一对夫妻生育后形成三口之家^[3]^。 \n\n### 3. **汉字与脑筋急转弯的答案** \n - **“王”或“田”**:通过汉字结构或数字符号的组合,1+1可解读为“王”(一+加号+一)或“田”(加入等号)^[2][3]^。 \n\n### 4. **其他领域的拓展** \n - **二进制**:1+1=10^[5]^。 \n - **布尔代数**:1+1=1(逻辑或运算)^[5]^。 \n - **单位不同**:如1小时+1分钟=61分钟^[5]^。 \n\n### 总结 \n在常规数学中,1+1=2是唯一正确答案^[1][5]^;其他答案需结合具体语境或领域。若用户需要更详细的特殊场景分析,可参考上述来源^[2][3][5]^。"
12 },
13 "finish_reason": "stop",
14 "flag": 0
15 }
16 ],
17 "usage": {
18 "prompt_tokens": 1675,
19 "completion_tokens": 386,
20 "total_tokens": 2061,
21 "prompt_tokens_details": {
22 "search_tokens": 1667,
23 "cached_tokens": 2
24 }
25 },
26 "search_results": [
27 {
28 "index": 1,
29 "url": "http://haokan.baidu.com/v?pd=wisenatural&vid=14403586733639629960",
30 "title": "1+1等于什么?很简单的送分题,仍有很多同学不会写"
31 },
32 {
33 "index": 2,
34 "url": "http://haokan.baidu.com/v?pd=wisenatural&vid=17043384653949804732",
35 "title": "1加1等于几,你能想到5种答案吗?"
36 },
37 {
38 "index": 3,
39 "url": "http://www.bilibili.com/video/BV1hPKSzxEth",
40 "title": "脑筋急转弯:1加1等于几?"
41 },
42 {
43 "index": 4,
44 "url": "http://haokan.baidu.com/v?pd=wisenatural&vid=3733275973985756412",
45 "title": "一年级数学课堂:一加一等于几?"
46 },
47 {
48 "index": 5,
49 "url": "http://haokan.baidu.com/v?pd=wisenatural&vid=16156504153451550642",
50 "title": "高中数学1加1等于几"
51 }
52 ]
53}
OpenAI SDK使用联网搜索
下面是基于 OpenAI 库使用联网搜索的例子:
Python
1from openai import OpenAI
2
3client = OpenAI(
4 api_key="your-key", #
5 base_url="https://qianfan.baidubce.com/v2", #
6)
7
8web_search={
9 "enable":True,
10 "enable_trace":True
11}
12extra_data={
13 "web_search":web_search
14}
15
16completion = client.chat.completions.create(
17 model="ernie-4.5-turbo-32k",
18 messages=[
19 {"role": "user", "content": "今天上海天气怎么样"}
20 ],
21 stream=True,
22 extra_body=extra_data
23)
24
25for chunk in completion:
26 if chunk.choices[0].delta.content is not None:
27 print(chunk.choices[0].delta.content, end="")