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

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