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

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