共用方式為


如何使用 DeepSeek-R1 推理模型

重要

本文中標示為 (預覽) 的項目目前處於公開預覽狀態。 此預覽版本沒有服務等級協定,不建議將其用於生產工作負載。 可能不支援特定功能,或可能已經限制功能。 如需詳細資訊,請參閱 Microsoft Azure 預覽版增補使用條款

在本文中,您將瞭解 DeepSeek-R1 及其使用方式。 DeepSeek-R1 擅長使用逐步訓練程序進行推理工作,例如語言、科學推理和編碼工作。 它具有 671B 個使用中參數的總參數,以及 128k 個內容長度。

重要

處於預覽狀態的模型會在模型目錄中的模型卡片上標示為 預覽

DeepSeek-R1

DeepSeek-R1 是以先前以推理為主的模型進度為基礎,藉由擴充 Thought 鏈結(CoT) 推理來改善效能。 DeepSeek-R1 藉由結合增強式學習 (RL) 與微調精心選擇的數據集,進一步採取動作。 它從舊版的 DeepSeek-R1-Zero 進化而來,它完全依賴 RL 並表現出強大的推理技能,但有難以閱讀的輸出和語言不一致等問題。 為了解決這些限制,DeepSeek-R1 會納入少量冷啟動數據,並遵循精簡的定型管線,將推理導向的 RL 與策劃數據集的受監督微調混合在一起,進而產生一個模型,以達到推理基準的最先進的效能。

您可以在其各自的模型卡片中深入了解模型:

必要條件

若要搭配 Azure AI Foundry 使用 DeepSeek-R1,您需要下列必要條件:

模型部署

部署至無伺服器 API

DeepSeek-R1 可以透過隨用隨付計費部署到無伺服器 API 端點。 這種部署可讓您以 API 的形式取用模型,而不必在您的訂用帳戶上裝載模型,同時讓組織保持所需的企業安全性和合規性。

部署至無伺服器 API 端點不需要您訂用帳戶的配額。 如果您的模型尚未部署,請使用 Azure AI Studio、適用於 Python 的 Azure Machine Learning SDK、Azure CLI 或 ARM 範本來將模型部署為無伺服器 API (英文)。

已安裝推斷套件

您可以透過使用 azure-ai-inference 套件搭配 Python,從此模型取用預測。 若要安裝此套件,您需要下列先決條件:

  • 已安裝 Python 3.8 或更新版本,包括 pip。
  • 端點 URL。 若要建構用戶端程式庫,您必須傳遞端點 URL。 端點 URL 具有 https://your-host-name.your-azure-region.inference.ai.azure.com 的形式,其中 your-host-name 是您唯一的模型部署主機名稱,且 your-azure-region 是模型所部署的 Azure 區域 (例如 eastus2)。
  • 視您的模型部署和驗證喜好設定而定,您需要金鑰來針對服務進行驗證,或是 Microsoft Entra ID 認證。 金鑰是 32 個字元的字串。

具備這些先決條件之後,請使用下列命令安裝 Azure AI 推斷套件:

pip install azure-ai-inference

深入了解 [Azure AI 推斷套件和參考]

使用聊天完成

在本節中,您會使用 [Azure AI 模型推斷 API] 搭配聊天完成模型來用於聊天。

提示

Azure AI 模型推斷 API 可讓您使用相同程式代碼和結構,包括 DeepSeek-R1,與 Azure AI Foundry 中部署的大部分模型交談。

建立用戶端以取用模型

首先,建立用戶端以取用模型。 下列程式碼會使用儲存在環境變數中的端點 URL 和金鑰。

import os
from azure.ai.inference import ChatCompletionsClient
from azure.core.credentials import AzureKeyCredential

client = ChatCompletionsClient(
    endpoint=os.environ["AZURE_INFERENCE_ENDPOINT"],
    credential=AzureKeyCredential(os.environ["AZURE_INFERENCE_CREDENTIAL"]),
)

取得模型的功能

/info 路由會傳回部署至端點之模型的相關資訊。 透過呼叫下列方法,以傳回模型的資訊:

model_info = client.get_model_info()

回應如下:

print("Model name:", model_info.model_name)
print("Model type:", model_info.model_type)
print("Model provider name:", model_info.model_provider_name)
Model name: DeepSeek-R1
Model type: chat-completions
Model provider name: DeepSeek

建立聊天完成要求

下列範例示範如何針對模型建立基本聊天完成要求。

from azure.ai.inference.models import SystemMessage, UserMessage

response = client.complete(
    messages=[
        SystemMessage(content="You are a helpful assistant."),
        UserMessage(content="How many languages are in the world?"),
    ],
)

回應如下,您可以在其中查看模型的使用量統計資料:

print("Response:", 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)
Response: As of now, it's estimated that there are about 7,000 languages spoken around the world. However, this number can vary as some languages become extinct and new ones develop. It's also important to note that the number of speakers can greatly vary between languages, with some having millions of speakers and others only a few hundred.
Model: DeepSeek-R1
Usage: 
  Prompt tokens: 19
  Total tokens: 91
  Completion tokens: 72

檢查回應中的 usage 區段,以查看提示所使用的權杖數目、產生的權杖總數,以及用於完成文字的權杖數目。

瞭解推理

某些推理模型,例如 DeepSeek-R1,會產生完成,並包含其背後的推理。 與完成相關聯的推理包含在回應的內容標籤 <think></think>內。 模型可能會選取要產生推理內容的案例。 例如:

response = client.complete(
    messages=[
        SystemMessage(content="You are a helpful assistant."),
        UserMessage(content="How many languages are in the world?"),
    ],
)

您可以從回應擷取推理內容,以瞭解模型的想法程式,如下所示:

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 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. \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-R1
Usage: 
  Prompt tokens: 11
  Total tokens: 897
  Completion tokens: 886

串流內容

根據預設,完成 API 會在單一回應中傳回整個產生的內容。 如果您正在產生的完成很長,則等候回應可能需要數秒鐘的時間。

您可以 [串流] 內容,以在內容產生期間取得它。 串流內容可讓您在內容變成可用時立即開始處理完成。 此模式會傳回以 [僅限資料的伺服器傳送事件] 形式將回應串流回來的物件。 從差異欄位擷取區塊,而不是訊息欄位。

result = client.complete(
    messages=[
        SystemMessage(content="You are a helpful assistant."),
        UserMessage(content="How many languages are in the world?"),
    ],
    temperature=0,
    top_p=1,
    max_tokens=2048,
    stream=True,
)

若要串流完成,請在呼叫模型時設定 stream=True

若要將輸出視覺化,請定義協助程式函式來列印串流。

def print_stream(result):
    """
    Prints the chat completion with streaming.
    """
    for update in result:
        if update.choices:
            print(update.choices[0].delta.content, end="")

您可以將串流產生內容的方式視覺化:

print_stream(result)

套用內容安全

Azure AI 模型推斷 API 支援 Azure AI 內容安全。 當您使用已開啟 Azure AI 內容安全的部署時,輸入和輸出都會通過旨在偵測及防止有害內容輸出的一組分類模型。 內容篩選 (預覽) 系統會偵測並針對輸入提示和輸出完成中潛在有害內容的特定類別採取動作。

下列範例示範當模型偵測到輸入提示中的有害內容並啟用內容安全時,如何處理事件。

from azure.ai.inference.models import AssistantMessage, UserMessage, SystemMessage

try:
    response = client.complete(
        messages=[
            SystemMessage(content="You are an AI assistant that helps people find information."),
            UserMessage(content="Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills."),
        ]
    )

    print(response.choices[0].message.content)

except HttpResponseError as ex:
    if ex.status_code == 400:
        response = ex.response.json()
        if isinstance(response, dict) and "error" in response:
            print(f"Your request triggered an {response['error']['code']} error:\n\t {response['error']['message']}")
        else:
            raise
    raise

提示

若要深入了解如何設定及控制 Azure AI 內容安全設定,請參閱 Azure AI 內容安全文件

DeepSeek-R1

DeepSeek-R1 是以先前以推理為主的模型進度為基礎,藉由擴充 Thought 鏈結(CoT) 推理來改善效能。 DeepSeek-R1 藉由結合增強式學習 (RL) 與微調精心選擇的數據集,進一步採取動作。 它從舊版的 DeepSeek-R1-Zero 進化而來,它完全依賴 RL 並表現出強大的推理技能,但有難以閱讀的輸出和語言不一致等問題。 為了解決這些限制,DeepSeek-R1 會納入少量冷啟動數據,並遵循精簡的定型管線,將推理導向的 RL 與策劃數據集的受監督微調混合在一起,進而產生一個模型,以達到推理基準的最先進的效能。

您可以在其各自的模型卡片中深入了解模型:

必要條件

若要搭配 Azure AI Foundry 使用 DeepSeek-R1,您需要下列必要條件:

模型部署

部署至無伺服器 API

DeepSeek-R1 可以透過隨用隨付計費部署到無伺服器 API 端點。 這種部署可讓您以 API 的形式取用模型,而不必在您的訂用帳戶上裝載模型,同時讓組織保持所需的企業安全性和合規性。

部署至無伺服器 API 端點不需要您訂用帳戶的配額。 如果您的模型尚未部署,請使用 Azure AI Studio、適用於 Python 的 Azure Machine Learning SDK、Azure CLI 或 ARM 範本來將模型部署為無伺服器 API (英文)。

已安裝推斷套件

您可以使用 npm@azure-rest/ai-inference 套件來取用此模型的預測。 若要安裝此套件,您需要下列先決條件:

  • 具有 npmNode.js LTS 版本。
  • 端點 URL。 若要建構用戶端程式庫,您必須傳遞端點 URL。 端點 URL 具有 https://your-host-name.your-azure-region.inference.ai.azure.com 的形式,其中 your-host-name 是您唯一的模型部署主機名稱,且 your-azure-region 是模型所部署的 Azure 區域 (例如 eastus2)。
  • 視您的模型部署和驗證喜好設定而定,您需要金鑰來針對服務進行驗證,或是 Microsoft Entra ID 認證。 金鑰是 32 個字元的字串。

具備這些先決條件之後,使用下列命令來安裝適用於 JavaScript 的 Azure 推斷程式庫:

npm install @azure-rest/ai-inference

使用聊天完成

在本節中,您會使用 [Azure AI 模型推斷 API] 搭配聊天完成模型來用於聊天。

提示

Azure AI 模型推斷 API 可讓您使用相同程式代碼和結構,包括 DeepSeek-R1,與 Azure AI Foundry 中部署的大部分模型交談。

建立用戶端以取用模型

首先,建立用戶端以取用模型。 下列程式碼會使用儲存在環境變數中的端點 URL 和金鑰。

import ModelClient from "@azure-rest/ai-inference";
import { isUnexpected } from "@azure-rest/ai-inference";
import { AzureKeyCredential } from "@azure/core-auth";

const client = new ModelClient(
    process.env.AZURE_INFERENCE_ENDPOINT, 
    new AzureKeyCredential(process.env.AZURE_INFERENCE_CREDENTIAL)
);

取得模型的功能

/info 路由會傳回部署至端點之模型的相關資訊。 透過呼叫下列方法,以傳回模型的資訊:

var model_info = await client.path("/info").get()

回應如下:

console.log("Model name: ", model_info.body.model_name)
console.log("Model type: ", model_info.body.model_type)
console.log("Model provider name: ", model_info.body.model_provider_name)
Model name: DeepSeek-R1
Model type: chat-completions
Model provider name: DeepSeek

建立聊天完成要求

下列範例示範如何針對模型建立基本聊天完成要求。

var messages = [
    { role: "system", content: "You are a helpful assistant" },
    { role: "user", content: "How many languages are in the world?" },
];

var response = await client.path("/chat/completions").post({
    body: {
        messages: messages,
    }
});

回應如下,您可以在其中查看模型的使用量統計資料:

if (isUnexpected(response)) {
    throw response.body.error;
}

console.log("Response: ", response.body.choices[0].message.content);
console.log("Model: ", response.body.model);
console.log("Usage:");
console.log("\tPrompt tokens:", response.body.usage.prompt_tokens);
console.log("\tTotal tokens:", response.body.usage.total_tokens);
console.log("\tCompletion tokens:", response.body.usage.completion_tokens);
Response: As of now, it's estimated that there are about 7,000 languages spoken around the world. However, this number can vary as some languages become extinct and new ones develop. It's also important to note that the number of speakers can greatly vary between languages, with some having millions of speakers and others only a few hundred.
Model: DeepSeek-R1
Usage: 
  Prompt tokens: 19
  Total tokens: 91
  Completion tokens: 72

檢查回應中的 usage 區段,以查看提示所使用的權杖數目、產生的權杖總數,以及用於完成文字的權杖數目。

瞭解推理

某些推理模型,例如 DeepSeek-R1,會產生完成,並包含其背後的推理。 與完成相關聯的推理包含在回應的內容標籤 <think></think>內。 模型可能會選取要產生推理內容的案例。 例如:

var messages = [
    { role: "system", content: "You are a helpful assistant" },
    { role: "user", content: "How many languages are in the world?" },
];

var response = await client.path("/chat/completions").post({
    body: {
        messages: messages,
    }
});

您可以從回應擷取推理內容,以瞭解模型的想法程式,如下所示:

var content = response.body.choices[0].message.content
var match = content.match(/<think>(.*?)<\/think>(.*)/s);

console.log("Response:");
if (match) {
    console.log("\tThinking:", match[1]);
    console.log("\Answer:", match[2]);
}
else {
    console.log("Response:", content);
}
console.log("Model: ", response.body.model);
console.log("Usage:");
console.log("\tPrompt tokens:", response.body.usage.prompt_tokens);
console.log("\tTotal tokens:", response.body.usage.total_tokens);
console.log("\tCompletion tokens:", response.body.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 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. \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-R1
Usage: 
  Prompt tokens: 11
  Total tokens: 897
  Completion tokens: 886

串流內容

根據預設,完成 API 會在單一回應中傳回整個產生的內容。 如果您正在產生的完成很長,則等候回應可能需要數秒鐘的時間。

您可以 [串流] 內容,以在內容產生期間取得它。 串流內容可讓您在內容變成可用時立即開始處理完成。 此模式會傳回以 [僅限資料的伺服器傳送事件] 形式將回應串流回來的物件。 從差異欄位擷取區塊,而不是訊息欄位。

var messages = [
    { role: "system", content: "You are a helpful assistant" },
    { role: "user", content: "How many languages are in the world?" },
];

var response = await client.path("/chat/completions").post({
    body: {
        messages: messages,
    }
}).asNodeStream();

若要串流完成,在呼叫模型時使用 .asNodeStream()

您可以將串流產生內容的方式視覺化:

var stream = response.body;
if (!stream) {
    stream.destroy();
    throw new Error(`Failed to get chat completions with status: ${response.status}`);
}

if (response.status !== "200") {
    throw new Error(`Failed to get chat completions: ${response.body.error}`);
}

var sses = createSseStream(stream);

for await (const event of sses) {
    if (event.data === "[DONE]") {
        return;
    }
    for (const choice of (JSON.parse(event.data)).choices) {
        console.log(choice.delta?.content ?? "");
    }
}

套用內容安全

Azure AI 模型推斷 API 支援 Azure AI 內容安全。 當您使用已開啟 Azure AI 內容安全的部署時,輸入和輸出都會通過旨在偵測及防止有害內容輸出的一組分類模型。 內容篩選 (預覽) 系統會偵測並針對輸入提示和輸出完成中潛在有害內容的特定類別採取動作。

下列範例示範當模型偵測到輸入提示中的有害內容並啟用內容安全時,如何處理事件。

try {
    var messages = [
        { role: "system", content: "You are an AI assistant that helps people find information." },
        { role: "user", content: "Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills." },
    ];

    var response = await client.path("/chat/completions").post({
        body: {
            messages: messages,
        }
    });

    console.log(response.body.choices[0].message.content);
}
catch (error) {
    if (error.status_code == 400) {
        var response = JSON.parse(error.response._content);
        if (response.error) {
            console.log(`Your request triggered an ${response.error.code} error:\n\t ${response.error.message}`);
        }
        else
        {
            throw error;
        }
    }
}

提示

若要深入了解如何設定及控制 Azure AI 內容安全設定,請參閱 Azure AI 內容安全文件

DeepSeek-R1

DeepSeek-R1 是以先前以推理為主的模型進度為基礎,藉由擴充 Thought 鏈結(CoT) 推理來改善效能。 DeepSeek-R1 藉由結合增強式學習 (RL) 與微調精心選擇的數據集,進一步採取動作。 它從舊版的 DeepSeek-R1-Zero 進化而來,它完全依賴 RL 並表現出強大的推理技能,但有難以閱讀的輸出和語言不一致等問題。 為了解決這些限制,DeepSeek-R1 會納入少量冷啟動數據,並遵循精簡的定型管線,將推理導向的 RL 與策劃數據集的受監督微調混合在一起,進而產生一個模型,以達到推理基準的最先進的效能。

您可以在其各自的模型卡片中深入了解模型:

必要條件

若要搭配 Azure AI Foundry 使用 DeepSeek-R1,您需要下列必要條件:

模型部署

部署至無伺服器 API

DeepSeek-R1 可以透過隨用隨付計費部署到無伺服器 API 端點。 這種部署可讓您以 API 的形式取用模型,而不必在您的訂用帳戶上裝載模型,同時讓組織保持所需的企業安全性和合規性。

部署至無伺服器 API 端點不需要您訂用帳戶的配額。 如果您的模型尚未部署,請使用 Azure AI Studio、適用於 Python 的 Azure Machine Learning SDK、Azure CLI 或 ARM 範本來將模型部署為無伺服器 API (英文)。

已安裝推斷套件

您可以從 [NuGet] 使用 Azure.AI.Inference 套件來取用此模型的預測。 若要安裝此套件,您需要下列先決條件:

  • 端點 URL。 若要建構用戶端程式庫,您必須傳遞端點 URL。 端點 URL 具有 https://your-host-name.your-azure-region.inference.ai.azure.com 的形式,其中 your-host-name 是您唯一的模型部署主機名稱,且 your-azure-region 是模型所部署的 Azure 區域 (例如 eastus2)。
  • 視您的模型部署和驗證喜好設定而定,您需要金鑰來針對服務進行驗證,或是 Microsoft Entra ID 認證。 金鑰是 32 個字元的字串。

具備這些先決條件之後,使用下列命令來安裝 Azure AI 推斷程式庫:

dotnet add package Azure.AI.Inference --prerelease

您也可以使用 Microsoft Entra ID (先前稱為 Azure Active Directory) 進行驗證。 若要使用 Azure SDK 所提供的認證提供者,請安裝 Azure.Identity 套件:

dotnet add package Azure.Identity

匯入下列命名空間:

using Azure;
using Azure.Identity;
using Azure.AI.Inference;

此範例也會使用下列命名空間,但您可能不一定需要它們:

using System.Text.Json;
using System.Text.Json.Serialization;
using System.Reflection;

使用聊天完成

在本節中,您會使用 [Azure AI 模型推斷 API] 搭配聊天完成模型來用於聊天。

提示

Azure AI 模型推斷 API 可讓您使用相同程式代碼和結構,包括 DeepSeek-R1,與 Azure AI Foundry 中部署的大部分模型交談。

建立用戶端以取用模型

首先,建立用戶端以取用模型。 下列程式碼會使用儲存在環境變數中的端點 URL 和金鑰。

ChatCompletionsClient client = new ChatCompletionsClient(
    new Uri(Environment.GetEnvironmentVariable("AZURE_INFERENCE_ENDPOINT")),
    new AzureKeyCredential(Environment.GetEnvironmentVariable("AZURE_INFERENCE_CREDENTIAL")),
    "DeepSeek-R1"
);

取得模型的功能

/info 路由會傳回部署至端點之模型的相關資訊。 透過呼叫下列方法,以傳回模型的資訊:

Response<ModelInfo> modelInfo = client.GetModelInfo();

回應如下:

Console.WriteLine($"Model name: {modelInfo.Value.ModelName}");
Console.WriteLine($"Model type: {modelInfo.Value.ModelType}");
Console.WriteLine($"Model provider name: {modelInfo.Value.ModelProviderName}");
Model name: DeepSeek-R1
Model type: chat-completions
Model provider name: DeepSeek

建立聊天完成要求

下列範例示範如何針對模型建立基本聊天完成要求。

ChatCompletionsOptions requestOptions = new ChatCompletionsOptions()
{
    Messages = {
        new ChatRequestSystemMessage("You are a helpful assistant."),
        new ChatRequestUserMessage("How many languages are in the world?")
    },
};

Response<ChatCompletions> response = client.Complete(requestOptions);

回應如下,您可以在其中查看模型的使用量統計資料:

Console.WriteLine($"Response: {response.Value.Content}");
Console.WriteLine($"Model: {response.Value.Model}");
Console.WriteLine("Usage:");
Console.WriteLine($"\tPrompt tokens: {response.Value.Usage.PromptTokens}");
Console.WriteLine($"\tTotal tokens: {response.Value.Usage.TotalTokens}");
Console.WriteLine($"\tCompletion tokens: {response.Value.Usage.CompletionTokens}");
Response: As of now, it's estimated that there are about 7,000 languages spoken around the world. However, this number can vary as some languages become extinct and new ones develop. It's also important to note that the number of speakers can greatly vary between languages, with some having millions of speakers and others only a few hundred.
Model: DeepSeek-R1
Usage: 
  Prompt tokens: 19
  Total tokens: 91
  Completion tokens: 72

檢查回應中的 usage 區段,以查看提示所使用的權杖數目、產生的權杖總數,以及用於完成文字的權杖數目。

瞭解推理

某些推理模型,例如 DeepSeek-R1,會產生完成,並包含其背後的推理。 與完成相關聯的推理包含在回應的內容標籤 <think></think>內。 模型可能會選取要產生推理內容的案例。 例如:

ChatCompletionsOptions requestOptions = new ChatCompletionsOptions()
{
    Messages = {
        new ChatRequestSystemMessage("You are a helpful assistant."),
        new ChatRequestUserMessage("How many languages are in the world?")
    },
};

Response<ChatCompletions> response = client.Complete(requestOptions);

您可以從回應擷取推理內容,以瞭解模型的想法程式,如下所示:

Regex regex = new Regex(pattern, RegexOptions.Singleline);
Match match = regex.Match(response.Value.Content);

Console.WriteLine("Response:");
if (match.Success)
{
    Console.WriteLine($"\tThinking: {match.Groups[1].Value}");
    Console.WriteLine($"\tAnswer: {match.Groups[2].Value}");
else
{
    Console.WriteLine($"Response: {response.Value.Content}");
}
Console.WriteLine($"Model: {response.Value.Model}");
Console.WriteLine("Usage:");
Console.WriteLine($"\tPrompt tokens: {response.Value.Usage.PromptTokens}");
Console.WriteLine($"\tTotal tokens: {response.Value.Usage.TotalTokens}");
Console.WriteLine($"\tCompletion tokens: {response.Value.Usage.CompletionTokens}");
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 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. \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-R1
Usage: 
  Prompt tokens: 11
  Total tokens: 897
  Completion tokens: 886

串流內容

根據預設,完成 API 會在單一回應中傳回整個產生的內容。 如果您正在產生的完成很長,則等候回應可能需要數秒鐘的時間。

您可以 [串流] 內容,以在內容產生期間取得它。 串流內容可讓您在內容變成可用時立即開始處理完成。 此模式會傳回以 [僅限資料的伺服器傳送事件] 形式將回應串流回來的物件。 從差異欄位擷取區塊,而不是訊息欄位。

static async Task StreamMessageAsync(ChatCompletionsClient client)
{
    ChatCompletionsOptions requestOptions = new ChatCompletionsOptions()
    {
        Messages = {
            new ChatRequestSystemMessage("You are a helpful assistant."),
            new ChatRequestUserMessage("How many languages are in the world? Write an essay about it.")
        },
        MaxTokens=4096
    };

    StreamingResponse<StreamingChatCompletionsUpdate> streamResponse = await client.CompleteStreamingAsync(requestOptions);

    await PrintStream(streamResponse);
}

若要串流完成,在呼叫模型時使用 CompleteStreamingAsync 方法。 請注意,在此範例中,呼叫會包裝在非同步方法中。

若要將輸出視覺化,請定義非同步方法,以在主控台中列印串流。

static async Task PrintStream(StreamingResponse<StreamingChatCompletionsUpdate> response)
{
    await foreach (StreamingChatCompletionsUpdate chatUpdate in response)
    {
        if (chatUpdate.Role.HasValue)
        {
            Console.Write($"{chatUpdate.Role.Value.ToString().ToUpperInvariant()}: ");
        }
        if (!string.IsNullOrEmpty(chatUpdate.ContentUpdate))
        {
            Console.Write(chatUpdate.ContentUpdate);
        }
    }
}

您可以將串流產生內容的方式視覺化:

StreamMessageAsync(client).GetAwaiter().GetResult();

套用內容安全

Azure AI 模型推斷 API 支援 Azure AI 內容安全。 當您使用已開啟 Azure AI 內容安全的部署時,輸入和輸出都會通過旨在偵測及防止有害內容輸出的一組分類模型。 內容篩選 (預覽) 系統會偵測並針對輸入提示和輸出完成中潛在有害內容的特定類別採取動作。

下列範例示範當模型偵測到輸入提示中的有害內容並啟用內容安全時,如何處理事件。

try
{
    requestOptions = new ChatCompletionsOptions()
    {
        Messages = {
            new ChatRequestSystemMessage("You are an AI assistant that helps people find information."),
            new ChatRequestUserMessage(
                "Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills."
            ),
        },
    };

    response = client.Complete(requestOptions);
    Console.WriteLine(response.Value.Content);
}
catch (RequestFailedException ex)
{
    if (ex.ErrorCode == "content_filter")
    {
        Console.WriteLine($"Your query has trigger Azure Content Safety: {ex.Message}");
    }
    else
    {
        throw;
    }
}

提示

若要深入了解如何設定及控制 Azure AI 內容安全設定,請參閱 Azure AI 內容安全文件

DeepSeek-R1

DeepSeek-R1 是以先前以推理為主的模型進度為基礎,藉由擴充 Thought 鏈結(CoT) 推理來改善效能。 DeepSeek-R1 藉由結合增強式學習 (RL) 與微調精心選擇的數據集,進一步採取動作。 它從舊版的 DeepSeek-R1-Zero 進化而來,它完全依賴 RL 並表現出強大的推理技能,但有難以閱讀的輸出和語言不一致等問題。 為了解決這些限制,DeepSeek-R1 會納入少量冷啟動數據,並遵循精簡的定型管線,將推理導向的 RL 與策劃數據集的受監督微調混合在一起,進而產生一個模型,以達到推理基準的最先進的效能。

您可以在其各自的模型卡片中深入了解模型:

必要條件

若要搭配 Azure AI Foundry 使用 DeepSeek-R1,您需要下列必要條件:

模型部署

部署至無伺服器 API

DeepSeek-R1 可以透過隨用隨付計費部署到無伺服器 API 端點。 這種部署可讓您以 API 的形式取用模型,而不必在您的訂用帳戶上裝載模型,同時讓組織保持所需的企業安全性和合規性。

部署至無伺服器 API 端點不需要您訂用帳戶的配額。 如果您的模型尚未部署,請使用 Azure AI Studio、適用於 Python 的 Azure Machine Learning SDK、Azure CLI 或 ARM 範本來將模型部署為無伺服器 API (英文)。

REST 用戶端

使用 [Azure AI 模型推斷 API] 部署的模型,可以使用任何 REST 用戶端來取用。 若要使用 REST 用戶端,您需要下列先決條件:

  • 若要建構要求,您必須傳入端點 URL。 端點 URL 具有 https://your-host-name.your-azure-region.inference.ai.azure.com 形式,其中 your-host-name`` is your unique model deployment host name and your-azure-region`` 是模型部署所在的 Azure 區域 (例如 eastus2)。
  • 視您的模型部署和驗證喜好設定而定,您需要金鑰來針對服務進行驗證,或是 Microsoft Entra ID 認證。 金鑰是 32 個字元的字串。

使用聊天完成

在本節中,您會使用 [Azure AI 模型推斷 API] 搭配聊天完成模型來用於聊天。

提示

Azure AI 模型推斷 API 可讓您使用相同程式代碼和結構,包括 DeepSeek-R1,與 Azure AI Foundry 中部署的大部分模型交談。

建立用戶端以取用模型

首先,建立用戶端以取用模型。 下列程式碼會使用儲存在環境變數中的端點 URL 和金鑰。

取得模型的功能

/info 路由會傳回部署至端點之模型的相關資訊。 透過呼叫下列方法,以傳回模型的資訊:

GET /info HTTP/1.1
Host: <ENDPOINT_URI>
Authorization: Bearer <TOKEN>
Content-Type: application/json

回應如下:

{
    "model_name": "DeepSeek-R1",
    "model_type": "chat-completions",
    "model_provider_name": "DeepSeek"
}

建立聊天完成要求

下列範例示範如何針對模型建立基本聊天完成要求。

{
    "model": "DeepSeek-R1",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "How many languages are in the world?"
        }
    ]
}

回應如下,您可以在其中查看模型的使用量統計資料:

{
    "id": "0a1234b5de6789f01gh2i345j6789klm",
    "object": "chat.completion",
    "created": 1718726686,
    "model": "DeepSeek-R1",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "As of now, it's estimated that there are about 7,000 languages spoken around the world. However, this number can vary as some languages become extinct and new ones develop. It's also important to note that the number of speakers can greatly vary between languages, with some having millions of speakers and others only a few hundred.",
                "tool_calls": null
            },
            "finish_reason": "stop",
            "logprobs": null
        }
    ],
    "usage": {
        "prompt_tokens": 19,
        "total_tokens": 91,
        "completion_tokens": 72
    }
}

檢查回應中的 usage 區段,以查看提示所使用的權杖數目、產生的權杖總數,以及用於完成文字的權杖數目。

瞭解推理

某些推理模型,例如 DeepSeek-R1,會產生完成,並包含其背後的推理。 與完成相關聯的推理包含在回應的內容標籤 <think></think>內。 模型可能會選取要產生推理內容的案例。 例如:

{
    "model": "DeepSeek-R1",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "How many languages are in the world?"
        }
    ]
}

您可以從回應擷取推理內容,以瞭解模型的想法程式,如下所示:

{
    "id": "0a1234b5de6789f01gh2i345j6789klm",
    "object": "chat.completion",
    "created": 1718726686,
    "model": "DeepSeek-R1",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "<think>\nOkay, 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 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. \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.\n</think>\n\nThe 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.",
                "tool_calls": null
            },
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 11,
        "total_tokens": 897,
        "completion_tokens": 886
    }
}

串流內容

根據預設,完成 API 會在單一回應中傳回整個產生的內容。 如果您正在產生的完成很長,則等候回應可能需要數秒鐘的時間。

您可以 [串流] 內容,以在內容產生期間取得它。 串流內容可讓您在內容變成可用時立即開始處理完成。 此模式會傳回以 [僅限資料的伺服器傳送事件] 形式將回應串流回來的物件。 從差異欄位擷取區塊,而不是訊息欄位。

{
    "model": "DeepSeek-R1",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "How many languages are in the world?"
        }
    ],
    "stream": true,
    "temperature": 0,
    "top_p": 1,
    "max_tokens": 2048
}

您可以將串流產生內容的方式視覺化:

{
    "id": "23b54589eba14564ad8a2e6978775a39",
    "object": "chat.completion.chunk",
    "created": 1718726371,
    "model": "DeepSeek-R1",
    "choices": [
        {
            "index": 0,
            "delta": {
                "role": "assistant",
                "content": ""
            },
            "finish_reason": null,
            "logprobs": null
        }
    ]
}

串流中的最後一則訊息已設定 finish_reason,其會指出產生流程停止的原因。

{
    "id": "23b54589eba14564ad8a2e6978775a39",
    "object": "chat.completion.chunk",
    "created": 1718726371,
    "model": "DeepSeek-R1",
    "choices": [
        {
            "index": 0,
            "delta": {
                "content": ""
            },
            "finish_reason": "stop",
            "logprobs": null
        }
    ],
    "usage": {
        "prompt_tokens": 19,
        "total_tokens": 91,
        "completion_tokens": 72
    }
}

套用內容安全

Azure AI 模型推斷 API 支援 Azure AI 內容安全。 當您使用已開啟 Azure AI 內容安全的部署時,輸入和輸出都會通過旨在偵測及防止有害內容輸出的一組分類模型。 內容篩選 (預覽) 系統會偵測並針對輸入提示和輸出完成中潛在有害內容的特定類別採取動作。

下列範例示範當模型偵測到輸入提示中的有害內容並啟用內容安全時,如何處理事件。

{
    "model": "DeepSeek-R1",
    "messages": [
        {
            "role": "system",
            "content": "You are an AI assistant that helps people find information."
        },
                {
            "role": "user",
            "content": "Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills."
        }
    ]
}
{
    "error": {
        "message": "The response was filtered due to the prompt triggering Microsoft's content management policy. Please modify your prompt and retry.",
        "type": null,
        "param": "prompt",
        "code": "content_filter",
        "status": 400
    }
}

提示

若要深入了解如何設定及控制 Azure AI 內容安全設定,請參閱 Azure AI 內容安全文件

更多推斷範例

如需如何使用 DeepSeek 模型的詳細資訊,請參閱下列範例和教學課程:

描述 語言 範例
適用於 Python 的 Azure AI 推斷套件 Python 連結
適用於 JavaScript 的 Azure AI 推斷套件 JavaScript 連結
適用於 C 的 Azure AI 推斷套件# C# 連結
適用於 Java 的 Azure AI 推斷套件 Java 連結

部署為無伺服器 API 端點的 DeepSeek 模型的成本和配額考慮

配額會根據每個部署管理。 每個部署的速率限制為每分鐘 200,000 個權杖,每分鐘 1,000 個 API 要求。 不過,我們目前限制每個專案的每個模型為一個部署。 如果目前的速率限制無法滿足您的情節,請連絡 Microsoft Azure 支援。