使用Microsoft.Extensions.AI和私有部署的DeepSeek R1构建智能程序

B站影视 2025-02-07 09:00 2

摘要:上一篇文章本地私有化部署DeepSeek R1介绍了如何在本地部署一个完全私有的DeepSeek模型,并使用OpenWebUI来访问。私有化部署可以部分代替在线DeepSeek功能,特别是当前DeepSeek访问量非常大,总是出现“服务器繁忙,请稍后再试。“错

上一篇文章本地私有化部署DeepSeek R1介绍了如何在本地部署一个完全私有的DeepSeek模型,并使用OpenWebUI来访问。私有化部署可以部分代替在线DeepSeek功能,特别是当前DeepSeek访问量非常大,总是出现“服务器繁忙,请稍后再试。“错误。

作为程序员我们当然想要进一步将其AI功能集成到我们的程序中,以方便用户的使用。这里使用微软的C#语言基于Microsoft.Extensions.AI库来访问本地Ollama上部署的DeepSeek R1 1.5b大语言模型。

01

什么是Microsoft.Extensions.AI

Microsoft.Extensions.AI 是一组核心 .NET 库,是在与整个 .NET 生态系统(包括语义内核)的开发人员协作中创建的。 这些库提供统一的 C# 抽象层,用于与 AI 服务交互,例如小型和大型语言模型(SLA 和 LLM)、嵌入和中间件。

详见微软官方文档:https://learn.microsoft.com/zh-cn/dotnet/ai/ai-extensions

02

引用Microsoft.Extensions.AI.Ollama

当前有很多不同的模型服务提供者,比如OpenAI, AzureAI, 还有本地部署的Ollama等。每个提供者可能有自己不同的API规范,给开发者造成了很大的困挠。微软开发了Microsoft.Extensions.AI库帮我们屏蔽了这些差异,我们只需要关心如何使用了统一的接口,将来如果需要更换使用不同的服务,不需要修改我们自己的业务代码。

因为我们本地部署使用的是Ollama,我们这里使用Microsoft.Extensions.AI.Ollama.

请确保本地Ollama服务已经启动,并且DeepSeek 1.5b模型已经下载,详见:本地私有化部署DeepSeek R1。

在VS Code或Visual Studio里面新建一个.net core 控制台程序,并引用Microsoft.Extensions.AI.Ollama。

02 —

与DeepSeek R1交互

我们这里编一个简单的智能问答程序,用下面的代码代替program.cs中的内容:

using Microsoft.Extensions.AI;Uri uri = new Uri("http://localhost:11434");string modelName = "deepseek-r1:1.5b";IChatClient chatClient = new OllamaChatClient(uri, modelName);Console.WriteLine("Welcome to DeepSeek Chatbot");Console.WriteLine("Type 'exit' to exit the chatbot");while (true){ Console.WriteLine("\n Console.Write("You:"); var question = Console.ReadLine; if (string.IsOrEmpty(question)) { continue; } if (question.StartsWith("exit", StringComparison.OrdinalIgnoreCase)) { break; } var response = chatClient.CompleteStreamingAsync(question); Console.Write("DeepSeek's answer:"); await foreach (var message in response) { Console.Write(message); } Console.WriteLine;}Console.ReadKey;

代码也比较简单易懂。我们的主要业务功能是与抽象的接口IChatClient打交道,这里使用的是OllamaChatClient。如果需要使用OpenAI的服务,只需要引用NuGet包Microsoft.Extensions.AI.OpenAI,并用下面的代码来获得一个IChatCLient接口实例(详见:https://www.nuget.org/packages/Microsoft.Extensions.AI.OpenAI/)

IChatClient client = new OpenAIClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY")) .AsChatClient("gpt-4o-mini");

下面是程序运行的效果

Welcome to DeepSeek ChatbotType 'exit' to exit the chatbotYou:write a joke about programmerDeepSeek's answer:Okay, so I need to come up with a joke about programmers. Hmm, let me think about what makes programmers funny or something that people might find amusing.First, programming languages. Like Python, Java, JavaScript... They're really popular and used everywhere now. Maybe joke about the coolness of coding. Or maybe about how they process information.Another angle is the idea of code breaking or hacking. People often joke about how hackers get into systems. Maybe a joke that plays on that theme.What else? How about the emotional aspect of programming. Like how some people are more technical than others, or how it can be dangerous if not done right. But those might be too sensitive or offensive.I remember once someone asked me about my favorite programming language, and I had to explain why I like it for reasons related to its features, not just the syntax. Maybe that could work as a basis for a joke.Let's try something else. How about the idea of code as a tool for creativity? Like how code can be used in art or music. Or maybe joke about how programmers use their skills for social media campaigns or something.Another thought: what about the idea of code being an escape, like how hackers do it to stay alive. That could lead to a pun about escape or escapee.Wait, I have one more angle. Maybe joke about how some people become so technical they don't need human interaction anymore. Like how they're just machines now or something.Alright, let me try putting that together. So, the punchline would be something like "Programmers are machines who never asked for a rest." That plays on both the idea of them being mechanical and also the humorous double entendre when it comes to the word "rest."Yeah, that makes sense. It touches on two layers: as tools (machines) and as people with human qualities.**Joke:**"Programmers are machines who never asked for a rest."The punchline cleverly plays on the double entendre of the word "programmer," suggesting both their role as mechanical processes and their unyielding, human-like dedication to tasks.You:

本文简单介绍了如何使用Microsoft.Extensions.AI来与本地部署的DeepSeek R1服务交互。也很容易扩展到使用付费的OpenAI或DeepSeek 在线API服务。

来源:opendotnet

相关推荐