Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fix: Json Decoder aggessively pulls json #867

Merged
merged 5 commits into from Mar 5, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 47 additions & 26 deletions src/lib/server/endpoints/llamacpp/endpointLlamacpp.ts
Expand Up @@ -55,10 +55,13 @@ export function endpointLlamacpp(
let stop = false;
let generatedText = "";
let tokenId = 0;
let accumulatedData = ""; // Buffer to accumulate data chunks

while (!stop) {
// read the stream and log the outputs to console
// Read the stream and log the outputs to console
const out = (await reader?.read()) ?? { done: false, value: undefined };
// we read, if it's done we cancel

// If it's done, we cancel
if (out.done) {
reader?.cancel();
return;
Expand All @@ -68,31 +71,49 @@ export function endpointLlamacpp(
return;
}

if (out.value.startsWith("data: ")) {
let data = null;
try {
data = JSON.parse(out.value.slice(6));
} catch (e) {
return;
}
if (data.content || data.stop) {
generatedText += data.content;
const output: TextGenerationStreamOutput = {
token: {
id: tokenId++,
text: data.content ?? "",
logprob: 0,
special: false,
},
generated_text: data.stop ? generatedText : null,
details: null,
};
if (data.stop) {
stop = true;
reader?.cancel();
// Accumulate the data chunk
accumulatedData += out.value;

// Process each complete JSON object in the accumulated data
while (accumulatedData.includes("\n")) {
// Assuming each JSON object ends with a newline
const endIndex = accumulatedData.indexOf("\n");
let jsonString = accumulatedData.substring(0, endIndex).trim();

// Remove the processed part from the buffer
accumulatedData = accumulatedData.substring(endIndex + 1);

if (jsonString.startsWith("data: ")) {
jsonString = jsonString.slice(6);
let data = null;

try {
data = JSON.parse(jsonString);
} catch (e) {
console.error("Failed to parse JSON", e);
console.error("Problematic JSON string:", jsonString);
continue; // Skip this iteration and try the next chunk
}

// Handle the parsed data
if (data.content || data.stop) {
generatedText += data.content;
const output: TextGenerationStreamOutput = {
token: {
id: tokenId++,
text: data.content ?? "",
logprob: 0,
special: false,
},
generated_text: data.stop ? generatedText : null,
details: null,
};
if (data.stop) {
stop = true;
reader?.cancel();
}
yield output;
}
yield output;
// take the data.content value and yield it
}
}
}
Expand Down