Skip to content

Commit

Permalink
fix: JSON parsing error of streaming "[DONE]" message
Browse files Browse the repository at this point in the history
  • Loading branch information
Neet-Nestor committed Jan 29, 2025
1 parent a9493fa commit d52d954
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions app/client/mlcllm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import log from "loglevel";
import { ChatOptions, LLMApi } from "./api";
import { ChatCompletionFinishReason, CompletionUsage } from "@mlc-ai/web-llm";
import {
ChatCompletionFinishReason,
CompletionUsage,
ChatCompletion,
} from "@mlc-ai/web-llm";

export class MlcLLMApi implements LLMApi {
private endpoint: string;
Expand Down Expand Up @@ -45,22 +49,29 @@ export class MlcLLMApi implements LLMApi {
const chunk = new TextDecoder("utf-8").decode(value);
const result = chunk.match(/data: (.+)/);
if (result) {
const data = JSON.parse(result[1]);
if (data.choices && data.choices.length > 0) {
reply += data.choices[0].delta.content; // Append the content
options.onUpdate?.(reply, chunk); // Handle the chunk update
try {
const data = JSON.parse(result[1]);
if (data.choices && data.choices.length > 0) {
reply += data.choices[0].delta.content; // Append the content
options.onUpdate?.(reply, chunk); // Handle the chunk update

if (data.choices[0].finish_reason) {
stopReason = data.choices[0].finish_reason;
}
if (data.choices[0].finish_reason) {
stopReason = data.choices[0].finish_reason;
}

if (data.usage) {
usage = data.usage;
if (data.usage) {
usage = data.usage;
}
}
} catch (e) {
log.error(
"Error parsing streaming response from MLC-LLM server",
e,
);
}
}

if (chunk === "[DONE]") {
if (chunk.includes("[DONE]")) {
// Ending the stream when "[DONE]" is found
break;
}
Expand Down

0 comments on commit d52d954

Please sign in to comment.