|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using Azure.Identity; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Microsoft.Extensions.VectorData; |
| 6 | +using Microsoft.SemanticKernel; |
| 7 | +using Microsoft.SemanticKernel.Connectors.AzureOpenAI; |
| 8 | +using Microsoft.SemanticKernel.Connectors.InMemory; |
| 9 | +using Microsoft.SemanticKernel.Embeddings; |
| 10 | + |
| 11 | +namespace Memory; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// A simple example showing how to ingest data into a vector store and then use vector search to find related records to a given string |
| 15 | +/// with enabled telemetry. |
| 16 | +/// </summary> |
| 17 | +public class VectorStore_Telemetry(ITestOutputHelper output) : BaseTest(output) |
| 18 | +{ |
| 19 | + [Fact] |
| 20 | + public async Task LoggingManualRegistrationAsync() |
| 21 | + { |
| 22 | + // Create an embedding generation service. |
| 23 | + var textEmbeddingGenerationService = new AzureOpenAITextEmbeddingGenerationService( |
| 24 | + TestConfiguration.AzureOpenAIEmbeddings.DeploymentName, |
| 25 | + TestConfiguration.AzureOpenAIEmbeddings.Endpoint, |
| 26 | + new AzureCliCredential()); |
| 27 | + |
| 28 | + // Manually construct an InMemory vector store with enabled logging. |
| 29 | + var vectorStore = new InMemoryVectorStore() |
| 30 | + .AsBuilder() |
| 31 | + .UseLogging(this.LoggerFactory) |
| 32 | + .Build(); |
| 33 | + |
| 34 | + await RunExampleAsync(textEmbeddingGenerationService, vectorStore); |
| 35 | + |
| 36 | + // Output: |
| 37 | + // CreateCollectionIfNotExistsAsync invoked. |
| 38 | + // CreateCollectionIfNotExistsAsync completed. |
| 39 | + // UpsertAsync invoked. |
| 40 | + // UpsertAsync completed. |
| 41 | + // UpsertAsync invoked. |
| 42 | + // UpsertAsync completed. |
| 43 | + // UpsertAsync invoked. |
| 44 | + // UpsertAsync completed. |
| 45 | + // VectorizedSearchAsync invoked. |
| 46 | + // VectorizedSearchAsync completed. |
| 47 | + |
| 48 | + // Search string: What is an Application Programming Interface |
| 49 | + // Result: Application Programming Interface. A set of rules and specifications that allow software components to communicate and exchange data. |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public async Task LoggingDependencyInjectionAsync() |
| 54 | + { |
| 55 | + var serviceCollection = new ServiceCollection(); |
| 56 | + |
| 57 | + // Add an embedding generation service. |
| 58 | + serviceCollection.AddAzureOpenAITextEmbeddingGeneration( |
| 59 | + TestConfiguration.AzureOpenAIEmbeddings.DeploymentName, |
| 60 | + TestConfiguration.AzureOpenAIEmbeddings.Endpoint, |
| 61 | + new AzureCliCredential()); |
| 62 | + |
| 63 | + // Add InMemory vector store |
| 64 | + serviceCollection.AddInMemoryVectorStore(); |
| 65 | + |
| 66 | + // Register InMemoryVectorStore with enabled logging. |
| 67 | + serviceCollection |
| 68 | + .AddVectorStore(s => s.GetRequiredService<InMemoryVectorStore>()) |
| 69 | + .UseLogging(this.LoggerFactory); |
| 70 | + |
| 71 | + var services = serviceCollection.BuildServiceProvider(); |
| 72 | + |
| 73 | + var vectorStore = services.GetRequiredService<IVectorStore>(); |
| 74 | + var textEmbeddingGenerationService = services.GetRequiredService<ITextEmbeddingGenerationService>(); |
| 75 | + |
| 76 | + await RunExampleAsync(textEmbeddingGenerationService, vectorStore); |
| 77 | + |
| 78 | + // Output: |
| 79 | + // CreateCollectionIfNotExistsAsync invoked. |
| 80 | + // CreateCollectionIfNotExistsAsync completed. |
| 81 | + // UpsertAsync invoked. |
| 82 | + // UpsertAsync completed. |
| 83 | + // UpsertAsync invoked. |
| 84 | + // UpsertAsync completed. |
| 85 | + // UpsertAsync invoked. |
| 86 | + // UpsertAsync completed. |
| 87 | + // VectorizedSearchAsync invoked. |
| 88 | + // VectorizedSearchAsync completed. |
| 89 | + |
| 90 | + // Search string: What is an Application Programming Interface |
| 91 | + // Result: Application Programming Interface. A set of rules and specifications that allow software components to communicate and exchange data. |
| 92 | + } |
| 93 | + |
| 94 | + private async Task RunExampleAsync( |
| 95 | + ITextEmbeddingGenerationService textEmbeddingGenerationService, |
| 96 | + IVectorStore vectorStore) |
| 97 | + { |
| 98 | + // Get and create collection if it doesn't exist. |
| 99 | + var collection = vectorStore.GetCollection<ulong, Glossary>("skglossary"); |
| 100 | + await collection.CreateCollectionIfNotExistsAsync(); |
| 101 | + |
| 102 | + // Create glossary entries and generate embeddings for them. |
| 103 | + var glossaryEntries = CreateGlossaryEntries().ToList(); |
| 104 | + var tasks = glossaryEntries.Select(entry => Task.Run(async () => |
| 105 | + { |
| 106 | + entry.DefinitionEmbedding = await textEmbeddingGenerationService.GenerateEmbeddingAsync(entry.Definition); |
| 107 | + })); |
| 108 | + await Task.WhenAll(tasks); |
| 109 | + |
| 110 | + // Upsert the glossary entries into the collection and return their keys. |
| 111 | + var upsertedKeysTasks = glossaryEntries.Select(x => collection.UpsertAsync(x)); |
| 112 | + var upsertedKeys = await Task.WhenAll(upsertedKeysTasks); |
| 113 | + |
| 114 | + // Search the collection using a vector search. |
| 115 | + var searchString = "What is an Application Programming Interface"; |
| 116 | + var searchVector = await textEmbeddingGenerationService.GenerateEmbeddingAsync(searchString); |
| 117 | + var searchResult = await collection.VectorizedSearchAsync(searchVector, new() { Top = 1 }); |
| 118 | + var resultRecords = await searchResult.Results.ToListAsync(); |
| 119 | + |
| 120 | + Console.WriteLine("Search string: " + searchString); |
| 121 | + Console.WriteLine("Result: " + resultRecords.First().Record.Definition); |
| 122 | + Console.WriteLine(); |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Sample model class that represents a glossary entry. |
| 127 | + /// </summary> |
| 128 | + /// <remarks> |
| 129 | + /// Note that each property is decorated with an attribute that specifies how the property should be treated by the vector store. |
| 130 | + /// This allows us to create a collection in the vector store and upsert and retrieve instances of this class without any further configuration. |
| 131 | + /// </remarks> |
| 132 | + private sealed class Glossary |
| 133 | + { |
| 134 | + [VectorStoreRecordKey] |
| 135 | + public ulong Key { get; set; } |
| 136 | + |
| 137 | + [VectorStoreRecordData(IsFilterable = true)] |
| 138 | + public string Category { get; set; } |
| 139 | + |
| 140 | + [VectorStoreRecordData] |
| 141 | + public string Term { get; set; } |
| 142 | + |
| 143 | + [VectorStoreRecordData] |
| 144 | + public string Definition { get; set; } |
| 145 | + |
| 146 | + [VectorStoreRecordVector(1536)] |
| 147 | + public ReadOnlyMemory<float> DefinitionEmbedding { get; set; } |
| 148 | + } |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// Create some sample glossary entries. |
| 152 | + /// </summary> |
| 153 | + /// <returns>A list of sample glossary entries.</returns> |
| 154 | + private static IEnumerable<Glossary> CreateGlossaryEntries() |
| 155 | + { |
| 156 | + yield return new Glossary |
| 157 | + { |
| 158 | + Key = 1, |
| 159 | + Category = "External Definitions", |
| 160 | + Term = "API", |
| 161 | + Definition = "Application Programming Interface. A set of rules and specifications that allow software components to communicate and exchange data." |
| 162 | + }; |
| 163 | + |
| 164 | + yield return new Glossary |
| 165 | + { |
| 166 | + Key = 2, |
| 167 | + Category = "Core Definitions", |
| 168 | + Term = "Connectors", |
| 169 | + Definition = "Connectors allow you to integrate with various services provide AI capabilities, including LLM, AudioToText, TextToAudio, Embedding generation, etc." |
| 170 | + }; |
| 171 | + |
| 172 | + yield return new Glossary |
| 173 | + { |
| 174 | + Key = 3, |
| 175 | + Category = "External Definitions", |
| 176 | + Term = "RAG", |
| 177 | + Definition = "Retrieval Augmented Generation - a term that refers to the process of retrieving additional data to provide as context to an LLM to use when generating a response (completion) to a user’s question (prompt)." |
| 178 | + }; |
| 179 | + } |
| 180 | +} |
0 commit comments