-
Notifications
You must be signed in to change notification settings - Fork 165
/
streaming-chat.ts
72 lines (62 loc) · 2.32 KB
/
streaming-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
/**
* Takes an attribute called prompt and sends it to the LLM API
* Waits for the results to stream in and prints them in real time.
*/
export class StreamingChat extends HTMLElement {
constructor() {
super()
const prompt = this.attributes.getNamedItem('prompt')
const chatId = this.attributes.getNamedItem('chat-id')
if(prompt && prompt.value && chatId && chatId.value) {
this.streamResult(prompt.value, chatId.value)
}
}
async streamResult(prompt: string, chatId: string) {
const response = await fetch('/v1/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'ggml-gpt4all-j',
prompt: prompt,
stream: true,
}),
});
const reader = response.body?.pipeThrough(new TextDecoderStream()).getReader();
var result = '';
while (true && reader) {
// eslint-disable-next-line no-await-in-loop
const { value, done } = await reader.read();
if (done) break;
let dataDone = false;
const arr = value.split('\n');
arr.forEach((data) => {
if (data.length === 0) return; // ignore empty message
if (data.startsWith(':')) return; // ignore sse comment message
if (data.substring(6) === '[DONE]') {
dataDone = true;
} else {
const json = JSON.parse(data.substring(6));
if(json.choices[0].text) {
result += json.choices[0].text
this.innerHTML = `${result}`;
}
}
});
if (dataDone) break;
}
// Save the results
const form = document.getElementById(`chat-form-${chatId}`)
const llmResult = document.getElementById(`chat-result-${chatId}`)
if(form instanceof HTMLFormElement && llmResult instanceof HTMLInputElement) {
llmResult.value = result
form.submit()
}
}
}
document.addEventListener('readystatechange', () => {
if (document.readyState == 'complete') {
customElements.define('streaming-chat', StreamingChat)
}
})