教學:開始使用 Microsoft Foundry 模型中的 DeepSeek 推理模型

在這個教學中,你將學習如何在 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 套件

DeepSeek-V4-Pro 是一個推理模型,能在回答的同時產生解釋。 它支援文字聊天完成,但不支援工具呼叫。 詳情請參見 關於推理模型 。

創造資源

要建立支援 DeepSeek-V4-Pro 部署的 Foundry 專案,請依照以下步驟操作。 您也可以使用 Azure CLI 或 基礎結構即程式碼,搭配 Bicep 來建立資源。

  1. 登入 Microsoft Foundry。 確定 新鑄造廠 的開關是開啟的。 這些步驟指的是 Foundry (新版)。
  2. 你正在進行的專案會出現在左上角。
  3. 要建立新專案,請選擇專案名稱,然後 建立新專案。
  4. 給你的專案一個名稱,然後選擇 建立專案。

部署模型

  1. 在你的專案中新增一個模型。 在頁面中間選擇 「建構 」,然後選擇 「模型」。
  2. 選擇 部署基礎模型 以開啟模型目錄。
  3. 找到並選擇 DeepSeek-V4-Pro 型號圖塊以開啟其型號卡。 選擇部署>預設設定以使用預設設定,或選擇>部署自訂設定以查看並更改部署設定。

部署完成後,系統會帶您進入其遊樂場,您可以在其中開始與該部署進行互動。 請確認部署狀態顯示 「成功」,確認部署已準備好。 請注意部署 名稱 和 端點 URI ,這兩個都必須在部署細節中填寫——程式碼部分需要兩者。

如果你偏好先互動式探索模型,可以跳到「 在遊樂場中使用模型」。

用程式碼來使用模型

使用 Foundry Models 的端點和憑證來連接模型。

  1. 從遊樂場上方窗格選擇 「詳情 」窗格,查看部署細節。 在這裡,你可以找到部署的 URI 和 API 金鑰。
  2. 從部署的 URI 取得資源名稱,用來透過程式碼推論模型。

使用下一代 v1 Azure OpenAI API 來在你的程式碼中使用模型。 這些程式碼範例採用安全且無金鑰的認證方法,Microsoft Entra ID,透過 Azure 身份函式庫。

以下程式碼範例示範如何:

  1. 使用 DefaultAzureCredential 與 Microsoft Entra ID 進行驗證,該工具會自動嘗試多種驗證方法(環境變數、管理身份、Azure CLI等)。 具體順序取決於你使用的 Azure Identity SDK 版本。

    提示

    在本地開發時,請確保你已透過 Azure CLI 驗證,執行 az login。 在 Azure 中部署生產環境時,請為你的應用程式設定管理身份。

  2. 建立一個與你的模型部署連結的聊天完成客戶端

  3. 向 DeepSeek-V4-Pro 模型發送一個基本提示

  4. 接收並顯示回應

預期產出: 一個包含模型答案、推理過程(標籤內 <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 參考:

推理可能會產生更長的回應時間,並消耗更多代幣。 請參閱適用於 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 參考。

在操場上使用模型

在遊樂場使用模型,了解模型的功能。

部署完成後,你就會進入模型的遊樂場,開始與部署互動。 例如,你可以在遊樂場輸入提示,例如「世界上有多少種語言?」。

故障排除

如果你在跟著這個教學時遇到問題,請參考以下指引來解決常見問題。

認證錯誤(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 資源相符。

部署問題

  • 部署名稱與模型名稱。 model API 呼叫中的參數指的是你的部署名稱,而不是模型名稱。 如果你在建立時自訂了部署名稱,請使用該名稱而非 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 呼叫
  • 發送推理請求並接收推理輸出
  • 從模型回應中解析推理內容,以理解模型的思考過程