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 1.9 kB

2 years ago
2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using LLamaSharp.KernelMemory;
  7. using Microsoft.KernelMemory;
  8. using Microsoft.KernelMemory.Handlers;
  9. namespace LLama.Examples.Examples
  10. {
  11. public class KernelMemory
  12. {
  13. public static async Task Run()
  14. {
  15. Console.WriteLine("Example from: https://github.com/microsoft/kernel-memory/blob/main/examples/101-using-core-nuget/Program.cs");
  16. Console.Write("Please input your model path: ");
  17. var modelPath = Console.ReadLine();
  18. var memory = new KernelMemoryBuilder()
  19. .WithLLamaSharpDefaults(new LLamaSharpConfig(modelPath)
  20. {
  21. DefaultInferenceParams = new Common.InferenceParams
  22. {
  23. AntiPrompts = new List<string> { "\n\n" }
  24. }
  25. })
  26. .With(new TextPartitioningOptions
  27. {
  28. MaxTokensPerParagraph = 300,
  29. MaxTokensPerLine = 100,
  30. OverlappingTokens = 30
  31. })
  32. .BuildServerlessClient();
  33. await memory.ImportDocumentAsync(@"./Assets/sample-SK-Readme.pdf", steps: Constants.PipelineWithoutSummary);
  34. var question = "What's Semantic Kernel?";
  35. Console.WriteLine($"\n\nQuestion: {question}");
  36. var answer = await memory.AskAsync(question);
  37. Console.WriteLine($"\nAnswer: {answer.Result}");
  38. Console.WriteLine("\n\n Sources:\n");
  39. foreach (var x in answer.RelevantSources)
  40. {
  41. Console.WriteLine($" - {x.SourceName} - {x.Link} [{x.Partitions.First().LastUpdate:D}]");
  42. }
  43. }
  44. }
  45. }