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

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