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.md 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Chat session with history
  2. ```cs
  3. using LLama.Common;
  4. namespace LLama.Examples.Examples;
  5. public class ChatSessionWithHistory
  6. {
  7. public static async Task Run()
  8. {
  9. Console.Write("Please input your model path: ");
  10. var modelPath = Console.ReadLine();
  11. var parameters = new ModelParams(modelPath)
  12. {
  13. ContextSize = 1024,
  14. Seed = 1337,
  15. GpuLayerCount = 5
  16. };
  17. using var model = LLamaWeights.LoadFromFile(parameters);
  18. using var context = model.CreateContext(parameters);
  19. var executor = new InteractiveExecutor(context);
  20. ChatSession session;
  21. if (Directory.Exists("Assets/chat-with-bob"))
  22. {
  23. Console.ForegroundColor = ConsoleColor.Yellow;
  24. Console.WriteLine("Loading session from disk.");
  25. Console.ForegroundColor = ConsoleColor.White;
  26. session = new ChatSession(executor);
  27. session.LoadSession("Assets/chat-with-bob");
  28. }
  29. else
  30. {
  31. var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
  32. ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
  33. session = new ChatSession(executor, chatHistory);
  34. }
  35. session.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
  36. new string[] { "User:", "Assistant:" },
  37. redundancyLength: 8));
  38. InferenceParams inferenceParams = new InferenceParams()
  39. {
  40. Temperature = 0.9f,
  41. AntiPrompts = new List<string> { "User:" }
  42. };
  43. Console.ForegroundColor = ConsoleColor.Yellow;
  44. Console.WriteLine("The chat session has started.");
  45. // show the prompt
  46. Console.ForegroundColor = ConsoleColor.Green;
  47. string userInput = Console.ReadLine() ?? "";
  48. while (userInput != "exit")
  49. {
  50. if (userInput == "save")
  51. {
  52. session.SaveSession("Assets/chat-with-bob");
  53. Console.ForegroundColor = ConsoleColor.Yellow;
  54. Console.WriteLine("Session saved.");
  55. }
  56. else if (userInput == "regenerate")
  57. {
  58. Console.ForegroundColor = ConsoleColor.Yellow;
  59. Console.WriteLine("Regenerating last response ...");
  60. await foreach (
  61. var text
  62. in session.RegenerateAssistantMessageAsync(
  63. inferenceParams))
  64. {
  65. Console.ForegroundColor = ConsoleColor.White;
  66. Console.Write(text);
  67. }
  68. }
  69. else
  70. {
  71. await foreach (
  72. var text
  73. in session.ChatAsync(
  74. new ChatHistory.Message(AuthorRole.User, userInput),
  75. inferenceParams))
  76. {
  77. Console.ForegroundColor = ConsoleColor.White;
  78. Console.Write(text);
  79. }
  80. }
  81. Console.ForegroundColor = ConsoleColor.Green;
  82. userInput = Console.ReadLine() ?? "";
  83. Console.ForegroundColor = ConsoleColor.White;
  84. }
  85. }
  86. }
  87. ```