Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnClaw authored Nov 25, 2024
1 parent 960bac6 commit fee81b4
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Main.kt
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
}
}
}
Binary file added ggml.dll
Binary file not shown.
3 changes: 3 additions & 0 deletions libchatllm.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
headers = libchatllm.h
compilerOpts = -I.
linkerOpts = -L. -llibchatllm
Binary file added libchatllm.dll
Binary file not shown.
44 changes: 44 additions & 0 deletions libchatllm.h
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 added libchatllm.lib
Binary file not shown.

0 comments on commit fee81b4

Please sign in to comment.