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.9 kB

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