@kotoshu/wasm 0.1.0 → 0.3.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/kotoshu_wasm.d.ts CHANGED
@@ -1,6 +1,18 @@
1
1
  /* tslint:disable */
2
2
  /* eslint-disable */
3
3
 
4
+ /**
5
+ * One loaded embedding tier: the wasm twin of the `onnx` feature's
6
+ * ort provider — same artifacts, scored in pure Rust. Handle-shaped:
7
+ * opaque on the JS side, freed by GC or `free()` (see the module docs
8
+ * for the memory footprint).
9
+ */
10
+ export class KotoshuModel {
11
+ private constructor();
12
+ free(): void;
13
+ [Symbol.dispose](): void;
14
+ }
15
+
4
16
  /**
5
17
  * One loaded dictionary: the JS twin of the `ruby` feature's
6
18
  * `Kotoshu::Native::Dictionary` — same engine, same suggestion-row shape.
@@ -37,3 +49,29 @@ export class KotoshuWasm {
37
49
  */
38
50
  static readonly VERSION: string;
39
51
  }
52
+
53
+ /**
54
+ * Load an int8-per-row embedding tier from the byte CONTENTS of its
55
+ * `.onnx` artifact and `.vocab.json` sibling (wasm has no filesystem;
56
+ * the host fetches the pair — mini ≈ 3 MB, fluency ≈ 15 MB).
57
+ * Failures reject with the Rust error message.
58
+ */
59
+ export function loadModel(model_bytes: Uint8Array, vocab_bytes: Uint8Array): KotoshuModel;
60
+
61
+ /**
62
+ * Score `word` against `context` (free text): the mean cosine over the
63
+ * in-vocabulary context tokens, in `[-1, 1]` — `0.0` when the word or
64
+ * every token is out of vocabulary. Ranking by this score orders
65
+ * candidates as the gem's context boost does; see the module docs.
66
+ */
67
+ export function rerank(model: KotoshuModel, word: string, context: string): number;
68
+
69
+ /**
70
+ * The `k` nearest vocabulary words to `word` by cosine — semantic
71
+ * candidate GENERATION (see `Int8Model::semantic_neighbors`): one plain
72
+ * `{ word, score }` row per neighbor, score-descending, the query word
73
+ * itself excluded. Empty when `k` is 0 or the word embeds nowhere (out
74
+ * of vocabulary with no in-vocabulary character n-gram). `k` defaults
75
+ * to 5 when omitted.
76
+ */
77
+ export function semanticSuggest(model: KotoshuModel, word: string, k?: number | null): Array<any>;
package/kotoshu_wasm.js CHANGED
@@ -5,5 +5,5 @@ import { __wbg_set_wasm } from "./kotoshu_wasm_bg.js";
5
5
  __wbg_set_wasm(wasm);
6
6
  wasm.__wbindgen_start();
7
7
  export {
8
- KotoshuWasm
8
+ KotoshuModel, KotoshuWasm, loadModel, rerank, semanticSuggest
9
9
  } from "./kotoshu_wasm_bg.js";
@@ -1,3 +1,29 @@
1
+ /**
2
+ * One loaded embedding tier: the wasm twin of the `onnx` feature's
3
+ * ort provider — same artifacts, scored in pure Rust. Handle-shaped:
4
+ * opaque on the JS side, freed by GC or `free()` (see the module docs
5
+ * for the memory footprint).
6
+ */
7
+ export class KotoshuModel {
8
+ static __wrap(ptr) {
9
+ const obj = Object.create(KotoshuModel.prototype);
10
+ obj.__wbg_ptr = ptr;
11
+ KotoshuModelFinalization.register(obj, obj.__wbg_ptr, obj);
12
+ return obj;
13
+ }
14
+ __destroy_into_raw() {
15
+ const ptr = this.__wbg_ptr;
16
+ this.__wbg_ptr = 0;
17
+ KotoshuModelFinalization.unregister(this);
18
+ return ptr;
19
+ }
20
+ free() {
21
+ const ptr = this.__destroy_into_raw();
22
+ wasm.__wbg_kotoshumodel_free(ptr, 0);
23
+ }
24
+ }
25
+ if (Symbol.dispose) KotoshuModel.prototype[Symbol.dispose] = KotoshuModel.prototype.free;
26
+
1
27
  /**
2
28
  * One loaded dictionary: the JS twin of the `ruby` feature's
3
29
  * `Kotoshu::Native::Dictionary` — same engine, same suggestion-row shape.
@@ -84,6 +110,67 @@ export class KotoshuWasm {
84
110
  }
85
111
  }
86
112
  if (Symbol.dispose) KotoshuWasm.prototype[Symbol.dispose] = KotoshuWasm.prototype.free;
113
+
114
+ /**
115
+ * Load an int8-per-row embedding tier from the byte CONTENTS of its
116
+ * `.onnx` artifact and `.vocab.json` sibling (wasm has no filesystem;
117
+ * the host fetches the pair — mini ≈ 3 MB, fluency ≈ 15 MB).
118
+ * Failures reject with the Rust error message.
119
+ * @param {Uint8Array} model_bytes
120
+ * @param {Uint8Array} vocab_bytes
121
+ * @returns {KotoshuModel}
122
+ */
123
+ export function loadModel(model_bytes, vocab_bytes) {
124
+ const ptr0 = passArray8ToWasm0(model_bytes, wasm.__wbindgen_malloc);
125
+ const len0 = WASM_VECTOR_LEN;
126
+ const ptr1 = passArray8ToWasm0(vocab_bytes, wasm.__wbindgen_malloc);
127
+ const len1 = WASM_VECTOR_LEN;
128
+ const ret = wasm.loadModel(ptr0, len0, ptr1, len1);
129
+ if (ret[2]) {
130
+ throw takeFromExternrefTable0(ret[1]);
131
+ }
132
+ return KotoshuModel.__wrap(ret[0]);
133
+ }
134
+
135
+ /**
136
+ * Score `word` against `context` (free text): the mean cosine over the
137
+ * in-vocabulary context tokens, in `[-1, 1]` — `0.0` when the word or
138
+ * every token is out of vocabulary. Ranking by this score orders
139
+ * candidates as the gem's context boost does; see the module docs.
140
+ * @param {KotoshuModel} model
141
+ * @param {string} word
142
+ * @param {string} context
143
+ * @returns {number}
144
+ */
145
+ export function rerank(model, word, context) {
146
+ _assertClass(model, KotoshuModel);
147
+ const ptr0 = passStringToWasm0(word, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
148
+ const len0 = WASM_VECTOR_LEN;
149
+ const ptr1 = passStringToWasm0(context, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
150
+ const len1 = WASM_VECTOR_LEN;
151
+ const ret = wasm.rerank(model.__wbg_ptr, ptr0, len0, ptr1, len1);
152
+ return ret;
153
+ }
154
+
155
+ /**
156
+ * The `k` nearest vocabulary words to `word` by cosine — semantic
157
+ * candidate GENERATION (see `Int8Model::semantic_neighbors`): one plain
158
+ * `{ word, score }` row per neighbor, score-descending, the query word
159
+ * itself excluded. Empty when `k` is 0 or the word embeds nowhere (out
160
+ * of vocabulary with no in-vocabulary character n-gram). `k` defaults
161
+ * to 5 when omitted.
162
+ * @param {KotoshuModel} model
163
+ * @param {string} word
164
+ * @param {number | null} [k]
165
+ * @returns {Array<any>}
166
+ */
167
+ export function semanticSuggest(model, word, k) {
168
+ _assertClass(model, KotoshuModel);
169
+ const ptr0 = passStringToWasm0(word, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
170
+ const len0 = WASM_VECTOR_LEN;
171
+ const ret = wasm.semanticSuggest(model.__wbg_ptr, ptr0, len0, isLikeNone(k) ? Number.MAX_SAFE_INTEGER : (k) >>> 0);
172
+ return ret;
173
+ }
87
174
  export function __wbg_Error_408e67f47ca7b58b(arg0, arg1) {
88
175
  const ret = Error(getStringFromWasm0(arg0, arg1));
89
176
  return ret;
@@ -155,6 +242,9 @@ export function __wbindgen_init_externref_table() {
155
242
  table.set(offset + 2, true);
156
243
  table.set(offset + 3, false);
157
244
  }
245
+ const KotoshuModelFinalization = (typeof FinalizationRegistry === 'undefined')
246
+ ? { register: () => {}, unregister: () => {} }
247
+ : new FinalizationRegistry(ptr => wasm.__wbg_kotoshumodel_free(ptr, 1));
158
248
  const KotoshuWasmFinalization = (typeof FinalizationRegistry === 'undefined')
159
249
  ? { register: () => {}, unregister: () => {} }
160
250
  : new FinalizationRegistry(ptr => wasm.__wbg_kotoshuwasm_free(ptr, 1));
@@ -165,6 +255,12 @@ function addToExternrefTable0(obj) {
165
255
  return idx;
166
256
  }
167
257
 
258
+ function _assertClass(instance, klass) {
259
+ if (!(instance instanceof klass)) {
260
+ throw new Error(`expected instance of ${klass.name}`);
261
+ }
262
+ }
263
+
168
264
  function debugString(val) {
169
265
  // primitive types
170
266
  const type = typeof val;
@@ -263,6 +359,13 @@ function isLikeNone(x) {
263
359
  return x === undefined || x === null;
264
360
  }
265
361
 
362
+ function passArray8ToWasm0(arg, malloc) {
363
+ const ptr = malloc(arg.length * 1, 1) >>> 0;
364
+ getUint8ArrayMemory0().set(arg, ptr / 1);
365
+ WASM_VECTOR_LEN = arg.length;
366
+ return ptr;
367
+ }
368
+
266
369
  function passStringToWasm0(arg, malloc, realloc) {
267
370
  if (realloc === undefined) {
268
371
  const buf = cachedTextEncoder.encode(arg);
Binary file
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "Ribose Inc. <open.source@ribose.com>"
6
6
  ],
7
7
  "description": "npm @kotoshu/wasm packaging shim: wasm-pack build of the kotoshu engine's wasm-bindgen surface",
8
- "version": "0.1.0",
8
+ "version": "0.3.0",
9
9
  "license": "BSD-2-Clause",
10
10
  "repository": {
11
11
  "type": "git",