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
This commit is contained in:
matbee.eth 2024-01-15 15:46:29 -05:00 • committed by GitHub
parent 5b56150a8d
commit 982c0cd156
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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];
}
}
}