kjarni-wasm 0.1.4

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.
@@ -0,0 +1,259 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * A local language model running in the browser.
6
+ *
7
+ * Text generation is synchronous here, and deliberately so. The engine's streaming
8
+ * path builds a background task with `tokio::task::spawn_blocking`, and wasm has no
9
+ * blocking pool for that to run on — it compiles but would fail at runtime. Calling
10
+ * the generation loop directly and draining the channel afterwards avoids the
11
+ * problem entirely.
12
+ *
13
+ * The cost is that a call blocks until the whole response is ready, which for a few
14
+ * hundred tokens is seconds, not milliseconds. Run it in a Web Worker; on the main
15
+ * thread it will freeze the tab.
16
+ */
17
+ export class WasmChat {
18
+ private constructor();
19
+ free(): void;
20
+ [Symbol.dispose](): void;
21
+ /**
22
+ * The model's context window, in tokens.
23
+ */
24
+ context_size(): number;
25
+ /**
26
+ * Generate a completion for `prompt`.
27
+ */
28
+ generate(prompt: string, max_new_tokens: number, temperature: number): string;
29
+ /**
30
+ * Generate a completion, invoking `on_token` with each piece as it arrives.
31
+ *
32
+ * The callback runs on whichever thread called this, synchronously between
33
+ * decoding steps. On the main thread that still blocks the page, so this is
34
+ * meant to be driven from a Web Worker; the `kjarni-wasm` npm wrapper does
35
+ * exactly that and turns it into an async iterator.
36
+ *
37
+ * Returning `false` from the callback does not stop generation yet; the
38
+ * value is ignored.
39
+ *
40
+ * Browser-only: `js_sys::Function` has no native equivalent. The streaming
41
+ * itself is in `generate_stream_core`, which builds and is tested natively.
42
+ */
43
+ generate_stream(prompt: string, max_new_tokens: number, temperature: number, on_token: Function): string;
44
+ /**
45
+ * Load a decoder model from a `.kjq` container.
46
+ *
47
+ * `model_id` selects the chat template, e.g. `qwen2.5-0.5b-instruct`. Without
48
+ * one the model still generates, but its turn markers will not be applied.
49
+ */
50
+ static load(data: Uint8Array, model_id?: string | null): WasmChat;
51
+ }
52
+
53
+ /**
54
+ * Sequence classification: sentiment, emotion, toxicity.
55
+ *
56
+ * New to the browser. Classification was always in the engine but never had a
57
+ * binding here, because this crate previously carried its own encoder and nobody
58
+ * wrote a second classifier head for it.
59
+ */
60
+ export class WasmClassifier {
61
+ private constructor();
62
+ free(): void;
63
+ [Symbol.dispose](): void;
64
+ /**
65
+ * Classify one text, returning every label with its score, highest first.
66
+ *
67
+ * Serializing to a `JsValue` panics outside wasm32, so the work lives in
68
+ * [`WasmClassifier::classify_core`] and this is only the conversion. That is
69
+ * what makes the behaviour testable on the host.
70
+ */
71
+ classify(text: string): any;
72
+ /**
73
+ * Classify a batch, returning the single best label for each input.
74
+ *
75
+ * One result per input, in input order, so the caller can line them up with
76
+ * whatever it passed in.
77
+ */
78
+ classify_batch(texts: string[]): any;
79
+ /**
80
+ * The labels this model predicts, in index order.
81
+ */
82
+ labels(): string[];
83
+ /**
84
+ * Load a classifier from a `.kjq` container.
85
+ */
86
+ static load(data: Uint8Array): WasmClassifier;
87
+ /**
88
+ * How many labels the model predicts.
89
+ */
90
+ num_labels(): number;
91
+ }
92
+
93
+ export class WasmEncoder {
94
+ private constructor();
95
+ free(): void;
96
+ [Symbol.dispose](): void;
97
+ encode_file(text: string, source_path: string): any;
98
+ static new(model_data: Uint8Array): WasmEncoder;
99
+ }
100
+
101
+ export class WasmIndexBuilder {
102
+ private constructor();
103
+ free(): void;
104
+ [Symbol.dispose](): void;
105
+ add_chunk(text: string, embedding: Float32Array, source: string, chunk_index: number): void;
106
+ add_file(text: string, source_path: string): number;
107
+ doc_count(): number;
108
+ finish(): Uint8Array;
109
+ static new(model_data: Uint8Array): WasmIndexBuilder;
110
+ }
111
+
112
+ export class WasmModel {
113
+ free(): void;
114
+ [Symbol.dispose](): void;
115
+ /**
116
+ * Encode a batch, returning every vector concatenated into one flat array.
117
+ *
118
+ * The engine's encode is `async` because the GPU path awaits buffer readback.
119
+ * There is no GPU path here, and the wasm CPU implementation contains no await
120
+ * at all, so the future is always ready on its first poll and `block_on`
121
+ * returns without ever parking the browser's main thread.
122
+ */
123
+ encode(texts: string[], normalize: boolean): Float32Array;
124
+ /**
125
+ * Load from a `.kjq` container: config, tokenizer and int8 tensors in one file.
126
+ */
127
+ static from_quantized(data: Uint8Array): WasmModel;
128
+ /**
129
+ * Fetch a model straight from Hugging Face. Browser-only: it uses `fetch`.
130
+ */
131
+ static from_type(model_type: WasmModelType): Promise<WasmModel>;
132
+ /**
133
+ * Load from raw safetensors plus its config and tokenizer.
134
+ */
135
+ constructor(weights_data: Uint8Array, config_json: string, tokenizer_json: string);
136
+ }
137
+
138
+ export enum WasmModelType {
139
+ MiniLML6V2 = 0,
140
+ }
141
+
142
+ export class WasmReranker {
143
+ private constructor();
144
+ free(): void;
145
+ [Symbol.dispose](): void;
146
+ /**
147
+ * Load a cross-encoder from a `.kjq` container.
148
+ *
149
+ * The classifier head used to be pulled out of the weights by hand here; the
150
+ * engine's `CrossEncoder` loads it as part of the model.
151
+ */
152
+ static load(data: Uint8Array): WasmReranker;
153
+ /**
154
+ * Score every document against the query and return the best `limit`,
155
+ * highest first. Scores are raw logits, so negative values are normal.
156
+ */
157
+ rerank(query: string, documents: string[], limit: number): any;
158
+ /**
159
+ * Score a single query-document pair.
160
+ */
161
+ score(query: string, document: string): number;
162
+ }
163
+
164
+ export class WasmSearch {
165
+ private constructor();
166
+ free(): void;
167
+ [Symbol.dispose](): void;
168
+ add_chunk(text: string, embedding: Float32Array, source: string, chunk_index: number): void;
169
+ doc_count(): number;
170
+ static load(model_data: Uint8Array, index_data: Uint8Array): WasmSearch;
171
+ remove_file(source_path: string): number;
172
+ save_index(): Uint8Array;
173
+ search(query: string, limit: number): any;
174
+ search_keywords(query: string, limit: number): any;
175
+ search_semantic(query: string, limit: number): any;
176
+ update_file(text: string, source_path: string): number;
177
+ }
178
+
179
+ export function init(): void;
180
+
181
+ export function set_debug_logging(enabled: boolean): void;
182
+
183
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
184
+
185
+ export interface InitOutput {
186
+ readonly memory: WebAssembly.Memory;
187
+ readonly __wbg_wasmchat_free: (a: number, b: number) => void;
188
+ readonly __wbg_wasmclassifier_free: (a: number, b: number) => void;
189
+ readonly __wbg_wasmencoder_free: (a: number, b: number) => void;
190
+ readonly __wbg_wasmindexbuilder_free: (a: number, b: number) => void;
191
+ readonly __wbg_wasmmodel_free: (a: number, b: number) => void;
192
+ readonly set_debug_logging: (a: number) => void;
193
+ readonly wasmchat_context_size: (a: number) => number;
194
+ readonly wasmchat_generate: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
195
+ readonly wasmchat_generate_stream: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
196
+ readonly wasmchat_load: (a: number, b: number, c: number, d: number, e: number) => void;
197
+ readonly wasmclassifier_classify: (a: number, b: number, c: number, d: number) => void;
198
+ readonly wasmclassifier_classify_batch: (a: number, b: number, c: number, d: number) => void;
199
+ readonly wasmclassifier_labels: (a: number, b: number) => void;
200
+ readonly wasmclassifier_load: (a: number, b: number, c: number) => void;
201
+ readonly wasmclassifier_num_labels: (a: number) => number;
202
+ readonly wasmencoder_encode_file: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
203
+ readonly wasmencoder_new: (a: number, b: number, c: number) => void;
204
+ readonly wasmindexbuilder_add_chunk: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => void;
205
+ readonly wasmindexbuilder_add_file: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
206
+ readonly wasmindexbuilder_doc_count: (a: number) => number;
207
+ readonly wasmindexbuilder_finish: (a: number, b: number) => void;
208
+ readonly wasmindexbuilder_new: (a: number, b: number, c: number) => void;
209
+ readonly wasmmodel_encode: (a: number, b: number, c: number, d: number, e: number) => void;
210
+ readonly wasmmodel_from_quantized: (a: number, b: number, c: number) => void;
211
+ readonly wasmmodel_from_type: (a: number) => number;
212
+ readonly wasmmodel_new: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
213
+ readonly wasmreranker_load: (a: number, b: number, c: number) => void;
214
+ readonly wasmreranker_rerank: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
215
+ readonly wasmreranker_score: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
216
+ readonly wasmsearch_load: (a: number, b: number, c: number, d: number, e: number) => void;
217
+ readonly wasmsearch_remove_file: (a: number, b: number, c: number) => number;
218
+ readonly wasmsearch_save_index: (a: number, b: number) => void;
219
+ readonly wasmsearch_search: (a: number, b: number, c: number, d: number, e: number) => void;
220
+ readonly wasmsearch_search_keywords: (a: number, b: number, c: number, d: number, e: number) => void;
221
+ readonly wasmsearch_search_semantic: (a: number, b: number, c: number, d: number, e: number) => void;
222
+ readonly wasmsearch_update_file: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
223
+ readonly init: () => void;
224
+ readonly wasmsearch_doc_count: (a: number) => number;
225
+ readonly wasmsearch_add_chunk: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => void;
226
+ readonly __wbg_wasmreranker_free: (a: number, b: number) => void;
227
+ readonly __wbg_wasmsearch_free: (a: number, b: number) => void;
228
+ readonly __wasm_bindgen_func_elem_5410: (a: number, b: number, c: number, d: number) => void;
229
+ readonly __wasm_bindgen_func_elem_5426: (a: number, b: number, c: number, d: number) => void;
230
+ readonly __wbindgen_export: (a: number, b: number) => number;
231
+ readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
232
+ readonly __wbindgen_export3: (a: number) => void;
233
+ readonly __wbindgen_export4: (a: number, b: number, c: number) => void;
234
+ readonly __wbindgen_export5: (a: number, b: number) => void;
235
+ readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
236
+ readonly __wbindgen_start: () => void;
237
+ }
238
+
239
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
240
+
241
+ /**
242
+ * Instantiates the given `module`, which can either be bytes or
243
+ * a precompiled `WebAssembly.Module`.
244
+ *
245
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
246
+ *
247
+ * @returns {InitOutput}
248
+ */
249
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
250
+
251
+ /**
252
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
253
+ * for everything else, calls `WebAssembly.instantiate` directly.
254
+ *
255
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
256
+ *
257
+ * @returns {Promise<InitOutput>}
258
+ */
259
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;