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.

ChatSessionWithRoleName.cs 1.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using LLama.Common;
  2. namespace LLama.Examples.Examples;
  3. public class ChatSessionWithRoleName
  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. InferenceParams inferenceParams = new InferenceParams()
  22. {
  23. Temperature = 0.9f,
  24. AntiPrompts = new List<string> { "User:" }
  25. };
  26. Console.ForegroundColor = ConsoleColor.Yellow;
  27. Console.WriteLine("The chat session has started.");
  28. // show the prompt
  29. Console.ForegroundColor = ConsoleColor.Green;
  30. string userInput = Console.ReadLine() ?? "";
  31. while (userInput != "exit")
  32. {
  33. await foreach (
  34. var text
  35. in session.ChatAsync(
  36. new ChatHistory.Message(AuthorRole.User, userInput),
  37. inferenceParams))
  38. {
  39. Console.ForegroundColor = ConsoleColor.White;
  40. Console.Write(text);
  41. }
  42. Console.ForegroundColor = ConsoleColor.Green;
  43. userInput = Console.ReadLine() ?? "";
  44. Console.ForegroundColor = ConsoleColor.White;
  45. }
  46. }
  47. }