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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. 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.White;
  58. Console.Write("用户:");
  59. Console.ForegroundColor = ConsoleColor.Green;
  60. string userInput = Console.ReadLine() ?? "";
  61. while (userInput != "exit")
  62. {
  63. // Convert the encoding from gb2312 to utf8 for the language model
  64. // and later saving to the history json file.
  65. userInput = ConvertEncoding(userInput, Encoding.GetEncoding("gb2312"), Encoding.UTF8);
  66. if (userInput == "save")
  67. {
  68. session.SaveSession("Assets/chat-with-kunkun-chinese");
  69. Console.ForegroundColor = ConsoleColor.Yellow;
  70. Console.WriteLine("Session saved.");
  71. }
  72. else if (userInput == "regenerate")
  73. {
  74. Console.ForegroundColor = ConsoleColor.Yellow;
  75. Console.WriteLine("Regenerating last response ...");
  76. await foreach (
  77. var text
  78. in session.RegenerateAssistantMessageAsync(
  79. inferenceParams))
  80. {
  81. Console.ForegroundColor = ConsoleColor.White;
  82. // Convert the encoding from utf8 to gb2312 for the console output.
  83. Console.Write(ConvertEncoding(text, Encoding.UTF8, Encoding.GetEncoding("gb2312")));
  84. }
  85. }
  86. else
  87. {
  88. await foreach (
  89. var text
  90. in session.ChatAsync(
  91. new ChatHistory.Message(AuthorRole.User, userInput),
  92. inferenceParams))
  93. {
  94. Console.ForegroundColor = ConsoleColor.White;
  95. Console.Write(text);
  96. }
  97. }
  98. Console.ForegroundColor = ConsoleColor.Green;
  99. userInput = Console.ReadLine() ?? "";
  100. Console.ForegroundColor = ConsoleColor.White;
  101. }
  102. }
  103. }