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.

ChatSessionWithRestart.cs 3.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using LLama.Common;
  2. namespace LLama.Examples.Examples;
  3. public class ChatSessionWithRestart
  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. var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
  18. ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
  19. ChatSession prototypeSession =
  20. await ChatSession.InitializeSessionFromHistoryAsync(executor, chatHistory);
  21. prototypeSession.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
  22. new string[] { "User:", "Assistant:" },
  23. redundancyLength: 8));
  24. var resetState = prototypeSession.GetSessionState();
  25. ChatSession session = new ChatSession(executor);
  26. session.LoadSession(resetState);
  27. InferenceParams inferenceParams = new InferenceParams()
  28. {
  29. Temperature = 0.9f,
  30. AntiPrompts = new List<string> { "User:" }
  31. };
  32. Console.ForegroundColor = ConsoleColor.Yellow;
  33. Console.WriteLine("The chat session has started.");
  34. // show the prompt
  35. Console.ForegroundColor = ConsoleColor.Green;
  36. string userInput = Console.ReadLine() ?? "";
  37. while (userInput != "exit")
  38. {
  39. if(userInput == "reset")
  40. {
  41. session.LoadSession(resetState);
  42. Console.WriteLine($"History: {session.HistoryTransform.HistoryToText(session.History)}");
  43. Console.ForegroundColor = ConsoleColor.Yellow;
  44. Console.WriteLine("Session reset.");
  45. }
  46. else if (userInput == "save")
  47. {
  48. session.SaveSession("Assets/chat-with-bob");
  49. Console.ForegroundColor = ConsoleColor.Yellow;
  50. Console.WriteLine("Session saved.");
  51. }
  52. else if (userInput == "regenerate")
  53. {
  54. Console.ForegroundColor = ConsoleColor.Yellow;
  55. Console.WriteLine("Regenerating last response ...");
  56. await foreach (
  57. var text
  58. in session.RegenerateAssistantMessageAsync(
  59. inferenceParams))
  60. {
  61. Console.ForegroundColor = ConsoleColor.White;
  62. Console.Write(text);
  63. }
  64. }
  65. else
  66. {
  67. await foreach (
  68. var text
  69. in session.ChatAsync(
  70. new ChatHistory.Message(AuthorRole.User, userInput),
  71. inferenceParams))
  72. {
  73. Console.ForegroundColor = ConsoleColor.White;
  74. Console.Write(text);
  75. }
  76. }
  77. Console.ForegroundColor = ConsoleColor.Green;
  78. userInput = Console.ReadLine() ?? "";
  79. Console.ForegroundColor = ConsoleColor.White;
  80. }
  81. }
  82. }