broken chunk handling improved see #147

This commit is contained in:
mic 2024-10-03 21:58:19 +02:00
parent 53e6a408ea
commit 70ea3c39bb
3 changed files with 34 additions and 21 deletions

View file

@ -68,7 +68,7 @@ self.onmessage = async function(event) {
const reader = response.body.getReader(); const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8"); const decoder = new TextDecoder("utf-8");
let chunk = ''; let buffer= '';
try { try {
while (true) { while (true) {
@ -89,12 +89,12 @@ self.onmessage = async function(event) {
break; break;
} }
// lots of low-level Ollama response parsing stuff // lots of low-level Ollama response parsing stuff
chunk += decoder.decode(value); const chunk = decoder.decode(value);
taLog.log("chunk: " + chunk); buffer += chunk;
const lines = chunk.split("\n"); taLog.log("buffer: " + buffer);
const lines = buffer.split("\n");
buffer = lines.pop();
let parsedLines = []; let parsedLines = [];
chunk = chunk.substring(chunk.lastIndexOf('\n') + 1);
taLog.log("last chunk: " + JSON.stringify(chunk));
try{ try{
parsedLines = lines parsedLines = lines
.map((line) => line.replace(/^chunk: /, "").trim()) // Remove the "chunk: " prefix .map((line) => line.replace(/^chunk: /, "").trim()) // Remove the "chunk: " prefix
@ -105,7 +105,7 @@ self.onmessage = async function(event) {
return JSON.parse(line); return JSON.parse(line);
}); });
}catch(e){ }catch(e){
taLog.warn("broken chunk: " + e); taLog.error("Error parsing lines: " + e);
} }
for (const parsedLine of parsedLines) { for (const parsedLine of parsedLines) {
@ -133,3 +133,16 @@ self.onmessage = async function(event) {
break; //stop break; //stop
} }
}; };
// Function to split a string in half
function splitStringInHalf(inputString) {
// Get the middle index of the string
const middleIndex = Math.ceil(inputString.length / 2);
// Split the string into two halves
const firstHalf = inputString.slice(0, middleIndex);
const secondHalf = inputString.slice(middleIndex);
return { firstHalf, secondHalf };
}

View file

@ -63,7 +63,7 @@ self.onmessage = async function(event) {
const reader = response.body.getReader(); const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8"); const decoder = new TextDecoder("utf-8");
let chunk = ''; let buffer = '';
while (true) { while (true) {
if (stopStreaming) { if (stopStreaming) {
@ -82,12 +82,12 @@ self.onmessage = async function(event) {
break; break;
} }
// lots of low-level OpenAI response parsing stuff // lots of low-level OpenAI response parsing stuff
chunk += decoder.decode(value); const chunk = decoder.decode(value);
taLog.log("chunk: " + JSON.stringify(chunk)); buffer += chunk;
const lines = chunk.split("\n"); taLog.log("buffer " + buffer);
const lines = buffer.split("\n");
buffer = lines.pop();
let parsedLines = []; let parsedLines = [];
chunk = chunk.substring(chunk.lastIndexOf('\n') + 1);
taLog.log("last chunk: " + JSON.stringify(chunk));
try{ try{
parsedLines = lines parsedLines = lines
.map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix .map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix
@ -98,7 +98,7 @@ self.onmessage = async function(event) {
return JSON.parse(line); return JSON.parse(line);
}); });
}catch(e){ }catch(e){
taLog.warn("broken chunk: " + e); taLog.error("Error parsing lines: " + e);
} }
for (const parsedLine of parsedLines) { for (const parsedLine of parsedLines) {

View file

@ -67,7 +67,7 @@ self.onmessage = async function(event) {
const reader = response.body.getReader(); const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8"); const decoder = new TextDecoder("utf-8");
let chunk = ''; let buffer = '';
while (true) { while (true) {
if (stopStreaming) { if (stopStreaming) {
@ -86,12 +86,12 @@ self.onmessage = async function(event) {
break; break;
} }
// lots of low-level OpenAI response parsing stuff // lots of low-level OpenAI response parsing stuff
chunk += decoder.decode(value); const chunk = decoder.decode(value);
taLog.log("chunk: " + JSON.stringify(chunk)); buffer += chunk;
const lines = chunk.split("\n"); taLog.log("buffer: " + buffer);
const lines = buffer.split("\n");
buffer = lines.pop();
let parsedLines = []; let parsedLines = [];
chunk = chunk.substring(chunk.lastIndexOf('\n') + 1);
taLog.log("last chunk: " + JSON.stringify(chunk));
try{ try{
parsedLines = lines parsedLines = lines
.map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix .map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix
@ -102,7 +102,7 @@ self.onmessage = async function(event) {
return JSON.parse(line); return JSON.parse(line);
}); });
}catch(e){ }catch(e){
taLog.warn("broken chunk: " + e); taLog.error("Error parsing lines: " + e);
} }
for (const parsedLine of parsedLines) { for (const parsedLine of parsedLines) {