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

2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using LLama.Common;
  2. namespace LLama.Examples.NewVersion
  3. {
  4. public class ChatSessionStripRoleName
  5. {
  6. public static async Task Run()
  7. {
  8. Console.Write("Please input your model path: ");
  9. var modelPath = Console.ReadLine();
  10. var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();
  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. var session = new ChatSession(executor).WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { "User:", "Bob:" }, redundancyLength: 8));
  21. Console.ForegroundColor = ConsoleColor.Yellow;
  22. Console.WriteLine("The chat session has started. The role names won't be printed.");
  23. Console.ForegroundColor = ConsoleColor.White;
  24. // show the prompt
  25. Console.Write(prompt);
  26. while (true)
  27. {
  28. await foreach (var text in session.ChatAsync(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "User:" } }))
  29. {
  30. Console.Write(text);
  31. }
  32. Console.ForegroundColor = ConsoleColor.Green;
  33. prompt = Console.ReadLine();
  34. Console.ForegroundColor = ConsoleColor.White;
  35. }
  36. }
  37. }
  38. }