webslm 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +373 -2
- package/dist/ai-sdk/index.d.ts +81 -0
- package/dist/ai-sdk/index.js +90 -0
- package/dist/ai-sdk/language-model.d.ts +77 -0
- package/dist/ai-sdk/language-model.js +422 -0
- package/dist/ai-sdk/loader.d.ts +54 -0
- package/dist/ai-sdk/loader.js +170 -0
- package/dist/ai-sdk/output.d.ts +43 -0
- package/dist/ai-sdk/output.js +152 -0
- package/dist/ai-sdk/prompt.d.ts +21 -0
- package/dist/ai-sdk/prompt.js +111 -0
- package/dist/async-queue.d.ts +22 -0
- package/dist/async-queue.js +70 -0
- package/dist/cache/cache-api.d.ts +29 -0
- package/dist/cache/cache-api.js +115 -0
- package/dist/cache/index.d.ts +21 -0
- package/dist/cache/index.js +42 -0
- package/dist/cache/memory.d.ts +12 -0
- package/dist/cache/memory.js +61 -0
- package/dist/cache/models.d.ts +67 -0
- package/dist/cache/models.js +107 -0
- package/dist/cache/node-fs.d.ts +21 -0
- package/dist/cache/node-fs.js +147 -0
- package/dist/cache/opfs.d.ts +69 -0
- package/dist/cache/opfs.js +181 -0
- package/dist/cache/store.d.ts +53 -0
- package/dist/cache/store.js +64 -0
- package/dist/cache-management.d.ts +23 -0
- package/dist/cache-management.js +58 -0
- package/dist/capabilities.d.ts +67 -0
- package/dist/capabilities.js +55 -0
- package/dist/client.d.ts +61 -0
- package/dist/client.js +256 -0
- package/dist/curated.d.ts +22 -0
- package/dist/curated.js +53 -0
- package/dist/download.d.ts +51 -0
- package/dist/download.js +289 -0
- package/dist/engine.d.ts +54 -0
- package/dist/engine.js +510 -0
- package/dist/errors.d.ts +81 -0
- package/dist/errors.js +89 -0
- package/dist/hub.d.ts +185 -0
- package/dist/hub.js +409 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +41 -0
- package/dist/models.d.ts +225 -0
- package/dist/models.js +730 -0
- package/dist/node-pool-worker.d.ts +6 -0
- package/dist/node-pool-worker.js +32 -0
- package/dist/node-web-worker.d.ts +23 -0
- package/dist/node-web-worker.js +84 -0
- package/dist/node-worker.d.ts +2 -0
- package/dist/node-worker.js +9 -0
- package/dist/pool-worker.d.ts +36 -0
- package/dist/pool-worker.js +30 -0
- package/dist/pool.d.ts +17 -0
- package/dist/pool.js +41 -0
- package/dist/protocol.d.ts +246 -0
- package/dist/protocol.js +19 -0
- package/dist/runtime.d.ts +21 -0
- package/dist/runtime.js +53 -0
- package/dist/safetensors.d.ts +20 -0
- package/dist/safetensors.js +2 -0
- package/dist/simple.d.ts +310 -0
- package/dist/simple.js +553 -0
- package/dist/types.d.ts +412 -0
- package/dist/types.js +2 -0
- package/dist/wasm-flavour.d.ts +85 -0
- package/dist/wasm-flavour.js +169 -0
- package/dist/wasm.d.ts +34 -0
- package/dist/wasm.js +38 -0
- package/dist/worker.d.ts +6 -0
- package/dist/worker.js +11 -0
- package/package.json +92 -3
- package/wasm/relaxed/webslm_wasm.d.ts +325 -0
- package/wasm/relaxed/webslm_wasm.js +1899 -0
- package/wasm/relaxed/webslm_wasm_bg.wasm +0 -0
- package/wasm/relaxed/webslm_wasm_bg.wasm.d.ts +56 -0
- package/wasm/simd/webslm_wasm.d.ts +325 -0
- package/wasm/simd/webslm_wasm.js +1899 -0
- package/wasm/simd/webslm_wasm_bg.wasm +0 -0
- package/wasm/simd/webslm_wasm_bg.wasm.d.ts +56 -0
- package/wasm/threads/snippets/wasm-bindgen-rayon-38edf6e439f6d70d/src/workerHelpers.js +108 -0
- package/wasm/threads/webslm_wasm.d.ts +357 -0
- package/wasm/threads/webslm_wasm.js +2100 -0
- package/wasm/threads/webslm_wasm_bg.wasm +0 -0
- package/wasm/threads/webslm_wasm_bg.wasm.d.ts +66 -0
package/dist/models.d.ts
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/** Public model API: {@link loadModel}, {@link Model} and {@link Session}. */
|
|
2
|
+
import { EngineClient } from "./client.js";
|
|
3
|
+
import type { AdapterSource, ApplyAdapterOptions, ChatMessage, ChatStream, CompletionOptions, Device, FetchFile, GenerateOptions, LoadModelOptions, LoraInfo, ModelInfo, ModelSource, ScoreOptions, ScoreOutput, StepOptions } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* An engine (a worker, or the calling thread) that several models can share
|
|
6
|
+
* (see {@link LoadModelOptions.engine}): one WebAssembly instance, its memory
|
|
7
|
+
* and thread pool, for a small helper model next to a larger one.
|
|
8
|
+
*/
|
|
9
|
+
export declare class EngineHandle {
|
|
10
|
+
#private;
|
|
11
|
+
/** @internal */
|
|
12
|
+
readonly client: EngineClient;
|
|
13
|
+
/** @internal */
|
|
14
|
+
constructor(client: EngineClient);
|
|
15
|
+
/** Stops the engine: every model loaded in it is unloaded. */
|
|
16
|
+
terminate(): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Starts an engine to share between models: pass it as `engine` to
|
|
20
|
+
* {@link loadModel}. Unloading a model then frees it but keeps the engine;
|
|
21
|
+
* {@link EngineHandle.terminate} stops it.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* const engine = await createEngine();
|
|
25
|
+
* const small = await loadModel("HuggingFaceTB/SmolLM2-135M-Instruct", { engine });
|
|
26
|
+
* const large = await loadModel("Qwen/Qwen3-0.6B", { engine });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function createEngine(options?: {
|
|
30
|
+
readonly worker?: boolean;
|
|
31
|
+
}): Promise<EngineHandle>;
|
|
32
|
+
/**
|
|
33
|
+
* Validates `device` (default `"auto"`). The engine decides what actually
|
|
34
|
+
* runs: `"auto"` is WebGPU for models above ~100M parameters when an adapter
|
|
35
|
+
* is available, the CPU otherwise; `"webgpu"` falls back to the CPU (with
|
|
36
|
+
* {@link ModelInfo.deviceNote} saying why) when no GPU device can be created.
|
|
37
|
+
*/
|
|
38
|
+
export declare function resolveDevice(device: Device | undefined): Device;
|
|
39
|
+
/** Normalized form of a {@link ModelSource}. */
|
|
40
|
+
export type NormalizedSource = {
|
|
41
|
+
readonly kind: "hub";
|
|
42
|
+
readonly repo: string;
|
|
43
|
+
readonly revision?: string;
|
|
44
|
+
readonly hubUrl?: string;
|
|
45
|
+
} | {
|
|
46
|
+
readonly kind: "files";
|
|
47
|
+
readonly files: NonNullable<Extract<ModelSource, {
|
|
48
|
+
files: unknown;
|
|
49
|
+
}>["files"]>;
|
|
50
|
+
} | {
|
|
51
|
+
readonly kind: "fetch";
|
|
52
|
+
readonly fetch: FetchFile;
|
|
53
|
+
};
|
|
54
|
+
/** Checks a source and turns a repository id string into a hub source. */
|
|
55
|
+
export declare function normalizeSource(source: string | ModelSource, revision?: string): NormalizedSource;
|
|
56
|
+
/**
|
|
57
|
+
* Checks a LoRA adapter source: like {@link normalizeSource}, but `files`
|
|
58
|
+
* must hold `adapter_config.json` and `adapter_model.safetensors`.
|
|
59
|
+
*/
|
|
60
|
+
export declare function normalizeAdapterSource(source: AdapterSource, revision?: string): NormalizedSource;
|
|
61
|
+
/**
|
|
62
|
+
* Loads a model from the Hugging Face Hub, local files or a custom store.
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* const model = await loadModel("HuggingFaceTB/SmolLM2-135M-Instruct", {
|
|
66
|
+
* onProgress: (p) => console.log(p.phase, p.overallLoadedBytes, p.overallTotalBytes),
|
|
67
|
+
* });
|
|
68
|
+
* for await (const delta of model.chat([{ role: "user", content: "Hi!" }])) console.log(delta);
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* Hub checkpoints are downloaded once into the cache (OPFS in browsers,
|
|
72
|
+
* `~/.cache/webslm` in Node) and read from it afterwards, without network.
|
|
73
|
+
*/
|
|
74
|
+
export declare function loadModel(source: string | ModelSource, options?: LoadModelOptions): Promise<Model>;
|
|
75
|
+
/** Operations shared by {@link Model} (its default session) and {@link Session}. */
|
|
76
|
+
declare abstract class Conversation {
|
|
77
|
+
#private;
|
|
78
|
+
protected readonly client: EngineClient;
|
|
79
|
+
protected readonly modelId: number;
|
|
80
|
+
protected abstract readonly sessionId: number | undefined;
|
|
81
|
+
protected abstract readonly hasChatTemplate: boolean;
|
|
82
|
+
protected abstract onLength(length: number): void;
|
|
83
|
+
constructor(client: EngineClient, modelId: number);
|
|
84
|
+
/**
|
|
85
|
+
* Answers a conversation, streaming text deltas. Earlier turns already in
|
|
86
|
+
* the session's cache are not recomputed. Stopping (`stop()`, `break`, or
|
|
87
|
+
* the signal) ends the stream normally with `finishReason: "aborted"`.
|
|
88
|
+
*/
|
|
89
|
+
chat(messages: readonly ChatMessage[], options?: GenerateOptions): ChatStream;
|
|
90
|
+
/**
|
|
91
|
+
* Generates a whole answer. A string prompt is sent as a user message when
|
|
92
|
+
* the checkpoint has a chat template (pass `raw: true` to complete it as
|
|
93
|
+
* plain text); an array is a conversation. Rejects with `ABORTED` when the
|
|
94
|
+
* signal fires.
|
|
95
|
+
*/
|
|
96
|
+
generate(prompt: string | readonly ChatMessage[], options?: CompletionOptions): Promise<string>;
|
|
97
|
+
/** Encodes text into token ids. */
|
|
98
|
+
tokenize(text: string, options?: {
|
|
99
|
+
addSpecialTokens?: boolean;
|
|
100
|
+
}): Promise<Uint32Array>;
|
|
101
|
+
/** Decodes token ids into text. */
|
|
102
|
+
detokenize(ids: ArrayLike<number>, options?: {
|
|
103
|
+
skipSpecialTokens?: boolean;
|
|
104
|
+
}): Promise<string>;
|
|
105
|
+
/**
|
|
106
|
+
* Renders a conversation with the chat template, ending with the header of
|
|
107
|
+
* a new assistant turn. `templateKwargs` are passed to the template (e.g.
|
|
108
|
+
* `{ enable_thinking: false }`).
|
|
109
|
+
*/
|
|
110
|
+
renderChat(messages: readonly ChatMessage[], templateKwargs?: Readonly<Record<string, unknown>>): Promise<string>;
|
|
111
|
+
}
|
|
112
|
+
/** A loaded model. Its own `chat`/`generate` use a default session. */
|
|
113
|
+
export declare class Model extends Conversation {
|
|
114
|
+
#private;
|
|
115
|
+
protected readonly sessionId: undefined;
|
|
116
|
+
/**
|
|
117
|
+
* @internal `base` holds what later reads of the model's own files need:
|
|
118
|
+
* the custom store of a `fetch` source and the Node cache directory.
|
|
119
|
+
* `ownsEngine` is false for a model loaded into a shared engine.
|
|
120
|
+
*/
|
|
121
|
+
constructor(client: EngineClient, modelId: number, info: ModelInfo, base?: {
|
|
122
|
+
readonly fetch?: FetchFile;
|
|
123
|
+
readonly cacheDir?: string;
|
|
124
|
+
}, ownsEngine?: boolean);
|
|
125
|
+
protected get hasChatTemplate(): boolean;
|
|
126
|
+
protected onLength(): void;
|
|
127
|
+
/** Architecture, sizes, quant, backend and the files loaded (with SHA-256). */
|
|
128
|
+
info(): ModelInfo;
|
|
129
|
+
/** Clears the default session's cache. */
|
|
130
|
+
reset(): Promise<void>;
|
|
131
|
+
/**
|
|
132
|
+
* Merges a PEFT LoRA adapter (`adapter_config.json` and
|
|
133
|
+
* `adapter_model.safetensors`, as published on the Hub) into the weights,
|
|
134
|
+
* replacing the adapter applied before, if any. The affected base
|
|
135
|
+
* matrices are read again from the model's source and merged in f32
|
|
136
|
+
* before quantization: decoding runs at the base model's speed.
|
|
137
|
+
*
|
|
138
|
+
* Rejects with `INVALID_ADAPTER`, leaving the model unchanged, when the
|
|
139
|
+
* adapter does not fit the model (a module it does not have, other
|
|
140
|
+
* shapes, or a target without weights). Every session (the default one
|
|
141
|
+
* included) starts over at its next call.
|
|
142
|
+
*
|
|
143
|
+
* ```ts
|
|
144
|
+
* const lora = await model.applyAdapter("user/my-lora", { scale: 0.8 });
|
|
145
|
+
* console.log(lora.modules.length, "modules adapted");
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
applyAdapter(source: AdapterSource, options?: ApplyAdapterOptions): Promise<LoraInfo>;
|
|
149
|
+
/**
|
|
150
|
+
* Removes the LoRA adapter, restoring the base weights exactly (the
|
|
151
|
+
* logits are bit-identical to those of the base model). Resolves to the
|
|
152
|
+
* adapter removed, or `undefined` when there was none.
|
|
153
|
+
*/
|
|
154
|
+
removeAdapter(options?: {
|
|
155
|
+
readonly signal?: AbortSignal;
|
|
156
|
+
}): Promise<LoraInfo | undefined>;
|
|
157
|
+
/** Starts an independent session (its own KV cache) for low-level use. */
|
|
158
|
+
session(): Promise<Session>;
|
|
159
|
+
/**
|
|
160
|
+
* Frees the model and stops its worker (unless it runs in a shared
|
|
161
|
+
* {@link EngineHandle}). Every later call rejects with `DISPOSED`.
|
|
162
|
+
*/
|
|
163
|
+
unload(): Promise<void>;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* An independent conversation over a model, with low-level access for
|
|
167
|
+
* callers that pick tokens themselves: teacher-forced `score`, `step`,
|
|
168
|
+
* logits and hidden states.
|
|
169
|
+
*/
|
|
170
|
+
export declare class Session extends Conversation {
|
|
171
|
+
#private;
|
|
172
|
+
protected readonly sessionId: number;
|
|
173
|
+
/** @internal */
|
|
174
|
+
constructor(client: EngineClient, modelId: number, sessionId: number, info: ModelInfo, onDispose: () => void);
|
|
175
|
+
protected get hasChatTemplate(): boolean;
|
|
176
|
+
protected onLength(length: number): void;
|
|
177
|
+
/** Positions in the cache (as of the last completed call). */
|
|
178
|
+
get length(): number;
|
|
179
|
+
/** Teacher-forced pass: appends `ids` to the cache and returns logits and hidden states. */
|
|
180
|
+
score(ids: ArrayLike<number>, options?: ScoreOptions): Promise<ScoreOutput>;
|
|
181
|
+
/** One decode step with a token the caller picked. */
|
|
182
|
+
step(id: number, options?: StepOptions): Promise<ScoreOutput>;
|
|
183
|
+
/**
|
|
184
|
+
* A new session holding a copy of this one's cache: both continue from
|
|
185
|
+
* the same computed prefix (generate or score several continuations
|
|
186
|
+
* without recomputing it).
|
|
187
|
+
*/
|
|
188
|
+
fork(): Promise<Session>;
|
|
189
|
+
/**
|
|
190
|
+
* Log-probabilities of several continuations of one prefix: entry `i`
|
|
191
|
+
* holds one value per token of `continuations[i]`, given the prefix and
|
|
192
|
+
* the tokens before it. The prefix runs through the model once (not at
|
|
193
|
+
* all when the session already holds it, but for its last token); the
|
|
194
|
+
* session then holds the prefix.
|
|
195
|
+
*/
|
|
196
|
+
scoreContinuations(prefix: ArrayLike<number>, continuations: readonly ArrayLike<number>[]): Promise<Float32Array[]>;
|
|
197
|
+
/**
|
|
198
|
+
* Context shift: drops `drop` cached positions after the first `keep`
|
|
199
|
+
* and recomputes the rest, so a long conversation keeps going (see also
|
|
200
|
+
* the `contextShift` generation option).
|
|
201
|
+
*/
|
|
202
|
+
shiftContext(keep: number, drop: number): Promise<void>;
|
|
203
|
+
/**
|
|
204
|
+
* Keeps the first `length` positions. Recurrent models (LFM2, LFM3, Qwen3.5) recompute their
|
|
205
|
+
* state for those positions, which costs a prefill of `length` tokens.
|
|
206
|
+
*/
|
|
207
|
+
truncate(length: number): Promise<void>;
|
|
208
|
+
/** Empties the cache. */
|
|
209
|
+
reset(): Promise<void>;
|
|
210
|
+
/** Frees the session's cache. */
|
|
211
|
+
dispose(): Promise<void>;
|
|
212
|
+
}
|
|
213
|
+
/** A model family. */
|
|
214
|
+
export interface ModelFamily {
|
|
215
|
+
readonly name: string;
|
|
216
|
+
/** `model_type` in `config.json`, when known. */
|
|
217
|
+
readonly modelType?: string;
|
|
218
|
+
readonly modality: "text" | "vision-language";
|
|
219
|
+
/** A checkpoint the family is validated against. */
|
|
220
|
+
readonly exampleRepo?: string;
|
|
221
|
+
readonly status: "planned" | "supported";
|
|
222
|
+
}
|
|
223
|
+
/** Model families: supported today and on the roadmap. */
|
|
224
|
+
export declare const MODEL_FAMILIES: readonly ModelFamily[];
|
|
225
|
+
export {};
|