@browser-ai/web-llm 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +271 -0
- package/dist/index.d.mts +181 -0
- package/dist/index.d.ts +181 -0
- package/dist/index.js +1370 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1343 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +71 -0
package/README.md
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
# WebLLM provider for Vercel AI SDK
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
<img src="./hero.png">
|
|
5
|
+
</div>
|
|
6
|
+
|
|
7
|
+
<div align="center">
|
|
8
|
+
|
|
9
|
+
[](https://www.npmjs.com/package/@built-in-ai/web-llm)
|
|
10
|
+
[](https://www.npmjs.com/package/@built-in-ai/web-llm)
|
|
11
|
+
|
|
12
|
+
> [!NOTE]
|
|
13
|
+
> This library is still in a very early state where updates might come quite frequently.
|
|
14
|
+
|
|
15
|
+
</div>
|
|
16
|
+
|
|
17
|
+
[WebLLM](https://github.com/mlc-ai/web-llm) model provider for [Vercel AI SDK](https://ai-sdk.dev/). This library enables you to easily use the AI SDK with popular open-source models running directly in your web browser.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm i @built-in-ai/web-llm
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The `@built-in-ai/web-llm` package is the AI SDK provider for open-source built-in AI models leveraging the [WebLLM](https://github.com/mlc-ai/web-llm) inference engine.
|
|
26
|
+
|
|
27
|
+
## Browser Requirements
|
|
28
|
+
|
|
29
|
+
A WebGPU-compatible browser is needed to run these models. Check out the [API](https://developer.mozilla.org/en-US/docs/Web/API/WebGPU_API) for more information.
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
### Basic Usage
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { streamText } from "ai";
|
|
37
|
+
import { webLLM } from "@built-in-ai/web-llm";
|
|
38
|
+
|
|
39
|
+
const result = streamText({
|
|
40
|
+
// or generateText
|
|
41
|
+
model: webLLM("Llama-3.2-3B-Instruct-q4f16_1-MLC"),
|
|
42
|
+
messages: [{ role: "user", content: "Hello, how are you?" }],
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
for await (const chunk of result.textStream) {
|
|
46
|
+
console.log(chunk);
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Advanced Usage
|
|
51
|
+
|
|
52
|
+
If you're already familiar with the WebLLM engine library (or in general inference with models in the browser), you'll know that to make it run effeciently, you probably know that you need to use [web workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) to offload the heavy model computation to a different thread than the UI. You can check out WebLLM's official [docs](https://webllm.mlc.ai/docs/user/advanced_usage.html) for more information.
|
|
53
|
+
|
|
54
|
+
1. Create your `worker.ts` file:
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { WebWorkerMLCEngineHandler } from "@built-in-ai/web-llm";
|
|
58
|
+
|
|
59
|
+
const handler = new WebWorkerMLCEngineHandler();
|
|
60
|
+
self.onmessage = (msg: MessageEvent) => {
|
|
61
|
+
handler.onmessage(msg);
|
|
62
|
+
};
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
2. Provide it in the model instance:
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { streamText } from "ai";
|
|
69
|
+
import { webLLM } from "@built-in-ai/web-llm";
|
|
70
|
+
|
|
71
|
+
const result = streamText({ // or generateText
|
|
72
|
+
model: webLLM('Qwen3-0.6B-q0f16-MLC', {
|
|
73
|
+
worker: new Worker(new URL("./worker.ts", import.meta.url), {
|
|
74
|
+
type: "module",
|
|
75
|
+
}),
|
|
76
|
+
});,
|
|
77
|
+
messages: [{ role: "user", content: "Hello, how are you?" }],
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
for await (const chunk of result.textStream) {
|
|
81
|
+
console.log(chunk);
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Download Progress Tracking
|
|
86
|
+
|
|
87
|
+
When using the open-source models for the first time, the model needs to be downloaded before use.
|
|
88
|
+
|
|
89
|
+
You'll probably want to show download progress in your applications to improve UX.
|
|
90
|
+
|
|
91
|
+
### Basic Progress Monitoring
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { streamText } from "ai";
|
|
95
|
+
import { webLLM } from "@built-in-ai/web-llm";
|
|
96
|
+
|
|
97
|
+
const model = webLLM("Llama-3.2-3B-Instruct-q4f16_1-MLC");
|
|
98
|
+
const availability = await model.availability();
|
|
99
|
+
|
|
100
|
+
if (availability === "unavailable") {
|
|
101
|
+
console.log("Browser doesn't support built-in AI models");
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (availability === "downloadable") {
|
|
106
|
+
await model.createSessionWithProgress((progress) => {
|
|
107
|
+
console.log(`Download progress: ${Math.round(progress * 100)}%`);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Model is ready
|
|
112
|
+
const result = streamText({
|
|
113
|
+
model,
|
|
114
|
+
messages: [{ role: "user", content: "Hello!" }],
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Tool calling
|
|
119
|
+
|
|
120
|
+
> Be aware that some models might struggle with this.
|
|
121
|
+
> If you want to try it out with best succes, I suggest using a reasoning model (Qwen3).
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
const result = streamText({
|
|
125
|
+
model: webLLM("Qwen3-1.7B-q4f16_1-MLC"),
|
|
126
|
+
tools: {
|
|
127
|
+
weather: tool({
|
|
128
|
+
description: "Get the weather in a location",
|
|
129
|
+
inputSchema: z.object({
|
|
130
|
+
location: z.string().describe("The location to get the weather for"),
|
|
131
|
+
}),
|
|
132
|
+
execute: async ({ location }) => ({
|
|
133
|
+
location,
|
|
134
|
+
temperature: 72 + Math.floor(Math.random() * 21) - 10,
|
|
135
|
+
}),
|
|
136
|
+
}),
|
|
137
|
+
},
|
|
138
|
+
stopWhen: stepCountIs(5),
|
|
139
|
+
prompt: "What is the weather in San Francisco?",
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
And then in your useChat use `sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls`.
|
|
144
|
+
|
|
145
|
+
## Integration with useChat Hook
|
|
146
|
+
|
|
147
|
+
When using this library with the `useChat` hook, you'll need to create a [custom transport](https://v5.ai-sdk.dev/docs/ai-sdk-ui/transport#transport) implementation to handle client-side AI with download progress.
|
|
148
|
+
|
|
149
|
+
You can do this by importing `WebLLMUIMessage` from `@built-in-ai/web-llm` that extends `UIMessage` to include [data parts](https://v5.ai-sdk.dev/docs/ai-sdk-ui/streaming-data) such as download progress.
|
|
150
|
+
|
|
151
|
+
See the complete working example: **[`/examples/next-hybrid/app/web-llm/util/web-llm-chat-transport.ts`](../../examples/next-hybrid/app/web-llm/util/web-llm-chat-transport.ts)** and the **[`/examples/next-hybrid/app/web-llm/page.tsx`](../../examples/next-hybrid/app/web-llm/page.tsx)** components.
|
|
152
|
+
|
|
153
|
+
This example includes:
|
|
154
|
+
|
|
155
|
+
- Download progress with UI progress bar and status message updates
|
|
156
|
+
- Hybrid client/server architecture with fallback
|
|
157
|
+
- Error handling and notifications
|
|
158
|
+
- Full integration with `useChat` hook
|
|
159
|
+
|
|
160
|
+
## API Reference
|
|
161
|
+
|
|
162
|
+
### `webLLM(modelId, settings?)`
|
|
163
|
+
|
|
164
|
+
Creates a WebLLM model instance.
|
|
165
|
+
|
|
166
|
+
**Parameters:**
|
|
167
|
+
|
|
168
|
+
- `modelId`: The model identifier from the [supported list of models](https://github.com/mlc-ai/web-llm/blob/main/src/config.ts)
|
|
169
|
+
- `settings` (optional): Configuration options for the WebLLM model
|
|
170
|
+
- `appConfig?: AppConfig` - Custom app configuration for WebLLM
|
|
171
|
+
- `initProgressCallback?: (progress: WebLLMProgress) => void` - Progress callback for model initialization
|
|
172
|
+
- `engineConfig?: MLCEngineConfig` - Engine configuration options
|
|
173
|
+
- `worker?: Worker` - A web worker instance to run the model in for better performance
|
|
174
|
+
|
|
175
|
+
**Returns:** `WebLLMLanguageModel` instance
|
|
176
|
+
|
|
177
|
+
### `doesBrowserSupportWebLLM(): boolean`
|
|
178
|
+
|
|
179
|
+
Quick check if the browser supports the WebLLM. Useful for component-level decisions and feature flags.
|
|
180
|
+
|
|
181
|
+
**Returns:** `boolean` - `true` if browser supports WebLLM, `false` otherwise
|
|
182
|
+
|
|
183
|
+
**Example:**
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
import { doesBrowserSupportWebLLM } from "@built-in-ai/web-llm";
|
|
187
|
+
|
|
188
|
+
if (doesBrowserSupportWebLLM()) {
|
|
189
|
+
// Show built-in AI option in UI
|
|
190
|
+
} else {
|
|
191
|
+
// Show server-side option only
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### `WebLLMUIMessage`
|
|
196
|
+
|
|
197
|
+
Extended UI message type for use with the `useChat` hook that includes custom data parts for WebLLM functionality.
|
|
198
|
+
|
|
199
|
+
**Type Definition:**
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
type WebLLMUIMessage = UIMessage<
|
|
203
|
+
never,
|
|
204
|
+
{
|
|
205
|
+
modelDownloadProgress: {
|
|
206
|
+
status: "downloading" | "complete" | "error";
|
|
207
|
+
progress?: number;
|
|
208
|
+
message: string;
|
|
209
|
+
};
|
|
210
|
+
notification: {
|
|
211
|
+
message: string;
|
|
212
|
+
level: "info" | "warning" | "error";
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
>;
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
**Data Parts:**
|
|
219
|
+
|
|
220
|
+
- `modelDownloadProgress` - Tracks browser AI model download status and progress
|
|
221
|
+
- `notification` - Displays temporary messages and alerts to users
|
|
222
|
+
|
|
223
|
+
### `WebLLMLanguageModel.createSessionWithProgress(onDownloadProgress?)`
|
|
224
|
+
|
|
225
|
+
Creates a language model session with optional download progress monitoring.
|
|
226
|
+
|
|
227
|
+
**Parameters:**
|
|
228
|
+
|
|
229
|
+
- `onDownloadProgress?: (progress: WebLLMProgress) => void` - Optional callback that receives progress reports during model download
|
|
230
|
+
|
|
231
|
+
**Returns:** `Promise<MLCEngineInterface>` - The configured language model session
|
|
232
|
+
|
|
233
|
+
**Example:**
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
const model = webLLM("Llama-3.2-3B-Instruct-q4f16_1-MLC");
|
|
237
|
+
await model.createSessionWithProgress((report) => {
|
|
238
|
+
console.log(`Download: ${report.text}`);
|
|
239
|
+
});
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### `WebLLMLanguageModel.availability()`
|
|
243
|
+
|
|
244
|
+
Checks the current availability status of the WebLLM model.
|
|
245
|
+
|
|
246
|
+
**Returns:** `Promise<"unavailable" | "downloadable" | "downloading" | "available">`
|
|
247
|
+
|
|
248
|
+
- `"unavailable"` - Model is not supported in the browser
|
|
249
|
+
- `"downloadable"` - Model is supported but needs to be downloaded first
|
|
250
|
+
- `"downloading"` - Model is currently being downloaded
|
|
251
|
+
- `"available"` - Model is ready to use
|
|
252
|
+
|
|
253
|
+
### `WebLLMProgress`
|
|
254
|
+
|
|
255
|
+
The progress report type returned during model initialization.
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
interface InitProgressReport {
|
|
259
|
+
progress: number; // 0-1
|
|
260
|
+
timeElapsed: number; // in ms
|
|
261
|
+
text: string; // progress text
|
|
262
|
+
}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## Author
|
|
266
|
+
|
|
267
|
+
2025 © Jakob Hoeg Mørk
|
|
268
|
+
|
|
269
|
+
## Credits
|
|
270
|
+
|
|
271
|
+
The WebLLM & Vercel teams
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { LanguageModelV2, LanguageModelV2CallOptions, LanguageModelV2Content, LanguageModelV2FinishReason, LanguageModelV2CallWarning, LanguageModelV2StreamPart } from '@ai-sdk/provider';
|
|
2
|
+
import { AppConfig, InitProgressReport, MLCEngineConfig, MLCEngineInterface } from '@mlc-ai/web-llm';
|
|
3
|
+
export { InitProgressReport as WebLLMProgress, WebWorkerMLCEngineHandler } from '@mlc-ai/web-llm';
|
|
4
|
+
import { UIMessage } from 'ai';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* UI message type for built-in AI features with custom data parts.
|
|
8
|
+
*
|
|
9
|
+
* Extends base UIMessage to include specific data part schemas
|
|
10
|
+
* such as model download progress
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // Import and use with useChat hook from @ai-sdk/react
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { useChat } from "@ai-sdk/react";
|
|
16
|
+
* import { WebLLMUIMessage } from "@built-in-ai/web-llm";
|
|
17
|
+
*
|
|
18
|
+
* const { messages, sendMessage } = useChat<WebLLMUIMessage>({
|
|
19
|
+
* onData: (dataPart) => {
|
|
20
|
+
* if (dataPart.type === 'data-modelDownloadProgress') {
|
|
21
|
+
* console.log(`Download: ${dataPart.data.progress}%`);
|
|
22
|
+
* }
|
|
23
|
+
* if (dataPart.type === 'data-notification') {
|
|
24
|
+
* console.log(`${dataPart.data.level}: ${dataPart.data.message}`);
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
* });
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @see {@link https://v5.ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat | useChat hook documentation}
|
|
31
|
+
*/
|
|
32
|
+
type WebLLMUIMessage = UIMessage<
|
|
33
|
+
never, // No custom metadata type
|
|
34
|
+
{
|
|
35
|
+
/**
|
|
36
|
+
* Model download progress data part for tracking browser AI model download status.
|
|
37
|
+
* Used to display download progress bars and status messages to users.
|
|
38
|
+
*/
|
|
39
|
+
modelDownloadProgress: {
|
|
40
|
+
/** Current download/initialization status */
|
|
41
|
+
status: "downloading" | "complete" | "error";
|
|
42
|
+
/** Download progress percentage (0-100), undefined for non-downloading states */
|
|
43
|
+
progress?: number;
|
|
44
|
+
/** Human-readable status message to display to users */
|
|
45
|
+
message: string;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* User notification data part for displaying temporary messages and alerts.
|
|
49
|
+
* These are typically transient and not persisted in message history.
|
|
50
|
+
*/
|
|
51
|
+
notification: {
|
|
52
|
+
/** The notification message text */
|
|
53
|
+
message: string;
|
|
54
|
+
/** Notification severity level for styling and priority */
|
|
55
|
+
level: "info" | "warning" | "error";
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
>;
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
type Availability =
|
|
62
|
+
| "unavailable"
|
|
63
|
+
| "downloadable"
|
|
64
|
+
| "downloading"
|
|
65
|
+
| "available";
|
|
66
|
+
|
|
67
|
+
declare global {
|
|
68
|
+
interface Navigator {
|
|
69
|
+
gpu?: unknown;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
type WebLLMModelId = string;
|
|
73
|
+
interface WebLLMSettings {
|
|
74
|
+
/**
|
|
75
|
+
* Custom app configuration for WebLLM
|
|
76
|
+
*/
|
|
77
|
+
appConfig?: AppConfig;
|
|
78
|
+
/**
|
|
79
|
+
* Progress callback for model initialization
|
|
80
|
+
*/
|
|
81
|
+
initProgressCallback?: (progress: InitProgressReport) => void;
|
|
82
|
+
/**
|
|
83
|
+
* Engine configuration options
|
|
84
|
+
*/
|
|
85
|
+
engineConfig?: MLCEngineConfig;
|
|
86
|
+
/**
|
|
87
|
+
* A web worker instance to run the model in.
|
|
88
|
+
* When provided, the model will run in a separate thread.
|
|
89
|
+
*
|
|
90
|
+
* @default undefined
|
|
91
|
+
*/
|
|
92
|
+
worker?: Worker;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Check if the browser supports WebLLM
|
|
96
|
+
* @returns true if the browser supports WebLLM, false otherwise
|
|
97
|
+
*/
|
|
98
|
+
declare function doesBrowserSupportWebLLM(): boolean;
|
|
99
|
+
declare class WebLLMLanguageModel implements LanguageModelV2 {
|
|
100
|
+
readonly specificationVersion = "v2";
|
|
101
|
+
readonly modelId: WebLLMModelId;
|
|
102
|
+
readonly provider = "web-llm";
|
|
103
|
+
private readonly config;
|
|
104
|
+
private engine?;
|
|
105
|
+
private isInitialized;
|
|
106
|
+
private initializationPromise?;
|
|
107
|
+
constructor(modelId: WebLLMModelId, options?: WebLLMSettings);
|
|
108
|
+
readonly supportedUrls: Record<string, RegExp[]>;
|
|
109
|
+
/**
|
|
110
|
+
* Check if the model is initialized and ready to use
|
|
111
|
+
* @returns true if the model is initialized, false otherwise
|
|
112
|
+
*/
|
|
113
|
+
get isModelInitialized(): boolean;
|
|
114
|
+
private getEngine;
|
|
115
|
+
private _initializeEngine;
|
|
116
|
+
private getArgs;
|
|
117
|
+
/**
|
|
118
|
+
* Generates a complete text response using WebLLM
|
|
119
|
+
* @param options
|
|
120
|
+
* @returns Promise resolving to the generated content with finish reason, usage stats, and any warnings
|
|
121
|
+
* @throws {LoadSettingError} When WebLLM is not available or model needs to be downloaded
|
|
122
|
+
* @throws {UnsupportedFunctionalityError} When unsupported features like file input are used
|
|
123
|
+
*/
|
|
124
|
+
doGenerate(options: LanguageModelV2CallOptions): Promise<{
|
|
125
|
+
content: LanguageModelV2Content[];
|
|
126
|
+
finishReason: LanguageModelV2FinishReason;
|
|
127
|
+
usage: {
|
|
128
|
+
inputTokens: number | undefined;
|
|
129
|
+
outputTokens: number | undefined;
|
|
130
|
+
totalTokens: number | undefined;
|
|
131
|
+
};
|
|
132
|
+
request: {
|
|
133
|
+
body: any;
|
|
134
|
+
};
|
|
135
|
+
warnings: LanguageModelV2CallWarning[];
|
|
136
|
+
}>;
|
|
137
|
+
/**
|
|
138
|
+
* Check the availability of the WebLLM model
|
|
139
|
+
* @returns Promise resolving to "unavailable", "available", or "available-after-download"
|
|
140
|
+
*/
|
|
141
|
+
availability(): Promise<Availability>;
|
|
142
|
+
/**
|
|
143
|
+
* Creates an engine session with download progress monitoring.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* const engine = await model.createSessionWithProgress(
|
|
148
|
+
* (progress) => {
|
|
149
|
+
* console.log(`Download progress: ${Math.round(progress.loaded * 100)}%`);
|
|
150
|
+
* }
|
|
151
|
+
* );
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* @param onInitProgress Optional callback receiving progress reports during model download
|
|
155
|
+
* @returns Promise resolving to a configured WebLLM engine
|
|
156
|
+
* @throws {LoadSettingError} When WebLLM is not available or model is unavailable
|
|
157
|
+
*/
|
|
158
|
+
createSessionWithProgress(onInitProgress?: (progress: InitProgressReport) => void): Promise<MLCEngineInterface>;
|
|
159
|
+
/**
|
|
160
|
+
* Generates a streaming text response using WebLLM
|
|
161
|
+
* @param options
|
|
162
|
+
* @returns Promise resolving to a readable stream of text chunks and request metadata
|
|
163
|
+
* @throws {LoadSettingError} When WebLLM is not available or model needs to be downloaded
|
|
164
|
+
* @throws {UnsupportedFunctionalityError} When unsupported features like file input are used
|
|
165
|
+
*/
|
|
166
|
+
doStream(options: LanguageModelV2CallOptions): Promise<{
|
|
167
|
+
stream: ReadableStream<LanguageModelV2StreamPart>;
|
|
168
|
+
request: {
|
|
169
|
+
body: any;
|
|
170
|
+
};
|
|
171
|
+
}>;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Create a new WebLLMLanguageModel.
|
|
176
|
+
* @param modelId The model ID to use (e.g., 'Llama-3.1-8B-Instruct-q4f32_1-MLC')
|
|
177
|
+
* @param settings Options for the model
|
|
178
|
+
*/
|
|
179
|
+
declare function webLLM(modelId: WebLLMModelId, settings?: WebLLMSettings): WebLLMLanguageModel;
|
|
180
|
+
|
|
181
|
+
export { WebLLMLanguageModel, type WebLLMModelId, type WebLLMSettings, type WebLLMUIMessage, doesBrowserSupportWebLLM, webLLM };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { LanguageModelV2, LanguageModelV2CallOptions, LanguageModelV2Content, LanguageModelV2FinishReason, LanguageModelV2CallWarning, LanguageModelV2StreamPart } from '@ai-sdk/provider';
|
|
2
|
+
import { AppConfig, InitProgressReport, MLCEngineConfig, MLCEngineInterface } from '@mlc-ai/web-llm';
|
|
3
|
+
export { InitProgressReport as WebLLMProgress, WebWorkerMLCEngineHandler } from '@mlc-ai/web-llm';
|
|
4
|
+
import { UIMessage } from 'ai';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* UI message type for built-in AI features with custom data parts.
|
|
8
|
+
*
|
|
9
|
+
* Extends base UIMessage to include specific data part schemas
|
|
10
|
+
* such as model download progress
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // Import and use with useChat hook from @ai-sdk/react
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { useChat } from "@ai-sdk/react";
|
|
16
|
+
* import { WebLLMUIMessage } from "@built-in-ai/web-llm";
|
|
17
|
+
*
|
|
18
|
+
* const { messages, sendMessage } = useChat<WebLLMUIMessage>({
|
|
19
|
+
* onData: (dataPart) => {
|
|
20
|
+
* if (dataPart.type === 'data-modelDownloadProgress') {
|
|
21
|
+
* console.log(`Download: ${dataPart.data.progress}%`);
|
|
22
|
+
* }
|
|
23
|
+
* if (dataPart.type === 'data-notification') {
|
|
24
|
+
* console.log(`${dataPart.data.level}: ${dataPart.data.message}`);
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
* });
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @see {@link https://v5.ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat | useChat hook documentation}
|
|
31
|
+
*/
|
|
32
|
+
type WebLLMUIMessage = UIMessage<
|
|
33
|
+
never, // No custom metadata type
|
|
34
|
+
{
|
|
35
|
+
/**
|
|
36
|
+
* Model download progress data part for tracking browser AI model download status.
|
|
37
|
+
* Used to display download progress bars and status messages to users.
|
|
38
|
+
*/
|
|
39
|
+
modelDownloadProgress: {
|
|
40
|
+
/** Current download/initialization status */
|
|
41
|
+
status: "downloading" | "complete" | "error";
|
|
42
|
+
/** Download progress percentage (0-100), undefined for non-downloading states */
|
|
43
|
+
progress?: number;
|
|
44
|
+
/** Human-readable status message to display to users */
|
|
45
|
+
message: string;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* User notification data part for displaying temporary messages and alerts.
|
|
49
|
+
* These are typically transient and not persisted in message history.
|
|
50
|
+
*/
|
|
51
|
+
notification: {
|
|
52
|
+
/** The notification message text */
|
|
53
|
+
message: string;
|
|
54
|
+
/** Notification severity level for styling and priority */
|
|
55
|
+
level: "info" | "warning" | "error";
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
>;
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
type Availability =
|
|
62
|
+
| "unavailable"
|
|
63
|
+
| "downloadable"
|
|
64
|
+
| "downloading"
|
|
65
|
+
| "available";
|
|
66
|
+
|
|
67
|
+
declare global {
|
|
68
|
+
interface Navigator {
|
|
69
|
+
gpu?: unknown;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
type WebLLMModelId = string;
|
|
73
|
+
interface WebLLMSettings {
|
|
74
|
+
/**
|
|
75
|
+
* Custom app configuration for WebLLM
|
|
76
|
+
*/
|
|
77
|
+
appConfig?: AppConfig;
|
|
78
|
+
/**
|
|
79
|
+
* Progress callback for model initialization
|
|
80
|
+
*/
|
|
81
|
+
initProgressCallback?: (progress: InitProgressReport) => void;
|
|
82
|
+
/**
|
|
83
|
+
* Engine configuration options
|
|
84
|
+
*/
|
|
85
|
+
engineConfig?: MLCEngineConfig;
|
|
86
|
+
/**
|
|
87
|
+
* A web worker instance to run the model in.
|
|
88
|
+
* When provided, the model will run in a separate thread.
|
|
89
|
+
*
|
|
90
|
+
* @default undefined
|
|
91
|
+
*/
|
|
92
|
+
worker?: Worker;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Check if the browser supports WebLLM
|
|
96
|
+
* @returns true if the browser supports WebLLM, false otherwise
|
|
97
|
+
*/
|
|
98
|
+
declare function doesBrowserSupportWebLLM(): boolean;
|
|
99
|
+
declare class WebLLMLanguageModel implements LanguageModelV2 {
|
|
100
|
+
readonly specificationVersion = "v2";
|
|
101
|
+
readonly modelId: WebLLMModelId;
|
|
102
|
+
readonly provider = "web-llm";
|
|
103
|
+
private readonly config;
|
|
104
|
+
private engine?;
|
|
105
|
+
private isInitialized;
|
|
106
|
+
private initializationPromise?;
|
|
107
|
+
constructor(modelId: WebLLMModelId, options?: WebLLMSettings);
|
|
108
|
+
readonly supportedUrls: Record<string, RegExp[]>;
|
|
109
|
+
/**
|
|
110
|
+
* Check if the model is initialized and ready to use
|
|
111
|
+
* @returns true if the model is initialized, false otherwise
|
|
112
|
+
*/
|
|
113
|
+
get isModelInitialized(): boolean;
|
|
114
|
+
private getEngine;
|
|
115
|
+
private _initializeEngine;
|
|
116
|
+
private getArgs;
|
|
117
|
+
/**
|
|
118
|
+
* Generates a complete text response using WebLLM
|
|
119
|
+
* @param options
|
|
120
|
+
* @returns Promise resolving to the generated content with finish reason, usage stats, and any warnings
|
|
121
|
+
* @throws {LoadSettingError} When WebLLM is not available or model needs to be downloaded
|
|
122
|
+
* @throws {UnsupportedFunctionalityError} When unsupported features like file input are used
|
|
123
|
+
*/
|
|
124
|
+
doGenerate(options: LanguageModelV2CallOptions): Promise<{
|
|
125
|
+
content: LanguageModelV2Content[];
|
|
126
|
+
finishReason: LanguageModelV2FinishReason;
|
|
127
|
+
usage: {
|
|
128
|
+
inputTokens: number | undefined;
|
|
129
|
+
outputTokens: number | undefined;
|
|
130
|
+
totalTokens: number | undefined;
|
|
131
|
+
};
|
|
132
|
+
request: {
|
|
133
|
+
body: any;
|
|
134
|
+
};
|
|
135
|
+
warnings: LanguageModelV2CallWarning[];
|
|
136
|
+
}>;
|
|
137
|
+
/**
|
|
138
|
+
* Check the availability of the WebLLM model
|
|
139
|
+
* @returns Promise resolving to "unavailable", "available", or "available-after-download"
|
|
140
|
+
*/
|
|
141
|
+
availability(): Promise<Availability>;
|
|
142
|
+
/**
|
|
143
|
+
* Creates an engine session with download progress monitoring.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* const engine = await model.createSessionWithProgress(
|
|
148
|
+
* (progress) => {
|
|
149
|
+
* console.log(`Download progress: ${Math.round(progress.loaded * 100)}%`);
|
|
150
|
+
* }
|
|
151
|
+
* );
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* @param onInitProgress Optional callback receiving progress reports during model download
|
|
155
|
+
* @returns Promise resolving to a configured WebLLM engine
|
|
156
|
+
* @throws {LoadSettingError} When WebLLM is not available or model is unavailable
|
|
157
|
+
*/
|
|
158
|
+
createSessionWithProgress(onInitProgress?: (progress: InitProgressReport) => void): Promise<MLCEngineInterface>;
|
|
159
|
+
/**
|
|
160
|
+
* Generates a streaming text response using WebLLM
|
|
161
|
+
* @param options
|
|
162
|
+
* @returns Promise resolving to a readable stream of text chunks and request metadata
|
|
163
|
+
* @throws {LoadSettingError} When WebLLM is not available or model needs to be downloaded
|
|
164
|
+
* @throws {UnsupportedFunctionalityError} When unsupported features like file input are used
|
|
165
|
+
*/
|
|
166
|
+
doStream(options: LanguageModelV2CallOptions): Promise<{
|
|
167
|
+
stream: ReadableStream<LanguageModelV2StreamPart>;
|
|
168
|
+
request: {
|
|
169
|
+
body: any;
|
|
170
|
+
};
|
|
171
|
+
}>;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Create a new WebLLMLanguageModel.
|
|
176
|
+
* @param modelId The model ID to use (e.g., 'Llama-3.1-8B-Instruct-q4f32_1-MLC')
|
|
177
|
+
* @param settings Options for the model
|
|
178
|
+
*/
|
|
179
|
+
declare function webLLM(modelId: WebLLMModelId, settings?: WebLLMSettings): WebLLMLanguageModel;
|
|
180
|
+
|
|
181
|
+
export { WebLLMLanguageModel, type WebLLMModelId, type WebLLMSettings, type WebLLMUIMessage, doesBrowserSupportWebLLM, webLLM };
|