> ## Content Index
> Fetch the complete content index at: https://www.hubwiz.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# 在.Net应用中集成DeepSeek-R1
- URL: https://www.hubwiz.com/blog/integrate-deepseek-r1-into-dotnet-apps/
- Published: 2025-02-08T05:08:47.000Z
- Updated: 2025-02-08T05:08:47.000Z
- Description: 本文将引导你了解如何在 GitHub Models 上将 Microsoft.Extensions.AI (MEAI) 库与 DeepSeek R1 结合使用，以便你今天就可以开始尝试使用 R1 模型。
- Author: admin
- Tags: APPLICATION, DEEPSEEKR1, DOTNET, MEAI, GITHUB MODELS, AZURE AI FOUNDRY, OLLAMA, GITHUB CODESPACE, #Import 2026-10-14 14:41

DeepSeek R1 模型最近引起了广泛关注。我们被问到的一个问题是：“我可以在我的 .NET 应用程序中使用 DeepSeek 吗？”答案是肯定的！我将引导你了解如何在 GitHub Models 上将[ Microsoft.Extensions.AI (MEAI) 库](https://learn.microsoft.com/dotnet/ai/ai-extensions?ref=hubwiz.com)与 DeepSeek R1 结合使用，以便你今天就可以开始尝试使用 R1 模型。

## 1、MEAI 让使用 AI 服务变得简单

MEAI 库提供了一组统一的抽象和中间件，以简化 AI 服务与 .NET 应用程序中的集成。

换句话说，如果你使用 MEAI 开发应用程序，无论决定在幕后使用哪种模型，你的代码都将使用相同的 API。这降低了构建 .NET AI 应用程序所需的摩擦，因为无论您使用哪种 AI 服务，您只需要记住一个库（MEAI）的做事方式。

对于 MEAI，你将使用的主要界面是 `IChatClient`。

## 1、让我们与 DeepSeek R1 聊天

GitHub Models 允许你尝试大量不同的 AI 模型，而无需担心托管。这是免费开始 AI 开发之旅的好方法。而且 GitHub Models 会不断更新新模型，例如 DeepSeek 的 R1。

![](https://www.hubwiz.com/blog/content/images/2025/02/image-232.png)

我们要构建的演示应用程序是一个简单的控制台应用程序，可在 GitHub 上的 [codemillmatt/deepseek-dotnet](https://github.com/codemillmatt/deepseek-dotnet?ref=hubwiz.com) 上找到。你可以克隆或分叉它以继续操作，但我们也会在下面讨论重要部分。

首先让我们处理一些先决条件：

- 前往 GitHub 并生成个人访问令牌 (PAT)。这将是你访问 GitHub Models 的密钥。按照[这些说明](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens?ref=hubwiz.com#creating-a-personal-access-token-classic)创建 PAT。你将需要一个经典令牌。
- 打开 DeepSeek.Console.GHModels 项目。你可以在 Visual Studio 中打开完整解决方案，如果使用 VS Code，则只需打开项目文件夹。
- 为 GitHub PAT 创建一个新的用户机密条目。将其命名为 `GH_TOKEN`，并粘贴您生成的 PAT 作为值。

现在让我们稍微探索一下代码：

- 打开 DeepSeek.Console.GHModels 项目中的 Program.cs 文件。
- 首先要注意的两件事是我们初始化 `modelEndpoint` 和 `modelName` 变量的位置。这些是 GitHub Models 服务的标准，它们将始终相同。
- 现在到了有趣的部分！我们将初始化我们的聊天客户端。这是我们连接到 DeepSeek R1 模型的地方。

```
IChatClient client = new ChatCompletionsClient(modelEndpoint, new AzureKeyCredential(Configuration["GH_TOKEN"])).AsChatClient(modelName);
```

这使用 `Microsoft.Extensions.AI.AzureAIInference` 包连接到 GitHub Models 服务。但是 `AsChatClient` 函数返回一个 `IChatClient` 实现。这太酷了。因为无论我们从 GitHub Models 中选择哪个模型，我们仍然会根据 `IChatClient` 接口编写应用程序！

- 接下来，我们将问题或提示传递给模型。我们将使用确保我们得到流式响应，这样我们就可以显示它。

```
var response = client.CompleteStreamingAsync(question);

await foreach (var item in response)
{
    Console.Write(item);
}
```

  
就是这样！继续运行该项目。可能需要几秒钟才能得到响应（很多人都在尝试这个模型！）。你会注意到响应与你在“正常”聊天机器人中看到的不一样。DeepSeek R1 是一个推理模型，所以它想要找出问题并推理。响应的第一部分是其推理，以 `<think>` 分隔，非常有趣。响应的第二部分是你所提问题的答案。

以下是响应的部分示例：

```
<think>
Okay, let's try to figure this out. The problem says: If I have 3 apples and eat 2, how many bananas do I have? Hmm, at first glance, that seems a bit confusing. Let me break it down step by step.

So, the person starts with 3 apples. Then they eat 2 of them. That part is straightforward. If you eat 2 apples out of 3, you'd have 1 apple left, right? But then the question shifts to bananas. Wait, where did bananas come from? The original problem only mentions apples. There's no mention of bananas at all.

...
```

## 2、我必须使用 GitHub 模型吗？

你不仅限于在 GitHub Models 上运行 DeepSeek R1。你可以在 Azure 上运行它，甚至可以在本地（或在 GitHub Codespaces 上）通过 Ollama 运行它。我在 GitHub 存储库中提供了另外 2 个控制台应用程序，向你展示了如何执行此操作。

GitHub Models 版本之间的最大区别在于 DeepSeek R1 模型的部署位置、用于连接它的凭据以及特定的模型名称。

如果你在 Azure AI Foundry 上部署，代码完全相同。[这里](https://azure.microsoft.com/blog/deepseek-r1-is-now-available-on-azure-ai-foundry-and-github/?ref=hubwiz.com)是有关如何将 DeepSeek R1 模型部署到 Azure AI Foundry 的一些说明。

如果你想在 Ollama 上本地运行，我们提供了一个 devcontainer 定义，你可以使用它在 Docker 中运行 Ollama。它会自动拉下一个小参数版本的 DeepSeek R1 并为你启动它。唯一的区别是你将使用 `Microsoft.Extensions.AI.Ollama` NuGet 包并使用 `OllamaChatClient` 初始化 `IChatClient`。与 DeepSeek R1 的交互是相同的。

> 注意：如果你[在 GitHub Codespace 中运行它](https://codespaces.new/codemillmatt/deepseek-dotnet?ref=hubwiz.com)，则需要几分钟才能启动，并且将使用大约 8GB 的​​空间 - 因此请注意你的 Codespace 计划。

当然，这些都是简单的控制台应用程序。如果你使用的是 .NET Aspire，则可以轻松使用 Ollama 和 DeepSeek R1。得益于 .NET Aspire Community Toolkit 的 Ollama 集成，只需添加一行即可完成所有设置！

```
var chat = ollama.AddModel("chat", "deepseek-r1");
```

查看[这篇博客文章](https://devblogs.microsoft.com/dotnet/local-ai-models-with-dotnet-aspire/?ref=hubwiz.com)，了解如何开始的所有详细信息。

## 3、结束语

DeepSeek R1 是一个令人兴奋的新推理模型，它引起了很多关注，你可以使用 Microsoft.Extensions.AI 库构建使用它的 .NET 应用程序。GitHub Models 降低了入门和试验的阻力。继续试用[示例](https://github.com/codemillmatt/deepseek-dotnet?ref=hubwiz.com)并查看我们的[其他 MEAI 示例](https://github.com/dotnet/ai-samples/tree/main/src/microsoft-extensions-ai?ref=hubwiz.com)！

---

原文链接：[Build Intelligent Apps with .NET and DeepSeek R1 Today!](https://devblogs.microsoft.com/dotnet/start-building-an-intelligent-app-with-dotnet-and-deep-seek/?ref=hubwiz.com)

汇智网翻译整理，转载请标明出处