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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using LLama.Common;
  2. namespace LLama.Examples.Examples;
  3. public class ChatSessionWithRoleName
  4. {
  5. public static async Task Run()
  6. {
  7. string modelPath = UserSettings.GetModelPath();
  8. var parameters = new ModelParams(modelPath)
  9. {
  10. ContextSize = 1024,
  11. Seed = 1337,
  12. GpuLayerCount = 5
  13. };
  14. using var model = LLamaWeights.LoadFromFile(parameters);
  15. using var context = model.CreateContext(parameters);
  16. var executor = new InteractiveExecutor(context);
  17. var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
  18. ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
  19. ChatSession session = new(executor, chatHistory);
  20. InferenceParams inferenceParams = new InferenceParams()
  21. {
  22. Temperature = 0.9f,
  23. AntiPrompts = new List<string> { "User:" }
  24. };
  25. Console.ForegroundColor = ConsoleColor.Yellow;
  26. Console.WriteLine("The chat session has started.");
  27. // show the prompt
  28. Console.ForegroundColor = ConsoleColor.Green;
  29. string userInput = Console.ReadLine() ?? "";
  30. while (userInput != "exit")
  31. {
  32. await foreach (
  33. var text
  34. in session.ChatAsync(
  35. new ChatHistory.Message(AuthorRole.User, userInput),
  36. inferenceParams))
  37. {
  38. Console.ForegroundColor = ConsoleColor.White;
  39. Console.Write(text);
  40. }
  41. Console.ForegroundColor = ConsoleColor.Green;
  42. userInput = Console.ReadLine() ?? "";
  43. Console.ForegroundColor = ConsoleColor.White;
  44. }
  45. }
  46. }