Microsoft.Extensions.AI是一个创新的 .NET 库,它为平台开发人员提供了一个内聚的 C# 抽象层,简化了与大型语言模型 (LLMs) 和嵌入等 AI 服务的交互。它支持通过一组一致且标准化的 API 和约定将 AI 功能无缝集成到 .NET 应用程序中。
注意:目前Microsoft.Extensions.AI还是处于预览版,预计该库将在2024年11月的.NET 9版本之前都是保持预览状态(需要收集反馈意见),耐心等待微软官方发布正式版!

Microsoft.Extensions.AI类库不仅简化了AI功能的集成,还促进了.NET生态系统的创新。它使得开发者可以更加专注于应用程序的业务逻辑和功能实现,而不必花费大量时间和精力在AI服务的集成和调试上。
项目NuGet包https://www.nuget.org/packages/Microsoft.Extensions.AI命令安装:
dotnet add package Microsoft.Extensions.AI --version 9.0.0-preview.9.24556.5AI服务的常见抽象IChatClient 接口允许使用语言模型,无论是远程托管还是本地运行。任何提供 AI 客户端的 .NET 包都可以实现此接口,从而实现与正在使用的 .NET 代码的无缝集成。
public interface IChatClient : IDisposable { Task<ChatCompletion> CompleteAsync(...); IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteStreamingAsync(...); ChatClientMetadata Metadata { get; } TService? GetService<TService>(object? key = ) where TService :; } OpenAIusing OpenAI;using Microsoft.Extensions.AI;IChatClient client = new OpenAIClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY")) .AsChatClient(modelId: "gpt-4o-mini");var response = await client.CompleteAsync("C#是什么?");Console.WriteLine(response.Message);Azure OpenAIusing Azure.AI.OpenAI;using Azure.Identity;using Microsoft.Extensions.AI;IChatClient client = new AzureOpenAIClient( new Uri(Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")), new DefaultAzureCredential()) .AsChatClient(modelId: "gpt-4o-mini");var response = await client.CompleteAsync("C#是什么?");Console.WriteLine(response.Message);参考文章https://devblogs.microsoft.com/dotnet/introducing-microsoft-extensions-ai-preview项目源码地址更多项目实用功能和特性欢迎前往项目开源地址查看👀,别忘了给项目一个Star支持💖。
开源地址:https://github.com/dotnet/extensions