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 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # ChatSession - load & save
  2. ⚠️Warning: this example has been outdated for the latest version of LLamaSharp, please refer to [this example](./ChatSessionWithRestart.md) to see how to save and load state for `ChatSession`. If you are using some old versions of LLamaSharp, this example may help you.
  3. ```cs
  4. using LLama.Common;
  5. namespace LLama.Examples.Examples
  6. {
  7. public class SaveAndLoadSession
  8. {
  9. public static async Task Run()
  10. {
  11. string modelPath = UserSettings.GetModelPath();
  12. var prompt = (await File.ReadAllTextAsync("Assets/chat-with-bob.txt")).Trim();
  13. var parameters = new ModelParams(modelPath)
  14. {
  15. ContextSize = 1024,
  16. Seed = 1337,
  17. GpuLayerCount = 5
  18. };
  19. using var model = LLamaWeights.LoadFromFile(parameters);
  20. using var context = model.CreateContext(parameters);
  21. var ex = new InteractiveExecutor(context);
  22. var session = new ChatSession(ex);
  23. Console.ForegroundColor = ConsoleColor.Yellow;
  24. 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.");
  25. Console.ForegroundColor = ConsoleColor.White;
  26. // show the prompt
  27. Console.Write(prompt);
  28. while (true)
  29. {
  30. await foreach (
  31. var text
  32. in session.ChatAsync(
  33. new ChatHistory.Message(AuthorRole.User, prompt),
  34. new InferenceParams()
  35. {
  36. Temperature = 0.6f,
  37. AntiPrompts = new List<string> { "User:" }
  38. }))
  39. {
  40. Console.Write(text);
  41. }
  42. Console.ForegroundColor = ConsoleColor.Green;
  43. prompt = Console.ReadLine();
  44. Console.ForegroundColor = ConsoleColor.White;
  45. if (prompt == "save")
  46. {
  47. Console.Write("Preparing to save the state, please input the path you want to save it: ");
  48. Console.ForegroundColor = ConsoleColor.Green;
  49. var statePath = Console.ReadLine();
  50. session.SaveSession(statePath);
  51. Console.ForegroundColor = ConsoleColor.White;
  52. Console.ForegroundColor = ConsoleColor.Yellow;
  53. Console.WriteLine("Saved session!");
  54. Console.ForegroundColor = ConsoleColor.White;
  55. ex.Context.Dispose();
  56. ex = new(new LLamaContext(model, parameters));
  57. session = new ChatSession(ex);
  58. session.LoadSession(statePath);
  59. Console.ForegroundColor = ConsoleColor.Yellow;
  60. Console.WriteLine("Loaded session!");
  61. Console.ForegroundColor = ConsoleColor.White;
  62. Console.Write("Now you can continue your session: ");
  63. Console.ForegroundColor = ConsoleColor.Green;
  64. prompt = Console.ReadLine();
  65. Console.ForegroundColor = ConsoleColor.White;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. ```