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.

ChatChineseGB2312.cs 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Text;
  2. using LLama.Common;
  3. namespace LLama.Examples.Examples;
  4. public class ChatChineseGB2312
  5. {
  6. private static string ConvertEncoding(string input, Encoding original, Encoding target)
  7. {
  8. byte[] bytes = original.GetBytes(input);
  9. var convertedBytes = Encoding.Convert(original, target, bytes);
  10. return target.GetString(convertedBytes);
  11. }
  12. public static async Task Run()
  13. {
  14. // Register provider for GB2312 encoding
  15. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  16. Console.ForegroundColor = ConsoleColor.Yellow;
  17. Console.WriteLine("This example shows how to use Chinese with gb2312 encoding, which is common in windows. It's recommended" +
  18. " to use https://huggingface.co/hfl/chinese-alpaca-2-7b-gguf/blob/main/ggml-model-q5_0.gguf, which has been verified by LLamaSharp developers.");
  19. Console.ForegroundColor = ConsoleColor.White;
  20. Console.Write("Please input your model path: ");
  21. var modelPath = Console.ReadLine();
  22. var parameters = new ModelParams(modelPath)
  23. {
  24. ContextSize = 1024,
  25. Seed = 1337,
  26. GpuLayerCount = 5,
  27. Encoding = Encoding.UTF8
  28. };
  29. using var model = LLamaWeights.LoadFromFile(parameters);
  30. using var context = model.CreateContext(parameters);
  31. var executor = new InteractiveExecutor(context);
  32. ChatSession session;
  33. if (Directory.Exists("Assets/chat-with-kunkun-chinese"))
  34. {
  35. Console.ForegroundColor = ConsoleColor.Yellow;
  36. Console.WriteLine("Loading session from disk.");
  37. Console.ForegroundColor = ConsoleColor.White;
  38. session = new ChatSession(executor);
  39. session.LoadSession("Assets/chat-with-kunkun-chinese");
  40. }
  41. else
  42. {
  43. var chatHistoryJson = File.ReadAllText("Assets/chat-with-kunkun-chinese.json");
  44. ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
  45. session = new ChatSession(executor, chatHistory);
  46. }
  47. session
  48. .WithHistoryTransform(new LLamaTransforms.DefaultHistoryTransform("用户", "坤坤"));
  49. InferenceParams inferenceParams = new InferenceParams()
  50. {
  51. Temperature = 0.9f,
  52. AntiPrompts = new List<string> { "用户:" }
  53. };
  54. Console.ForegroundColor = ConsoleColor.Yellow;
  55. Console.WriteLine("The chat session has started.");
  56. // show the prompt
  57. Console.ForegroundColor = ConsoleColor.Green;
  58. string userInput = Console.ReadLine() ?? "";
  59. while (userInput != "exit")
  60. {
  61. // Convert the encoding from gb2312 to utf8 for the language model
  62. // and later saving to the history json file.
  63. userInput = ConvertEncoding(userInput, Encoding.GetEncoding("gb2312"), Encoding.UTF8);
  64. if (userInput == "save")
  65. {
  66. session.SaveSession("Assets/chat-with-kunkun-chinese");
  67. Console.ForegroundColor = ConsoleColor.Yellow;
  68. Console.WriteLine("Session saved.");
  69. }
  70. else if (userInput == "regenerate")
  71. {
  72. Console.ForegroundColor = ConsoleColor.Yellow;
  73. Console.WriteLine("Regenerating last response ...");
  74. await foreach (
  75. var text
  76. in session.RegenerateAssistantMessageAsync(
  77. inferenceParams))
  78. {
  79. Console.ForegroundColor = ConsoleColor.White;
  80. // Convert the encoding from utf8 to gb2312 for the console output.
  81. Console.Write(ConvertEncoding(text, Encoding.UTF8, Encoding.GetEncoding("gb2312")));
  82. }
  83. }
  84. else
  85. {
  86. await foreach (
  87. var text
  88. in session.ChatAsync(
  89. new ChatHistory.Message(AuthorRole.User, userInput),
  90. inferenceParams))
  91. {
  92. Console.ForegroundColor = ConsoleColor.White;
  93. Console.Write(text);
  94. }
  95. }
  96. Console.ForegroundColor = ConsoleColor.Green;
  97. userInput = Console.ReadLine() ?? "";
  98. Console.ForegroundColor = ConsoleColor.White;
  99. }
  100. }
  101. }