#r "nuget: Microsoft.SemanticKernel, 1.24.1" #r "nuget: Microsoft.SemanticKernel.Connectors.InMemory, 1.24.1-preview" #r "nuget: Microsoft.Extensions.VectorData.Abstractions, 9.0.0-preview.1.24518.1" #r "nuget: System.Linq.Async, 6.0.1" #!import config/Settings.cs using Microsoft.SemanticKernel; using Kernel = Microsoft.SemanticKernel.Kernel; #pragma warning disable SKEXP0010 var builder = Kernel.CreateBuilder(); // Configure AI service credentials used by the kernel var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = Settings.LoadFromFile(); if (useAzureOpenAI) { builder.AddAzureOpenAITextEmbeddingGeneration("text-embedding-ada-002", azureEndpoint, apiKey); } else { builder.AddOpenAITextEmbeddingGeneration("text-embedding-ada-002", apiKey, orgId); } var kernel = builder.Build(); using Microsoft.Extensions.VectorData; public sealed class Glossary { [VectorStoreRecordKey] public ulong Key { get; set; } [VectorStoreRecordData] public string Term { get; set; } [VectorStoreRecordData] public string Definition { get; set; } [VectorStoreRecordVector(Dimensions: 1536)] public ReadOnlyMemory DefinitionEmbedding { get; set; } } public sealed class GlossaryWithoutAttributes { public ulong Key { get; set; } public string Term { get; set; } public string Definition { get; set; } public ReadOnlyMemory DefinitionEmbedding { get; set; } } var recordDefinition = new VectorStoreRecordDefinition() { Properties = new List() { new VectorStoreRecordKeyProperty("Key", typeof(ulong)), new VectorStoreRecordDataProperty("Term", typeof(string)), new VectorStoreRecordDataProperty("Definition", typeof(string)), new VectorStoreRecordVectorProperty("DefinitionEmbedding", typeof(ReadOnlyMemory)) { Dimensions = 1536 } } }; using Microsoft.SemanticKernel.Connectors.InMemory; #pragma warning disable SKEXP0020 // Define vector store var vectorStore = new InMemoryVectorStore(); // Get a collection instance using vector store var collection = vectorStore.GetCollection("skglossary"); // Get a collection instance by initializing it directly var collection2 = new InMemoryVectorStoreRecordCollection("skglossary"); await collection.CreateCollectionIfNotExistsAsync(); var glossaryEntries = new List() { new Glossary() { Key = 1, Term = "API", Definition = "Application Programming Interface. A set of rules and specifications that allow software components to communicate and exchange data." }, new Glossary() { Key = 2, Term = "Connectors", Definition = "Connectors allow you to integrate with various services provide AI capabilities, including LLM, AudioToText, TextToAudio, Embedding generation, etc." }, new Glossary() { Key = 3, Term = "RAG", 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)." } }; using Microsoft.SemanticKernel.Embeddings; #pragma warning disable SKEXP0001 var textEmbeddingGenerationService = kernel.GetRequiredService(); var tasks = glossaryEntries.Select(entry => Task.Run(async () => { entry.DefinitionEmbedding = await textEmbeddingGenerationService.GenerateEmbeddingAsync(entry.Definition); })); await Task.WhenAll(tasks); await foreach (var key in collection.UpsertBatchAsync(glossaryEntries)) { Console.WriteLine(key); } var options = new GetRecordOptions() { IncludeVectors = true }; await foreach (var record in collection.GetBatchAsync(keys: [1, 2, 3], options)) { Console.WriteLine($"Key: {record.Key}"); Console.WriteLine($"Term: {record.Term}"); Console.WriteLine($"Definition: {record.Definition}"); Console.WriteLine($"Definition Embedding: {JsonSerializer.Serialize(record.DefinitionEmbedding)}"); } #pragma warning disable SKEXP0001 var searchString = "I want to learn more about Connectors"; var searchVector = await textEmbeddingGenerationService.GenerateEmbeddingAsync(searchString); var searchResult = await collection.VectorizedSearchAsync(searchVector); await foreach (var result in searchResult.Results) { Console.WriteLine($"Search score: {result.Score}"); Console.WriteLine($"Key: {result.Record.Key}"); Console.WriteLine($"Term: {result.Record.Term}"); Console.WriteLine($"Definition: {result.Record.Definition}"); Console.WriteLine("========="); }