几个月前,我们推出了AI与向量数据扩展的首个预览版——这些强大的.NET库旨在简化AI模型和向量存储的集成[1]。经过与合作伙伴及社区的深入协作,我们完成了API稳定化改进并整合了宝贵建议。今天,我们正式宣布这些扩展全面上市(GA),为开发者构建可扩展、可维护且具备互操作性的AI应用打下坚实基础[1]。
扩展层级架构图
AI与向量数据扩展是一组提供统一抽象接口的.NET库,涵盖AI模型和向量存储的操作。其包括三个核心NuGet包[1]:
这些库为高层组件提供基础支持,实现了:
跨模型与向量存储的无缝切换 无论是开发环境使用本地模型,还是生产环境部署云服务,扩展提供统一的API接口[1]:
IChatClient chatClient =
environment == "Development"
? new OllamaApiClient("YOUR-OLLAMA-ENDPOINT", "qwen3")
: new AzureOpenAIClient("YOUR-AZURE-OPENAI-ENDPOINT", new DefaultAzureCredential())
.GetChatClient("gpt-4.1")
.AsIChatClient();
IEmbeddingGenerator<string, Embedding<float>> embeddingGenerator =
environment == "Development"
? new OllamaApiClient("YOUR-OLLAMA-ENDPOINT", "all-minilm")
: new AzureOpenAIClient("YOUR-AZURE-OPENAI-ENDPOINT", new DefaultAzureCredential())
.GetEmbeddingClient("text-embedding-3-small")
.AsIEmbeddingGenerator();
渐进式功能增强 通过.NET原生基础设施(如日志记录、分布式缓存、OpenTelemetry)快速构建生产级应用[1]:
IChatClient chatClient =
new ChatClientBuilder(...)
.UseLogging()
.UseDistributedCache()
.UseOpenTelemetry()
.Build();
支持自定义限流策略等扩展能力:
IChatClient client =
new ChatClientBuilder(...)
.UseDistributedCache()
.Use(async (messages, options, nextAsync, cancellationToken) =>
{
using var lease = await rateLimiter.AcquireAsync(permitCount: , cancellationToken);
if (!lease.IsAcquired)
throw new InvalidOperationException("Unable to acquire lease.");
await nextAsync(messages, options, cancellationToken);
})
.Build();
结构化输出与工具调用 支持多模态输入与JSON格式结构化输出,直接映射至C#类型[1]:
record Receipt(string Merchant, List<Item> Items, float Total, Category Category);
var response = await chatClient.GetResponseAsync<Receipt>(message);
response.TryGetResult(out var receiptData);
工具调用示例通过特性标注实现函数自动调度:
[Description("根据收据和税率计算税额")]
float CalculateTax(Receipt receipt, float taxRate) => receipt.Total * ( + taxRate);
var response = await functionChatClient.GetResponseAsync<ReceiptTotal>(
message,
new ChatOptions { Tools = [AIFunctionFactory.Create(CalculateTax)] });
简化的嵌入生成与向量存储 与向量数据库(如SQLite、Qdrant)深度集成,自动处理嵌入生成与存储[1]:
record Product
{
[VectorStoreKey] publicint Id { get; set; }
[VectorStoreData] publicrequiredstring Name { get; set; }
[VectorStoreVector(Dimensions: 1536)] publicstring? Embedding { get; set; }
}
await collection.UpsertAsync(new Product
{
Id = ,
Name = "电热水壶",
Embedding = "快速加热大容量电热水壶,适合泡茶"
});
扩展已获得300万次下载,超过100个公开NuGet包依赖,集成范围涵盖[1]:
通过.NET AI模板[4]快速上手,查阅官方文档[5]获取详细指南。
期待见证您的创新成果!
作者:Luis Quintanilla[6],.NET机器学习项目经理
[1]
Microsoft.Extensions.AI.Abstractions:https://www.nuget.org/packages/Microsoft.Extensions.AI.Abstractions
[2]
Microsoft.Extensions.AI:https://www.nuget.org/packages/Microsoft.Extensions.AI
[3]
Microsoft.Extensions.VectorData.Abstractions:https://www.nuget.org/packages/Microsoft.Extensions.VectorData.Abstractions/
[4]
.NET AI模板:https://learn.microsoft.com/dotnet/ai/quickstarts/ai-templates?tabs=visual-studio%2Cconfigure-visual-studio&pivots=github-models
[5]
官方文档:https://learn.microsoft.com/dotnet/ai/
[6]
Luis Quintanilla:https://devblogs.microsoft.com/dotnet/author/lquintanilla/