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.

ChatSessionWithHistory.md 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # ChatSession - with history
  2. ```cs
  3. using LLama.Common;
  4. namespace LLama.Examples.Examples;
  5. // This example shows how to save the state and history of chat session and load it again.
  6. public class ChatSessionWithHistory
  7. {
  8. public static async Task Run()
  9. {
  10. string modelPath = UserSettings.GetModelPath();
  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 executor = new InteractiveExecutor(context);
  20. ChatSession session;
  21. if (Directory.Exists("Assets/chat-with-bob"))
  22. {
  23. Console.ForegroundColor = ConsoleColor.Yellow;
  24. Console.WriteLine("Loading session from disk.");
  25. Console.ForegroundColor = ConsoleColor.White;
  26. session = new ChatSession(executor);
  27. session.LoadSession("Assets/chat-with-bob");
  28. }
  29. else
  30. {
  31. var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
  32. ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
  33. session = new ChatSession(executor, chatHistory);
  34. }
  35. session.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
  36. new string[] { "User:", "Assistant:" },
  37. redundancyLength: 8));
  38. InferenceParams inferenceParams = new InferenceParams()
  39. {
  40. Temperature = 0.9f,
  41. AntiPrompts = new List<string> { "User:" }
  42. };
  43. Console.ForegroundColor = ConsoleColor.Yellow;
  44. Console.WriteLine("The chat session has started.");
  45. Console.WriteLine("Type 'exit' to end the chat session.");
  46. Console.WriteLine("Type 'save' to save the chat session to disk.");
  47. Console.WriteLine("Type 'load' to load the chat session from disk.");
  48. Console.WriteLine("Type 'regenerate' to regenerate the last response.");
  49. // show the prompt
  50. Console.ForegroundColor = ConsoleColor.Green;
  51. string userInput = Console.ReadLine() ?? "";
  52. while (userInput != "exit")
  53. {
  54. // Save the chat state to disk
  55. if (userInput == "save")
  56. {
  57. session.SaveSession("Assets/chat-with-bob");
  58. Console.ForegroundColor = ConsoleColor.Yellow;
  59. Console.WriteLine("Session saved.");
  60. }
  61. // Load the chat state from disk
  62. else if (userInput == "load")
  63. {
  64. session.LoadSession("Assets/chat-with-bob");
  65. Console.ForegroundColor = ConsoleColor.Yellow;
  66. Console.WriteLine("Session loaded.");
  67. }
  68. else if (userInput == "regenerate")
  69. {
  70. Console.ForegroundColor = ConsoleColor.Yellow;
  71. Console.WriteLine("Regenerating last response ...");
  72. await foreach (
  73. var text
  74. in session.RegenerateAssistantMessageAsync(
  75. inferenceParams))
  76. {
  77. Console.ForegroundColor = ConsoleColor.White;
  78. Console.Write(text);
  79. }
  80. }
  81. else
  82. {
  83. await foreach (
  84. var text
  85. in session.ChatAsync(
  86. new ChatHistory.Message(AuthorRole.User, userInput),
  87. inferenceParams))
  88. {
  89. Console.ForegroundColor = ConsoleColor.White;
  90. Console.Write(text);
  91. }
  92. }
  93. Console.ForegroundColor = ConsoleColor.Green;
  94. userInput = Console.ReadLine() ?? "";
  95. Console.ForegroundColor = ConsoleColor.White;
  96. }
  97. }
  98. }
  99. ```