Skip to content

Kernel memory integration - basic

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
using LLamaSharp.KernelMemory;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.Configuration;
using System.Diagnostics;

namespace LLama.Examples.Examples
{
    // This example is from Microsoft's official kernel memory "custom prompts" example:
    // https://github.com/microsoft/kernel-memory/blob/6d516d70a23d50c6cb982e822e6a3a9b2e899cfa/examples/101-dotnet-custom-Prompts/Program.cs#L1-L86

    // Microsoft.KernelMemory has more features than Microsoft.SemanticKernel.
    // See https://microsoft.github.io/kernel-memory/ for details.

    public class KernelMemory
    {
        public static async Task Run()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(
                """

                This program uses the Microsoft.KernelMemory package to ingest documents
                and answer questions about them in an interactive chat prompt.

                """);

            // Setup the kernel memory with the LLM model
            string modelPath = UserSettings.GetModelPath();
            IKernelMemory memory = CreateMemory(modelPath);

            // Ingest documents (format is automatically detected from the filename)
            string[] filesToIngest = [
                Path.GetFullPath(@"./Assets/sample-SK-Readme.pdf"),
                Path.GetFullPath(@"./Assets/sample-KM-Readme.pdf"),
            ];

            for (int i = 0; i < filesToIngest.Length; i++)
            {
                string path = filesToIngest[i];
                Stopwatch sw = Stopwatch.StartNew();
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine($"Importing {i + 1} of {filesToIngest.Length}: {path}");
                await memory.ImportDocumentAsync(path, steps: Constants.PipelineWithoutSummary);
                Console.WriteLine($"Completed in {sw.Elapsed}\n");
            }

            // Ask a predefined question
            Console.ForegroundColor = ConsoleColor.Green;
            string question1 = "What formats does KM support";
            Console.WriteLine($"Question: {question1}");
            await AnswerQuestion(memory, question1);

            // Let the user ask additional questions
            while (true)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("Question: ");
                string question = Console.ReadLine()!;
                if (string.IsNullOrEmpty(question))
                    return;

                await AnswerQuestion(memory, question);
            }
        }

        private static IKernelMemory CreateMemory(string modelPath)
        {
            Common.InferenceParams infParams = new() { AntiPrompts = ["\n\n"] };

            LLamaSharpConfig lsConfig = new(modelPath) { DefaultInferenceParams = infParams };

            SearchClientConfig searchClientConfig = new()
            {
                MaxMatchesCount = 1,
                AnswerTokens = 100,
            };

            TextPartitioningOptions parseOptions = new()
            {
                MaxTokensPerParagraph = 300,
                MaxTokensPerLine = 100,
                OverlappingTokens = 30
            };

            return new KernelMemoryBuilder()
                .WithLLamaSharpDefaults(lsConfig)
                .WithSearchClientConfig(searchClientConfig)
                .With(parseOptions)
                .Build();
        }

        private static async Task AnswerQuestion(IKernelMemory memory, string question)
        {
            Stopwatch sw = Stopwatch.StartNew();
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine($"Generating answer...");

            MemoryAnswer answer = await memory.AskAsync(question);
            Console.WriteLine($"Answer generated in {sw.Elapsed}");

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine($"Answer: {answer.Result}");
            foreach (var source in answer.RelevantSources)
            {
                Console.WriteLine($"Source: {source.SourceName}");
            }
            Console.WriteLine();
        }
    }
}