webslm 0.5.0 → 0.6.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 +30 -2
- package/dist/ai-sdk/language-model.d.ts +4 -1
- package/dist/ai-sdk/language-model.js +6 -3
- package/dist/ai-sdk/prompt.d.ts +5 -3
- package/dist/ai-sdk/prompt.js +57 -5
- package/dist/client.d.ts +1 -2
- package/dist/curated.js +9 -0
- package/dist/engine.js +8 -2
- package/dist/fit.d.ts +1 -1
- package/dist/fit.js +2 -1
- package/dist/hub.js +7 -0
- package/dist/index.d.ts +1 -1
- package/dist/media.d.ts +72 -0
- package/dist/media.js +358 -0
- package/dist/memory.d.ts +5 -0
- package/dist/memory.js +47 -6
- package/dist/models.d.ts +8 -4
- package/dist/models.js +69 -10
- package/dist/native.d.ts +3 -0
- package/dist/protocol.d.ts +28 -1
- package/dist/recommended-sampling.js +6 -1
- package/dist/simple.d.ts +11 -2
- package/dist/simple.js +17 -8
- package/dist/types.d.ts +95 -0
- package/native-manifest.json +29 -29
- package/package.json +1 -1
- package/wasm/relaxed/webslm_wasm.d.ts +23 -4
- package/wasm/relaxed/webslm_wasm.js +69 -16
- package/wasm/relaxed/webslm_wasm_bg.wasm +0 -0
- package/wasm/relaxed/webslm_wasm_bg.wasm.d.ts +5 -3
- package/wasm/simd/webslm_wasm.d.ts +23 -4
- package/wasm/simd/webslm_wasm.js +69 -16
- package/wasm/simd/webslm_wasm_bg.wasm +0 -0
- package/wasm/simd/webslm_wasm_bg.wasm.d.ts +5 -3
- package/wasm/threads/webslm_wasm.d.ts +24 -5
- package/wasm/threads/webslm_wasm.js +74 -21
- package/wasm/threads/webslm_wasm_bg.wasm +0 -0
- package/wasm/threads/webslm_wasm_bg.wasm.d.ts +6 -4
package/dist/models.js
CHANGED
|
@@ -5,6 +5,7 @@ import { recommendedSampling } from "./curated.js";
|
|
|
5
5
|
import { WebslmError, abortError, isAborted, toWebslmError } from "./errors.js";
|
|
6
6
|
import { autoQuant, getBrowserResources, readCheckpointShapes } from "./fit.js";
|
|
7
7
|
import { checkModel, isGgufFile } from "./hub.js";
|
|
8
|
+
import { messageParts, prepareMessages, wireMessage } from "./media.js";
|
|
8
9
|
import { isNode } from "./runtime.js";
|
|
9
10
|
const QUANTS = ["q8", "q4", "f32"];
|
|
10
11
|
const ADAPTER_MODES = ["unmerged", "merged"];
|
|
@@ -263,6 +264,7 @@ export async function loadModel(source, options = {}) {
|
|
|
263
264
|
...(options.cacheDir === undefined ? {} : { cacheDir: options.cacheDir }),
|
|
264
265
|
...(options.weightCache === false ? { weightCache: false } : {}),
|
|
265
266
|
...(options.deterministic === true ? { deterministic: true } : {}),
|
|
267
|
+
...(options.vision === false ? { vision: false } : {}),
|
|
266
268
|
...(options.activations === undefined ? {} : { activations: options.activations }),
|
|
267
269
|
...(options.wasmFlavour === undefined ? {} : { wasmFlavour: options.wasmFlavour }),
|
|
268
270
|
...(options.threads === undefined ? {} : { threads: options.threads }),
|
|
@@ -314,7 +316,7 @@ export async function resolveAutoQuant(source, options) {
|
|
|
314
316
|
else {
|
|
315
317
|
shapes = await readCheckpointShapes(checkpointReader(source));
|
|
316
318
|
}
|
|
317
|
-
return autoQuant(shapes, resources, options.contextLength);
|
|
319
|
+
return autoQuant(shapes, resources, options.contextLength, options.vision);
|
|
318
320
|
}
|
|
319
321
|
catch (error) {
|
|
320
322
|
if (isAborted(options.signal))
|
|
@@ -387,6 +389,15 @@ function wireOptions(options) {
|
|
|
387
389
|
out.loopDetection = options.loopDetection;
|
|
388
390
|
if (options.contextShift !== undefined)
|
|
389
391
|
out.contextShift = options.contextShift;
|
|
392
|
+
if (options.media !== undefined) {
|
|
393
|
+
const { maxFrames, fps, longestEdge, processor } = options.media;
|
|
394
|
+
out.media = {
|
|
395
|
+
...(maxFrames === undefined ? {} : { maxFrames }),
|
|
396
|
+
...(fps === undefined ? {} : { fps }),
|
|
397
|
+
...(longestEdge === undefined ? {} : { longestEdge }),
|
|
398
|
+
...(processor === undefined ? {} : { processor }),
|
|
399
|
+
};
|
|
400
|
+
}
|
|
390
401
|
return out;
|
|
391
402
|
}
|
|
392
403
|
/**
|
|
@@ -412,6 +423,16 @@ export function withRecommendedSampling(options, checkpoint) {
|
|
|
412
423
|
return out;
|
|
413
424
|
}
|
|
414
425
|
/** What picks a loaded model's recommended sampling. */
|
|
426
|
+
/** Refuses images and videos a model does not take, before they are read. */
|
|
427
|
+
function checkMediaSupport(messages, vision) {
|
|
428
|
+
const parts = messages.flatMap(messageParts);
|
|
429
|
+
if (vision?.images !== true && parts.some((p) => p.type === "image")) {
|
|
430
|
+
throw new WebslmError("INVALID_ARGUMENT", "this model does not take images (see `info().vision`)");
|
|
431
|
+
}
|
|
432
|
+
if (vision?.videos !== true && parts.some((p) => p.type === "video")) {
|
|
433
|
+
throw new WebslmError("INVALID_ARGUMENT", "this model does not take videos (see `info().vision`)");
|
|
434
|
+
}
|
|
435
|
+
}
|
|
415
436
|
function checkpointTraits(info) {
|
|
416
437
|
return { repo: info.repo, modelType: info.modelType, doSample: info.generationConfig.doSample };
|
|
417
438
|
}
|
|
@@ -432,12 +453,21 @@ function checkMessages(messages) {
|
|
|
432
453
|
throw new WebslmError("INVALID_ARGUMENT", "messages must be a non-empty array");
|
|
433
454
|
}
|
|
434
455
|
return messages.map((m) => {
|
|
435
|
-
if (typeof m?.role !== "string" || typeof m.content !== "string") {
|
|
436
|
-
throw new WebslmError("INVALID_ARGUMENT", "each message needs a string `role` and `content`");
|
|
456
|
+
if (typeof m?.role !== "string" || (typeof m.content !== "string" && !Array.isArray(m.content))) {
|
|
457
|
+
throw new WebslmError("INVALID_ARGUMENT", "each message needs a string `role` and a string or array `content`");
|
|
437
458
|
}
|
|
438
|
-
|
|
459
|
+
// Checks the parts (the images and videos are read when sent).
|
|
460
|
+
wireMessage(m, []);
|
|
461
|
+
if (typeof m.content !== "string")
|
|
462
|
+
return { role: m.role, content: m.content };
|
|
463
|
+
const media = "media" in m ? m.media : undefined;
|
|
464
|
+
return media === undefined ? { role: m.role, content: m.content } : { role: m.role, content: m.content, media };
|
|
439
465
|
});
|
|
440
466
|
}
|
|
467
|
+
/** The messages as the engine reads them, images and videos left out. */
|
|
468
|
+
function wireMessages(messages) {
|
|
469
|
+
return messages.map((m) => wireMessage(m, []));
|
|
470
|
+
}
|
|
441
471
|
/** Shared implementation of a streamed generation. */
|
|
442
472
|
class GenerationStream {
|
|
443
473
|
result;
|
|
@@ -517,6 +547,7 @@ class Conversation {
|
|
|
517
547
|
*/
|
|
518
548
|
chat(messages, options = {}) {
|
|
519
549
|
const checked = checkMessages(messages);
|
|
550
|
+
checkMediaSupport(checked, this.vision);
|
|
520
551
|
return this.#stream({ type: "chat", messages: checked }, options);
|
|
521
552
|
}
|
|
522
553
|
/**
|
|
@@ -554,9 +585,23 @@ class Conversation {
|
|
|
554
585
|
// normally, with `finishReason: "aborted"`.
|
|
555
586
|
const controller = new AbortController();
|
|
556
587
|
return new GenerationStream(async (onDelta) => {
|
|
557
|
-
|
|
588
|
+
// Images and videos are read here, where the caller's objects live,
|
|
589
|
+
// and their pixels transferred to the engine.
|
|
590
|
+
let request;
|
|
591
|
+
let transfer = [];
|
|
592
|
+
if (body.type === "chat") {
|
|
593
|
+
const { messages, media } = await prepareMessages(body.messages, generateOptions.media);
|
|
594
|
+
request = { type: "chat", messages, ...(media === undefined ? {} : { media }), model: this.modelId, ...session, options };
|
|
595
|
+
if (media !== undefined)
|
|
596
|
+
transfer = [media.data.buffer];
|
|
597
|
+
}
|
|
598
|
+
else {
|
|
599
|
+
request = { ...body, model: this.modelId, ...session, options };
|
|
600
|
+
}
|
|
601
|
+
const result = await this.client.request(request, {
|
|
558
602
|
onDelta,
|
|
559
603
|
signal: controller.signal,
|
|
604
|
+
transfer,
|
|
560
605
|
...(onPrefill === undefined ? {} : { onPrefill: (done, total) => onPrefill({ done, total }) }),
|
|
561
606
|
});
|
|
562
607
|
this.onLength(result.length);
|
|
@@ -590,7 +635,7 @@ class Conversation {
|
|
|
590
635
|
return this.client.request({
|
|
591
636
|
type: "renderChat",
|
|
592
637
|
model: this.modelId,
|
|
593
|
-
messages: checkMessages(messages),
|
|
638
|
+
messages: wireMessages(checkMessages(messages)),
|
|
594
639
|
templateKwargs,
|
|
595
640
|
});
|
|
596
641
|
}
|
|
@@ -620,6 +665,9 @@ export class Model extends Conversation {
|
|
|
620
665
|
get checkpoint() {
|
|
621
666
|
return checkpointTraits(this.#info);
|
|
622
667
|
}
|
|
668
|
+
get vision() {
|
|
669
|
+
return this.#info.vision;
|
|
670
|
+
}
|
|
623
671
|
onLength() { }
|
|
624
672
|
/** Architecture, sizes, quant, backend and the files loaded (with SHA-256). */
|
|
625
673
|
info() {
|
|
@@ -731,6 +779,9 @@ export class Session extends Conversation {
|
|
|
731
779
|
get checkpoint() {
|
|
732
780
|
return checkpointTraits(this.#info);
|
|
733
781
|
}
|
|
782
|
+
get vision() {
|
|
783
|
+
return this.#info.vision;
|
|
784
|
+
}
|
|
734
785
|
onLength(length) {
|
|
735
786
|
this.#length = length;
|
|
736
787
|
}
|
|
@@ -864,7 +915,8 @@ export const MODEL_FAMILIES = [
|
|
|
864
915
|
{
|
|
865
916
|
name: "Qwen3.5",
|
|
866
917
|
modelType: "qwen3_5",
|
|
867
|
-
|
|
918
|
+
// Images and videos too: its checkpoints hold the vision tower.
|
|
919
|
+
modality: "vision-language",
|
|
868
920
|
exampleRepo: "Qwen/Qwen3.5-0.8B",
|
|
869
921
|
status: "supported",
|
|
870
922
|
},
|
|
@@ -876,10 +928,17 @@ export const MODEL_FAMILIES = [
|
|
|
876
928
|
status: "supported",
|
|
877
929
|
},
|
|
878
930
|
{
|
|
879
|
-
name: "SmolVLM",
|
|
931
|
+
name: "SmolVLM / SmolVLM2",
|
|
880
932
|
modelType: "idefics3",
|
|
881
933
|
modality: "vision-language",
|
|
882
|
-
exampleRepo: "HuggingFaceTB/
|
|
883
|
-
status: "
|
|
934
|
+
exampleRepo: "HuggingFaceTB/SmolVLM2-256M-Video-Instruct",
|
|
935
|
+
status: "supported",
|
|
936
|
+
},
|
|
937
|
+
{
|
|
938
|
+
name: "LFM2-VL / LFM2.5-VL",
|
|
939
|
+
modelType: "lfm2_vl",
|
|
940
|
+
modality: "vision-language",
|
|
941
|
+
exampleRepo: "LiquidAI/LFM2.5-VL-450M",
|
|
942
|
+
status: "supported",
|
|
884
943
|
},
|
|
885
944
|
];
|
package/dist/native.d.ts
CHANGED
|
@@ -35,6 +35,8 @@ export type NativePrefillCallback = (done: number, total: number) => void;
|
|
|
35
35
|
/** A session of a native model; every call resolves on the model's thread. */
|
|
36
36
|
export interface NativeSession {
|
|
37
37
|
chat(messagesJson: string, optionsJson: string, onDelta: NativeDeltaCallback | null, onPrefill: NativePrefillCallback | null, cancel: NativeCancelToken | null): Promise<unknown>;
|
|
38
|
+
/** `chat` with the images and videos of the messages' parts (see `media.ts`). */
|
|
39
|
+
chatWithMedia(messagesJson: string, optionsJson: string, mediaJson: string, mediaData: Uint8Array, onDelta: NativeDeltaCallback | null, onPrefill: NativePrefillCallback | null, cancel: NativeCancelToken | null): Promise<unknown>;
|
|
38
40
|
generate(prompt: string, optionsJson: string, onDelta: NativeDeltaCallback | null, onPrefill: NativePrefillCallback | null, cancel: NativeCancelToken | null): Promise<unknown>;
|
|
39
41
|
fork(): Promise<NativeSession>;
|
|
40
42
|
scoreContinuations(prefix: Uint32Array, continuationsJson: string): Promise<Float32Array[]>;
|
|
@@ -60,6 +62,7 @@ export interface NativeModel {
|
|
|
60
62
|
removeAdapter(): Promise<unknown>;
|
|
61
63
|
createSession(): Promise<NativeSession>;
|
|
62
64
|
chat: NativeSession["chat"];
|
|
65
|
+
chatWithMedia: NativeSession["chatWithMedia"];
|
|
63
66
|
generate: NativeSession["generate"];
|
|
64
67
|
reset(): Promise<void>;
|
|
65
68
|
tokenize(text: string, addSpecialTokens: boolean): Promise<Uint32Array>;
|
package/dist/protocol.d.ts
CHANGED
|
@@ -47,6 +47,7 @@ export interface WireLoadOptions {
|
|
|
47
47
|
/** `false` turns the converted-weight cache off. */
|
|
48
48
|
readonly weightCache?: boolean;
|
|
49
49
|
readonly deterministic?: boolean;
|
|
50
|
+
readonly vision?: boolean;
|
|
50
51
|
readonly activations?: Activations;
|
|
51
52
|
readonly wasmFlavour?: WasmFlavour;
|
|
52
53
|
readonly threads?: number;
|
|
@@ -76,10 +77,35 @@ export interface WireGenerateOptions {
|
|
|
76
77
|
readonly contextShift?: boolean | {
|
|
77
78
|
readonly keep?: number;
|
|
78
79
|
};
|
|
80
|
+
readonly media?: {
|
|
81
|
+
readonly maxFrames?: number;
|
|
82
|
+
readonly fps?: number;
|
|
83
|
+
readonly longestEdge?: number;
|
|
84
|
+
readonly processor?: Readonly<Record<string, unknown>>;
|
|
85
|
+
};
|
|
79
86
|
}
|
|
80
87
|
export interface WireMessage {
|
|
81
88
|
readonly role: string;
|
|
82
|
-
|
|
89
|
+
/** Text, or parts whose images and videos travel in {@link WireMedia}. */
|
|
90
|
+
readonly content: string | readonly WirePart[];
|
|
91
|
+
}
|
|
92
|
+
/** A content part as the engine reads it. */
|
|
93
|
+
export type WirePart = {
|
|
94
|
+
readonly type: "text";
|
|
95
|
+
readonly text: string;
|
|
96
|
+
} | {
|
|
97
|
+
readonly type: "image";
|
|
98
|
+
} | {
|
|
99
|
+
readonly type: "video";
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* The images and videos of a `chat` request, one per image or video part
|
|
103
|
+
* in order: `meta` is a JSON array describing each (see `media.ts`),
|
|
104
|
+
* `data` their pixels or image files back to back.
|
|
105
|
+
*/
|
|
106
|
+
export interface WireMedia {
|
|
107
|
+
readonly meta: string;
|
|
108
|
+
readonly data: Uint8Array;
|
|
83
109
|
}
|
|
84
110
|
type WithId<T> = T & {
|
|
85
111
|
readonly id: number;
|
|
@@ -106,6 +132,7 @@ export type Request = WithId<{
|
|
|
106
132
|
readonly model: number;
|
|
107
133
|
readonly session?: number;
|
|
108
134
|
readonly messages: readonly WireMessage[];
|
|
135
|
+
readonly media?: WireMedia;
|
|
109
136
|
readonly options: WireGenerateOptions;
|
|
110
137
|
} | {
|
|
111
138
|
readonly type: "generate";
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
// Each model card: SmolLM2 (its usage example), LFM2.5 and LFM3
|
|
9
9
|
// ("generation parameters", LFM3's "general" set), Qwen3 (best practices,
|
|
10
10
|
// thinking and non-thinking), Qwen3.5 (the settings its benchmarks used in
|
|
11
|
-
// each mode).
|
|
11
|
+
// each mode), LFM2.5-VL (its text generation parameters).
|
|
12
12
|
export const RECOMMENDED_SAMPLING = {
|
|
13
13
|
sets: {
|
|
14
14
|
smollm2: {
|
|
@@ -30,6 +30,9 @@ export const RECOMMENDED_SAMPLING = {
|
|
|
30
30
|
answer: { temperature: 0.7, topK: 20, topP: 0.8, minP: 0, presencePenalty: 1.5 },
|
|
31
31
|
thinking: { temperature: 0.6, topK: 20, topP: 0.95, minP: 0, presencePenalty: 1.5 },
|
|
32
32
|
},
|
|
33
|
+
lfm2_vl: {
|
|
34
|
+
answer: { temperature: 0.1, minP: 0.15, repetitionPenalty: 1.05 },
|
|
35
|
+
},
|
|
33
36
|
},
|
|
34
37
|
repos: {
|
|
35
38
|
"HuggingFaceTB/SmolLM2-135M-Instruct": "smollm2",
|
|
@@ -38,6 +41,7 @@ export const RECOMMENDED_SAMPLING = {
|
|
|
38
41
|
"Qwen/Qwen3-0.6B": "qwen3",
|
|
39
42
|
"Qwen/Qwen3.5-0.8B": "qwen3_5",
|
|
40
43
|
"LiquidAI/LFM3-1.7B": "lfm3",
|
|
44
|
+
"LiquidAI/LFM2.5-VL-450M": "lfm2_vl",
|
|
41
45
|
},
|
|
42
46
|
modelTypes: {
|
|
43
47
|
lfm2: "lfm2",
|
|
@@ -45,6 +49,7 @@ export const RECOMMENDED_SAMPLING = {
|
|
|
45
49
|
qwen3: "qwen3",
|
|
46
50
|
qwen3_5: "qwen3_5",
|
|
47
51
|
qwen3_5_text: "qwen3_5",
|
|
52
|
+
lfm2_vl: "lfm2_vl",
|
|
48
53
|
},
|
|
49
54
|
};
|
|
50
55
|
/**
|
package/dist/simple.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* {@link Session}.
|
|
5
5
|
*/
|
|
6
6
|
import { type Model } from "./models.js";
|
|
7
|
-
import type { ChatMessage, LoadModelOptions, ModelInfo, ModelSource } from "./types.js";
|
|
7
|
+
import type { ChatMessage, ImageInput, LoadModelOptions, MediaOptions, ModelInfo, ModelSource, VideoInput } from "./types.js";
|
|
8
8
|
/**
|
|
9
9
|
* The model {@link load}, {@link ask} and {@link stream} use when none is
|
|
10
10
|
* given: LFM2.5 350M, small (about 710 MB to download once), ungated, quick
|
|
@@ -80,6 +80,15 @@ export interface PromptOptions {
|
|
|
80
80
|
readonly onToken?: (text: string) => void;
|
|
81
81
|
/** Called with each piece of the reasoning, for models that think before answering. */
|
|
82
82
|
readonly onReasoning?: (text: string) => void;
|
|
83
|
+
/**
|
|
84
|
+
* Images sent with the prompt, before its text (vision-language models,
|
|
85
|
+
* see `ModelInfo.vision`).
|
|
86
|
+
*/
|
|
87
|
+
readonly images?: readonly ImageInput[];
|
|
88
|
+
/** A video sent with the prompt, before its text (models with `vision.videos`). */
|
|
89
|
+
readonly video?: VideoInput;
|
|
90
|
+
/** How images and videos are read: video sampling, processor settings. */
|
|
91
|
+
readonly media?: MediaOptions;
|
|
83
92
|
}
|
|
84
93
|
/** Options of {@link LLM.ask} and {@link Chat.send}. */
|
|
85
94
|
export interface AskOptions extends PromptOptions {
|
|
@@ -124,7 +133,7 @@ export interface ChatOptions {
|
|
|
124
133
|
/** Turns to start from (user and assistant messages). */
|
|
125
134
|
readonly messages?: readonly ChatMessage[];
|
|
126
135
|
/** Default options of every turn; those given to `send` or `stream` take precedence. */
|
|
127
|
-
readonly defaults?: Omit<AskOptions, "system" | "signal" | "onToken" | "onReasoning" | "withReasoning">;
|
|
136
|
+
readonly defaults?: Omit<AskOptions, "system" | "signal" | "onToken" | "onReasoning" | "withReasoning" | "images" | "video">;
|
|
128
137
|
}
|
|
129
138
|
/** Options of {@link Chat.send} and {@link Chat.stream}: the conversation sets the system prompt. */
|
|
130
139
|
export type ChatTurnOptions = Omit<AskOptions, "system">;
|
package/dist/simple.js
CHANGED
|
@@ -282,12 +282,12 @@ class LLMImpl {
|
|
|
282
282
|
lock: this.#lock,
|
|
283
283
|
// A bare string lets `generate` complete it as raw text when the
|
|
284
284
|
// checkpoint has no chat template.
|
|
285
|
-
input: () =>
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
285
|
+
input: () => {
|
|
286
|
+
const user = userMessage(prompt, options);
|
|
287
|
+
if (system === undefined && user.media === undefined)
|
|
288
|
+
return prompt;
|
|
289
|
+
return [...(system === undefined ? [] : [{ role: "system", content: system }]), user];
|
|
290
|
+
},
|
|
291
291
|
});
|
|
292
292
|
}
|
|
293
293
|
/** @internal Runs one generation into a stream. */
|
|
@@ -366,6 +366,7 @@ function passThrough(options) {
|
|
|
366
366
|
"thinkingBudget",
|
|
367
367
|
"loopDetection",
|
|
368
368
|
"contextShift",
|
|
369
|
+
"media",
|
|
369
370
|
];
|
|
370
371
|
const out = {};
|
|
371
372
|
for (const key of keys)
|
|
@@ -373,6 +374,14 @@ function passThrough(options) {
|
|
|
373
374
|
out[key] = options[key];
|
|
374
375
|
return out;
|
|
375
376
|
}
|
|
377
|
+
/** A user message: `text`, with the images and video of `options`. */
|
|
378
|
+
function userMessage(text, options) {
|
|
379
|
+
const media = [
|
|
380
|
+
...(options.images ?? []).map((image) => ({ type: "image", image })),
|
|
381
|
+
...(options.video === undefined ? [] : [{ type: "video", video: options.video }]),
|
|
382
|
+
];
|
|
383
|
+
return media.length === 0 ? { role: "user", content: text } : { role: "user", content: text, media };
|
|
384
|
+
}
|
|
376
385
|
/** `templateKwargs`, with `think` as `enable_thinking` when the template has that switch. */
|
|
377
386
|
function templateKwargs(options, thinkingSwitch) {
|
|
378
387
|
const kwargs = {
|
|
@@ -424,7 +433,7 @@ class ChatImpl {
|
|
|
424
433
|
this.#llm = llm;
|
|
425
434
|
this.system = options.system;
|
|
426
435
|
this.#defaults = options.defaults;
|
|
427
|
-
this.#history = (options.messages ?? []).map((m) =>
|
|
436
|
+
this.#history = (options.messages ?? []).map((m) => m.media === undefined ? { role: m.role, content: m.content } : { role: m.role, content: m.content, media: m.media });
|
|
428
437
|
}
|
|
429
438
|
get history() {
|
|
430
439
|
return this.#history.slice();
|
|
@@ -439,7 +448,7 @@ class ChatImpl {
|
|
|
439
448
|
if (this.#disposed)
|
|
440
449
|
throw new WebslmError("DISPOSED", "this chat was disposed");
|
|
441
450
|
const epoch = this.#epoch;
|
|
442
|
-
const user =
|
|
451
|
+
const user = userMessage(content, options);
|
|
443
452
|
await this.#llm.run(context, merged, {
|
|
444
453
|
target: () => this.#openSession(),
|
|
445
454
|
lock: this.#lock,
|
package/dist/types.d.ts
CHANGED
|
@@ -225,6 +225,12 @@ export interface LoadModelOptions {
|
|
|
225
225
|
* same logits, whatever the browser, CPU or thread count.
|
|
226
226
|
*/
|
|
227
227
|
readonly deterministic?: boolean;
|
|
228
|
+
/**
|
|
229
|
+
* `false` loads a vision-language checkpoint without its vision encoder,
|
|
230
|
+
* as a text-only model, to save its memory (Qwen3.5-0.8B's encoder has
|
|
231
|
+
* 100M parameters). Default `true`.
|
|
232
|
+
*/
|
|
233
|
+
readonly vision?: boolean;
|
|
228
234
|
/**
|
|
229
235
|
* WebAssembly build to run (advanced; defaults to the fastest one the
|
|
230
236
|
* environment supports: `"threads"` when the page is cross-origin
|
|
@@ -262,7 +268,86 @@ export interface CpuInfo {
|
|
|
262
268
|
}
|
|
263
269
|
export interface ChatMessage {
|
|
264
270
|
readonly role: "system" | "user" | "assistant" | (string & {});
|
|
271
|
+
/** The text of the message. */
|
|
265
272
|
readonly content: string;
|
|
273
|
+
/**
|
|
274
|
+
* Images and videos sent with the message, placed before its text
|
|
275
|
+
* (vision-language models, see `ModelInfo.vision`).
|
|
276
|
+
*/
|
|
277
|
+
readonly media?: readonly MediaPart[];
|
|
278
|
+
}
|
|
279
|
+
/** An image or a video of a message. */
|
|
280
|
+
export type MediaPart = {
|
|
281
|
+
readonly type: "image";
|
|
282
|
+
readonly image: ImageInput;
|
|
283
|
+
} | {
|
|
284
|
+
readonly type: "video";
|
|
285
|
+
readonly video: VideoInput;
|
|
286
|
+
};
|
|
287
|
+
/** One part of a {@link MultimodalMessage}. */
|
|
288
|
+
export type ContentPart = {
|
|
289
|
+
readonly type: "text";
|
|
290
|
+
readonly text: string;
|
|
291
|
+
} | MediaPart;
|
|
292
|
+
/**
|
|
293
|
+
* A message whose content interleaves text, images and videos in any
|
|
294
|
+
* order, as `Model.chat`, `Session.chat` and `generate` also take.
|
|
295
|
+
*/
|
|
296
|
+
export interface MultimodalMessage {
|
|
297
|
+
readonly role: "system" | "user" | "assistant" | (string & {});
|
|
298
|
+
readonly content: readonly ContentPart[];
|
|
299
|
+
}
|
|
300
|
+
/** What the chat methods take: a {@link ChatMessage} or a {@link MultimodalMessage}. */
|
|
301
|
+
export type ChatInputMessage = ChatMessage | MultimodalMessage;
|
|
302
|
+
/**
|
|
303
|
+
* Decoded RGBA pixels, row by row (`channels`: 4 by default; 3 for RGB, 1
|
|
304
|
+
* for gray).
|
|
305
|
+
*/
|
|
306
|
+
export interface RawImage {
|
|
307
|
+
readonly data: Uint8Array | Uint8ClampedArray;
|
|
308
|
+
readonly width: number;
|
|
309
|
+
readonly height: number;
|
|
310
|
+
readonly channels?: 1 | 3 | 4;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* An image: a URL (`https:`, `data:`, `blob:`), the bytes of a PNG, JPEG,
|
|
314
|
+
* WebP or GIF file, a `Blob`, an `ImageBitmap`, `ImageData`, an image,
|
|
315
|
+
* canvas or video element (its current frame), or decoded pixels. In Node,
|
|
316
|
+
* image files are decoded by the native engine; the WebAssembly engine
|
|
317
|
+
* takes pixels there.
|
|
318
|
+
*/
|
|
319
|
+
export type ImageInput = string | URL | Uint8Array | ArrayBuffer | Blob | ImageBitmap | ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | OffscreenCanvas | RawImage;
|
|
320
|
+
/** Frames of a video, already sampled. */
|
|
321
|
+
export interface VideoFrames {
|
|
322
|
+
readonly frames: readonly ImageInput[];
|
|
323
|
+
/** Frames per second of `frames` (the timestamps are `i / fps`). */
|
|
324
|
+
readonly fps?: number;
|
|
325
|
+
/** Time of each frame in seconds (in place of `fps`). */
|
|
326
|
+
readonly timestamps?: readonly number[];
|
|
327
|
+
/** Length of the whole video in seconds. */
|
|
328
|
+
readonly duration?: number;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* A video: a `<video>` element, a URL or a `Blob` (sampled in the browser
|
|
332
|
+
* by seeking a `<video>` element, see {@link MediaOptions}), or frames.
|
|
333
|
+
*/
|
|
334
|
+
export type VideoInput = HTMLVideoElement | string | URL | Blob | VideoFrames;
|
|
335
|
+
/** How the images and videos of a conversation are read. */
|
|
336
|
+
export interface MediaOptions {
|
|
337
|
+
/** Most frames sampled from a video (default 32). */
|
|
338
|
+
readonly maxFrames?: number;
|
|
339
|
+
/** Frames sampled per second of video (default 2; the model's processor may keep fewer). */
|
|
340
|
+
readonly fps?: number;
|
|
341
|
+
/**
|
|
342
|
+
* Longest image side the processor scales images to, in place of the
|
|
343
|
+
* checkpoint's (SmolVLM): smaller values give fewer tiles and tokens.
|
|
344
|
+
*/
|
|
345
|
+
readonly longestEdge?: number;
|
|
346
|
+
/**
|
|
347
|
+
* Processor settings replacing the checkpoint's (`preprocessor_config.json`
|
|
348
|
+
* keys, e.g. `{ max_image_tokens: 64 }` for LFM2-VL).
|
|
349
|
+
*/
|
|
350
|
+
readonly processor?: Readonly<Record<string, unknown>>;
|
|
266
351
|
}
|
|
267
352
|
/**
|
|
268
353
|
* Options of a generation. Unset sampling options come from the model card's
|
|
@@ -329,6 +414,8 @@ export interface GenerateOptions {
|
|
|
329
414
|
readonly contextShift?: boolean | {
|
|
330
415
|
readonly keep?: number;
|
|
331
416
|
};
|
|
417
|
+
/** How the images and videos of the messages are read (vision-language models). */
|
|
418
|
+
readonly media?: MediaOptions;
|
|
332
419
|
/**
|
|
333
420
|
* Called with every generated token. Returning `false` stops the
|
|
334
421
|
* generation right there (`finishReason: "aborted"`): the output ends
|
|
@@ -502,6 +589,14 @@ export interface ModelInfo {
|
|
|
502
589
|
readonly tiesEmbeddings: boolean;
|
|
503
590
|
readonly hasRecurrentState: boolean;
|
|
504
591
|
readonly hasChatTemplate: boolean;
|
|
592
|
+
/**
|
|
593
|
+
* What a vision-language model takes besides text: images, and videos
|
|
594
|
+
* when its processor samples them (absent for text models).
|
|
595
|
+
*/
|
|
596
|
+
readonly vision?: {
|
|
597
|
+
readonly images: boolean;
|
|
598
|
+
readonly videos: boolean;
|
|
599
|
+
};
|
|
505
600
|
readonly stopTokens: readonly number[];
|
|
506
601
|
readonly tokenizer: {
|
|
507
602
|
readonly vocabSize: number;
|
package/native-manifest.json
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.
|
|
2
|
+
"version": "0.6.0",
|
|
3
3
|
"assets": {
|
|
4
4
|
"darwin-arm64": {
|
|
5
|
-
"file": "webslm-node-0.
|
|
6
|
-
"size":
|
|
7
|
-
"sha256": "
|
|
8
|
-
"nodeSha256": "
|
|
5
|
+
"file": "webslm-node-0.6.0-darwin-arm64.node.gz",
|
|
6
|
+
"size": 5143165,
|
|
7
|
+
"sha256": "f2700ee1eb7d60ccd0c50053cf67e15d34c6056413f22b4fd69e73ed8ef45783",
|
|
8
|
+
"nodeSha256": "66bfdd8160ddb128beb6055f42161e0c47d21f82772035cdd09e58db6f6fd502"
|
|
9
9
|
},
|
|
10
10
|
"darwin-x64": {
|
|
11
|
-
"file": "webslm-node-0.
|
|
12
|
-
"size":
|
|
13
|
-
"sha256": "
|
|
14
|
-
"nodeSha256": "
|
|
11
|
+
"file": "webslm-node-0.6.0-darwin-x64.node.gz",
|
|
12
|
+
"size": 5416993,
|
|
13
|
+
"sha256": "4390851cb0bf1698b0308b7932dee58a10bd6cdb0390333fefc8e316fb7f9601",
|
|
14
|
+
"nodeSha256": "5f9cfd9c62d275415a04c6f3fb12c9fa7435e538a8952cef80d4992e4b52d31f"
|
|
15
15
|
},
|
|
16
16
|
"linux-x64-gnu": {
|
|
17
|
-
"file": "webslm-node-0.
|
|
18
|
-
"size":
|
|
19
|
-
"sha256": "
|
|
20
|
-
"nodeSha256": "
|
|
17
|
+
"file": "webslm-node-0.6.0-linux-x64-gnu.node.gz",
|
|
18
|
+
"size": 5453454,
|
|
19
|
+
"sha256": "66b4efe9cd4b944addd326c0d927abada2e78f96425e4976aa230756aca75d88",
|
|
20
|
+
"nodeSha256": "8d77ea1816275d4516253f49cd94720a2c722f9f2e9ce218efb437c32ebe6acd"
|
|
21
21
|
},
|
|
22
22
|
"linux-arm64-gnu": {
|
|
23
|
-
"file": "webslm-node-0.
|
|
24
|
-
"size":
|
|
25
|
-
"sha256": "
|
|
26
|
-
"nodeSha256": "
|
|
23
|
+
"file": "webslm-node-0.6.0-linux-arm64-gnu.node.gz",
|
|
24
|
+
"size": 5121058,
|
|
25
|
+
"sha256": "84bf11007c312761dc2377de89725bd839cb538834c01f80b26e1b0ba9456d7a",
|
|
26
|
+
"nodeSha256": "4596e8f5180ed2e1cb20721ac01142f4a1fa8e062a86280053acf377159d8ba2"
|
|
27
27
|
},
|
|
28
28
|
"linux-x64-musl": {
|
|
29
|
-
"file": "webslm-node-0.
|
|
30
|
-
"size":
|
|
31
|
-
"sha256": "
|
|
32
|
-
"nodeSha256": "
|
|
29
|
+
"file": "webslm-node-0.6.0-linux-x64-musl.node.gz",
|
|
30
|
+
"size": 5842409,
|
|
31
|
+
"sha256": "9a8ddc2a0f0649714ae8cbca9e31fa2405fdaf628ac14ec97296877c37e4506f",
|
|
32
|
+
"nodeSha256": "25f73331e30d7e2e2295af07fcbc7f0d747f5ef80803da2830fd175f61e670b1"
|
|
33
33
|
},
|
|
34
34
|
"linux-arm64-musl": {
|
|
35
|
-
"file": "webslm-node-0.
|
|
36
|
-
"size":
|
|
37
|
-
"sha256": "
|
|
38
|
-
"nodeSha256": "
|
|
35
|
+
"file": "webslm-node-0.6.0-linux-arm64-musl.node.gz",
|
|
36
|
+
"size": 5627078,
|
|
37
|
+
"sha256": "44178e33156cb2b6321414f74d4d4f9944183b2a2625cd5be19d21e16fd6dc21",
|
|
38
|
+
"nodeSha256": "3d76dacae55375d839b3d731634704fed52a84429288c2c9878be017d9a5ac78"
|
|
39
39
|
},
|
|
40
40
|
"win32-x64-msvc": {
|
|
41
|
-
"file": "webslm-node-0.
|
|
42
|
-
"size":
|
|
43
|
-
"sha256": "
|
|
44
|
-
"nodeSha256": "
|
|
41
|
+
"file": "webslm-node-0.6.0-win32-x64-msvc.node.gz",
|
|
42
|
+
"size": 5886895,
|
|
43
|
+
"sha256": "9c96790b22159549d6e620460311aa243bee8c217d8ff0d536f29e4381792c2d",
|
|
44
|
+
"nodeSha256": "911cfe54c80b1e4f8476c3778981e73dc774e34cc1108ed848fbe93240e16e8d"
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webslm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Run small language models in the browser and in Node, straight from their original Hugging Face checkpoints: a Rust engine compiled to WebAssembly, in a worker, with an offline cache.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Maxime Golfier",
|
|
@@ -66,6 +66,11 @@ export class WasmModel {
|
|
|
66
66
|
* model. Sessions start over at their next call.
|
|
67
67
|
*/
|
|
68
68
|
applyAdapter(lora: JsSource, options_json: string): Promise<any>;
|
|
69
|
+
/**
|
|
70
|
+
* Answers a conversation with images or videos in the default
|
|
71
|
+
* session; see [`WasmSession::chat_with_media`].
|
|
72
|
+
*/
|
|
73
|
+
chatWithMedia(messages_json: string, options_json: string, media_json: string, media_data: Uint8Array, on_delta: Function, on_prefill?: Function | null): Promise<any>;
|
|
69
74
|
/**
|
|
70
75
|
* Answers a conversation in the default session; see
|
|
71
76
|
* [`WasmSession::chat`].
|
|
@@ -93,7 +98,9 @@ export class WasmModel {
|
|
|
93
98
|
* `{ format?: "q8" | "q4" | "q4_0" | "f32" | "f16" | "bf16",
|
|
94
99
|
* contextLength?: number, deterministic?: boolean,
|
|
95
100
|
* activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto",
|
|
96
|
-
* adapterScale?: number, adapterMode?: "unmerged" | "merged"
|
|
101
|
+
* adapterScale?: number, adapterMode?: "unmerged" | "merged",
|
|
102
|
+
* vision?: boolean }` (`vision: false` skips the vision encoder of a
|
|
103
|
+
* vision-language checkpoint).
|
|
97
104
|
*
|
|
98
105
|
* `device` defaults to `"cpu"`. `"auto"` picks WebGPU when an adapter
|
|
99
106
|
* is available and the model has more than
|
|
@@ -150,6 +157,16 @@ export class WasmSession {
|
|
|
150
157
|
private constructor();
|
|
151
158
|
free(): void;
|
|
152
159
|
[Symbol.dispose](): void;
|
|
160
|
+
/**
|
|
161
|
+
* Answers a conversation whose messages hold image or video parts
|
|
162
|
+
* (`{"type": "image"}`, `{"type": "video"}`), with a vision-language
|
|
163
|
+
* model. `media_json` describes the images and videos, one per part in
|
|
164
|
+
* order, and `media_data` holds their RGBA pixels back to back (see
|
|
165
|
+
* `webslm_bindings::media`); the `media` generation option sets the
|
|
166
|
+
* video sampling (`maxFrames`, `fps`) and processor settings. Same
|
|
167
|
+
* callbacks and result as [`WasmSession::chat`].
|
|
168
|
+
*/
|
|
169
|
+
chatWithMedia(messages_json: string, options_json: string, media_json: string, media_data: Uint8Array, on_delta: Function, on_prefill?: Function | null): Promise<any>;
|
|
153
170
|
/**
|
|
154
171
|
* Answers a conversation: `messages_json` is a JSON array of
|
|
155
172
|
* `{ role, content }`, `options_json` a JSON object of generation
|
|
@@ -292,6 +309,7 @@ export interface InitOutput {
|
|
|
292
309
|
readonly version: () => [number, number];
|
|
293
310
|
readonly wasmmodel_applyAdapter: (a: number, b: number, c: number, d: number) => any;
|
|
294
311
|
readonly wasmmodel_chat: (a: number, b: number, c: number, d: number, e: number, f: any, g: number) => any;
|
|
312
|
+
readonly wasmmodel_chatWithMedia: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: any, k: number) => any;
|
|
295
313
|
readonly wasmmodel_createSession: (a: number) => [number, number, number];
|
|
296
314
|
readonly wasmmodel_detokenize: (a: number, b: number, c: number, d: number) => [number, number, number, number];
|
|
297
315
|
readonly wasmmodel_generate: (a: number, b: number, c: number, d: number, e: number, f: any, g: number) => any;
|
|
@@ -303,6 +321,7 @@ export interface InitOutput {
|
|
|
303
321
|
readonly wasmmodel_tokenize: (a: number, b: number, c: number, d: number) => [number, number, number, number];
|
|
304
322
|
readonly wasmsession_capacity: (a: number) => [number, number, number];
|
|
305
323
|
readonly wasmsession_chat: (a: number, b: number, c: number, d: number, e: number, f: any, g: number) => any;
|
|
324
|
+
readonly wasmsession_chatWithMedia: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: any, k: number) => any;
|
|
306
325
|
readonly wasmsession_computed: (a: number) => [number, number, number];
|
|
307
326
|
readonly wasmsession_fork: (a: number) => [number, number, number];
|
|
308
327
|
readonly wasmsession_generate: (a: number, b: number, c: number, d: number, e: number, f: any, g: number) => any;
|
|
@@ -317,10 +336,10 @@ export interface InitOutput {
|
|
|
317
336
|
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___js_sys_56365fc49d784db6___Function_fn_wasm_bindgen_8cf186fa7724d16d___JsValue_____wasm_bindgen_8cf186fa7724d16d___sys__Undefined___js_sys_56365fc49d784db6___Function_fn_wasm_bindgen_8cf186fa7724d16d___JsValue_____wasm_bindgen_8cf186fa7724d16d___sys__Undefined_______true_: (a: number, b: number, c: any, d: any) => void;
|
|
318
337
|
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___JsValue__core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true_: (a: number, b: number, c: any) => [number, number];
|
|
319
338
|
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___sys__JsNullable_wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true_: (a: number, b: number, c: any) => [number, number];
|
|
320
|
-
readonly
|
|
321
|
-
readonly
|
|
339
|
+
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___sys__JsNullable_wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true__40: (a: number, b: number, c: any) => [number, number];
|
|
340
|
+
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___sys__JsNullable_wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true__41: (a: number, b: number, c: any) => [number, number];
|
|
322
341
|
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuDeviceLostInfo__GpuDeviceLostInfo______true_: (a: number, b: number, c: any) => void;
|
|
323
|
-
readonly
|
|
342
|
+
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuDeviceLostInfo__GpuDeviceLostInfo______true__39: (a: number, b: number, c: any) => void;
|
|
324
343
|
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
325
344
|
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
326
345
|
readonly __wbindgen_exn_store: (a: number) => void;
|