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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // show the prompt
  43. Console.ForegroundColor = ConsoleColor.Green;
  44. string userInput = Console.ReadLine() ?? "";
  45. while (userInput != "exit")
  46. {
  47. if (userInput == "save")
  48. {
  49. session.SaveSession("Assets/chat-with-bob");
  50. Console.ForegroundColor = ConsoleColor.Yellow;
  51. Console.WriteLine("Session saved.");
  52. }
  53. else if (userInput == "load")
  54. {
  55. session.LoadSession("Assets/chat-with-bob");
  56. Console.ForegroundColor = ConsoleColor.Yellow;
  57. Console.WriteLine("Session loaded.");
  58. }
  59. else if (userInput == "regenerate")
  60. {
  61. Console.ForegroundColor = ConsoleColor.Yellow;
  62. Console.WriteLine("Regenerating last response ...");
  63. await foreach (
  64. var text
  65. in session.RegenerateAssistantMessageAsync(
  66. inferenceParams))
  67. {
  68. Console.ForegroundColor = ConsoleColor.White;
  69. Console.Write(text);
  70. }
  71. }
  72. else
  73. {
  74. await foreach (
  75. var text
  76. in session.ChatAsync(
  77. new ChatHistory.Message(AuthorRole.User, userInput),
  78. inferenceParams))
  79. {
  80. Console.ForegroundColor = ConsoleColor.White;
  81. Console.Write(text);
  82. }
  83. }
  84. Console.ForegroundColor = ConsoleColor.Green;
  85. userInput = Console.ReadLine() ?? "";
  86. Console.ForegroundColor = ConsoleColor.White;
  87. }
  88. }
  89. }