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

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