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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using LLama.Common;
  2. namespace LLama.Examples.Examples;
  3. public class ChatSessionWithHistory
  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 = await LLamaWeights.LoadFromFileAsync(parameters);
  14. using var context = model.CreateContext(parameters);
  15. var executor = new InteractiveExecutor(context);
  16. ChatSession session;
  17. if (Directory.Exists("Assets/chat-with-bob"))
  18. {
  19. Console.ForegroundColor = ConsoleColor.Yellow;
  20. Console.WriteLine("Loading session from disk.");
  21. Console.ForegroundColor = ConsoleColor.White;
  22. session = new ChatSession(executor);
  23. session.LoadSession("Assets/chat-with-bob");
  24. }
  25. else
  26. {
  27. var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
  28. ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
  29. session = new ChatSession(executor, chatHistory);
  30. }
  31. session.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
  32. new string[] { "User:", "Assistant:" },
  33. redundancyLength: 8));
  34. InferenceParams inferenceParams = new InferenceParams()
  35. {
  36. Temperature = 0.9f,
  37. AntiPrompts = new List<string> { "User:" }
  38. };
  39. Console.ForegroundColor = ConsoleColor.Yellow;
  40. Console.WriteLine("The chat session has started.");
  41. Console.WriteLine("Type 'exit' to end the chat session.");
  42. Console.WriteLine("Type 'save' to save the chat session to disk.");
  43. Console.WriteLine("Type 'load' to load the chat session from disk.");
  44. Console.WriteLine("Type 'regenerate' to regenerate the last response.");
  45. // show the prompt
  46. Console.ForegroundColor = ConsoleColor.Green;
  47. string userInput = Console.ReadLine() ?? "";
  48. while (userInput != "exit")
  49. {
  50. // Save the chat state to disk
  51. if (userInput == "save")
  52. {
  53. session.SaveSession("Assets/chat-with-bob");
  54. Console.ForegroundColor = ConsoleColor.Yellow;
  55. Console.WriteLine("Session saved.");
  56. }
  57. // Load the chat state from disk
  58. else if (userInput == "load")
  59. {
  60. session.LoadSession("Assets/chat-with-bob");
  61. Console.ForegroundColor = ConsoleColor.Yellow;
  62. Console.WriteLine("Session loaded.");
  63. }
  64. else if (userInput == "regenerate")
  65. {
  66. Console.ForegroundColor = ConsoleColor.Yellow;
  67. Console.WriteLine("Regenerating last response ...");
  68. await foreach (
  69. var text
  70. in session.RegenerateAssistantMessageAsync(
  71. inferenceParams))
  72. {
  73. Console.ForegroundColor = ConsoleColor.White;
  74. Console.Write(text);
  75. }
  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. }