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.

LoadAndSaveState.md 2.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Load and save model/executor state
  2. ```cs
  3. using LLama.Common;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. public class LoadAndSaveState
  10. {
  11. public static void Run()
  12. {
  13. Console.Write("Please input your model path: ");
  14. string modelPath = Console.ReadLine();
  15. var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();
  16. InteractiveExecutor ex = new(new LLamaModel(new ModelParams(modelPath, contextSize: 256)));
  17. Console.ForegroundColor = ConsoleColor.Yellow;
  18. Console.WriteLine("The executor has been enabled. In this example, the prompt is printed, the maximum tokens is set to 64 and the context size is 256. (an example for small scale usage)");
  19. Console.ForegroundColor = ConsoleColor.White;
  20. Console.Write(prompt);
  21. var inferenceParams = new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "User:" } };
  22. while (true)
  23. {
  24. foreach (var text in ex.Infer(prompt, inferenceParams))
  25. {
  26. Console.Write(text);
  27. }
  28. prompt = Console.ReadLine();
  29. if (prompt == "save")
  30. {
  31. Console.Write("Your path to save model state: ");
  32. string modelStatePath = Console.ReadLine();
  33. ex.Model.SaveState(modelStatePath);
  34. Console.Write("Your path to save executor state: ");
  35. string executorStatePath = Console.ReadLine();
  36. ex.SaveState(executorStatePath);
  37. Console.ForegroundColor = ConsoleColor.Yellow;
  38. Console.WriteLine("All states saved!");
  39. Console.ForegroundColor = ConsoleColor.White;
  40. var model = ex.Model;
  41. model.LoadState(modelStatePath);
  42. ex = new InteractiveExecutor(model);
  43. ex.LoadState(executorStatePath);
  44. Console.ForegroundColor = ConsoleColor.Yellow;
  45. Console.WriteLine("Loaded state!");
  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. ```