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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using LLama.Common;
  2. namespace LLama.Examples.Examples;
  3. // When using chatsession, it's a common case that you want to strip the role names
  4. // rather than display them. This example shows how to use transforms to strip them.
  5. public class ChatSessionStripRoleName
  6. {
  7. public static async Task Run()
  8. {
  9. string modelPath = UserSettings.GetModelPath();
  10. var parameters = new ModelParams(modelPath)
  11. {
  12. ContextSize = 1024,
  13. Seed = 1337,
  14. GpuLayerCount = 5
  15. };
  16. using var model = LLamaWeights.LoadFromFile(parameters);
  17. using var context = model.CreateContext(parameters);
  18. var executor = new InteractiveExecutor(context);
  19. var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
  20. ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
  21. ChatSession session = new(executor, chatHistory);
  22. session.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
  23. new string[] { "User:", "Assistant:" },
  24. redundancyLength: 8));
  25. InferenceParams inferenceParams = new InferenceParams()
  26. {
  27. Temperature = 0.9f,
  28. AntiPrompts = new List<string> { "User:" }
  29. };
  30. Console.ForegroundColor = ConsoleColor.Yellow;
  31. Console.WriteLine("The chat session has started.");
  32. // show the prompt
  33. Console.ForegroundColor = ConsoleColor.Green;
  34. string userInput = Console.ReadLine() ?? "";
  35. while (userInput != "exit")
  36. {
  37. await foreach (
  38. var text
  39. in session.ChatAsync(
  40. new ChatHistory.Message(AuthorRole.User, userInput),
  41. inferenceParams))
  42. {
  43. Console.ForegroundColor = ConsoleColor.White;
  44. Console.Write(text);
  45. }
  46. Console.ForegroundColor = ConsoleColor.Green;
  47. userInput = Console.ReadLine() ?? "";
  48. Console.ForegroundColor = ConsoleColor.White;
  49. }
  50. }
  51. }