From 982c0cd15674b415ae24536899b736f471ef9f84 Mon Sep 17 00:00:00 2001 From: "matbee.eth" Date: Mon, 15 Jan 2024 15:46:29 -0500 Subject: [PATCH] Fix logic error in streamed JSON object parsing (#752) buffer becomes any character after `\n`, as anything before `\n` should be considered a valid JSON object --- core/llm/llms/Ollama.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/llm/llms/Ollama.ts b/core/llm/llms/Ollama.ts index 32615e37f..a21a94483 100644 --- a/core/llm/llms/Ollama.ts +++ b/core/llm/llms/Ollama.ts @@ -123,6 +123,7 @@ class Ollama extends BaseLLM { buffer += value; // Split the buffer into individual JSON chunks const chunks = buffer.split("\n"); + for (let i = 0; i < chunks.length; i++) { const chunk = chunks[i]; if (chunk.trim() !== "") { @@ -161,6 +162,7 @@ class Ollama extends BaseLLM { buffer += value; // Split the buffer into individual JSON chunks const chunks = buffer.split("\n"); + buffer = chunks.pop() ?? ''; for (let i = 0; i < chunks.length; i++) { const chunk = chunks[i]; if (chunk.trim() !== "") { @@ -179,8 +181,6 @@ class Ollama extends BaseLLM { } } } - // Assign the last chunk to the buffer - buffer = chunks[chunks.length - 1]; } } }