Skip to content

Commit 87bc89e

Browse files
committed
feat: collect usage for mlcllm response
1 parent 493503e commit 87bc89e

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

app/client/mlcllm.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import log from "loglevel";
22
import { ChatOptions, LLMApi } from "./api";
3-
import { ChatCompletionFinishReason } from "@mlc-ai/web-llm";
3+
import { ChatCompletionFinishReason, CompletionUsage } from "@mlc-ai/web-llm";
44

55
export class MlcLLMApi implements LLMApi {
66
private endpoint: string;
@@ -24,6 +24,7 @@ export class MlcLLMApi implements LLMApi {
2424

2525
let reply: string = "";
2626
let stopReason: ChatCompletionFinishReason | undefined;
27+
let usage: CompletionUsage | undefined;
2728

2829
try {
2930
const response = await fetch(`${this.endpoint}/v1/chat/completions`, {
@@ -52,6 +53,10 @@ export class MlcLLMApi implements LLMApi {
5253
if (data.choices[0].finish_reason) {
5354
stopReason = data.choices[0].finish_reason;
5455
}
56+
57+
if (data.usage) {
58+
usage = data.usage;
59+
}
5560
}
5661
}
5762

@@ -60,12 +65,13 @@ export class MlcLLMApi implements LLMApi {
6065
break;
6166
}
6267
}
63-
options.onFinish(reply, stopReason);
68+
options.onFinish(reply, stopReason, usage);
6469
} else {
6570
const data = await response.json();
6671
options.onFinish(
6772
data.choices[0].message.content,
68-
data.choices[0].finish_reason,
73+
data.choices[0].finish_reason || undefined,
74+
data.usage || undefined,
6975
);
7076
}
7177
} catch (error: any) {

0 commit comments

Comments
 (0)