11import log from "loglevel" ;
22import { ChatOptions , LLMApi } from "./api" ;
3- import { ChatCompletionFinishReason } from "@mlc-ai/web-llm" ;
3+ import { ChatCompletionFinishReason , CompletionUsage } from "@mlc-ai/web-llm" ;
44
55export 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