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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. 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. ChatSession session;
  18. if (Directory.Exists("Assets/chat-with-bob"))
  19. {
  20. Console.ForegroundColor = ConsoleColor.Yellow;
  21. Console.WriteLine("Loading session from disk.");
  22. Console.ForegroundColor = ConsoleColor.White;
  23. session = new ChatSession(executor);
  24. session.LoadSession("Assets/chat-with-bob");
  25. }
  26. else
  27. {
  28. var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
  29. ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
  30. session = new ChatSession(executor, chatHistory);
  31. }
  32. session.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
  33. new string[] { "User:", "Assistant:" },
  34. redundancyLength: 8));
  35. InferenceParams inferenceParams = new InferenceParams()
  36. {
  37. Temperature = 0.9f,
  38. AntiPrompts = new List<string> { "User:" }
  39. };
  40. Console.ForegroundColor = ConsoleColor.Yellow;
  41. Console.WriteLine("The chat session has started.");
  42. Console.WriteLine("Type 'exit' to end the chat session.");
  43. Console.WriteLine("Type 'save' to save the chat session to disk.");
  44. Console.WriteLine("Type 'load' to load the chat session from disk.");
  45. Console.WriteLine("Type 'regenerate' to regenerate the last response.");
  46. // show the prompt
  47. Console.ForegroundColor = ConsoleColor.Green;
  48. string userInput = Console.ReadLine() ?? "";
  49. while (userInput != "exit")
  50. {
  51. // Save the chat state to disk
  52. if (userInput == "save")
  53. {
  54. session.SaveSession("Assets/chat-with-bob");
  55. Console.ForegroundColor = ConsoleColor.Yellow;
  56. Console.WriteLine("Session saved.");
  57. }
  58. // Load the chat state from disk
  59. else if (userInput == "load")
  60. {
  61. session.LoadSession("Assets/chat-with-bob");
  62. Console.ForegroundColor = ConsoleColor.Yellow;
  63. Console.WriteLine("Session loaded.");
  64. }
  65. else if (userInput == "regenerate")
  66. {
  67. Console.ForegroundColor = ConsoleColor.Yellow;
  68. Console.WriteLine("Regenerating last response ...");
  69. await foreach (
  70. var text
  71. in session.RegenerateAssistantMessageAsync(
  72. inferenceParams))
  73. {
  74. Console.ForegroundColor = ConsoleColor.White;
  75. Console.Write(text);
  76. }
  77. }
  78. else
  79. {
  80. await foreach (
  81. var text
  82. in session.ChatAsync(
  83. new ChatHistory.Message(AuthorRole.User, userInput),
  84. inferenceParams))
  85. {
  86. Console.ForegroundColor = ConsoleColor.White;
  87. Console.Write(text);
  88. }
  89. }
  90. Console.ForegroundColor = ConsoleColor.Green;
  91. userInput = Console.ReadLine() ?? "";
  92. Console.ForegroundColor = ConsoleColor.White;
  93. }
  94. }
  95. }