-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.ts
79 lines (57 loc) · 1.54 KB
/
chat.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
import { parse as parseToml } from "https://deno.land/[email protected]/toml/parse.ts";
import { Buffer } from "node:buffer";
const tomlFile = '/Users/goranslettemark/.tabby-client/agent/config.toml';
const question = Deno.args[0];
console.log('Q:', question);
const {
server: {
endpoint,
token
}
} = await Deno.readTextFile(tomlFile).then(
(content) => parseToml(content)
);
const headers = new Headers();
headers.set('Authorization', `Bearer ${token}`);
const healthCheck = await fetch(
endpoint + '/v1/health',
{ headers }
).then(res => res.json());
if (healthCheck){
headers.set('Content-Type', 'application/json');
headers.set('Accept', 'text/event-stream');
const reply = await fetch(
endpoint + '/v1/chat/completions',
{
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'Accept': 'text/event-stream'
},
body: JSON.stringify({
messages: [
{
content: question,
role: "user"
}
]
})
}
);
if (reply.body){
let runningLine = 'A: ';
for await(const chunk of reply.body){
// read chunk as json
const buf = new Buffer(chunk).toString('utf-8');
const json = JSON.parse(buf.replace('data: ', ''));
const latestWord = json.choices[0].delta.content;
runningLine += latestWord;
if (runningLine.length > 80){
console.log(runningLine);
runningLine = '';
}
}
console.log(runningLine);
}
}