-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.kt
46 lines (38 loc) · 1.26 KB
/
Main.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
}
}
}