-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
311 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { LLMTypes } from "~llms" | ||
|
||
export type ModelList = { | ||
providers: LLMTypes[] | ||
models: string[] | ||
} | ||
|
||
const models: ModelList[] = [ | ||
{ | ||
providers: ['worker', 'cloudflare'], | ||
models: [ | ||
'@cf/qwen/qwen1.5-14b-chat-awq', | ||
'@cf/qwen/qwen1.5-7b-chat-awq', | ||
'@cf/qwen/qwen1.5-1.8b-chat', | ||
'@hf/google/gemma-7b-it', | ||
'@hf/nousresearch/hermes-2-pro-mistral-7b' | ||
] | ||
}, | ||
{ | ||
providers: [ 'webllm' ], | ||
models: [ | ||
'Qwen2-7B-Instruct-q4f32_1-MLC', | ||
'Qwen2.5-14B-Instruct-q4f16_1-MLC', | ||
'gemma-2-9b-it-q4f16_1-MLC', | ||
'Qwen2.5-3B-Instruct-q0f16-MLC', | ||
'Phi-3-mini-128k-instruct-q0f16-MLC', | ||
'Phi-3.5-mini-instruct-q4f16_1-MLC-1k' | ||
] | ||
} | ||
] | ||
|
||
|
||
export default models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import type { MLCEngine } from "@mlc-ai/web-llm"; | ||
import type { LLMProviders, Session } from "~llms"; | ||
import type { SettingSchema } from "~options/fragments/llm"; | ||
|
||
export default class WebLLM implements LLMProviders { | ||
|
||
private static readonly DEFAULT_MODEL: string = 'Qwen2-7B-Instruct-q4f32_1-MLC' | ||
|
||
private readonly model: string | ||
|
||
constructor(settings: SettingSchema) { | ||
this.model = settings.model || WebLLM.DEFAULT_MODEL | ||
} | ||
|
||
cumulative: boolean = true | ||
|
||
async validate(progresser?: (p: number, t: string) => void): Promise<void> { | ||
await this.initializeEngine(progresser) | ||
} | ||
|
||
async prompt(chat: string): Promise<string> { | ||
const session = await this.asSession() | ||
try { | ||
console.debug('[web-llm] prompting: ', chat) | ||
return session.prompt(chat) | ||
} finally { | ||
console.debug('[web-llm] done') | ||
await session[Symbol.asyncDispose]() | ||
} | ||
} | ||
|
||
async *promptStream(chat: string): AsyncGenerator<string> { | ||
const session = await this.asSession() | ||
try { | ||
console.debug('[web-llm] prompting stream: ', chat) | ||
const res = session.promptStream(chat) | ||
for await (const chunk of res) { | ||
yield chunk | ||
} | ||
} finally { | ||
console.debug('[web-llm] done') | ||
await session[Symbol.asyncDispose]() | ||
} | ||
} | ||
|
||
async asSession(): Promise<Session<LLMProviders>> { | ||
const engine = await this.initializeEngine() | ||
return { | ||
async prompt(chat: string) { | ||
await engine.interruptGenerate() | ||
const c = await engine.completions.create({ | ||
prompt: chat, | ||
max_tokens: 512, | ||
temperature: 0.2, | ||
}) | ||
return c.choices[0]?.text ?? engine.getMessage() | ||
}, | ||
async *promptStream(chat: string): AsyncGenerator<string> { | ||
await engine.interruptGenerate() | ||
const chunks = await engine.completions.create({ | ||
prompt: chat, | ||
max_tokens: 512, | ||
temperature: 0.2, | ||
stream: true | ||
}) | ||
for await (const chunk of chunks) { | ||
yield chunk.choices[0]?.text || ""; | ||
if (chunk.usage) { | ||
console.debug('Usage:', chunk.usage) | ||
} | ||
} | ||
}, | ||
[Symbol.asyncDispose]: engine.unload | ||
} | ||
} | ||
|
||
private async initializeEngine(progresser?: (p: number, t: string) => void): Promise<MLCEngine> { | ||
const { CreateMLCEngine } = await import('@mlc-ai/web-llm') | ||
return CreateMLCEngine(this.model, { | ||
initProgressCallback: (progress) => { | ||
progresser?.(progress.progress, "正在下载AI模型到本地") | ||
console.log('初始化进度:', progress) | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.