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.

LoadAndSaveSession.cs 2.8 kB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using LLama.Common;
  2. namespace LLama.Examples.NewVersion
  3. {
  4. public class SaveAndLoadSession
  5. {
  6. public static void Run()
  7. {
  8. Console.Write("Please input your model path: ");
  9. var modelPath = Console.ReadLine();
  10. var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();
  11. var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
  12. using var model = LLamaWeights.LoadFromFile(parameters);
  13. using var context = model.CreateContext(parameters);
  14. var ex = new InteractiveExecutor(context);
  15. var session = new ChatSession(ex);
  16. Console.ForegroundColor = ConsoleColor.Yellow;
  17. Console.WriteLine("The chat session has started. In this example, the prompt is printed for better visual result. Input \"save\" to save and reload the session.");
  18. Console.ForegroundColor = ConsoleColor.White;
  19. // show the prompt
  20. Console.Write(prompt);
  21. while (true)
  22. {
  23. foreach (var text in session.Chat(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "User:" } }))
  24. {
  25. Console.Write(text);
  26. }
  27. Console.ForegroundColor = ConsoleColor.Green;
  28. prompt = Console.ReadLine();
  29. Console.ForegroundColor = ConsoleColor.White;
  30. if (prompt == "save")
  31. {
  32. Console.Write("Preparing to save the state, please input the path you want to save it: ");
  33. Console.ForegroundColor = ConsoleColor.Green;
  34. var statePath = Console.ReadLine();
  35. session.SaveSession(statePath);
  36. Console.ForegroundColor = ConsoleColor.White;
  37. Console.ForegroundColor = ConsoleColor.Yellow;
  38. Console.WriteLine("Saved session!");
  39. Console.ForegroundColor = ConsoleColor.White;
  40. ex.Context.Dispose();
  41. ex = new(new LLamaContext(new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5)));
  42. session = new ChatSession(ex);
  43. session.LoadSession(statePath);
  44. Console.ForegroundColor = ConsoleColor.Yellow;
  45. Console.WriteLine("Loaded session!");
  46. Console.ForegroundColor = ConsoleColor.White;
  47. Console.Write("Now you can continue your session: ");
  48. Console.ForegroundColor = ConsoleColor.Green;
  49. prompt = Console.ReadLine();
  50. Console.ForegroundColor = ConsoleColor.White;
  51. }
  52. }
  53. }
  54. }
  55. }