基于Microsoft.Extensions.AI 和 Microsoft.Extensions.VectorData构建向量搜索 新建ClassCloudServiceWikiusing Microsoft.Extensions.VectorData; namespace VectorDataAIDemo; internal class CloudServiceWiki { [VectorStoreKey] public int Key { get; set; } [VectorStoreData] public string Name { get; set; } [VectorStoreData] public string Description { get; set; } [VectorStoreVector( Dimensions: 384, DistanceFunction DistanceFunction.CosineSimilarity)] public ReadOnlyMemoryfloat Vector { get; set; } }Microsoft.Extensions.VectorData 属性例如 VectorStoreKeyAttribute会影响在向量存储中使用时每个属性的处理方式。该Vector属性存储生成的嵌入表示Description值在矢量搜索中的语义含义。在Program.cs文件中添加以下代码以创建描述云服务知识库集合的数据集ListCloudServiceWiki cloudServices [ new() { Key 0, Name Azure App Service, Description Host .NET, Java, Node.js, and Python web applications and APIs in a fully managed Azure service. You only need to deploy your code to Azure. Azure takes care of all the infrastructure management like high availability, load balancing, and autoscaling. }, new() { Key 1, Name Azure Service Bus, Description A fully managed enterprise message broker supporting both point to point and publish-subscribe integrations. Its ideal for building decoupled applications, queue-based load leveling, or facilitating communication between microservices. }, new() { Key 2, Name Azure Blob Storage, Description Azure Blob Storage allows your applications to store and retrieve files in the cloud. Azure Storage is highly scalable to store massive amounts of data and data is stored redundantly to ensure high availability. }, new() { Key 3, Name Microsoft Entra ID, Description Manage user identities and control access to your apps, data, and resources. }, new() { Key 4, Name Azure Key Vault, Description Store and access application secrets like connection strings and API keys in an encrypted vault with restricted access to make sure your secrets and your application arent compromised. }, new() { Key 5, Name Azure AI Search, Description Information retrieval at scale for traditional and conversational search applications, with security and options for AI enrichment and vectorization. } ];创建和配置IEmbeddingGenerator实现以将请求发送到嵌入 AI 模型// Load the configuration values. IConfigurationRoot config new ConfigurationBuilder().AddUserSecretsProgram().Build(); string model config[ModelName]; string key config[OpenAIKey]; // Create the embedding generator. IEmbeddingGeneratorstring, Embeddingfloat generator new OpenAIClient(new ApiKeyCredential(key)) .GetEmbeddingClient(model: model) .AsIEmbeddingGenerator();使用云服务知识库数据创建和填充向量存储。 使用IEmbeddingGenerator实现为云服务知识库数据中的每个记录创建和分配嵌入向量// Create and populate the vector store. var vectorStore new InMemoryVectorStore(); VectorStoreCollectionint, CloudServiceWiki cloudServicesStore vectorStore.GetCollectionint, CloudServiceWiki(cloudServices); await cloudServicesStore.EnsureCollectionExistsAsync(); foreach (CloudServiceWiki service in cloudServices) { service.Vector await generator.GenerateVectorAsync(service.Description); await cloudServicesStore.UpsertAsync(service); }嵌入是每个数据记录语义含义的数字表示形式这使得它们与向量搜索功能兼容。搜索查询// Convert a search query to a vector // and search the vector store. string query Which Azure service should I use to store my Word documents?; ReadOnlyMemoryfloat queryEmbedding await generator.GenerateVectorAsync(query); IAsyncEnumerableVectorSearchResultCloudServiceWiki results cloudServicesStore.SearchAsync(queryEmbedding, top: 1); await foreach (VectorSearchResultCloudServiceWiki result in results) { Console.WriteLine($Name: {result.Record.Name}); Console.WriteLine($Description: {result.Record.Description}); Console.WriteLine($Vector match score: {result.Score}); }