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

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