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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Text;
  2. using LLama.Common;
  3. namespace LLama.Examples.Examples;
  4. // This example shows how to deal with Chinese input with gb2312 encoding.
  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. string modelPath = UserSettings.GetModelPath();
  22. var parameters = new ModelParams(modelPath)
  23. {
  24. Seed = 1337,
  25. GpuLayerCount = 5,
  26. Encoding = Encoding.UTF8
  27. };
  28. using var model = await LLamaWeights.LoadFromFileAsync(parameters);
  29. using var context = model.CreateContext(parameters);
  30. var executor = new InteractiveExecutor(context);
  31. ChatSession session;
  32. if (Directory.Exists("Assets/chat-with-kunkun-chinese"))
  33. {
  34. Console.ForegroundColor = ConsoleColor.Yellow;
  35. Console.WriteLine("Loading session from disk.");
  36. Console.ForegroundColor = ConsoleColor.White;
  37. session = new ChatSession(executor);
  38. session.LoadSession("Assets/chat-with-kunkun-chinese");
  39. }
  40. else
  41. {
  42. var chatHistoryJson = File.ReadAllText("Assets/chat-with-kunkun-chinese.json");
  43. ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
  44. session = new ChatSession(executor, chatHistory);
  45. }
  46. session
  47. .WithHistoryTransform(new LLamaTransforms.DefaultHistoryTransform("用户", "坤坤"));
  48. InferenceParams inferenceParams = new InferenceParams()
  49. {
  50. Temperature = 0.9f,
  51. AntiPrompts = new List<string> { "用户:" }
  52. };
  53. Console.ForegroundColor = ConsoleColor.Yellow;
  54. Console.WriteLine("The chat session has started.");
  55. // show the prompt
  56. Console.ForegroundColor = ConsoleColor.White;
  57. Console.Write("用户:");
  58. Console.ForegroundColor = ConsoleColor.Green;
  59. string userInput = Console.ReadLine() ?? "";
  60. while (userInput != "exit")
  61. {
  62. // Convert the encoding from gb2312 to utf8 for the language model
  63. // and later saving to the history json file.
  64. userInput = ConvertEncoding(userInput, Encoding.GetEncoding("gb2312"), Encoding.UTF8);
  65. if (userInput == "save")
  66. {
  67. session.SaveSession("Assets/chat-with-kunkun-chinese");
  68. Console.ForegroundColor = ConsoleColor.Yellow;
  69. Console.WriteLine("Session saved.");
  70. }
  71. else if (userInput == "regenerate")
  72. {
  73. Console.ForegroundColor = ConsoleColor.Yellow;
  74. Console.WriteLine("Regenerating last response ...");
  75. await foreach (
  76. var text
  77. in session.RegenerateAssistantMessageAsync(
  78. inferenceParams))
  79. {
  80. Console.ForegroundColor = ConsoleColor.White;
  81. // Convert the encoding from utf8 to gb2312 for the console output.
  82. Console.Write(ConvertEncoding(text, Encoding.UTF8, Encoding.GetEncoding("gb2312")));
  83. }
  84. }
  85. else
  86. {
  87. await foreach (
  88. var text
  89. in session.ChatAsync(
  90. new ChatHistory.Message(AuthorRole.User, userInput),
  91. inferenceParams))
  92. {
  93. Console.ForegroundColor = ConsoleColor.White;
  94. Console.Write(text);
  95. }
  96. }
  97. Console.ForegroundColor = ConsoleColor.Green;
  98. userInput = Console.ReadLine() ?? "";
  99. Console.ForegroundColor = ConsoleColor.White;
  100. }
  101. }
  102. }