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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. Write `save` to save session in memory."
  34. + " Write `reset` to start from the last saved checkpoint");
  35. // show the prompt
  36. Console.ForegroundColor = ConsoleColor.Green;
  37. string userInput = Console.ReadLine() ?? "";
  38. while (userInput != "exit")
  39. {
  40. if(userInput == "reset")
  41. {
  42. session.LoadSession(resetState);
  43. Console.WriteLine($"Reset to history:\n{session.HistoryTransform.HistoryToText(session.History)}");
  44. Console.ForegroundColor = ConsoleColor.Yellow;
  45. Console.WriteLine("Session reset.");
  46. }
  47. else if (userInput == "save")
  48. {
  49. resetState = session.GetSessionState();
  50. Console.ForegroundColor = ConsoleColor.Yellow;
  51. Console.WriteLine("Session saved.");
  52. }
  53. else if (userInput == "regenerate")
  54. {
  55. Console.ForegroundColor = ConsoleColor.Yellow;
  56. Console.WriteLine("Regenerating last response ...");
  57. await foreach (
  58. var text
  59. in session.RegenerateAssistantMessageAsync(
  60. inferenceParams))
  61. {
  62. Console.ForegroundColor = ConsoleColor.White;
  63. Console.Write(text);
  64. }
  65. }
  66. else
  67. {
  68. await foreach (
  69. var text
  70. in session.ChatAsync(
  71. new ChatHistory.Message(AuthorRole.User, userInput),
  72. inferenceParams))
  73. {
  74. Console.ForegroundColor = ConsoleColor.White;
  75. Console.Write(text);
  76. }
  77. }
  78. Console.ForegroundColor = ConsoleColor.Green;
  79. userInput = Console.ReadLine() ?? "";
  80. Console.ForegroundColor = ConsoleColor.White;
  81. }
  82. }
  83. }