|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using Microsoft.Extensions.VectorData; |
| 4 | +using Microsoft.SemanticKernel.Connectors.InMemory; |
| 5 | +using Microsoft.SemanticKernel.Embeddings; |
| 6 | + |
| 7 | +namespace GettingStartedWithVectorStores; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Example showing how to generate embeddings and ingest data into an in-memory vector store. |
| 11 | +/// </summary> |
| 12 | +public class Step1_Ingest_Data(ITestOutputHelper output, VectorStoresFixture fixture) : BaseTest(output), IClassFixture<VectorStoresFixture> |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Example showing how to ingest data into an in-memory vector store. |
| 16 | + /// </summary> |
| 17 | + [Fact] |
| 18 | + public async Task IngestDataIntoInMemoryVectorStoreAsync() |
| 19 | + { |
| 20 | + // Construct the vector store and get the collection. |
| 21 | + var vectorStore = new InMemoryVectorStore(); |
| 22 | + var collection = vectorStore.GetCollection<string, Glossary>("skglossary"); |
| 23 | + |
| 24 | + // Ingest data into the collection. |
| 25 | + await IngestDataIntoVectorStoreAsync(collection, fixture.TextEmbeddingGenerationService); |
| 26 | + |
| 27 | + // Retrieve an item from the collection and write it to the console. |
| 28 | + var record = await collection.GetAsync("4"); |
| 29 | + Console.WriteLine(record!.Definition); |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Ingest data into the given collection. |
| 34 | + /// </summary> |
| 35 | + /// <param name="collection">The collection to ingest data into.</param> |
| 36 | + /// <param name="textEmbeddingGenerationService">The service to use for generating embeddings.</param> |
| 37 | + /// <returns>The keys of the upserted records.</returns> |
| 38 | + internal static async Task<IEnumerable<string>> IngestDataIntoVectorStoreAsync( |
| 39 | + IVectorStoreRecordCollection<string, Glossary> collection, |
| 40 | + ITextEmbeddingGenerationService textEmbeddingGenerationService) |
| 41 | + { |
| 42 | + // Create the collection if it doesn't exist. |
| 43 | + await collection.CreateCollectionIfNotExistsAsync(); |
| 44 | + |
| 45 | + // Create glossary entries and generate embeddings for them. |
| 46 | + var glossaryEntries = CreateGlossaryEntries().ToList(); |
| 47 | + var tasks = glossaryEntries.Select(entry => Task.Run(async () => |
| 48 | + { |
| 49 | + entry.DefinitionEmbedding = await textEmbeddingGenerationService.GenerateEmbeddingAsync(entry.Definition); |
| 50 | + })); |
| 51 | + await Task.WhenAll(tasks); |
| 52 | + |
| 53 | + // Upsert the glossary entries into the collection and return their keys. |
| 54 | + var upsertedKeysTasks = glossaryEntries.Select(x => collection.UpsertAsync(x)); |
| 55 | + return await Task.WhenAll(upsertedKeysTasks); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Create some sample glossary entries. |
| 60 | + /// </summary> |
| 61 | + /// <returns>A list of sample glossary entries.</returns> |
| 62 | + private static IEnumerable<Glossary> CreateGlossaryEntries() |
| 63 | + { |
| 64 | + yield return new Glossary |
| 65 | + { |
| 66 | + Key = "1", |
| 67 | + Category = "Software", |
| 68 | + Term = "API", |
| 69 | + Definition = "Application Programming Interface. A set of rules and specifications that allow software components to communicate and exchange data." |
| 70 | + }; |
| 71 | + |
| 72 | + yield return new Glossary |
| 73 | + { |
| 74 | + Key = "2", |
| 75 | + Category = "Software", |
| 76 | + Term = "SDK", |
| 77 | + Definition = "Software development kit. A set of libraries and tools that allow software developers to build software more easily." |
| 78 | + }; |
| 79 | + |
| 80 | + yield return new Glossary |
| 81 | + { |
| 82 | + Key = "3", |
| 83 | + Category = "SK", |
| 84 | + Term = "Connectors", |
| 85 | + Definition = "Semantic Kernel Connectors allow software developers to integrate with various services providing AI capabilities, including LLM, AudioToText, TextToAudio, Embedding generation, etc." |
| 86 | + }; |
| 87 | + |
| 88 | + yield return new Glossary |
| 89 | + { |
| 90 | + Key = "4", |
| 91 | + Category = "SK", |
| 92 | + Term = "Semantic Kernel", |
| 93 | + Definition = "Semantic Kernel is a set of libraries that allow software developers to more easily develop applications that make use of AI experiences." |
| 94 | + }; |
| 95 | + |
| 96 | + yield return new Glossary |
| 97 | + { |
| 98 | + Key = "5", |
| 99 | + Category = "AI", |
| 100 | + Term = "RAG", |
| 101 | + 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)." |
| 102 | + }; |
| 103 | + |
| 104 | + yield return new Glossary |
| 105 | + { |
| 106 | + Key = "6", |
| 107 | + Category = "AI", |
| 108 | + Term = "LLM", |
| 109 | + Definition = "Large language model. A type of artificial ingelligence algorithm that is designed to understand and generate human language." |
| 110 | + }; |
| 111 | + } |
| 112 | +} |
0 commit comments