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

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using LLama.Common;
  2. namespace LLama.Examples.Examples
  3. {
  4. public class LoadAndSaveState
  5. {
  6. public static async Task Run()
  7. {
  8. string modelPath = UserSettings.GetModelPath();
  9. var prompt = (await File.ReadAllTextAsync("Assets/chat-with-bob.txt")).Trim();
  10. var parameters = new ModelParams(modelPath)
  11. {
  12. ContextSize = 1024,
  13. Seed = 1337,
  14. GpuLayerCount = 5
  15. };
  16. using var model = LLamaWeights.LoadFromFile(parameters);
  17. using var context = model.CreateContext(parameters);
  18. var ex = new InteractiveExecutor(context);
  19. Console.ForegroundColor = ConsoleColor.Yellow;
  20. Console.WriteLine("The executor has been enabled. In this example, the prompt is printed, " +
  21. "the maximum tokens is set to 64 and the context size is 256. (an example for small scale usage)");
  22. Console.ForegroundColor = ConsoleColor.White;
  23. Console.Write(prompt);
  24. var inferenceParams = new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "User:" } };
  25. while (true)
  26. {
  27. await foreach (var text in ex.InferAsync(prompt, inferenceParams))
  28. {
  29. Console.Write(text);
  30. }
  31. prompt = Console.ReadLine();
  32. if (prompt == "save")
  33. {
  34. Console.Write("Your path to save model state: ");
  35. var modelStatePath = Console.ReadLine();
  36. ex.Context.SaveState(modelStatePath);
  37. Console.Write("Your path to save executor state: ");
  38. var executorStatePath = Console.ReadLine();
  39. await ex.SaveState(executorStatePath);
  40. Console.ForegroundColor = ConsoleColor.Yellow;
  41. Console.WriteLine("All states saved!");
  42. Console.ForegroundColor = ConsoleColor.White;
  43. var ctx = ex.Context;
  44. ctx.LoadState(modelStatePath);
  45. ex = new InteractiveExecutor(ctx);
  46. await ex.LoadState(executorStatePath);
  47. Console.ForegroundColor = ConsoleColor.Yellow;
  48. Console.WriteLine("Loaded state!");
  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. }