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

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