chat event stream behaviour updated

This commit is contained in:
Timothy J. Baek 2023-10-08 19:42:54 -07:00
parent 568ce38fa1
commit 83ae37f154
1 changed files with 50 additions and 17 deletions

View File

@ -18,19 +18,41 @@
let textareaElement = ''; let textareaElement = '';
const splitStream = (splitOn) => {
let buffer = '';
return new TransformStream({
transform(chunk, controller) {
buffer += chunk;
const parts = buffer.split(splitOn);
parts.slice(0, -1).forEach((part) => controller.enqueue(part));
buffer = parts[parts.length - 1];
},
flush(controller) {
if (buffer) controller.enqueue(buffer);
}
});
};
const submitPrompt = async () => { const submitPrompt = async () => {
console.log('submitPrompt'); console.log('submitPrompt');
if (selectedModel !== '') { if (selectedModel !== '') {
console.log(prompt); console.log(prompt);
let user_prompt = prompt; let user_prompt = prompt;
chatHistory[Object.keys(chatHistory).length] = { chatHistory[Object.keys(chatHistory).length] = {
role: 'user', role: 'user',
content: user_prompt content: user_prompt
}; };
prompt = ''; prompt = '';
textareaElement.style.height = ''; textareaElement.style.height = '';
setTimeout(() => {
window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
}, 50);
const res = await fetch(`${ENDPOINT}/api/generate`, { const res = await fetch(`${ENDPOINT}/api/generate`, {
method: 'POST', method: 'POST',
headers: { headers: {
@ -47,36 +69,47 @@
role: 'assistant', role: 'assistant',
content: '' content: ''
}; };
window.scrollTo({ top: document.body.scrollHeight });
const reader = res.body.pipeThrough(new TextDecoderStream()).getReader(); const reader = res.body
.pipeThrough(new TextDecoderStream())
.pipeThrough(splitStream('\n'))
.getReader();
while (true) { while (true) {
const { value, done } = await reader.read(); const { value, done } = await reader.read();
if (done) break; if (done) break;
// toast.success(value);
try { try {
let data = JSON.parse(value); let lines = value.split('\n');
console.log(data);
if (data.done == false) { for (const line of lines) {
if ( if (line !== '') {
chatHistory[Object.keys(chatHistory).length - 1].content == '' && console.log(line);
data.response == '\n' let data = JSON.parse(line);
) {
continue; if (data.done == false) {
} else { if (
chatHistory[Object.keys(chatHistory).length - 1].content += data.response; chatHistory[Object.keys(chatHistory).length - 1].content == '' &&
data.response == '\n'
) {
continue;
} else {
chatHistory[Object.keys(chatHistory).length - 1].content += data.response;
}
} else {
context = data.context;
console.log(context);
chatHistory[Object.keys(chatHistory).length - 1].done = true;
}
} }
} else {
context = data.context;
console.log(context);
chatHistory[Object.keys(chatHistory).length - 1].done = true;
} }
} catch (error) { } catch (error) {
console.log(error); console.log(error);
} }
window.scrollTo(0, document.body.scrollHeight); window.scrollTo({ top: document.body.scrollHeight });
} }
window.scrollTo({ top: document.body.scrollHeight });
} else { } else {
toast.error('Model not selected'); toast.error('Model not selected');
} }