diff --git a/LLama.Examples/Assets/chat-with-kunkun-chinese.txt b/LLama.Examples/Assets/chat-with-kunkun-chinese.txt new file mode 100644 index 000000000..295e24d54 --- /dev/null +++ b/LLama.Examples/Assets/chat-with-kunkun-chinese.txt @@ -0,0 +1,8 @@ +ָ�������һ������û��ĶԻ��������������һ���ڸ����涼ӵ�зḻ�������������dz����ڻش��û�������Ͱ����û��� + +�Ñ�����ã������� +��������ã���ʲô���ܰ�������� +�Ñ����й����׶����������У� +�������й����׶��DZ����С� +�Ñ�����������˭�� +������ \ No newline at end of file diff --git a/LLama.Examples/Examples/ChatChineseGB2312.cs b/LLama.Examples/Examples/ChatChineseGB2312.cs new file mode 100644 index 000000000..ff27b9621 --- /dev/null +++ b/LLama.Examples/Examples/ChatChineseGB2312.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using LLama.Common; + +namespace LLama.Examples.Examples +{ + public class ChatChineseGB2312 + { + private static string ConvertFromEncodingToAnother(string input, Encoding original, Encoding target) + { + byte[] bytes = original.GetBytes(input); + var convertedBytes = Encoding.Convert(original, target, bytes); + return target.GetString(convertedBytes); + } + + public static async Task Run() + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); // Register gb2312 encoding + Console.Write("Please input your model path: "); + var modelPath = Console.ReadLine(); + var prompt = File.ReadAllText("Assets/chat-with-kunkun-chinese.txt", encoding: Encoding.GetEncoding("gb2312")).Trim(); + prompt = ConvertFromEncodingToAnother(prompt, Encoding.GetEncoding("gb2312"), Encoding.UTF8); + + var parameters = new ModelParams(modelPath) + { + ContextSize = 1024, + Seed = 1337, + GpuLayerCount = 20, + Encoding = Encoding.UTF8 + }; + using var model = LLamaWeights.LoadFromFile(parameters); + using var context = model.CreateContext(parameters); + var executor = new InteractiveExecutor(context); + + var session = new ChatSession(executor).WithHistoryTransform(new LLamaTransforms.DefaultHistoryTransform("用户")); + + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("This example shows how to use Chinese with gb2312 encoding, which is common in windows. It's recommended" + + " 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."); + Console.ForegroundColor = ConsoleColor.White; + + // show the prompt + Console.Write(prompt); + while (true) + { + await foreach (var text in session.ChatAsync(prompt, new InferenceParams() + { + Temperature = 0.3f, + TopK = 5, + TopP = 0.85f, + AntiPrompts = new List { "用户:" }, + MaxTokens = 2048, + RepeatPenalty = 1.05f + })) + { + //Console.Write(text); + Console.Write(ConvertFromEncodingToAnother(text, Encoding.UTF8, Encoding.GetEncoding("gb2312"))); + } + + Console.ForegroundColor = ConsoleColor.Green; + prompt = Console.ReadLine(); + Console.ForegroundColor = ConsoleColor.White; + } + } + } +} diff --git a/LLama.Examples/Examples/Runner.cs b/LLama.Examples/Examples/Runner.cs index aca0a7daa..0ccce20e5 100644 --- a/LLama.Examples/Examples/Runner.cs +++ b/LLama.Examples/Examples/Runner.cs @@ -23,6 +23,7 @@ public class Runner { "Coding Assistant.", CodingAssistant.Run }, { "Batch Decoding.", BatchedDecoding.Run }, { "SK Kernel Memory.", KernelMemory.Run }, + { "Chinese gb2312 chat", ChatChineseGB2312.Run }, { "Exit", async () => Environment.Exit(0) } }; diff --git a/LLama.Examples/LLama.Examples.csproj b/LLama.Examples/LLama.Examples.csproj index b73691727..87b57dcda 100644 --- a/LLama.Examples/LLama.Examples.csproj +++ b/LLama.Examples/LLama.Examples.csproj @@ -1,4 +1,4 @@ - + Exe @@ -68,6 +68,9 @@ PreserveNewest + + PreserveNewest + diff --git a/LLama/Native/NativeApi.cs b/LLama/Native/NativeApi.cs index a4f97a004..ca6027fc2 100644 --- a/LLama/Native/NativeApi.cs +++ b/LLama/Native/NativeApi.cs @@ -9,6 +9,17 @@ namespace LLama.Native { using llama_token = Int32; + public enum LLamaTokenType + { + LLAMA_TOKEN_TYPE_UNDEFINED = 0, + LLAMA_TOKEN_TYPE_NORMAL = 1, + LLAMA_TOKEN_TYPE_UNKNOWN = 2, + LLAMA_TOKEN_TYPE_CONTROL = 3, + LLAMA_TOKEN_TYPE_USER_DEFINED = 4, + LLAMA_TOKEN_TYPE_UNUSED = 5, + LLAMA_TOKEN_TYPE_BYTE = 6, + } + /// /// Callback from llama.cpp with log messages /// @@ -243,6 +254,9 @@ public static int llama_tokenize(SafeLLamaContextHandle ctx, string text, Encodi } } + [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)] + public static extern LLamaTokenType llama_token_get_type(SafeLlamaModelHandle model, llama_token token); + /// /// Get the size of the context window for the model for this context ///