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.

ChatSessionWithRestart.md 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # ChatSession - restarting
  2. ```cs
  3. using LLama.Common;
  4. namespace LLama.Examples.Examples;
  5. // This example shows how to restart the chat session
  6. public class ChatSessionWithRestart
  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. var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
  21. ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
  22. ChatSession prototypeSession =
  23. await ChatSession.InitializeSessionFromHistoryAsync(executor, chatHistory);
  24. prototypeSession.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
  25. new string[] { "User:", "Assistant:" },
  26. redundancyLength: 8));
  27. var resetState = prototypeSession.GetSessionState();
  28. ChatSession session = new ChatSession(executor);
  29. session.LoadSession(resetState);
  30. InferenceParams inferenceParams = new InferenceParams()
  31. {
  32. Temperature = 0.9f,
  33. AntiPrompts = new List<string> { "User:" }
  34. };
  35. Console.ForegroundColor = ConsoleColor.Yellow;
  36. Console.WriteLine("The chat session has started. Starting point saved.");
  37. Console.WriteLine("Type 'exit' to end the chat session.");
  38. Console.WriteLine("Type 'save' to save chat session state in memory.");
  39. Console.WriteLine("Type 'reset' to reset the chat session to its saved state.");
  40. Console.WriteLine("Type 'answer for assistant' to add and process provided user and assistant messages.");
  41. // show the prompt
  42. Console.ForegroundColor = ConsoleColor.Green;
  43. string userInput = Console.ReadLine() ?? "";
  44. while (userInput != "exit")
  45. {
  46. // Load the session state from the reset state
  47. if(userInput == "reset")
  48. {
  49. session.LoadSession(resetState);
  50. Console.WriteLine($"Reset to history:\n{session.HistoryTransform.HistoryToText(session.History)}");
  51. Console.ForegroundColor = ConsoleColor.Yellow;
  52. Console.WriteLine("Session reset.");
  53. }
  54. // Assign new reset state.
  55. else if (userInput == "save")
  56. {
  57. resetState = session.GetSessionState();
  58. Console.ForegroundColor = ConsoleColor.Yellow;
  59. Console.WriteLine("Session saved.");
  60. }
  61. // Provide user and override assistant answer with your own.
  62. else if (userInput == "answer for assistant")
  63. {
  64. Console.ForegroundColor = ConsoleColor.Yellow;
  65. Console.WriteLine("Provide user input: ");
  66. Console.ForegroundColor = ConsoleColor.Green;
  67. string userInputOverride = Console.ReadLine() ?? "";
  68. Console.ForegroundColor = ConsoleColor.Yellow;
  69. Console.WriteLine("Provide assistant input: ");
  70. Console.ForegroundColor = ConsoleColor.Green;
  71. string assistantInputOverride = Console.ReadLine() ?? "";
  72. await session.AddAndProcessUserMessage(userInputOverride);
  73. await session.AddAndProcessAssistantMessage(assistantInputOverride);
  74. Console.ForegroundColor = ConsoleColor.Yellow;
  75. Console.WriteLine("User and assistant messages processed. Provide next user message:");
  76. }
  77. else
  78. {
  79. await foreach (
  80. var text
  81. in session.ChatAsync(
  82. new ChatHistory.Message(AuthorRole.User, userInput),
  83. inferenceParams))
  84. {
  85. Console.ForegroundColor = ConsoleColor.White;
  86. Console.Write(text);
  87. }
  88. }
  89. Console.ForegroundColor = ConsoleColor.Green;
  90. userInput = Console.ReadLine() ?? "";
  91. Console.ForegroundColor = ConsoleColor.White;
  92. }
  93. }
  94. }
  95. ```