|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.SemanticKernel; |
| 6 | +using Microsoft.SemanticKernel.Connectors.Memory.Kusto; |
| 7 | +using Microsoft.SemanticKernel.Memory; |
| 8 | +using RepoUtils; |
| 9 | + |
| 10 | +// ReSharper disable once InconsistentNaming |
| 11 | +public static class Example53_Kusto |
| 12 | +{ |
| 13 | + private const string MemoryCollectionName = "kusto_test"; |
| 14 | + |
| 15 | + public static async Task RunAsync() |
| 16 | + { |
| 17 | + var connectionString = new Kusto.Data.KustoConnectionStringBuilder(TestConfiguration.Kusto.ConnectionString).WithAadUserPromptAuthentication(); |
| 18 | + using KustoMemoryStore memoryStore = new(connectionString, "MyDatabase"); |
| 19 | + |
| 20 | + IKernel kernel = Kernel.Builder |
| 21 | + .WithLogger(ConsoleLogger.Logger) |
| 22 | + .WithOpenAITextCompletionService( |
| 23 | + modelId: TestConfiguration.OpenAI.ModelId, |
| 24 | + apiKey: TestConfiguration.OpenAI.ApiKey) |
| 25 | + .WithOpenAITextEmbeddingGenerationService( |
| 26 | + modelId: TestConfiguration.OpenAI.EmbeddingModelId, |
| 27 | + apiKey: TestConfiguration.OpenAI.ApiKey) |
| 28 | + .WithMemoryStorage(memoryStore) |
| 29 | + .Build(); |
| 30 | + |
| 31 | + Console.WriteLine("== Printing Collections in DB =="); |
| 32 | + var collections = memoryStore.GetCollectionsAsync(); |
| 33 | + await foreach (var collection in collections) |
| 34 | + { |
| 35 | + Console.WriteLine(collection); |
| 36 | + } |
| 37 | + |
| 38 | + Console.WriteLine("== Adding Memories =="); |
| 39 | + |
| 40 | + var key1 = await kernel.Memory.SaveInformationAsync(MemoryCollectionName, id: "cat1", text: "british short hair"); |
| 41 | + var key2 = await kernel.Memory.SaveInformationAsync(MemoryCollectionName, id: "cat2", text: "orange tabby"); |
| 42 | + var key3 = await kernel.Memory.SaveInformationAsync(MemoryCollectionName, id: "cat3", text: "norwegian forest cat"); |
| 43 | + |
| 44 | + Console.WriteLine("== Printing Collections in DB =="); |
| 45 | + collections = memoryStore.GetCollectionsAsync(); |
| 46 | + await foreach (var collection in collections) |
| 47 | + { |
| 48 | + Console.WriteLine(collection); |
| 49 | + } |
| 50 | + |
| 51 | + Console.WriteLine("== Retrieving Memories Through the Kernel =="); |
| 52 | + MemoryQueryResult? lookup = await kernel.Memory.GetAsync(MemoryCollectionName, "cat1"); |
| 53 | + Console.WriteLine(lookup != null ? lookup.Metadata.Text : "ERROR: memory not found"); |
| 54 | + |
| 55 | + Console.WriteLine("== Retrieving Memories Directly From the Store =="); |
| 56 | + var memory1 = await memoryStore.GetAsync(MemoryCollectionName, key1); |
| 57 | + var memory2 = await memoryStore.GetAsync(MemoryCollectionName, key2); |
| 58 | + var memory3 = await memoryStore.GetAsync(MemoryCollectionName, key3); |
| 59 | + Console.WriteLine(memory1 != null ? memory1.Metadata.Text : "ERROR: memory not found"); |
| 60 | + Console.WriteLine(memory2 != null ? memory2.Metadata.Text : "ERROR: memory not found"); |
| 61 | + Console.WriteLine(memory3 != null ? memory3.Metadata.Text : "ERROR: memory not found"); |
| 62 | + |
| 63 | + Console.WriteLine("== Similarity Searching Memories: My favorite color is orange =="); |
| 64 | + var searchResults = kernel.Memory.SearchAsync(MemoryCollectionName, "My favorite color is orange", limit: 3, minRelevanceScore: 0.8); |
| 65 | + |
| 66 | + await foreach (var item in searchResults) |
| 67 | + { |
| 68 | + Console.WriteLine(item.Metadata.Text + " : " + item.Relevance); |
| 69 | + } |
| 70 | + |
| 71 | + Console.WriteLine("== Removing Collection {0} ==", MemoryCollectionName); |
| 72 | + await memoryStore.DeleteCollectionAsync(MemoryCollectionName); |
| 73 | + |
| 74 | + Console.WriteLine("== Printing Collections in DB =="); |
| 75 | + await foreach (var collection in collections) |
| 76 | + { |
| 77 | + Console.WriteLine(collection); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments