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