1
- import { CreateMLCEngine , MLCEngine } from "@mlc-ai/web-llm" ;
1
+ import type { MLCEngine } from "@mlc-ai/web-llm" ;
2
2
import type { LLMProviders , Session } from "~llms" ;
3
3
import type { SettingSchema } from "~options/fragments/llm" ;
4
4
5
-
6
5
export default class WebLLM implements LLMProviders {
7
6
8
7
private static readonly DEFAULT_MODEL : string = 'Qwen2-7B-Instruct-q4f32_1-MLC'
9
8
10
9
private readonly model : string
11
- private readonly engine : MLCEngine
12
10
13
11
constructor ( settings : SettingSchema ) {
14
12
this . model = settings . model || WebLLM . DEFAULT_MODEL
15
- this . engine = new MLCEngine ( )
16
13
}
17
14
18
15
cumulative : boolean = true
19
16
20
17
async validate ( ) : Promise < void > {
21
- await this . engine . reload ( this . model )
18
+ await this . initializeEngine ( )
22
19
}
23
20
24
21
async prompt ( chat : string ) : Promise < string > {
@@ -47,11 +44,7 @@ export default class WebLLM implements LLMProviders {
47
44
}
48
45
49
46
async asSession ( ) : Promise < Session < LLMProviders > > {
50
- const engine = await CreateMLCEngine ( this . model , {
51
- initProgressCallback : ( progress ) => {
52
- console . log ( '初始化进度:' , progress )
53
- }
54
- } )
47
+ const engine = await this . initializeEngine ( )
55
48
return {
56
49
async prompt ( chat : string ) {
57
50
await engine . interruptGenerate ( )
@@ -71,7 +64,7 @@ export default class WebLLM implements LLMProviders {
71
64
stream : true
72
65
} )
73
66
for await ( const chunk of chunks ) {
74
- yield chunk . choices [ 0 ] ?. text || "" ;
67
+ yield chunk . choices [ 0 ] ?. text || "" ;
75
68
if ( chunk . usage ) {
76
69
console . debug ( 'Usage:' , chunk . usage )
77
70
}
@@ -81,4 +74,13 @@ export default class WebLLM implements LLMProviders {
81
74
}
82
75
}
83
76
77
+ private async initializeEngine ( ) : Promise < MLCEngine > {
78
+ const { CreateMLCEngine } = await import ( '@mlc-ai/web-llm' )
79
+ return CreateMLCEngine ( this . model , {
80
+ initProgressCallback : ( progress ) => {
81
+ console . log ( '初始化进度:' , progress )
82
+ }
83
+ } )
84
+ }
85
+
84
86
}
0 commit comments