教程:Azure AI Foundry 模型中的 DeepSeek-R1 推理模型入门

在本教程中,学习以下内容:

  • 如何创建和配置 Azure 资源,以在 Foundry 模型中使用 DeepSeek-R1 模型。
  • 如何配置模型部署。
  • 如何使用 Azure AI 推理 SDK 或 REST API 使用 DeepSeek-R1。
  • 如何使用其他 SDK 来操作 DeepSeek-R1。

先决条件

若要完成本文,需要做好以下准备:

推理模型

推理模型可以在数学、编码、科学、策略和物流等领域达到更高的性能水平。 这些模型生成输出的方式是通过明确使用思维链来探索所有可能的路径,然后再生成答案。 他们在产生答案时会对其进行验证,这有助于他们得出更好、更准确的结论。 这意味着推理模型在提示时可能需要更少的上下文来产生有效的结果。

缩放模型性能的这种方式称为“推理计算时间”,因为它以较高的延迟和成本来换取性能。 它与通过“训练计算时间”缩放的其他方法形成鲜明对比。

然后,推理模型生成两种类型的输出:

  • 推理完成
  • 输出完成

这两个完成结果都算作由模型生成的内容,因此也计算在与模型相关的令牌限制和成本中。 某些模型可能会输出推理内容,例如 DeepSeek-R1。 其他模型(例如 o1)仅输出完成的输出部分。

创建资源

Foundry Models 是 Azure 中 Azure AI Foundry 资源中的一项功能。 可以在该资源下创建模型部署,以使用其预测。 还可以将资源连接到 Azure AI Foundry 中的 Azure AI 中心和项目,以根据需要创建智能应用程序。 下图显示了高级体系结构。

关系图显示本教程中创建的资源的高级体系结构。

若要创建支持 DeepSeek-R1 部署的 Azure AI 项目,请执行以下步骤。 还可以使用 Azure CLI基础结构即代码和 Bicep 来创建资源。

  1. 转到 Azure AI Foundry 门户 并使用帐户登录。

  2. 在登陆页上,选择“创建项目”。

    小窍门

    是否在 Azure AI Foundry 模型中使用 Azure OpenAI? 使用 Azure OpenAI 资源连接到 Azure AI Foundry 门户时,目录中仅显示 Azure OpenAI 模型。 若要查看模型的完整列表(包括 DeepSeek-R1),请使用顶部 的“公告 ”部分,并使用“ 浏览更多模型”选项找到卡片。

    显示卡片的屏幕截图,其中显示了用于浏览目录中所有模型的选项。

    将显示一个新窗口,其中包含模型的完整列表。 从列表中选择 DeepSeek-R1 ,然后选择“ 部署”。 向导要求创建新项目。

  3. 为项目命名,例如“my-project”。

  4. 在本教程中,我们将在新的 AI 中心下创建一个新的项目,因此选择“创建新中心”。 中心是多个项目的容器,允许跨所有项目共享资源。

  5. 为中心命名,例如“my-hub”,然后选择“下一步”。

  6. 向导会更新有关要创建的资源的详细信息。 选择要创建的 Azure 资源以查看详细信息。

    显示要创建的项目和中心的详细信息的屏幕截图。

  7. 可以看到已创建以下资源:

    财产 DESCRIPTION
    资源组 Azure 中所有资源的主容器。 这有助于获取协同工作的资源。 它还有助于确定与整个项目相关的成本的范围。
    位置 要创建的资源的区域。
    中心 Azure AI Foundry 中 AI 项目的主要容器。 中心可促进协作,并让你可以存储项目的信息。
    AI Foundry 资源允许访问 Azure AI 模型目录中的旗舰模型。 在本教程中,会创建一个新帐户,但 Azure AI Foundry 资源(以前已知的 Azure AI 服务)可以在多个中心和项目中共享。 中心使用与资源的连接来访问该资源可用的模型部署。 若要了解如何创建与 Azure AI Foundry 资源的连接,以使用模型,可阅读 “连接 AI 项目”。
  8. 选择 创建。 资源创建过程开始。

  9. 完成后,即可配置项目。

  10. Foundry 模型是需要在 Azure AI Foundry 中启用的预览功能。 在顶部导航栏的右上角,选择“预览功能”图标。 一个上下文边栏选项卡会显示在屏幕右侧。

  11. 打开 “将模型部署到 Azure AI 模型推理服务 ”功能。

    演示如何在 Azure AI Foundry 门户中启用 Azure AI 模型推理服务部署模型功能的动画。

  12. 关闭面板。

添加 DeepSeek-R1 模型部署

现在,让我们为 DeepSeek-R1 创建新的模型部署:

  1. 转到 Azure AI Foundry 门户中“模型目录”部分,找到模型 DeepSeek-R1 模型。

  2. 可以在模型卡中查看模型的详细信息。

  3. 选择“部署”。

  4. 向导会显示作为 Microsoft 第一方消费服务提供的 DeepSeek-R1 的模型条款和条件。 可以在 “数据”、“隐私和安全”下查看我们的隐私和安全承诺。

    小窍门

    通过选择 “定价”和“条款”查看模型的定价详细信息。

  5. 选择“订阅并部署”,即表示接受这些案例的条款。

    显示如何同意 DeepSeek-R1 模型的条款和条件的屏幕截图。

  6. 此时可以配置部署设置。 默认情况下,部署会接收正在部署的模型的名称。 部署名称用于请求路由到此特定模型部署的 model 参数中。 这也让你可以在附加特定配置时为模型配置特定名称。

  7. 我们会根据项目自动选择 Azure AI 服务连接。 使用“自定义”选项根据需求更改连接。 DeepSeek-R1 目前以 全局标准 部署类型提供,可提供更高的吞吐量和性能。

  8. 选择“部署”。

    显示如何部署模型的屏幕截图。

  9. 部署完成后,新模型会在页面中列出并可供使用。

在操场中使用模型

您可以通过在演示平台中使用该模型来开始了解模型的功能。

  1. 在部署详细信息页上,选择顶部栏中的“在沙盘中打开”选项。

  2. “部署 ”下拉列表中,已自动选择创建的部署。

  3. 根据需要配置系统提示。 一般情况下,推理模型不会采用与其他类型的模型相同的方式使用系统消息。

    屏幕截图显示如何在沙盒中选择要使用的模型部署、配置系统消息并进行测试。

  4. 键入提示并查看输出。

  5. 此外,可以使用“查看代码”,以便查看有关如何以编程方式访问模型部署的详细信息。

即时推理模型

生成推理模型的提示时,请考虑以下事项:

  • 使用简单的说明,避免使用思维链技巧。
  • 内置的推理功能使简单的零次提示和更复杂的方法一样有效。
  • 提供其他上下文或文档(例如在 RAG 方案中)时,仅包含最相关的信息可能有助于防止模型过度复杂化其响应。
  • 推理模型可能支持使用系统消息。 但是,它们可能不会像其他非推理模型那样严格地遵循它们。
  • 创建多轮次应用程序时,请考虑仅追加模型的最终答案,而不用包含其推理内容,详细信息请参阅推理内容部分。

请注意,推理模型可能需要更长的时间才能生成响应。 他们使用长期推理的思维链,从而实现更深层次、更结构化的问题解决。 他们还执行自我验证,交叉检查自己的答案,纠正自己的错误,展示新兴的自我反省行为。

在代码中使用模型

使用 Foundry 模型终结点和凭据连接到模型:

屏幕截图显示了如何获取与资源关联的 URL 和密钥。

可以使用 Azure AI 推理包在代码中使用模型:

使用包管理器(例如 pip)安装包 azure-ai-inference

pip install azure-ai-inference

然后,可以使用该包来调用模型。 以下示例演示如何创建客户端来使用聊天补全:

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

client = ChatCompletionsClient(
    endpoint="https://<resource>.services.ai.azure.com/models",
    credential=AzureKeyCredential(os.environ["AZURE_INFERENCE_CREDENTIAL"]),
)

浏览我们的示例,并阅读 API 参考文档以开始使用。

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

response = client.complete(
    messages=[
        UserMessage(content="How many languages are in the world?"),
    ],
    model="DeepSeek-R1"
)

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

推理可能会生成更长的响应,并消耗更多的令牌。 可以看到适用于 DeepSeek-R1 模型的 速率限制 。 请考虑使用重试策略来处理正在应用的速率限制。 还可以 请求增加默认限制

逻辑推理内容

某些推理模型(如 DeepSeek-R1)会生成补全内容并包含其背后的推理。 与补全关联的推理包含在标记 <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 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

参数

一般情况下,推理模型不支持在聊天完成模型中找到的以下参数:

  • 温度
  • 出现惩罚
  • 重复惩罚
  • 参数 top_p