-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import kotlinx.cinterop.* | ||
import libchatllm.* | ||
|
||
@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) | ||
fun chatllm_print(userData: COpaquePointer?, printType: UInt, utf8Str: CPointer<ByteVar>?) { | ||
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<String>) { | ||
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
headers = libchatllm.h | ||
compilerOpts = -I. | ||
linkerOpts = -L. -llibchatllm |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#ifndef LIBCHATLLM_H | ||
#define LIBCHATLLM_H | ||
|
||
#include <stdint.h> | ||
#include <stddef.h> | ||
|
||
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 |
Binary file not shown.