|
1 | 1 | import log from "loglevel"; |
2 | 2 | import { ChatOptions, LLMApi } from "./api"; |
3 | | -import { ChatCompletionFinishReason, CompletionUsage } from "@mlc-ai/web-llm"; |
| 3 | +import { |
| 4 | + ChatCompletionFinishReason, |
| 5 | + CompletionUsage, |
| 6 | + ChatCompletion, |
| 7 | +} from "@mlc-ai/web-llm"; |
4 | 8 |
|
5 | 9 | export class MlcLLMApi implements LLMApi { |
6 | 10 | private endpoint: string; |
@@ -45,22 +49,29 @@ export class MlcLLMApi implements LLMApi { |
45 | 49 | const chunk = new TextDecoder("utf-8").decode(value); |
46 | 50 | const result = chunk.match(/data: (.+)/); |
47 | 51 | if (result) { |
48 | | - const data = JSON.parse(result[1]); |
49 | | - if (data.choices && data.choices.length > 0) { |
50 | | - reply += data.choices[0].delta.content; // Append the content |
51 | | - options.onUpdate?.(reply, chunk); // Handle the chunk update |
| 52 | + try { |
| 53 | + const data = JSON.parse(result[1]); |
| 54 | + if (data.choices && data.choices.length > 0) { |
| 55 | + reply += data.choices[0].delta.content; // Append the content |
| 56 | + options.onUpdate?.(reply, chunk); // Handle the chunk update |
52 | 57 |
|
53 | | - if (data.choices[0].finish_reason) { |
54 | | - stopReason = data.choices[0].finish_reason; |
55 | | - } |
| 58 | + if (data.choices[0].finish_reason) { |
| 59 | + stopReason = data.choices[0].finish_reason; |
| 60 | + } |
56 | 61 |
|
57 | | - if (data.usage) { |
58 | | - usage = data.usage; |
| 62 | + if (data.usage) { |
| 63 | + usage = data.usage; |
| 64 | + } |
59 | 65 | } |
| 66 | + } catch (e) { |
| 67 | + log.error( |
| 68 | + "Error parsing streaming response from MLC-LLM server", |
| 69 | + e, |
| 70 | + ); |
60 | 71 | } |
61 | 72 | } |
62 | 73 |
|
63 | | - if (chunk === "[DONE]") { |
| 74 | + if (chunk.includes("[DONE]")) { |
64 | 75 | // Ending the stream when "[DONE]" is found |
65 | 76 | break; |
66 | 77 | } |
|
0 commit comments