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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. .WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
  50. // User and Assistant in Chinese (User is: 用户, Assistant is: 坤坤)
  51. new string[] { "用户:", "坤坤:" },
  52. redundancyLength: 8));
  53. InferenceParams inferenceParams = new InferenceParams()
  54. {
  55. Temperature = 0.9f,
  56. AntiPrompts = new List<string> { "用户:" }
  57. };
  58. Console.ForegroundColor = ConsoleColor.Yellow;
  59. Console.WriteLine("The chat session has started.");
  60. // show the prompt
  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. }