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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using LLama.Common;
  2. namespace LLama.Examples.Examples;
  3. public class ChatSessionStripRoleName
  4. {
  5. public static async Task Run()
  6. {
  7. Console.Write("Please input your model path: ");
  8. var modelPath = Console.ReadLine();
  9. var parameters = new ModelParams(modelPath)
  10. {
  11. ContextSize = 1024,
  12. Seed = 1337,
  13. GpuLayerCount = 5
  14. };
  15. using var model = LLamaWeights.LoadFromFile(parameters);
  16. using var context = model.CreateContext(parameters);
  17. var executor = new InteractiveExecutor(context);
  18. var chatHistoryJson = File.ReadAllText("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. }