-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathchatllm.ts
237 lines (199 loc) · 5.82 KB
/
chatllm.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { dlopen, FFIType, suffix, JSCallback, ptr, CString } from "bun:ffi";
const path = `libchatllm.${suffix}`;
enum PrintType {
PRINT_CHAT_CHUNK = 0,
PRINTLN_META = 1, // print a whole line: general information
PRINTLN_ERROR = 2, // print a whole line: error message
PRINTLN_REF = 3, // print a whole line: reference
PRINTLN_REWRITTEN_QUERY = 4, // print a whole line: rewritten query
}
const {
symbols: {
chatllm_create,
chatllm_append_param,
chatllm_start,
chatllm_user_input,
chatllm_abort_generation,
chatllm_restart,
},
} = dlopen(
path,
{
chatllm_create: {
args: [],
returns: FFIType.ptr,
},
chatllm_append_param: {
args: [FFIType.ptr, FFIType.cstring],
},
chatllm_start: {
args: [FFIType.ptr, FFIType.function, FFIType.function, FFIType.ptr],
returns: FFIType.i32
},
chatllm_user_input: {
args: [FFIType.ptr, FFIType.cstring],
returns: FFIType.i32
},
chatllm_abort_generation: {
args: [FFIType.ptr]
},
chatllm_restart: {
args: [FFIType.ptr, FFIType.cstring]
},
},
);
class ChatLLMHandler {
references: string[]
constructor() {
this.references = []
}
print(s: string) {
}
print_error(s: string) {
throw s;
}
print_reference(s: string) {
this.references.push(s);
}
print_rewritten_query(s: string) { }
start () {
this.references = [];
}
end() { }
};
class ChatLLM {
obj: any;
callback_print: JSCallback;
callback_print_reference: JSCallback;
callback_print_rewritten_query: JSCallback;
callback_end: JSCallback;
handler: ChatLLMHandler;
constructor(params: string[], handler: ChatLLMHandler) {
this.handler = handler;
this.obj = chatllm_create();
this.callback_print = new JSCallback(
(p_obj, print_type, ptr) => {
let txt = new CString(ptr);
switch (print_type) {
case PrintType.PRINT_CHAT_CHUNK:
this.handler.print(txt);
break;
case PrintType.PRINTLN_META:
this.handler.print(txt + '\n');
break;
case PrintType.PRINTLN_REF:
this.handler.print_reference(txt);
break;
case PrintType.PRINTLN_REWRITTEN_QUERY:
this.handler.print_rewritten_query(txt);
break;
case PrintType.PRINTLN_ERROR:
this.handler.print_error(txt);
break;
default:
throw print_type;
}
},
{
args: ["ptr", "i32", "ptr"],
},
);
this.callback_end = new JSCallback(
(p_obj) => this.handler.end(),
{
args: ["ptr"],
},
);
if (params.length > 0) {
for (let param of params)
this.append_param(param);
this.start();
}
}
append_param(s) {
let str = Buffer.from(s + '\0', "utf8");
chatllm_append_param(this.obj, ptr(str));
}
start() {
let r = chatllm_start(this.obj, this.callback_print, this.callback_end, 0);
if (r != 0) {
throw `ChatLLM::start error code ${r}`;
}
}
chat(s: string) {
let str = Buffer.from(s + '\0', "utf8");
let r = chatllm_user_input(this.obj, ptr(str));
if (r != 0) {
throw `ChatLLM::chat error code ${r}`;
}
}
abort() {
chatllm_abort_generation(this.obj);
}
restart(sys_prompt: string | null) {
// TODO: NULL pointer
chatllm_restart(this.obj, sys_prompt != null ? ptr(sys_prompt) : ptr(0));
}
};
class WorkerHandler extends ChatLLMHandler {
id: string;
constructor(id: string) {
super();
this.id = id;
}
print(s: string) {
if (this.id == '') return;
postMessage({type: 'chunk', id: this.id, content: s});
}
print_rewritten_query(s: string) {
if (this.id == '') return;
postMessage({type: 'rewritten_query', id: this.id, content: s});
}
end() {
if (this.id == '') return;
if (this.references.length > 0)
postMessage({type: 'references', id: this.id, content: this.references});
postMessage({type: 'end', id: this.id});
}
};
class StdIOHandler extends ChatLLMHandler {
print(s: string) {
process.stdout.write(s);
}
print_rewritten_query(s: string) {
console.log(`Searching ${s} ...`)
}
end() {
if (this.references.length < 1) {
console.log('');
return;
}
console.log("\nReferences:")
for (let s of this.references)
console.log(s)
}
};
if (Bun.argv.slice(2).length > 0)
{
let llm = new ChatLLM(Bun.argv.slice(2), new StdIOHandler());
const prompt = 'You > ';
const AI = 'A.I. > ';
process.stdout.write(prompt);
for await (const line of console) {
process.stdout.write(AI);
llm.chat(line);
process.stdout.write(prompt);
}
}
let llm: ChatLLM | null = null;
// FIXME: this does not work
onmessage = function(msg) {
console.log('Worker: ', msg.data);
if (msg.data.id == '#start') {
llm = new ChatLLM(msg.data.argv, new WorkerHandler(''));
return;
}
if (llm == null) return;
llm.handler = new WorkerHandler(msg.data.id);
llm.chat(msg.data.user);
}