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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # ChatSession - stripping role names
  2. ```cs
  3. using LLama.Common;
  4. namespace LLama.Examples.Examples;
  5. // When using chatsession, it's a common case that you want to strip the role names
  6. // rather than display them. This example shows how to use transforms to strip them.
  7. public class ChatSessionStripRoleName
  8. {
  9. public static async Task Run()
  10. {
  11. string modelPath = UserSettings.GetModelPath();
  12. var parameters = new ModelParams(modelPath)
  13. {
  14. ContextSize = 1024,
  15. Seed = 1337,
  16. GpuLayerCount = 5
  17. };
  18. using var model = LLamaWeights.LoadFromFile(parameters);
  19. using var context = model.CreateContext(parameters);
  20. var executor = new InteractiveExecutor(context);
  21. var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
  22. ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
  23. ChatSession session = new(executor, chatHistory);
  24. session.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
  25. new string[] { "User:", "Assistant:" },
  26. redundancyLength: 8));
  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. await foreach (
  40. var text
  41. in session.ChatAsync(
  42. new ChatHistory.Message(AuthorRole.User, userInput),
  43. inferenceParams))
  44. {
  45. Console.ForegroundColor = ConsoleColor.White;
  46. Console.Write(text);
  47. }
  48. Console.ForegroundColor = ConsoleColor.Green;
  49. userInput = Console.ReadLine() ?? "";
  50. Console.ForegroundColor = ConsoleColor.White;
  51. }
  52. }
  53. }
  54. ```