-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
52 lines (46 loc) · 1.17 KB
/
index.js
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
const chatglm = require('bindings')('chatglmjs');
const fs = require('fs');
const createChatFn = (method, defered) => function chatSync({
model_bin_path,
prompt,
temperature = 0.95,
top_p = 0.7,
top_k = 0,
onmessage,
onend,
onerror,
} = {}) {
const warn = (err) => onerror ? onerror(err) : console.error(err);
if (!fs.existsSync(model_bin_path)) {
warn(`model_bin_path: ${model_bin_path} is not existing.`);
return;
}
if (!prompt?.trim()) {
warn('prompt should not be empty.');
return;
}
let resolve, reject, message = '';
const promise = new Promise((r, j) => {
resolve = r;
reject = j;
});
const callback = (type, msg) => {
if (type === 'data') {
onmessage?.(msg);
message += msg;
}
else if (type === 'end') {
onend?.();
resolve(message);
}
else if (type === 'error') {
onerror?.(msg);
reject(new Error(msg));
}
};
const output = chatglm[method](callback, model_bin_path, prompt, temperature, top_p, top_k);
return defered ? promise : output;
};
const chat = createChatFn('chat', true);
const chatSync = createChatFn('chatSync');
module.exports = { chat, chatSync };