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

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