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.

ChatSessionStripRoleName.cs 1.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using LLama.Common;
  2. namespace LLama.Examples.Examples;
  3. public class ChatSessionStripRoleName
  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 session = new(executor, chatHistory);
  20. session.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
  21. new string[] { "User:", "Assistant:" },
  22. redundancyLength: 8));
  23. InferenceParams inferenceParams = new InferenceParams()
  24. {
  25. Temperature = 0.9f,
  26. AntiPrompts = new List<string> { "User:" }
  27. };
  28. Console.ForegroundColor = ConsoleColor.Yellow;
  29. Console.WriteLine("The chat session has started.");
  30. // show the prompt
  31. Console.ForegroundColor = ConsoleColor.Green;
  32. string userInput = Console.ReadLine() ?? "";
  33. while (userInput != "exit")
  34. {
  35. await foreach (
  36. var text
  37. in session.ChatAsync(
  38. new ChatHistory.Message(AuthorRole.User, userInput),
  39. inferenceParams))
  40. {
  41. Console.ForegroundColor = ConsoleColor.White;
  42. Console.Write(text);
  43. }
  44. Console.ForegroundColor = ConsoleColor.Green;
  45. userInput = Console.ReadLine() ?? "";
  46. Console.ForegroundColor = ConsoleColor.White;
  47. }
  48. }
  49. }