引子
DeepSeek官網注冊的API token是不支持聯網搜索的,這導致它無法輔助分析一些最新的情況或是幫忙查一下互聯網上的資料。本文從實戰角度提供一種穩定可靠的方法使得DeepSeek R1支持聯網搜索分析。
正文
首先登錄火山方舟控制臺,https://www.volcengine.com/product/ark
然后點這個鏈接一鍵創建DeepSeek-R1-聯網搜索版。
或者
按如下步驟:
- 模型廣場-》DeepSeek R1-》立即體驗
- 在如下圖API接入點開后,先申請API KEY,然后一鍵創建同款DeepSeek-R1-聯網搜索版即可。
- 創建應用里,根據需求選擇相應的聯網搜索相關配置項
以上幾步完成后,就得到一個可用的DeepSeek R1聯網搜索bot,接下來演示下如何使用。我用streamlit做了一個可視版本的,大家可以到GitHub上下載代碼體驗。鏈接:https://github.com/sencloud/deepseek_r1_online_search
官方也有給python的示例,如下,非常簡單;
- 選擇API Key
請按如下方式設置 API Key 作為環境變量,其中
“YOUR_API_KEY”
需要替換為您在平臺創建的 API Key
export ARK_API_KEY=“YOUR_API_KEY” - 請按如下命令安裝環境
pip install --upgrade “openai>=1.0” - 請參考如下示例代碼進行調用
import os
from openai import OpenAI# 請確保您已將 API Key 存儲在環境變量 ARK_API_KEY 中
# 初始化Openai客戶端,從環境變量中讀取您的API Key
client = OpenAI(# 此為默認路徑,您可根據業務所在地域進行配置base_url="https://ark.cn-beijing.volces.com/api/v3/bots",# 從環境變量中獲取您的 API Keyapi_key=os.environ.get("ARK_API_KEY")
)# 非流式輸出:
print("----- standard request -----")
completion = client.chat.completions.create(model="bot-20250329163710-8zcqm", # bot-20250329163710-8zcqm 為您當前的智能體的ID,注意此處與Chat API存在差異。差異對比詳見 SDK使用指南messages=[{"role": "system", "content": "你是DeepSeek,是一個 AI 人工智能助手"},{"role": "user", "content": "常見的十字花科植物有哪些?"},],
)
print(completion.choices[0].message.content)
if hasattr(completion, "references"):print(completion.references)
if hasattr(completion.choices[0].message, "reasoning_content"):print(completion.choices[0].message.reasoning_content) # 對于R1模型,輸出reasoning content# 多輪對話:
print("----- multiple rounds request -----")
completion = client.chat.completions.create(model="bot-20250329163710-8zcqm", # bot-20250329163710-8zcqm 為您當前的智能體的ID,注意此處與Chat API存在差異。差異對比詳見 SDK使用指南messages=[ # 通過會話傳遞歷史信息,模型會參考上下文消息{"role": "system", "content": "你是DeepSeek,是一個 AI 人工智能助手"},{"role": "user", "content": "花椰菜是什么?"},{"role": "assistant", "content": "花椰菜又稱菜花、花菜,是一種常見的蔬菜。"},{"role": "user", "content": "再詳細點"},],
)
print(completion.choices[0].message.content)
if hasattr(completion, "references"):print(completion.references)
if hasattr(completion.choices[0].message, "reasoning_content"):print(completion.choices[0].message.reasoning_content) # 對于R1模型,輸出reasoning content# 流式輸出:
print("----- streaming request -----")
stream = client.chat.completions.create(model="bot-20250329163710-8zcqm", # bot-20250329163710-8zcqm 為您當前的智能體的ID,注意此處與Chat API存在差異。差異對比詳見 SDK使用指南messages=[{"role": "system", "content": "你是DeepSeek,是一個 AI 人工智能助手"},{"role": "user", "content": "常見的十字花科植物有哪些?"},],stream=True,
)
for chunk in stream:if hasattr(chunk, "references"):print(chunk.references)if not chunk.choices:continueif chunk.choices[0].delta.content:print(chunk.choices[0].delta.content, end="")elif hasattr(chunk.choices[0].delta, "reasoning_content"):print(chunk.choices[0].delta.reasoning_content)
print()