|
| 1 | +using System; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | + |
| 4 | +public class ChatLLM |
| 5 | +{ |
| 6 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 7 | + private static extern IntPtr chatllm_create(); |
| 8 | + |
| 9 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 10 | + private static extern void chatllm_append_param(IntPtr obj, string param); |
| 11 | + |
| 12 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 13 | + private static extern int chatllm_start(IntPtr obj, PrintCallback printCallback, EndCallback endCallback, IntPtr userData); |
| 14 | + |
| 15 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 16 | + private static extern void chatllm_set_gen_max_tokens(IntPtr obj, int genMaxTokens); |
| 17 | + |
| 18 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 19 | + private static extern void chatllm_restart(IntPtr obj, string sysPrompt); |
| 20 | + |
| 21 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 22 | + private static extern int chatllm_user_input(IntPtr obj, string input); |
| 23 | + |
| 24 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 25 | + private static extern int chatllm_set_ai_prefix(IntPtr obj, string prefix); |
| 26 | + |
| 27 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 28 | + private static extern int chatllm_tool_input(IntPtr obj, string input); |
| 29 | + |
| 30 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 31 | + private static extern int chatllm_tool_completion(IntPtr obj, string completion); |
| 32 | + |
| 33 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 34 | + private static extern int chatllm_text_tokenize(IntPtr obj, string text); |
| 35 | + |
| 36 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 37 | + private static extern int chatllm_text_embedding(IntPtr obj, string text); |
| 38 | + |
| 39 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 40 | + private static extern int chatllm_qa_rank(IntPtr obj, string question, string answer); |
| 41 | + |
| 42 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 43 | + private static extern int chatllm_rag_select_store(IntPtr obj, string storeName); |
| 44 | + |
| 45 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 46 | + private static extern void chatllm_abort_generation(IntPtr obj); |
| 47 | + |
| 48 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 49 | + private static extern void chatllm_show_statistics(IntPtr obj); |
| 50 | + |
| 51 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 52 | + private static extern int chatllm_save_session(IntPtr obj, string fileName); |
| 53 | + |
| 54 | + [DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)] |
| 55 | + private static extern int chatllm_load_session(IntPtr obj, string fileName); |
| 56 | + |
| 57 | + [UnmanagedFunctionPointer(CallingConvention.StdCall)] |
| 58 | + private delegate void PrintCallback(IntPtr userData, int printType, string utf8Str); |
| 59 | + |
| 60 | + [UnmanagedFunctionPointer(CallingConvention.StdCall)] |
| 61 | + private delegate void EndCallback(IntPtr userData); |
| 62 | + |
| 63 | + private enum PrintType |
| 64 | + { |
| 65 | + PRINT_CHAT_CHUNK = 0, |
| 66 | + PRINTLN_META = 1, |
| 67 | + PRINTLN_ERROR = 2, |
| 68 | + PRINTLN_REF = 3, |
| 69 | + PRINTLN_REWRITTEN_QUERY = 4, |
| 70 | + PRINTLN_HISTORY_USER = 5, |
| 71 | + PRINTLN_HISTORY_AI = 6, |
| 72 | + PRINTLN_TOOL_CALLING = 7, |
| 73 | + PRINTLN_EMBEDDING = 8, |
| 74 | + PRINTLN_RANKING = 9, |
| 75 | + PRINTLN_TOKEN_IDS = 10, |
| 76 | + } |
| 77 | + |
| 78 | + private static void ChatLLMPrint(IntPtr userData, int printType, string utf8Str) |
| 79 | + { |
| 80 | + switch (printType) |
| 81 | + { |
| 82 | + case (int)PrintType.PRINT_CHAT_CHUNK: |
| 83 | + Console.Write(utf8Str); |
| 84 | + break; |
| 85 | + default: |
| 86 | + Console.WriteLine(utf8Str); |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static void ChatLLMEnd(IntPtr userData) |
| 92 | + { |
| 93 | + Console.WriteLine(); |
| 94 | + } |
| 95 | + |
| 96 | + public static void Main(string[] args) |
| 97 | + { |
| 98 | + IntPtr obj = chatllm_create(); |
| 99 | + foreach (string arg in args) |
| 100 | + { |
| 101 | + chatllm_append_param(obj, arg); |
| 102 | + } |
| 103 | + |
| 104 | + int r = chatllm_start(obj, ChatLLMPrint, ChatLLMEnd, IntPtr.Zero); |
| 105 | + if (r != 0) |
| 106 | + { |
| 107 | + Console.WriteLine(">>> chatllm_start error: " + r); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + while (true) |
| 112 | + { |
| 113 | + Console.Write("You > "); |
| 114 | + string input = Console.ReadLine(); |
| 115 | + if (string.IsNullOrEmpty(input)) continue; |
| 116 | + |
| 117 | + Console.Write("A.I. > "); |
| 118 | + r = chatllm_user_input(obj, input); |
| 119 | + if (r != 0) |
| 120 | + { |
| 121 | + Console.WriteLine(">>> chatllm_user_input error: " + r); |
| 122 | + break; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments