@browser-ai/web-llm 2.1.0 → 2.1.1
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/dist/index.d.mts +116 -12
- package/dist/index.d.ts +116 -12
- package/dist/index.js +203 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +202 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult } from '@ai-sdk/provider';
|
|
2
|
-
import { AppConfig, InitProgressReport, MLCEngineConfig } from '@mlc-ai/web-llm';
|
|
1
|
+
import { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, EmbeddingModelV3, EmbeddingModelV3CallOptions, EmbeddingModelV3Result, ProviderV3 } from '@ai-sdk/provider';
|
|
2
|
+
import { AppConfig, InitProgressReport, MLCEngineConfig, MLCEngineInterface } from '@mlc-ai/web-llm';
|
|
3
3
|
export { InitProgressReport as WebLLMProgress, WebWorkerMLCEngineHandler } from '@mlc-ai/web-llm';
|
|
4
4
|
import { UIMessage } from 'ai';
|
|
5
5
|
|
|
@@ -75,6 +75,11 @@ declare global {
|
|
|
75
75
|
gpu?: GPU;
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Check if the browser supports WebLLM (WebGPU)
|
|
80
|
+
*/
|
|
81
|
+
declare function doesBrowserSupportWebLLM(): boolean;
|
|
82
|
+
|
|
78
83
|
type WebLLMModelId = string;
|
|
79
84
|
interface WebLLMSettings {
|
|
80
85
|
/**
|
|
@@ -97,11 +102,6 @@ interface WebLLMSettings {
|
|
|
97
102
|
*/
|
|
98
103
|
worker?: Worker;
|
|
99
104
|
}
|
|
100
|
-
/**
|
|
101
|
-
* Check if the browser supports WebGPU (required for WebLLM).
|
|
102
|
-
* @returns boolean - true if WebGPU API is available
|
|
103
|
-
*/
|
|
104
|
-
declare function doesBrowserSupportWebLLM(): boolean;
|
|
105
105
|
declare class WebLLMLanguageModel implements LanguageModelV3 {
|
|
106
106
|
readonly specificationVersion = "v3";
|
|
107
107
|
readonly modelId: WebLLMModelId;
|
|
@@ -162,11 +162,115 @@ declare class WebLLMLanguageModel implements LanguageModelV3 {
|
|
|
162
162
|
doStream(options: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult>;
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
+
type WebLLMEmbeddingModelId = string;
|
|
166
|
+
interface WebLLMEmbeddingSettings {
|
|
167
|
+
/**
|
|
168
|
+
* Custom app configuration for WebLLM
|
|
169
|
+
*/
|
|
170
|
+
appConfig?: AppConfig;
|
|
171
|
+
/**
|
|
172
|
+
* Progress callback for model initialization
|
|
173
|
+
*/
|
|
174
|
+
initProgressCallback?: (progress: InitProgressReport) => void;
|
|
175
|
+
/**
|
|
176
|
+
* Engine configuration options
|
|
177
|
+
*/
|
|
178
|
+
engineConfig?: MLCEngineConfig;
|
|
179
|
+
/**
|
|
180
|
+
* A web worker instance to run the model in.
|
|
181
|
+
* When provided, the model will run in a separate thread.
|
|
182
|
+
*
|
|
183
|
+
* @default undefined
|
|
184
|
+
*/
|
|
185
|
+
worker?: Worker;
|
|
186
|
+
/**
|
|
187
|
+
* Maximum number of texts to embed in a single call.
|
|
188
|
+
* @default 100
|
|
189
|
+
*/
|
|
190
|
+
maxEmbeddingsPerCall?: number;
|
|
191
|
+
}
|
|
192
|
+
declare class WebLLMEmbeddingModel implements EmbeddingModelV3 {
|
|
193
|
+
readonly specificationVersion = "v3";
|
|
194
|
+
readonly provider = "web-llm";
|
|
195
|
+
readonly modelId: WebLLMEmbeddingModelId;
|
|
196
|
+
readonly maxEmbeddingsPerCall: number;
|
|
197
|
+
readonly supportsParallelCalls = false;
|
|
198
|
+
private readonly config;
|
|
199
|
+
private engine?;
|
|
200
|
+
private isInitialized;
|
|
201
|
+
private initializationPromise?;
|
|
202
|
+
constructor(modelId: WebLLMEmbeddingModelId, options?: WebLLMEmbeddingSettings);
|
|
203
|
+
/**
|
|
204
|
+
* Check if the model is initialized and ready to use
|
|
205
|
+
*/
|
|
206
|
+
get isModelInitialized(): boolean;
|
|
207
|
+
private getEngine;
|
|
208
|
+
private _initializeEngine;
|
|
209
|
+
/**
|
|
210
|
+
* Check the availability of the WebLLM embedding model
|
|
211
|
+
* @returns Promise resolving to "unavailable", "available", or "downloadable"
|
|
212
|
+
*/
|
|
213
|
+
availability(): Promise<Availability>;
|
|
214
|
+
/**
|
|
215
|
+
* Creates an engine session with download progress monitoring.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```typescript
|
|
219
|
+
* const engine = await model.createSessionWithProgress(
|
|
220
|
+
* (progress) => {
|
|
221
|
+
* console.log(`Download progress: ${Math.round(progress.progress * 100)}%`);
|
|
222
|
+
* }
|
|
223
|
+
* );
|
|
224
|
+
* ```
|
|
225
|
+
*
|
|
226
|
+
* @param onInitProgress Optional callback receiving progress reports during model download
|
|
227
|
+
* @returns Promise resolving to a configured WebLLM engine
|
|
228
|
+
* @throws {LoadSettingError} When WebLLM isn't available or model is unavailable
|
|
229
|
+
*/
|
|
230
|
+
createSessionWithProgress(onInitProgress?: (progress: InitProgressReport) => void): Promise<MLCEngineInterface>;
|
|
231
|
+
/**
|
|
232
|
+
* Embed texts using the WebLLM embedding model
|
|
233
|
+
*/
|
|
234
|
+
doEmbed(options: EmbeddingModelV3CallOptions): Promise<EmbeddingModelV3Result>;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
interface WebLLMProvider extends ProviderV3 {
|
|
238
|
+
(modelId: WebLLMModelId, settings?: WebLLMSettings): WebLLMLanguageModel;
|
|
239
|
+
/**
|
|
240
|
+
* Creates a model for text generation.
|
|
241
|
+
*/
|
|
242
|
+
languageModel(modelId: WebLLMModelId, settings?: WebLLMSettings): WebLLMLanguageModel;
|
|
243
|
+
/**
|
|
244
|
+
* Creates a model for text generation.
|
|
245
|
+
*/
|
|
246
|
+
chat(modelId: WebLLMModelId, settings?: WebLLMSettings): WebLLMLanguageModel;
|
|
247
|
+
/**
|
|
248
|
+
* Creates a model for text embeddings.
|
|
249
|
+
*/
|
|
250
|
+
embedding(modelId: WebLLMEmbeddingModelId, settings?: WebLLMEmbeddingSettings): EmbeddingModelV3;
|
|
251
|
+
/**
|
|
252
|
+
* Creates a model for text embeddings.
|
|
253
|
+
*/
|
|
254
|
+
embeddingModel: (modelId: WebLLMEmbeddingModelId, settings?: WebLLMEmbeddingSettings) => EmbeddingModelV3;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Create a WebLLM provider instance.
|
|
258
|
+
*/
|
|
259
|
+
declare function createWebLLM(): WebLLMProvider;
|
|
165
260
|
/**
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
* @
|
|
261
|
+
* Default WebLLM provider instance
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```typescript
|
|
265
|
+
* import { webLLM } from "@browser-ai/web-llm";
|
|
266
|
+
*
|
|
267
|
+
* // Language model
|
|
268
|
+
* const chat = webLLM("Llama-3.2-3B-Instruct-q4f16_1-MLC");
|
|
269
|
+
*
|
|
270
|
+
* // Embedding model
|
|
271
|
+
* const embed = webLLM.embeddingModel("snowflake-arctic-embed-m-q0f32-MLC-b32");
|
|
272
|
+
* ```
|
|
169
273
|
*/
|
|
170
|
-
declare
|
|
274
|
+
declare const webLLM: WebLLMProvider;
|
|
171
275
|
|
|
172
|
-
export { WebLLMLanguageModel, type WebLLMModelId, type WebLLMSettings, type WebLLMUIMessage, doesBrowserSupportWebLLM, webLLM };
|
|
276
|
+
export { WebLLMEmbeddingModel, type WebLLMEmbeddingModelId, type WebLLMEmbeddingSettings, WebLLMLanguageModel, type WebLLMModelId, type WebLLMProvider, type WebLLMSettings, type WebLLMUIMessage, createWebLLM, doesBrowserSupportWebLLM, webLLM };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult } from '@ai-sdk/provider';
|
|
2
|
-
import { AppConfig, InitProgressReport, MLCEngineConfig } from '@mlc-ai/web-llm';
|
|
1
|
+
import { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, EmbeddingModelV3, EmbeddingModelV3CallOptions, EmbeddingModelV3Result, ProviderV3 } from '@ai-sdk/provider';
|
|
2
|
+
import { AppConfig, InitProgressReport, MLCEngineConfig, MLCEngineInterface } from '@mlc-ai/web-llm';
|
|
3
3
|
export { InitProgressReport as WebLLMProgress, WebWorkerMLCEngineHandler } from '@mlc-ai/web-llm';
|
|
4
4
|
import { UIMessage } from 'ai';
|
|
5
5
|
|
|
@@ -75,6 +75,11 @@ declare global {
|
|
|
75
75
|
gpu?: GPU;
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Check if the browser supports WebLLM (WebGPU)
|
|
80
|
+
*/
|
|
81
|
+
declare function doesBrowserSupportWebLLM(): boolean;
|
|
82
|
+
|
|
78
83
|
type WebLLMModelId = string;
|
|
79
84
|
interface WebLLMSettings {
|
|
80
85
|
/**
|
|
@@ -97,11 +102,6 @@ interface WebLLMSettings {
|
|
|
97
102
|
*/
|
|
98
103
|
worker?: Worker;
|
|
99
104
|
}
|
|
100
|
-
/**
|
|
101
|
-
* Check if the browser supports WebGPU (required for WebLLM).
|
|
102
|
-
* @returns boolean - true if WebGPU API is available
|
|
103
|
-
*/
|
|
104
|
-
declare function doesBrowserSupportWebLLM(): boolean;
|
|
105
105
|
declare class WebLLMLanguageModel implements LanguageModelV3 {
|
|
106
106
|
readonly specificationVersion = "v3";
|
|
107
107
|
readonly modelId: WebLLMModelId;
|
|
@@ -162,11 +162,115 @@ declare class WebLLMLanguageModel implements LanguageModelV3 {
|
|
|
162
162
|
doStream(options: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult>;
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
+
type WebLLMEmbeddingModelId = string;
|
|
166
|
+
interface WebLLMEmbeddingSettings {
|
|
167
|
+
/**
|
|
168
|
+
* Custom app configuration for WebLLM
|
|
169
|
+
*/
|
|
170
|
+
appConfig?: AppConfig;
|
|
171
|
+
/**
|
|
172
|
+
* Progress callback for model initialization
|
|
173
|
+
*/
|
|
174
|
+
initProgressCallback?: (progress: InitProgressReport) => void;
|
|
175
|
+
/**
|
|
176
|
+
* Engine configuration options
|
|
177
|
+
*/
|
|
178
|
+
engineConfig?: MLCEngineConfig;
|
|
179
|
+
/**
|
|
180
|
+
* A web worker instance to run the model in.
|
|
181
|
+
* When provided, the model will run in a separate thread.
|
|
182
|
+
*
|
|
183
|
+
* @default undefined
|
|
184
|
+
*/
|
|
185
|
+
worker?: Worker;
|
|
186
|
+
/**
|
|
187
|
+
* Maximum number of texts to embed in a single call.
|
|
188
|
+
* @default 100
|
|
189
|
+
*/
|
|
190
|
+
maxEmbeddingsPerCall?: number;
|
|
191
|
+
}
|
|
192
|
+
declare class WebLLMEmbeddingModel implements EmbeddingModelV3 {
|
|
193
|
+
readonly specificationVersion = "v3";
|
|
194
|
+
readonly provider = "web-llm";
|
|
195
|
+
readonly modelId: WebLLMEmbeddingModelId;
|
|
196
|
+
readonly maxEmbeddingsPerCall: number;
|
|
197
|
+
readonly supportsParallelCalls = false;
|
|
198
|
+
private readonly config;
|
|
199
|
+
private engine?;
|
|
200
|
+
private isInitialized;
|
|
201
|
+
private initializationPromise?;
|
|
202
|
+
constructor(modelId: WebLLMEmbeddingModelId, options?: WebLLMEmbeddingSettings);
|
|
203
|
+
/**
|
|
204
|
+
* Check if the model is initialized and ready to use
|
|
205
|
+
*/
|
|
206
|
+
get isModelInitialized(): boolean;
|
|
207
|
+
private getEngine;
|
|
208
|
+
private _initializeEngine;
|
|
209
|
+
/**
|
|
210
|
+
* Check the availability of the WebLLM embedding model
|
|
211
|
+
* @returns Promise resolving to "unavailable", "available", or "downloadable"
|
|
212
|
+
*/
|
|
213
|
+
availability(): Promise<Availability>;
|
|
214
|
+
/**
|
|
215
|
+
* Creates an engine session with download progress monitoring.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```typescript
|
|
219
|
+
* const engine = await model.createSessionWithProgress(
|
|
220
|
+
* (progress) => {
|
|
221
|
+
* console.log(`Download progress: ${Math.round(progress.progress * 100)}%`);
|
|
222
|
+
* }
|
|
223
|
+
* );
|
|
224
|
+
* ```
|
|
225
|
+
*
|
|
226
|
+
* @param onInitProgress Optional callback receiving progress reports during model download
|
|
227
|
+
* @returns Promise resolving to a configured WebLLM engine
|
|
228
|
+
* @throws {LoadSettingError} When WebLLM isn't available or model is unavailable
|
|
229
|
+
*/
|
|
230
|
+
createSessionWithProgress(onInitProgress?: (progress: InitProgressReport) => void): Promise<MLCEngineInterface>;
|
|
231
|
+
/**
|
|
232
|
+
* Embed texts using the WebLLM embedding model
|
|
233
|
+
*/
|
|
234
|
+
doEmbed(options: EmbeddingModelV3CallOptions): Promise<EmbeddingModelV3Result>;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
interface WebLLMProvider extends ProviderV3 {
|
|
238
|
+
(modelId: WebLLMModelId, settings?: WebLLMSettings): WebLLMLanguageModel;
|
|
239
|
+
/**
|
|
240
|
+
* Creates a model for text generation.
|
|
241
|
+
*/
|
|
242
|
+
languageModel(modelId: WebLLMModelId, settings?: WebLLMSettings): WebLLMLanguageModel;
|
|
243
|
+
/**
|
|
244
|
+
* Creates a model for text generation.
|
|
245
|
+
*/
|
|
246
|
+
chat(modelId: WebLLMModelId, settings?: WebLLMSettings): WebLLMLanguageModel;
|
|
247
|
+
/**
|
|
248
|
+
* Creates a model for text embeddings.
|
|
249
|
+
*/
|
|
250
|
+
embedding(modelId: WebLLMEmbeddingModelId, settings?: WebLLMEmbeddingSettings): EmbeddingModelV3;
|
|
251
|
+
/**
|
|
252
|
+
* Creates a model for text embeddings.
|
|
253
|
+
*/
|
|
254
|
+
embeddingModel: (modelId: WebLLMEmbeddingModelId, settings?: WebLLMEmbeddingSettings) => EmbeddingModelV3;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Create a WebLLM provider instance.
|
|
258
|
+
*/
|
|
259
|
+
declare function createWebLLM(): WebLLMProvider;
|
|
165
260
|
/**
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
* @
|
|
261
|
+
* Default WebLLM provider instance
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```typescript
|
|
265
|
+
* import { webLLM } from "@browser-ai/web-llm";
|
|
266
|
+
*
|
|
267
|
+
* // Language model
|
|
268
|
+
* const chat = webLLM("Llama-3.2-3B-Instruct-q4f16_1-MLC");
|
|
269
|
+
*
|
|
270
|
+
* // Embedding model
|
|
271
|
+
* const embed = webLLM.embeddingModel("snowflake-arctic-embed-m-q0f32-MLC-b32");
|
|
272
|
+
* ```
|
|
169
273
|
*/
|
|
170
|
-
declare
|
|
274
|
+
declare const webLLM: WebLLMProvider;
|
|
171
275
|
|
|
172
|
-
export { WebLLMLanguageModel, type WebLLMModelId, type WebLLMSettings, type WebLLMUIMessage, doesBrowserSupportWebLLM, webLLM };
|
|
276
|
+
export { WebLLMEmbeddingModel, type WebLLMEmbeddingModelId, type WebLLMEmbeddingSettings, WebLLMLanguageModel, type WebLLMModelId, type WebLLMProvider, type WebLLMSettings, type WebLLMUIMessage, createWebLLM, doesBrowserSupportWebLLM, webLLM };
|
package/dist/index.js
CHANGED
|
@@ -20,8 +20,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
WebLLMEmbeddingModel: () => WebLLMEmbeddingModel,
|
|
23
24
|
WebLLMLanguageModel: () => WebLLMLanguageModel,
|
|
24
|
-
WebWorkerMLCEngineHandler: () =>
|
|
25
|
+
WebWorkerMLCEngineHandler: () => import_web_llm3.WebWorkerMLCEngineHandler,
|
|
26
|
+
createWebLLM: () => createWebLLM,
|
|
25
27
|
doesBrowserSupportWebLLM: () => doesBrowserSupportWebLLM,
|
|
26
28
|
webLLM: () => webLLM
|
|
27
29
|
});
|
|
@@ -983,7 +985,7 @@ ${existingContent}` : "")
|
|
|
983
985
|
];
|
|
984
986
|
}
|
|
985
987
|
|
|
986
|
-
// src/
|
|
988
|
+
// src/utils/browser.ts
|
|
987
989
|
function isMobile() {
|
|
988
990
|
if (typeof navigator === "undefined") return false;
|
|
989
991
|
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
@@ -1000,6 +1002,8 @@ function checkWebGPU() {
|
|
|
1000
1002
|
function doesBrowserSupportWebLLM() {
|
|
1001
1003
|
return checkWebGPU();
|
|
1002
1004
|
}
|
|
1005
|
+
|
|
1006
|
+
// src/web-llm-language-model.ts
|
|
1003
1007
|
function extractToolName(content) {
|
|
1004
1008
|
const jsonMatch = content.match(/\{\s*"name"\s*:\s*"([^"]+)"/);
|
|
1005
1009
|
if (jsonMatch) {
|
|
@@ -1714,17 +1718,211 @@ var WebLLMLanguageModel = class {
|
|
|
1714
1718
|
}
|
|
1715
1719
|
};
|
|
1716
1720
|
|
|
1717
|
-
// src/
|
|
1721
|
+
// src/web-llm-embedding-model.ts
|
|
1718
1722
|
var import_web_llm2 = require("@mlc-ai/web-llm");
|
|
1723
|
+
var WebLLMEmbeddingModel = class {
|
|
1724
|
+
constructor(modelId, options = {}) {
|
|
1725
|
+
this.specificationVersion = "v3";
|
|
1726
|
+
this.provider = "web-llm";
|
|
1727
|
+
this.supportsParallelCalls = false;
|
|
1728
|
+
this.isInitialized = false;
|
|
1729
|
+
this.modelId = modelId;
|
|
1730
|
+
this.maxEmbeddingsPerCall = options.maxEmbeddingsPerCall ?? 100;
|
|
1731
|
+
this.config = {
|
|
1732
|
+
provider: this.provider,
|
|
1733
|
+
modelId,
|
|
1734
|
+
options
|
|
1735
|
+
};
|
|
1736
|
+
}
|
|
1737
|
+
/**
|
|
1738
|
+
* Check if the model is initialized and ready to use
|
|
1739
|
+
*/
|
|
1740
|
+
get isModelInitialized() {
|
|
1741
|
+
return this.isInitialized;
|
|
1742
|
+
}
|
|
1743
|
+
async getEngine(options, onInitProgress) {
|
|
1744
|
+
const availability = await this.availability();
|
|
1745
|
+
if (availability === "unavailable") {
|
|
1746
|
+
throw new LoadSettingError({
|
|
1747
|
+
message: "WebLLM is not available. This library requires a browser with WebGPU support."
|
|
1748
|
+
});
|
|
1749
|
+
}
|
|
1750
|
+
if (this.engine && this.isInitialized) return this.engine;
|
|
1751
|
+
if (this.initializationPromise) {
|
|
1752
|
+
await this.initializationPromise;
|
|
1753
|
+
if (this.engine) return this.engine;
|
|
1754
|
+
}
|
|
1755
|
+
this.initializationPromise = this._initializeEngine(
|
|
1756
|
+
options,
|
|
1757
|
+
onInitProgress
|
|
1758
|
+
);
|
|
1759
|
+
await this.initializationPromise;
|
|
1760
|
+
if (!this.engine) {
|
|
1761
|
+
throw new LoadSettingError({
|
|
1762
|
+
message: "Engine initialization failed"
|
|
1763
|
+
});
|
|
1764
|
+
}
|
|
1765
|
+
return this.engine;
|
|
1766
|
+
}
|
|
1767
|
+
async _initializeEngine(options, onInitProgress) {
|
|
1768
|
+
try {
|
|
1769
|
+
const engineConfig = {
|
|
1770
|
+
...this.config.options.engineConfig,
|
|
1771
|
+
...options,
|
|
1772
|
+
initProgressCallback: onInitProgress || this.config.options.initProgressCallback
|
|
1773
|
+
};
|
|
1774
|
+
if (this.config.options.worker) {
|
|
1775
|
+
this.engine = await (0, import_web_llm2.CreateWebWorkerMLCEngine)(
|
|
1776
|
+
this.config.options.worker,
|
|
1777
|
+
this.modelId,
|
|
1778
|
+
engineConfig
|
|
1779
|
+
);
|
|
1780
|
+
} else {
|
|
1781
|
+
this.engine = new import_web_llm2.MLCEngine(engineConfig);
|
|
1782
|
+
await this.engine.reload(this.modelId);
|
|
1783
|
+
}
|
|
1784
|
+
this.isInitialized = true;
|
|
1785
|
+
} catch (error) {
|
|
1786
|
+
this.engine = void 0;
|
|
1787
|
+
this.isInitialized = false;
|
|
1788
|
+
this.initializationPromise = void 0;
|
|
1789
|
+
throw new LoadSettingError({
|
|
1790
|
+
message: `Failed to initialize WebLLM embedding engine: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1791
|
+
});
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
/**
|
|
1795
|
+
* Check the availability of the WebLLM embedding model
|
|
1796
|
+
* @returns Promise resolving to "unavailable", "available", or "downloadable"
|
|
1797
|
+
*/
|
|
1798
|
+
async availability() {
|
|
1799
|
+
if (this.isInitialized) {
|
|
1800
|
+
return "available";
|
|
1801
|
+
}
|
|
1802
|
+
if (this.config.options.worker && isMobile()) {
|
|
1803
|
+
return "downloadable";
|
|
1804
|
+
}
|
|
1805
|
+
const supported = checkWebGPU();
|
|
1806
|
+
return supported ? "downloadable" : "unavailable";
|
|
1807
|
+
}
|
|
1808
|
+
/**
|
|
1809
|
+
* Creates an engine session with download progress monitoring.
|
|
1810
|
+
*
|
|
1811
|
+
* @example
|
|
1812
|
+
* ```typescript
|
|
1813
|
+
* const engine = await model.createSessionWithProgress(
|
|
1814
|
+
* (progress) => {
|
|
1815
|
+
* console.log(`Download progress: ${Math.round(progress.progress * 100)}%`);
|
|
1816
|
+
* }
|
|
1817
|
+
* );
|
|
1818
|
+
* ```
|
|
1819
|
+
*
|
|
1820
|
+
* @param onInitProgress Optional callback receiving progress reports during model download
|
|
1821
|
+
* @returns Promise resolving to a configured WebLLM engine
|
|
1822
|
+
* @throws {LoadSettingError} When WebLLM isn't available or model is unavailable
|
|
1823
|
+
*/
|
|
1824
|
+
async createSessionWithProgress(onInitProgress) {
|
|
1825
|
+
return this.getEngine(void 0, onInitProgress);
|
|
1826
|
+
}
|
|
1827
|
+
/**
|
|
1828
|
+
* Embed texts using the WebLLM embedding model
|
|
1829
|
+
*/
|
|
1830
|
+
async doEmbed(options) {
|
|
1831
|
+
const { values, abortSignal } = options;
|
|
1832
|
+
if (values.length > this.maxEmbeddingsPerCall) {
|
|
1833
|
+
throw new TooManyEmbeddingValuesForCallError({
|
|
1834
|
+
provider: this.provider,
|
|
1835
|
+
modelId: this.modelId,
|
|
1836
|
+
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
1837
|
+
values
|
|
1838
|
+
});
|
|
1839
|
+
}
|
|
1840
|
+
if (abortSignal?.aborted) {
|
|
1841
|
+
throw new Error("Operation was aborted");
|
|
1842
|
+
}
|
|
1843
|
+
const engine = await this.getEngine();
|
|
1844
|
+
const abortHandler = () => {
|
|
1845
|
+
engine.interruptGenerate();
|
|
1846
|
+
};
|
|
1847
|
+
if (abortSignal) {
|
|
1848
|
+
abortSignal.addEventListener("abort", abortHandler);
|
|
1849
|
+
}
|
|
1850
|
+
try {
|
|
1851
|
+
const response = await engine.embeddings.create({
|
|
1852
|
+
input: values,
|
|
1853
|
+
model: this.modelId,
|
|
1854
|
+
...abortSignal && !this.config.options.worker && { signal: abortSignal }
|
|
1855
|
+
});
|
|
1856
|
+
const sortedEmbeddings = response.data.sort((a, b) => a.index - b.index).map((e) => e.embedding);
|
|
1857
|
+
return {
|
|
1858
|
+
embeddings: sortedEmbeddings,
|
|
1859
|
+
usage: {
|
|
1860
|
+
tokens: response.usage.total_tokens
|
|
1861
|
+
},
|
|
1862
|
+
providerMetadata: {
|
|
1863
|
+
webllm: {
|
|
1864
|
+
model: response.model,
|
|
1865
|
+
promptTokens: response.usage.prompt_tokens,
|
|
1866
|
+
totalTokens: response.usage.total_tokens,
|
|
1867
|
+
prefillTokensPerSecond: response.usage.extra?.prefill_tokens_per_s
|
|
1868
|
+
}
|
|
1869
|
+
},
|
|
1870
|
+
warnings: []
|
|
1871
|
+
};
|
|
1872
|
+
} catch (error) {
|
|
1873
|
+
throw new Error(
|
|
1874
|
+
`WebLLM embedding failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1875
|
+
);
|
|
1876
|
+
} finally {
|
|
1877
|
+
if (abortSignal) {
|
|
1878
|
+
abortSignal.removeEventListener("abort", abortHandler);
|
|
1879
|
+
}
|
|
1880
|
+
}
|
|
1881
|
+
}
|
|
1882
|
+
};
|
|
1883
|
+
|
|
1884
|
+
// src/index.ts
|
|
1885
|
+
var import_web_llm3 = require("@mlc-ai/web-llm");
|
|
1719
1886
|
|
|
1720
1887
|
// src/web-llm-provider.ts
|
|
1721
|
-
function
|
|
1722
|
-
|
|
1888
|
+
function createWebLLM() {
|
|
1889
|
+
const createLanguageModel = (modelId, settings) => {
|
|
1890
|
+
return new WebLLMLanguageModel(modelId, settings);
|
|
1891
|
+
};
|
|
1892
|
+
const createEmbeddingModel = (modelId, settings) => {
|
|
1893
|
+
return new WebLLMEmbeddingModel(modelId, settings);
|
|
1894
|
+
};
|
|
1895
|
+
const provider = function(modelId, settings) {
|
|
1896
|
+
if (new.target) {
|
|
1897
|
+
throw new Error(
|
|
1898
|
+
"The WebLLM model function cannot be called with the new keyword."
|
|
1899
|
+
);
|
|
1900
|
+
}
|
|
1901
|
+
return createLanguageModel(modelId, settings);
|
|
1902
|
+
};
|
|
1903
|
+
provider.specificationVersion = "v3";
|
|
1904
|
+
provider.languageModel = createLanguageModel;
|
|
1905
|
+
provider.chat = createLanguageModel;
|
|
1906
|
+
provider.embedding = createEmbeddingModel;
|
|
1907
|
+
provider.embeddingModel = createEmbeddingModel;
|
|
1908
|
+
provider.imageModel = (modelId) => {
|
|
1909
|
+
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
1910
|
+
};
|
|
1911
|
+
provider.speechModel = (modelId) => {
|
|
1912
|
+
throw new NoSuchModelError({ modelId, modelType: "speechModel" });
|
|
1913
|
+
};
|
|
1914
|
+
provider.transcriptionModel = (modelId) => {
|
|
1915
|
+
throw new NoSuchModelError({ modelId, modelType: "transcriptionModel" });
|
|
1916
|
+
};
|
|
1917
|
+
return provider;
|
|
1723
1918
|
}
|
|
1919
|
+
var webLLM = createWebLLM();
|
|
1724
1920
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1725
1921
|
0 && (module.exports = {
|
|
1922
|
+
WebLLMEmbeddingModel,
|
|
1726
1923
|
WebLLMLanguageModel,
|
|
1727
1924
|
WebWorkerMLCEngineHandler,
|
|
1925
|
+
createWebLLM,
|
|
1728
1926
|
doesBrowserSupportWebLLM,
|
|
1729
1927
|
webLLM
|
|
1730
1928
|
});
|