From 70ea3c39bb4e97f167ea680d85faba73905cabc0 Mon Sep 17 00:00:00 2001 From: mic Date: Thu, 3 Oct 2024 21:58:19 +0200 Subject: [PATCH] broken chunk handling improved see #147 --- api_webchat/model-worker-ollama.js | 27 ++++++++++++++++++------- api_webchat/model-worker-openai.js | 14 ++++++------- api_webchat/model-worker-openai_comp.js | 14 ++++++------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/api_webchat/model-worker-ollama.js b/api_webchat/model-worker-ollama.js index 67be969b..eea65d6d 100644 --- a/api_webchat/model-worker-ollama.js +++ b/api_webchat/model-worker-ollama.js @@ -68,7 +68,7 @@ self.onmessage = async function(event) { const reader = response.body.getReader(); const decoder = new TextDecoder("utf-8"); - let chunk = ''; + let buffer= ''; try { while (true) { @@ -89,12 +89,12 @@ self.onmessage = async function(event) { break; } // lots of low-level Ollama response parsing stuff - chunk += decoder.decode(value); - taLog.log("chunk: " + chunk); - const lines = chunk.split("\n"); + const chunk = decoder.decode(value); + buffer += chunk; + taLog.log("buffer: " + buffer); + const lines = buffer.split("\n"); + buffer = lines.pop(); let parsedLines = []; - chunk = chunk.substring(chunk.lastIndexOf('\n') + 1); - taLog.log("last chunk: " + JSON.stringify(chunk)); try{ parsedLines = lines .map((line) => line.replace(/^chunk: /, "").trim()) // Remove the "chunk: " prefix @@ -105,7 +105,7 @@ self.onmessage = async function(event) { return JSON.parse(line); }); }catch(e){ - taLog.warn("broken chunk: " + e); + taLog.error("Error parsing lines: " + e); } for (const parsedLine of parsedLines) { @@ -133,3 +133,16 @@ self.onmessage = async function(event) { 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 }; + } \ No newline at end of file diff --git a/api_webchat/model-worker-openai.js b/api_webchat/model-worker-openai.js index c03c426f..c23c0436 100644 --- a/api_webchat/model-worker-openai.js +++ b/api_webchat/model-worker-openai.js @@ -63,7 +63,7 @@ self.onmessage = async function(event) { const reader = response.body.getReader(); const decoder = new TextDecoder("utf-8"); - let chunk = ''; + let buffer = ''; while (true) { if (stopStreaming) { @@ -82,12 +82,12 @@ self.onmessage = async function(event) { break; } // lots of low-level OpenAI response parsing stuff - chunk += decoder.decode(value); - taLog.log("chunk: " + JSON.stringify(chunk)); - const lines = chunk.split("\n"); + const chunk = decoder.decode(value); + buffer += chunk; + taLog.log("buffer " + buffer); + const lines = buffer.split("\n"); + buffer = lines.pop(); let parsedLines = []; - chunk = chunk.substring(chunk.lastIndexOf('\n') + 1); - taLog.log("last chunk: " + JSON.stringify(chunk)); try{ parsedLines = lines .map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix @@ -98,7 +98,7 @@ self.onmessage = async function(event) { return JSON.parse(line); }); }catch(e){ - taLog.warn("broken chunk: " + e); + taLog.error("Error parsing lines: " + e); } for (const parsedLine of parsedLines) { diff --git a/api_webchat/model-worker-openai_comp.js b/api_webchat/model-worker-openai_comp.js index bcb0b2ca..5af68da8 100644 --- a/api_webchat/model-worker-openai_comp.js +++ b/api_webchat/model-worker-openai_comp.js @@ -67,7 +67,7 @@ self.onmessage = async function(event) { const reader = response.body.getReader(); const decoder = new TextDecoder("utf-8"); - let chunk = ''; + let buffer = ''; while (true) { if (stopStreaming) { @@ -86,12 +86,12 @@ self.onmessage = async function(event) { break; } // lots of low-level OpenAI response parsing stuff - chunk += decoder.decode(value); - taLog.log("chunk: " + JSON.stringify(chunk)); - const lines = chunk.split("\n"); + const chunk = decoder.decode(value); + buffer += chunk; + taLog.log("buffer: " + buffer); + const lines = buffer.split("\n"); + buffer = lines.pop(); let parsedLines = []; - chunk = chunk.substring(chunk.lastIndexOf('\n') + 1); - taLog.log("last chunk: " + JSON.stringify(chunk)); try{ parsedLines = lines .map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix @@ -102,7 +102,7 @@ self.onmessage = async function(event) { return JSON.parse(line); }); }catch(e){ - taLog.warn("broken chunk: " + e); + taLog.error("Error parsing lines: " + e); } for (const parsedLine of parsedLines) {