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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. Seed = 1337,
  13. GpuLayerCount = 5
  14. };
  15. using var model = await LLamaWeights.LoadFromFileAsync(parameters);
  16. using var context = model.CreateContext(parameters);
  17. var executor = new InteractiveExecutor(context);
  18. var chatHistoryJson = await File.ReadAllTextAsync("Assets/chat-with-bob.json");
  19. ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
  20. ChatSession session = new(executor, chatHistory);
  21. session.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
  22. new string[] { "User:", "Assistant:" },
  23. redundancyLength: 8));
  24. InferenceParams inferenceParams = new InferenceParams()
  25. {
  26. Temperature = 0.9f,
  27. AntiPrompts = new List<string> { "User:" }
  28. };
  29. Console.ForegroundColor = ConsoleColor.Yellow;
  30. Console.WriteLine("The chat session has started.");
  31. // show the prompt
  32. Console.ForegroundColor = ConsoleColor.Green;
  33. string userInput = Console.ReadLine() ?? "";
  34. while (userInput != "exit")
  35. {
  36. await foreach (
  37. var text
  38. in session.ChatAsync(
  39. new ChatHistory.Message(AuthorRole.User, userInput),
  40. inferenceParams))
  41. {
  42. Console.ForegroundColor = ConsoleColor.White;
  43. Console.Write(text);
  44. }
  45. Console.ForegroundColor = ConsoleColor.Green;
  46. userInput = Console.ReadLine() ?? "";
  47. Console.ForegroundColor = ConsoleColor.White;
  48. }
  49. }
  50. }