专栏名称: dotNET跨平台
专注于.NET Core的技术传播。在这里你可以谈微软.NET,Mono的跨平台开发技术。在这里可以让你的.NET项目有新的思路,不局限于微软的技术栈,横跨Windows,Linux 主流平台
目录
相关文章推荐
51好读  ›  专栏  ›  dotNET跨平台

探索 Microsoft.Extensions.VectorData 与 Qdrant、Azure AI 搜索结合使用

dotNET跨平台  · 公众号  ·  · 2025-01-07 08:00

正文

M

点击蓝字 / 微软开发者MSDN

关注我们

作者: Bruno Capuano

排版:Alan Wang


了解如何使用 Microsoft.Extensions.VectorData,通过 Qdrant 和 Azure AI 搜索实现语义搜索。

使用 Microsoft.Extensions.VectorData 深入探索语义搜索:Qdrant 和 Azure AI 搜索

语义搜索通过关注含义而不是单纯的关键字匹配来改变应用程序查找和解释数据的方式。随着 Microsoft.Extensions.VectorData 的发布,.NET 开发人员拥有了一套新的构建模块,可以将基于向量的搜索功能集成到他们的应用程序中。在本文中,我们将探讨在本地使用 Qdrant 和 Azure AI 搜索达成语义搜索的两种实际实现。

Microsoft.Extensions.VectorData 简介

Microsoft.Extensions.VectorData 是一套 .NET 代码库,旨在管理 .NET 应用程序中的基于向量的数据。这些库提供统一的 C# 抽象层,以用于同向量存储进行交互,使开发人员能够处理嵌入并高效地执行向量相似度查询。


要详细了解该库的架构和功能,我推荐阅读 Luis 的精彩博客文章


在这篇博客文章中,我们将展示两个实际用例:

  1. 本地使用 Qdrant 进行语义搜索。

  2. 利用 Azure AI 搜索实现企业级向量搜索。


要运行这些演示,您需要使用 Ollama 提供的模型之一来生成嵌入。在本示例中,使用的模型是 all-minilm。

  • 安装 Ollama

    https://ollama.com/download

  • 下载 all-minilm 模型

    https://ollama.com/library/all-minilm

  • 准备一个符合 OCI 标准的容器运行时,例如:

    • Docker Desktop 或 Podman。


Luis 的精彩博客文章

https://devblogs.microsoft.com/dotnet/introducing-microsoft-extensions-vector-data/

使用 Qdrant 实现语义搜索

Qdrant 是什么?

Qdrant 是一个向量相似性搜索引擎,它提供可用于生产的服务,配有便捷的 API,用于存储、搜索和管理点(即向量)及其附加负载。它非常适合需要高效相似性搜索的应用程序。您可以轻松地在 Docker 容器中本地运行 Qdrant,这使其成为开发人员友好的选择。


有关设置说明,请参考 Qdrant 快速入门指南 。以下是运行本地容器实例的示例命令:

docker run -p 6333:6333 -p 6334:6334 -v $(pwd)/qdrant_storage:/qdrant/storage:z qdrant/qdrant

容器创建后,您可以在 Docker 中查看它。

Qdrant 快速入门指南

https://qdrant.tech/documentation/quickstart/

Qdrant 与 Semantic Kernel

Semantic Kernel 为 Qdrant 提供了一个内置连接器,使 .NET 开发人员能够无缝存储嵌入并执行基于向量的查询。该连接器基于 Microsoft.Extensions.VectorData 和官方的 .NET Qdrant 客户端构建。


这种集成结合了 Qdrant 的高性能和 Semantic Kernel 的易用性。


要了解有关连接器的更多信息,请访问 Semantic Kernel 向量存储 Qdrant 连接器的官方文档


Semantic Kernel 向量存储 Qdrant 连接器的官方文档

https://learn.microsoft.com/semantic-kernel/concepts/vector-store-connectors/out-of-the-box-connectors/qdrant-connector?pivots=programming-language-csharp

场景概述 – Qdrant

  • 设置 :Qdrant 实例在 Docker 容器中本地运行。

  • 功能 :.NET 控制台应用程序使用 Semantic Kernel 的 Qdrant 连接器以实现:

    • 存储电影嵌入。

    • 执行语义搜索查询。


接下来,我们来看一个实现并运行此演示的示例类。

using




    
 Microsoft.Extensions.AI;using Microsoft.Extensions.VectorData;using Microsoft.SemanticKernel.Connectors.Qdrant;using Qdrant.Client;
var vectorStore = new QdrantVectorStore(new QdrantClient("localhost"));
// get movie listvar movies = vectorStore.GetCollection<ulong, MovieVector<ulong>>("movies");await movies.CreateCollectionIfNotExistsAsync();var movieData = MovieFactory<ulong>.GetMovieVectorList();
// get embeddings generator and generate embeddings for moviesIEmbeddingGenerator<string, Embedding<float>> generator =    new OllamaEmbeddingGenerator(new Uri("http://localhost:11434/"), "all-minilm");foreach (var movie in movieData){    movie.Vector = await generator.GenerateEmbeddingVectorAsync(movie.Description);    await movies.UpsertAsync(movie);}
// perform the searchvar query = "A family friendly movie that includes ogres and dragons";var queryEmbedding = await generator.GenerateEmbeddingVectorAsync(query);
var searchOptions = new VectorSearchOptions(){    Top = 2,    VectorPropertyName = "Vector"};
var results = await movies.VectorizedSearchAsync(queryEmbedding, searchOptions);await foreach (var result in results.Results){    Console.WriteLine($"Title: {result.Record.Title}");    Console.WriteLine($"Description: {result.Record.Description}");    Console.WriteLine($"Score: {result.Score}");    Console.WriteLine();}

演示运行后,示例输出如下:

Title: ShrekDescription: Shrek is an animated film that tells the story of an ogre named Shrek who embarks on a quest to rescue Princess Fiona from a dragon and bring her back to the kingdom of Duloc.Score: 0.5013245344161987
Title: Lion KingDescription: The Lion King is a classic Disney animated film that tells the story of a young lion named Simba who embarks on a journey to reclaim his throne as the king of the Pride Lands after the tragic death of his father.Score: 0.3225690722465515

为什么选择 Qdrant?

使用 Qdrant 进行语义搜索具有可扩展、高速相似性搜索的优势,使其成为需要大规模向量数据管理的应用程序的绝佳选择。此外,您还可以选择 在 Microsoft Azure 上运行 Qdrant Cloud


在 Microsoft Azure 上运行 Qdrant Cloud

https://qdrant.tech/blog/qdrant-cloud-on-microsoft-azure/

语义搜索与 Azure AI 搜索

Azure AI 搜索是什么?

Azure AI 搜索是微软的搜索即服务产品。它将传统的搜索功能与 AI 驱动的功能(如语义搜索和向量搜索)结合在一起。Azure AI 搜索具有可扩展性和可靠性,非常适合需要高级搜索功能的企业应用程序。您可以了解 更多关于 Azure AI 搜索的信息


在本示例中,我们将使用 Azure AI 搜索中集成的向量化功能 ,通过将文档和查询转换为向量来改善索引和查询性能。


更多关于 Azure AI 搜索的信息

https://learn.microsoft.com/azure/search/search-what-is-azure-search

Azure AI 搜索中集成的向量化功能

https://learn.microsoft.com/azure/search/vector-search-integrated-vectorization

Azure AI 搜索与 Semantic Kernel

此连接器基于 Microsoft.Extensions.VectorData 和适用于 .NET 的官方 Azure AI Search 库 构建。


有关更多信息,请参阅 Azure AI 搜索连接器文档


.NET 的官方 Azure AI Search 库

https://learn.microsoft.com/dotnet/api/overview/azure/search?view=azure-dotnet

Azure AI 搜索连接器文档

https://learn.microsoft.com/semantic-kernel/concepts/vector-store-connectors/out-of-the-box-connectors/azure-ai-search-connector?pivots=programming-language-csharp

场景概述 – Azure AI 搜索

  • 设置 :在您的 Azure 订阅中创建 Azure AI 搜索服务。

  • 功能 :控制台应用程序:

    • 存储电影的向量嵌入。

    • 执行基于向量的语义搜索查询。

  • 需求 :必须将 Azure AI 搜索端点作为用户机密添加到应用程序中。仅使用端点,应用程序将创建 Azure 默认凭据以连接到服务。如果您想使用机密访问 Azure AI 搜索,您还需要将该值添加为用户机密。


以下是如何添加用户机密的控制台命令示例:

dotnet user-secrets init dotnet user-secrets set "AZURE_AISEARCH_URI" "https://.search.windows.net" dotnet user-secrets set "AZURE_AISEARCH_SECRET" "AI Search Secret"

我们来看一个实现并运行该演示的示例类。

using Microsoft.Extensions.AI;using Microsoft.Extensions.VectorData;using Azure;using Azure.Search.Documents.Indexes;using Microsoft.SemanticKernel.Connectors.AzureAISearch;using Microsoft.Extensions.Configuration;using Azure.Identity;using Azure.Core;
// get the search index client using Azure Default Credentials or Azure Key Credential with the service secretvar client = GetSearchIndexClient();var vectorStore = new AzureAISearchVectorStore(searchIndexClient: client);
// get movie listvar movies = vectorStore.GetCollection<string, MovieVector<string>>("movies");await movies.CreateCollectionIfNotExistsAsync();var movieData = MovieFactory<string>.GetMovieVectorList();
// get embeddings generator and generate embeddings for moviesIEmbeddingGenerator<string, Embedding<float>> generator =    new OllamaEmbeddingGenerator(new Uri("http://localhost:11434/"), "all-minilm");foreach (var movie in movieData){    movie.Vector = await generator.GenerateEmbeddingVectorAsync(movie.Description);    await movies.UpsertAsync(movie);}
// perform the searchvar query = "A family friendly movie that includes ogres and dragons";var queryEmbedding = await generator.GenerateEmbeddingVectorAsync(query);
// show the results...

演示运行后,示例输出如下:

Title: ShrekDescription: Shrek is an animated film that tells the story of an ogre named Shrek who embarks on a quest to rescue Princess Fiona from a dragon and bring her back to the kingdom of Duloc.Score: 0.6672559

我们可以在 Azure AI 搜索服务中的 Azure 门户中看到包含电影字段的新索引。

为什么选择 Azure AI 搜索?

Azure AI 搜索 提供企业级的可扩展性和集成,使其成为生产环境中需要高级语义搜索的应用程序的强大解决方案。此外,AI 搜索具有内置的安全功能,如加密和安全认证,以保护您的数据。它还符合合规性标准,确保您的搜索解决方案满足监管要求。


Azure AI 搜索

https://azure.microsoft.com/products/ai-services/ai-search/

解释代码

用于演示的控制台应用程序

每个语义搜索演示都以 .NET 9 控制台应用程序的形式实现。示例的代码库可以追溯到 Luis 提供的原始演示 ,并针对 Azure AI Search 和 Qdrant 场景进行了拓展。


Luis 提供的原始演示

https://devblogs.microsoft.com/dotnet/introducing-microsoft-extensions-vector-data/

用于数据表示的共享类

共享类表示电影实体,其中包括:

  • 向量嵌入的字段 :这些嵌入用于执行语义搜索。







请到「今天看啥」查看全文