mirror of
https://github.com/mudler/LocalAI.git
synced 2025-06-01 00:14:59 +00:00
fix: fix chat webui response parsing (#2515)
fix: fix chat webui Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
This commit is contained in:
parent
d38e9090df
commit
0d62594099
2 changed files with 83 additions and 39 deletions
|
@ -176,29 +176,73 @@ function readInputImage() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to add content to the chat and handle DOM updates efficiently
|
||||||
|
const addToChat = (token) => {
|
||||||
|
const chatStore = Alpine.store("chat");
|
||||||
|
chatStore.add("assistant", token);
|
||||||
|
// Efficiently scroll into view without triggering multiple reflows
|
||||||
|
const messages = document.getElementById('messages');
|
||||||
|
messages.scrollTop = messages.scrollHeight;
|
||||||
|
};
|
||||||
|
|
||||||
|
let buffer = "";
|
||||||
|
let contentBuffer = [];
|
||||||
|
|
||||||
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
const { value, done } = await reader.read();
|
const { value, done } = await reader.read();
|
||||||
if (done) break;
|
if (done) break;
|
||||||
let dataDone = false;
|
|
||||||
const arr = value.split("\n");
|
buffer += value;
|
||||||
arr.forEach((data) => {
|
|
||||||
if (data.length === 0) return;
|
let lines = buffer.split("\n");
|
||||||
if (data.startsWith(":")) return;
|
buffer = lines.pop(); // Retain any incomplete line in the buffer
|
||||||
if (data === "data: [DONE]") {
|
|
||||||
dataDone = true;
|
lines.forEach((line) => {
|
||||||
|
if (line.length === 0 || line.startsWith(":")) return;
|
||||||
|
if (line === "data: [DONE]") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const token = JSON.parse(data.substring(6)).choices[0].delta.content;
|
|
||||||
if (!token) {
|
if (line.startsWith("data: ")) {
|
||||||
return;
|
try {
|
||||||
|
const jsonData = JSON.parse(line.substring(6));
|
||||||
|
const token = jsonData.choices[0].delta.content;
|
||||||
|
|
||||||
|
if (token) {
|
||||||
|
contentBuffer.push(token);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to parse line:", line, error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
hljs.highlightAll();
|
|
||||||
Alpine.store("chat").add("assistant", token);
|
|
||||||
document.getElementById('messages').scrollIntoView(false)
|
|
||||||
});
|
});
|
||||||
hljs.highlightAll();
|
|
||||||
if (dataDone) break;
|
// Efficiently update the chat in batch
|
||||||
|
if (contentBuffer.length > 0) {
|
||||||
|
addToChat(contentBuffer.join(""));
|
||||||
|
contentBuffer = [];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Final content flush if any data remains
|
||||||
|
if (contentBuffer.length > 0) {
|
||||||
|
addToChat(contentBuffer.join(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Highlight all code blocks once at the end
|
||||||
|
hljs.highlightAll();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("An error occurred while reading the stream:", error);
|
||||||
|
Alpine.store("chat").add(
|
||||||
|
"assistant",
|
||||||
|
`<span class='error'>Error: Failed to process stream</span>`,
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
// Perform any cleanup if necessary
|
||||||
|
reader.releaseLock();
|
||||||
|
}
|
||||||
|
|
||||||
// Remove class "loader" from the element with "loader" id
|
// Remove class "loader" from the element with "loader" id
|
||||||
//document.getElementById("loader").classList.remove("loader");
|
//document.getElementById("loader").classList.remove("loader");
|
||||||
document.getElementById("loader").style.display = "none";
|
document.getElementById("loader").style.display = "none";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue