You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

KernelMemory.md 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Kernel memory integration - basic
  2. ```cs
  3. using LLamaSharp.KernelMemory;
  4. using Microsoft.KernelMemory;
  5. using Microsoft.KernelMemory.Configuration;
  6. using System.Diagnostics;
  7. namespace LLama.Examples.Examples
  8. {
  9. // This example is from Microsoft's official kernel memory "custom prompts" example:
  10. // https://github.com/microsoft/kernel-memory/blob/6d516d70a23d50c6cb982e822e6a3a9b2e899cfa/examples/101-dotnet-custom-Prompts/Program.cs#L1-L86
  11. // Microsoft.KernelMemory has more features than Microsoft.SemanticKernel.
  12. // See https://microsoft.github.io/kernel-memory/ for details.
  13. public class KernelMemory
  14. {
  15. public static async Task Run()
  16. {
  17. Console.ForegroundColor = ConsoleColor.Yellow;
  18. Console.WriteLine(
  19. """
  20. This program uses the Microsoft.KernelMemory package to ingest documents
  21. and answer questions about them in an interactive chat prompt.
  22. """);
  23. // Setup the kernel memory with the LLM model
  24. string modelPath = UserSettings.GetModelPath();
  25. IKernelMemory memory = CreateMemory(modelPath);
  26. // Ingest documents (format is automatically detected from the filename)
  27. string[] filesToIngest = [
  28. Path.GetFullPath(@"./Assets/sample-SK-Readme.pdf"),
  29. Path.GetFullPath(@"./Assets/sample-KM-Readme.pdf"),
  30. ];
  31. for (int i = 0; i < filesToIngest.Length; i++)
  32. {
  33. string path = filesToIngest[i];
  34. Stopwatch sw = Stopwatch.StartNew();
  35. Console.ForegroundColor = ConsoleColor.Blue;
  36. Console.WriteLine($"Importing {i + 1} of {filesToIngest.Length}: {path}");
  37. await memory.ImportDocumentAsync(path, steps: Constants.PipelineWithoutSummary);
  38. Console.WriteLine($"Completed in {sw.Elapsed}\n");
  39. }
  40. // Ask a predefined question
  41. Console.ForegroundColor = ConsoleColor.Green;
  42. string question1 = "What formats does KM support";
  43. Console.WriteLine($"Question: {question1}");
  44. await AnswerQuestion(memory, question1);
  45. // Let the user ask additional questions
  46. while (true)
  47. {
  48. Console.ForegroundColor = ConsoleColor.Green;
  49. Console.Write("Question: ");
  50. string question = Console.ReadLine()!;
  51. if (string.IsNullOrEmpty(question))
  52. return;
  53. await AnswerQuestion(memory, question);
  54. }
  55. }
  56. private static IKernelMemory CreateMemory(string modelPath)
  57. {
  58. Common.InferenceParams infParams = new() { AntiPrompts = ["\n\n"] };
  59. LLamaSharpConfig lsConfig = new(modelPath) { DefaultInferenceParams = infParams };
  60. SearchClientConfig searchClientConfig = new()
  61. {
  62. MaxMatchesCount = 1,
  63. AnswerTokens = 100,
  64. };
  65. TextPartitioningOptions parseOptions = new()
  66. {
  67. MaxTokensPerParagraph = 300,
  68. MaxTokensPerLine = 100,
  69. OverlappingTokens = 30
  70. };
  71. return new KernelMemoryBuilder()
  72. .WithLLamaSharpDefaults(lsConfig)
  73. .WithSearchClientConfig(searchClientConfig)
  74. .With(parseOptions)
  75. .Build();
  76. }
  77. private static async Task AnswerQuestion(IKernelMemory memory, string question)
  78. {
  79. Stopwatch sw = Stopwatch.StartNew();
  80. Console.ForegroundColor = ConsoleColor.DarkGray;
  81. Console.WriteLine($"Generating answer...");
  82. MemoryAnswer answer = await memory.AskAsync(question);
  83. Console.WriteLine($"Answer generated in {sw.Elapsed}");
  84. Console.ForegroundColor = ConsoleColor.Gray;
  85. Console.WriteLine($"Answer: {answer.Result}");
  86. foreach (var source in answer.RelevantSources)
  87. {
  88. Console.WriteLine($"Source: {source.SourceName}");
  89. }
  90. Console.WriteLine();
  91. }
  92. }
  93. }
  94. ```