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.cs 4.2 kB

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