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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 == "regenerate")
  54. {
  55. Console.ForegroundColor = ConsoleColor.Yellow;
  56. Console.WriteLine("Regenerating last response ...");
  57. await foreach (
  58. var text
  59. in session.RegenerateAssistantMessageAsync(
  60. inferenceParams))
  61. {
  62. Console.ForegroundColor = ConsoleColor.White;
  63. Console.Write(text);
  64. }
  65. }
  66. else
  67. {
  68. await foreach (
  69. var text
  70. in session.ChatAsync(
  71. new ChatHistory.Message(AuthorRole.User, userInput),
  72. inferenceParams))
  73. {
  74. Console.ForegroundColor = ConsoleColor.White;
  75. Console.Write(text);
  76. }
  77. }
  78. Console.ForegroundColor = ConsoleColor.Green;
  79. userInput = Console.ReadLine() ?? "";
  80. Console.ForegroundColor = ConsoleColor.White;
  81. }
  82. }
  83. }