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

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