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.md 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Load and save chat session
  2. ```cs
  3. using LLama.Common;
  4. using LLama.OldVersion;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. public class SaveAndLoadSession
  11. {
  12. public static void Run()
  13. {
  14. Console.Write("Please input your model path: ");
  15. string modelPath = Console.ReadLine();
  16. var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();
  17. InteractiveExecutor ex = new(new LLamaModel(new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5)));
  18. ChatSession session = new ChatSession(ex); // The only change is to remove the transform for the output text stream.
  19. Console.ForegroundColor = ConsoleColor.Yellow;
  20. 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.");
  21. Console.ForegroundColor = ConsoleColor.White;
  22. // show the prompt
  23. Console.Write(prompt);
  24. while (true)
  25. {
  26. foreach (var text in session.Chat(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "User:" } }))
  27. {
  28. Console.Write(text);
  29. }
  30. Console.ForegroundColor = ConsoleColor.Green;
  31. prompt = Console.ReadLine();
  32. Console.ForegroundColor = ConsoleColor.White;
  33. if (prompt == "save")
  34. {
  35. Console.Write("Preparing to save the state, please input the path you want to save it: ");
  36. Console.ForegroundColor = ConsoleColor.Green;
  37. var statePath = Console.ReadLine();
  38. session.SaveSession(statePath);
  39. Console.ForegroundColor = ConsoleColor.White;
  40. Console.ForegroundColor = ConsoleColor.Yellow;
  41. Console.WriteLine("Saved session!");
  42. Console.ForegroundColor = ConsoleColor.White;
  43. ex.Model.Dispose();
  44. ex = new(new LLamaModel(new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5)));
  45. session = new ChatSession(ex).WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { "User:", "Bob:" }, redundancyLength: 8));
  46. session.LoadSession(statePath);
  47. Console.ForegroundColor = ConsoleColor.Yellow;
  48. Console.WriteLine("Loaded session!");
  49. Console.ForegroundColor = ConsoleColor.White;
  50. Console.Write("Now you can continue your session: ");
  51. Console.ForegroundColor = ConsoleColor.Green;
  52. prompt = Console.ReadLine();
  53. Console.ForegroundColor = ConsoleColor.White;
  54. }
  55. }
  56. }
  57. }
  58. ```