Skip to content

Commit e97faec

Browse files
committed
fixup! added web-llm as llm provider
1 parent 5a795d2 commit e97faec

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

src/llms/web-llm.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
import { CreateMLCEngine, MLCEngine } from "@mlc-ai/web-llm";
1+
import type { MLCEngine } from "@mlc-ai/web-llm";
22
import type { LLMProviders, Session } from "~llms";
33
import type { SettingSchema } from "~options/fragments/llm";
44

5-
65
export default class WebLLM implements LLMProviders {
76

87
private static readonly DEFAULT_MODEL: string = 'Qwen2-7B-Instruct-q4f32_1-MLC'
98

109
private readonly model: string
11-
private readonly engine: MLCEngine
1210

1311
constructor(settings: SettingSchema) {
1412
this.model = settings.model || WebLLM.DEFAULT_MODEL
15-
this.engine = new MLCEngine()
1613
}
1714

1815
cumulative: boolean = true
1916

2017
async validate(): Promise<void> {
21-
await this.engine.reload(this.model)
18+
await this.initializeEngine()
2219
}
2320

2421
async prompt(chat: string): Promise<string> {
@@ -47,11 +44,7 @@ export default class WebLLM implements LLMProviders {
4744
}
4845

4946
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()
5548
return {
5649
async prompt(chat: string) {
5750
await engine.interruptGenerate()
@@ -71,7 +64,7 @@ export default class WebLLM implements LLMProviders {
7164
stream: true
7265
})
7366
for await (const chunk of chunks) {
74-
yield chunk.choices[0]?.text|| "";
67+
yield chunk.choices[0]?.text || "";
7568
if (chunk.usage) {
7669
console.debug('Usage:', chunk.usage)
7770
}
@@ -81,4 +74,13 @@ export default class WebLLM implements LLMProviders {
8174
}
8275
}
8376

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+
8486
}

tests/units/llm.spec.ts

+51
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import logger from '@tests/helpers/logger'
33
import createLLMProvider from "~llms"
44

55

6+
test.slow()
7+
68
test('嘗試使用 Cloudflare AI 對話', { tag: "@scoped" }, async () => {
79

810
test.skip(!process.env.CF_ACCOUNT_ID || !process.env.CF_API_TOKEN, '請設定 CF_ACCOUNT_ID 和 CF_API_TOKEN 環境變數')
@@ -93,4 +95,53 @@ test('嘗試使用 Remote Worker 對話', { tag: "@scoped" }, async () => {
9395
expect(msg).not.toBeUndefined()
9496
expect(msg).not.toBe('')
9597

98+
})
99+
100+
test('嘗試使用 Web LLM 對話', { tag: "@scoped" }, async ({ modules, page }) => {
101+
102+
test.setTimeout(0)
103+
await modules['llm'].loadToPage()
104+
105+
logger.info('正在测试 json 返回请求...')
106+
const res = await page.evaluate(async () => {
107+
108+
const { llms } = window as any
109+
const llm = llms.createLLMProvider({ provider: 'webllm', module: 'Phi-3-mini-128k-instruct-q0f16-MLC' })
110+
const session = await llm.asSession()
111+
{
112+
(window as any).session = session
113+
}
114+
return await session.prompt('你好')
115+
})
116+
117+
logger.info('response: ', res)
118+
expect(res).not.toBeUndefined()
119+
expect(res).not.toBe('')
120+
121+
logger.info('正在测试 SSE 请求...')
122+
const msg = await page.evaluate(async () => {
123+
const { session } = window as any
124+
if (!session) {
125+
console.error('session not found')
126+
return undefined
127+
}
128+
const res = session.promptStream('地球为什么是圆的?')
129+
let msg = '';
130+
for await (const r of res) {
131+
console.log('response: ', r)
132+
msg = r
133+
}
134+
return msg
135+
})
136+
137+
expect(msg).not.toBeUndefined()
138+
expect(msg).not.toBe('')
139+
140+
logger.info("正在清除已下載的 LLM...")
141+
await page.evaluate(async () => {
142+
const { session } = window as any
143+
if (!session) return
144+
await session[Symbol.asyncDispose]()
145+
})
146+
96147
})

0 commit comments

Comments
 (0)