diff --git a/Main.kt b/Main.kt new file mode 100644 index 0000000..8260d92 --- /dev/null +++ b/Main.kt @@ -0,0 +1,46 @@ +import kotlinx.cinterop.* +import libchatllm.* + +@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) +fun chatllm_print(userData: COpaquePointer?, printType: UInt, utf8Str: CPointer?) { + when (printType) { + PRINT_CHAT_CHUNK -> print(utf8Str?.toKString()) + else -> println(utf8Str?.toKString()) + } +} + +@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) +fun chatllm_end(userData: COpaquePointer?) { + println() +} + +@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) +fun main(args: Array) { + val chat = chatllm_create() ?: error("Failed to create chatllm object") + + args.forEach { arg -> + chatllm_append_param(chat, arg) + } + + val printProc = staticCFunction(::chatllm_print) + val endProc = staticCFunction(::chatllm_end) + + val r = chatllm_start(chat, printProc, endProc, null) + if (r != 0) { + println(">>> chatllm_start error: $r") + return + } + + while (true) { + print("You > ") + val input = readlnOrNull() + if (input.isNullOrBlank()) continue + + print("A.I. > ") + val r = chatllm_user_input(chat, input) + if (r != 0) { + println(">>> chatllm_user_input error: $r") + break + } + } +} \ No newline at end of file diff --git a/ggml.dll b/ggml.dll new file mode 100644 index 0000000..995c79e Binary files /dev/null and b/ggml.dll differ diff --git a/libchatllm.def b/libchatllm.def new file mode 100644 index 0000000..6e04049 --- /dev/null +++ b/libchatllm.def @@ -0,0 +1,3 @@ +headers = libchatllm.h +compilerOpts = -I. +linkerOpts = -L. -llibchatllm \ No newline at end of file diff --git a/libchatllm.dll b/libchatllm.dll new file mode 100644 index 0000000..a177891 Binary files /dev/null and b/libchatllm.dll differ diff --git a/libchatllm.h b/libchatllm.h new file mode 100644 index 0000000..3375bb3 --- /dev/null +++ b/libchatllm.h @@ -0,0 +1,44 @@ +#ifndef LIBCHATLLM_H +#define LIBCHATLLM_H + +#include +#include + +typedef enum { + PRINT_CHAT_CHUNK = 0, + PRINTLN_META = 1, + PRINTLN_ERROR = 2, + PRINTLN_REF = 3, + PRINTLN_REWRITTEN_QUERY = 4, + PRINTLN_HISTORY_USER = 5, + PRINTLN_HISTORY_AI = 6, + PRINTLN_TOOL_CALLING = 7, + PRINTLN_EMBEDDING = 8, + PRINTLN_RANKING = 9, + PRINTLN_TOKEN_IDS = 10 +} PrintType; + +typedef struct ChatllmObj ChatllmObj; + +typedef void (*ChatllmPrintProc)(void* user_data, PrintType print_type, const char* utf8_str); +typedef void (*ChatllmEndProc)(void* user_data); + +ChatllmObj* chatllm_create(); +void chatllm_append_param(ChatllmObj* obj, const char* utf8_str); +int chatllm_start(ChatllmObj* obj, ChatllmPrintProc f_print, ChatllmEndProc f_end, void* user_data); +void chatllm_set_gen_max_tokens(ChatllmObj* obj, int gen_max_tokens); +void chatllm_restart(ChatllmObj* obj, const char* utf8_sys_prompt); +int chatllm_user_input(ChatllmObj* obj, const char* utf8_str); +int chatllm_set_ai_prefix(ChatllmObj* obj, const char* utf8_str); +int chatllm_tool_input(ChatllmObj* obj, const char* utf8_str); +int chatllm_tool_completion(ChatllmObj* obj, const char* utf8_str); +int chatllm_text_tokenize(ChatllmObj* obj, const char* utf8_str); +int chatllm_text_embedding(ChatllmObj* obj, const char* utf8_str); +int chatllm_qa_rank(ChatllmObj* obj, const char* utf8_str_q, const char* utf8_str_a); +int chatllm_rag_select_store(ChatllmObj* obj, const char* name); +void chatllm_abort_generation(ChatllmObj* obj); +void chatllm_show_statistics(ChatllmObj* obj); +int chatllm_save_session(ChatllmObj* obj, const char* utf8_str); +int chatllm_load_session(ChatllmObj* obj, const char* utf8_str); + +#endif // LIBCHATLLM_H \ No newline at end of file diff --git a/libchatllm.lib b/libchatllm.lib new file mode 100644 index 0000000..3295f3b Binary files /dev/null and b/libchatllm.lib differ