目前正在觀看:Foundry(經典)入口版本 - 切換到新 Foundry 入口網站版本
在這個教學中,你將學習如何在 Microsoft Foundry 中部署並使用 DeepSeek 推理模型。 這個教學用 DeepSeek-V4-Pro 來說明。
你的成就:
在這個教學中,你會部署 DeepSeek-V4-Pro 推理模型,利用程式碼程式化發送推理請求,並解析推理輸出,以理解模型如何得出答案。
你在這個教學中執行的步驟包括:
- 建立並設定 Azure 資源,以在 Foundry Models 中使用 DeepSeek-V4-Pro。
- 配置模型部署。
- 使用 DeepSeek-V4-Pro 搭配下一代 v1 Azure OpenAI API 來以程式碼形式處理模型。
先決條件
要完成這篇文章,你需要:
一個有有效付款方式的 Azure 訂閱。 如果你沒有Azure訂閱,請建立一個付費Azure帳號開始。
可取得 Microsoft Foundry 的適當權限來建立和管理資源。 通常需要資源群組中的貢獻者或擁有者角色來建立資源和部署模型。
認知服務使用者角色 (或更高) 指派至您在 Foundry 資源中的 Azure 帳戶。 此角色是使用 Microsoft Entra ID 進行推論呼叫的必要條件。 在 Azure 入口網站中,於 Foundry 資源的存取控制 (IAM)下進行指派。
安裝針對你的程式語言的 Azure OpenAI SDK:
-
Python:
pip install openai azure-identity -
.NET:
dotnet add package OpenAI與dotnet add package Azure.Identity -
JavaScript:
npm install openai @azure/identity -
Java:新增
com.openai:openai-java和com.azure:azure-identity套件
-
Python:
DeepSeek-V4-Pro 是一個推理模型,能在回答的同時產生解釋。 它支援文字聊天完成,但不支援工具呼叫。 詳情請參見 關於推理模型 。
創造資源
要建立支援 DeepSeek-V4-Pro 部署的 Foundry 專案,請依照以下步驟操作。 您也可以使用 Azure CLI 或 基礎結構即程式碼,搭配 Bicep 來建立資源。
提示
因為你可以在 Microsoft Foundry 入口網站中自訂左側窗格,所以你可能會看到與這些步驟中顯示的不同物品。 如果你找不到你想要的,選擇 ⋯⋯更多選項 在左側窗格底部。
-
登入 Microsoft Foundry。 確定 新鑄造廠 的切換開關是關閉的。 這些步驟指的是Foundry (classic)。
在登陸頁面,前往「探索模型與能力」區塊。
請使用螢幕上的搜尋框搜尋 DeepSeek-V4-Pro 型號並打開其型號卡。
選擇 使用此模型。 此動作會開啟精靈,引導您建立 Foundry 專案及相關資源,以便開始作業。 你可以保留專案的預設名稱或更改名稱。
在精靈的「進階選項」區塊中選擇下拉選單,即可查看與專案同時建立的設定及其他預設值細節。 這些預設值是為了達到最佳功能而選擇,包括:
財產 描述 資源群組 Azure 中所有資源的主容器。 這個容器幫助你組織協同運作的資源。 這也有助於你掌握整個專案的相關成本範圍。 區域 你正在建立的資源所在的區域。 鑄造資源 此資源可存取 Foundry 型號目錄中的旗艦車型。 在這個教學中,會建立一個新帳號,但 Foundry 資源(前稱 Azure AI 服務資源)可以在多個樞紐和專案間共享。 中樞會使用到資源的連線,以存取該處可用的模型部署。 想了解如何建立連結到 Foundry 資源以消費模型,請參見 「Connect your AI 專案」。 選擇 「建立 」以與其他預設檔一起建立 Foundry 專案。 等專案建立完成後再說。 這個過程只需幾分鐘。
部署模型
當你建立專案和資源時,會開啟一個部署精靈。 DeepSeek -V4-Pro 以 Foundry 型號形式由 Azure 販售。 您可以透過在 Foundry 模型定價頁面選擇 DeepSeek 標籤,查看該模型的價格細節。
設定部署設定。 根據預設,部署會收到您正在部署的模型名稱。 部署名稱用於
model參數中,用於路由至此特定模型部署的請求。 這個設定讓你在附加特定配置時,可以設定模型的具體名稱。- Foundry 會自動選擇你之前隨專案建立的 Foundry 資源。 使用 自訂 選項根據需求更改連線。 DeepSeekV4-Pro 提供 全球標準 與 全球配置 部署類型,提供更高的吞吐量與效能。
選擇 部署。
部署完成後,會開啟部署 詳細資訊頁面。 現在新款已經準備好使用。
如果你偏好先互動式探索模型,可以跳到「 在遊樂場中使用模型」。
用程式碼來使用模型
使用 Foundry Models 的端點和憑證來連接模型。
使用下一代 v1 Azure OpenAI API 來在你的程式碼中使用模型。 這些程式碼範例採用安全且無金鑰的認證方法,Microsoft Entra ID,透過 Azure 身份函式庫。
以下程式碼範例示範如何:
使用
DefaultAzureCredential與 Microsoft Entra ID 進行驗證,該工具會自動嘗試多種驗證方法(環境變數、管理身份、Azure CLI等)。 具體順序取決於你使用的 Azure Identity SDK 版本。提示
在本地開發時,請確保你已透過 Azure CLI 驗證,執行
az login。 在 Azure 中部署生產環境時,請為你的應用程式設定管理身份。建立一個與你的模型部署連結的聊天完成客戶端
向 DeepSeek-V4-Pro 模型發送一個基本提示
接收並顯示回應
預期產出: 一個包含模型答案、推理過程(標籤內 <think> )、標記使用統計資料(提示標記、完成標記、總標記數)及模型資訊的 JSON 回應。
安裝套件 openai 和 azure-identity,使用你的套件管理器,如 pip:
pip install --upgrade openai azure-identity
下列範例示範如何建立用戶端來使用聊天完成,然後產生並顯示回應:
from openai import OpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://ai.azure.com/.default"
)
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=token_provider,
)
response = client.chat.completions.create(
model="DeepSeek-V4-Pro", # Replace with your model deployment name.
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "How many languages are in the world?"}
]
)
#print(response.choices[0].message)
print(response.model_dump_json(indent=2))
提示
執行程式碼後,你應該會看到包含 choices[0].message.content 模型答案的 JSON 回應。 若模型產生推理,回應會包含被 <think>...</think> 標籤包裹的內容,然後再是最終答案。
API 參考:
- OpenAI Python 用戶端
- OpenAI JavaScript 用戶端
- OpenAI .NET 客戶端
- DefaultAzureCredential class
- 聊天完成 API 參考
- Azure身份庫概覽
推理可能會產生更長的回應時間,並消耗更多代幣。 請參閱適用於 DeepSeek 模型的 速率限制 。 考慮使用重試策略來處理速率限制的問題。 你也可以 申請提高到預設的上限。
關於推理模型
推理模型在數學、程式設計、科學、策略與物流等領域中能達到更高的表現水準。 這些模型產生輸出的方式是明確地利用思考鏈探索所有可能路徑,然後再產生答案。 他們在回答時會核實,有助於得出更準確的結論。 因此,推理模型可能需要較少的上下文提示來產生有效結果。
推理模型產生兩種類型的輸出內容:
- 推理完成
- 輸出完成
這兩種完成都會計入從模型產生的內容。 因此,它們會增加模型相關的代幣限額與成本。 有些模型,如 DeepSeek-V4-Pro,可能會以推理內容回應。 其他的,比如 o1,只輸出完成部分。
推理內容
有些推理模型,如 DeepSeek-V4-Pro,會產生完備性並包含其背後的推理。 與完成相關的推理包含在回應內容標籤 <think> 和 </think> 中。 模型可以選擇產生推理內容的情境。 以下範例展示了如何使用 Python 產生推理內容:
import re
match = re.match(r"<think>(.*?)</think>(.*)", response.choices[0].message.content, re.DOTALL)
print("Response:")
if match:
print("\tThinking:", match.group(1))
print("\tAnswer:", match.group(2))
else:
print("\tAnswer:", response.choices[0].message.content)
print("Model:", response.model)
print("Usage:")
print("\tPrompt tokens:", response.usage.prompt_tokens)
print("\tTotal tokens:", response.usage.total_tokens)
print("\tCompletion tokens:", response.usage.completion_tokens)
Thinking: Okay, the user is asking how many languages exist in the world. I need to provide a clear and accurate answer. Let's start by recalling the general consensus from linguistic sources. I remember that the number often cited is around 7,000, but maybe I should check some reputable organizations.\n\nEthnologue is a well-known resource for language data, and I think they list about 7,000 languages. But wait, do they update their numbers? It might be around 7,100 or so. Also, the exact count can vary because some sources might categorize dialects differently or have more recent data. \n\nAnother thing to consider is language endangerment. Many languages are endangered, with some having only a few speakers left. Organizations like UNESCO track endangered languages, so mentioning that adds context. Also, the distribution isn't even. Some countries or regions have hundreds of languages, like Papua New Guinea with over 800, while others have just a few. \n\nA user might also wonder why the exact number is hard to pin down. It's because the distinction between a language and a dialect can be political or cultural. For example, Mandarin and Cantonese are considered dialects of Chinese by some, but they're mutually unintelligible, so others classify them as separate languages. Also, some regions are under-researched, making it hard to document all languages. \n\nI should also touch on language families. The 7,000 languages are grouped into families like Indo-European, Sino-Tibetan, Niger-Congo, etc. Maybe mention a few of the largest families. But wait, the question is just about the count, not the families. Still, it's good to provide a bit more context. \n\nI need to make sure the information is up-to-date. Let me think – recent estimates still hover around 7,000. However, languages are dying out rapidly, so the number decreases over time. Including that note about endangerment and language extinction rates could be helpful. For instance, it's often stated that a language dies every few weeks. \n\nAnother point is sign languages. Does the count include them? Ethnologue includes some, but not all sources might. If the user is including sign languages, that adds more to the count, but I think the 7,000 figure typically refers to spoken languages. For thoroughness, maybe mention that there are also over 300 sign languages. \n\nSummarizing, the answer should state around 7,000, mention Ethnologue's figure, explain why the exact number varies, touch on endangerment, and possibly note sign languages as a separate category. Also, a brief mention of Papua New Guinea as the most linguistically diverse country/region. \n\nWait, let me verify Ethnologue's current number. As of their latest edition (25th, 2022), they list 7,168 living languages. But I should check if that's the case. Some sources might round to 7,000. Also, SIL International publishes Ethnologue, so citing them as reference makes sense. \n\nOther sources, like Glottolog, might have a different count because they use different criteria. Glottolog might list around 7,000 as well, but exact numbers vary. It's important to highlight that the count isn't exact because of differing definitions and ongoing research. \n\nIn conclusion, the approximate number is 7,000, with Ethnologue being a key source, considerations of endangerment, and the challenges in counting due to dialect vs. language distinctions. I should make sure the answer is clear, acknowledges the variability, and provides key points succinctly.
Answer: The exact number of languages in the world is challenging to determine due to differences in definitions (e.g., distinguishing languages from dialects) and ongoing documentation efforts. However, widely cited estimates suggest there are approximately **7,000 languages** globally.
Model: DeepSeek-V4-Pro
Usage:
Prompt tokens: 11
Total tokens: 897
Completion tokens: 886
API 參考:
提示式推理模型
在建立推理模型的提示時,請考慮以下幾點:
- 使用簡單的指示,避免使用思路連鎖的技巧。
- 內建的推理能力讓簡單的零樣本提示像更複雜的方法一樣有效。
- 在提供額外背景或文件時,如RAG情境,只包含最相關的資訊,有助於防止模型過度複雜化回應。
- 推理模型可能支持系統訊息的使用。 然而,它們可能不像其他非推理模型那樣嚴格遵循這些理論。
- 在建立多回合應用程式時,建議只附加模型的最終答案,而不包含其推理內容。
注意推理模型產生回應可能需要更長時間。 他們運用長的推理鏈,使得更深入且結構化的問題解決成為可能。 他們也會進行自我驗證,交叉核對答案並修正錯誤,從而展現出新興的自我反思行為。
參數
推理模型支援標準聊天完成參數的子集,以維持推理過程的完整性。
支援參數:
-
max_tokens- 回應中可產生的最大標記數量 -
stop- API 停止產生標記的序列 -
stream- 啟用串流回應 -
n- 需生成的完成數量
不支援參數 (推理模型不支援這些):
-
temperature- 修正以優化推理品質 -
top_p- 無法配置推理模型 -
presence_penalty- 不可用 -
repetition_penalty- 不適用於推理模型
範例如下:max_tokens
response = client.chat.completions.create(
model="DeepSeek-V4-Pro",
messages=[
{"role": "user", "content": "Explain quantum computing"}
],
max_tokens=1000 # Limit response length
)
完整支援參數清單,請參閱 Chat 完成 API 參考。
在操場上使用模型
在遊樂場使用模型,了解模型的功能。
在部署詳細資料頁面上,選取頂端列中的 [在 Playground 中開啟]。 這個動作開啟了聊天遊樂場。
在聊天遊樂場的 部署 下拉選單中,你建立的部署已經自動被選中。
根據需要設定系統提示。
輸入你的提示詞,看看輸出結果。
選擇 「檢視程式碼 」以查看如何以程式化方式存取模型部署的詳細資訊。
故障排除
如果你在跟著這個教學時遇到問題,請參考以下指引來解決常見問題。
認證錯誤(401/403)
- 請確保你已登入 Azure CLI。 在本地開發時,執行程式碼前先執行
az login。DefaultAzureCredential會用你的 Azure CLI 憑證作為備用,當沒有其他憑證可用時。 - 確認角色分配。 你的Azure帳號需要在 Foundry 資源中達到 Cognitive Services User 角色(或更高),才能與 Microsoft Entra ID 進行推論呼叫。 如果你還沒指派這個角色,請參考先決條件章節。
- 檢查端點格式。 端點 URL 必須遵循格式
https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/。 確認資源名稱與你的 Foundry 資源相符。
部署問題
- 部署名稱與模型名稱。
modelAPI 呼叫中的參數指的是你的部署名稱,而不是模型名稱。 如果你在建立時自訂了部署名稱,請使用該名稱而非DeepSeek-V4-Pro。 - 部署尚未準備好。 若收到 404 錯誤,請先在 Foundry 入口網站確認部署狀態顯示 為成功 ,再進行 API 呼叫。
速率限制(429 個錯誤)
- 實作重試邏輯。 推理模型產生較長的回應,消耗更多代幣。 使用指數退避來處理 429 個(請求過多)錯誤。
- 監控代幣使用情況。 DeepSeek-V4-Pro 的推理內容 (位於
<think>標籤內) 會計入您的詞元限制。 請參閱 配額與限制 以了解目前的速率限制。 - 請求增加配額。 如果你持續達到速率上限, 請求會增加到預設的限制。
套件安裝問題
- Python。 安裝這兩個必需的套件:
pip install openai azure-identity。 該azure-identity封包是DefaultAzureCredential的必備條件。 - JavaScript。 安裝這兩個必需的套件:
npm install openai @azure/identity。 - .NET。 安裝 Azure Identity 套件:
dotnet add package Azure.Identity。
你學到了什麼
在這個教學中,你完成了以下步驟:
- 建立 Foundry 資源以承載 AI 模型
- 部署了 DeepSeek-V4-Pro 推理模型
- 使用 Microsoft Entra ID 進行認證 API 呼叫
- 發送推理請求並接收推理輸出
- 從模型回應中解析推理內容,以理解模型的思考過程