ruvector 0.2.25 → 0.2.27
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/bin/cli.js +121 -16
- package/bin/mcp-server.js +94 -22
- package/dist/core/adaptive-embedder.d.ts +9 -0
- package/dist/core/adaptive-embedder.d.ts.map +1 -1
- package/dist/core/adaptive-embedder.js +11 -0
- package/dist/core/intelligence-engine.d.ts +1 -1
- package/dist/core/intelligence-engine.d.ts.map +1 -1
- package/dist/core/intelligence-engine.js +10 -14
- package/dist/core/learning-engine.d.ts +2 -0
- package/dist/core/learning-engine.d.ts.map +1 -1
- package/dist/core/learning-engine.js +23 -3
- package/dist/core/onnx/bundled-parallel.mjs +164 -0
- package/dist/core/onnx/embed-worker.mjs +67 -0
- package/dist/core/onnx/loader.js +434 -0
- package/dist/core/onnx/package.json +3 -0
- package/dist/core/onnx/pkg/LICENSE +21 -0
- package/dist/core/onnx/pkg/loader.js +348 -0
- package/dist/core/onnx/pkg/ruvector.db +0 -0
- package/dist/core/onnx/pkg/ruvector_onnx_embeddings_wasm.d.ts +112 -0
- package/dist/core/onnx/pkg/ruvector_onnx_embeddings_wasm.js +5 -0
- package/dist/core/onnx/pkg/ruvector_onnx_embeddings_wasm_bg.js +638 -0
- package/dist/core/onnx/pkg/ruvector_onnx_embeddings_wasm_bg.wasm +0 -0
- package/dist/core/onnx/pkg/ruvector_onnx_embeddings_wasm_bg.wasm.d.ts +29 -0
- package/dist/core/onnx-embedder.d.ts +39 -2
- package/dist/core/onnx-embedder.d.ts.map +1 -1
- package/dist/core/onnx-embedder.js +139 -20
- package/dist/core/onnx-optimized.d.ts.map +1 -1
- package/dist/core/onnx-optimized.js +6 -24
- package/dist/index.d.ts +54 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +102 -1
- package/package.json +4 -3
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bundled-WASM parallel embedder (issue #523 SOTA).
|
|
3
|
+
*
|
|
4
|
+
* A self-contained worker_threads pool — NO external dependency — that shards
|
|
5
|
+
* batches of text across CPU cores, each worker running the bundled ONNX WASM
|
|
6
|
+
* embedder over the SAME model bytes (shared via SharedArrayBuffer) and config.
|
|
7
|
+
* Output vectors are identical to the single-thread path (cosine-equivalent),
|
|
8
|
+
* so this is a pure throughput optimization with no quality change.
|
|
9
|
+
*
|
|
10
|
+
* Drop-in shape compatible with the optional `ruvector-onnx-embeddings-wasm/parallel`
|
|
11
|
+
* package: { numWorkers, dimension, init(), embedBatch(texts) -> number[][], shutdown() }.
|
|
12
|
+
*/
|
|
13
|
+
import { Worker } from 'node:worker_threads';
|
|
14
|
+
import * as os from 'node:os';
|
|
15
|
+
import { fileURLToPath } from 'node:url';
|
|
16
|
+
import * as path from 'node:path';
|
|
17
|
+
|
|
18
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
19
|
+
|
|
20
|
+
export class ParallelEmbedder {
|
|
21
|
+
/**
|
|
22
|
+
* @param {object} opts
|
|
23
|
+
* @param {Uint8Array} opts.modelBytes raw ONNX model bytes (loaded once by caller)
|
|
24
|
+
* @param {string} opts.tokenizerJson
|
|
25
|
+
* @param {number} [opts.maxLength=256]
|
|
26
|
+
* @param {number} [opts.dimension=384]
|
|
27
|
+
* @param {number} [opts.numWorkers] defaults to min(cpus-2, 16), >=2
|
|
28
|
+
*/
|
|
29
|
+
constructor(opts = {}) {
|
|
30
|
+
this.numWorkers = opts.numWorkers || Math.max(2, Math.min((os.cpus().length || 4) - 2, 16));
|
|
31
|
+
this.dimension = opts.dimension || 384;
|
|
32
|
+
this._modelBytes = opts.modelBytes;
|
|
33
|
+
this._tokenizerJson = opts.tokenizerJson;
|
|
34
|
+
this._maxLength = opts.maxLength || 256;
|
|
35
|
+
this._requestTimeoutMs = opts.requestTimeoutMs ?? 30000;
|
|
36
|
+
this._workers = [];
|
|
37
|
+
this._pending = new Map(); // id -> { resolve, reject, worker, timer }
|
|
38
|
+
this._seq = 0;
|
|
39
|
+
this._shuttingDown = false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async init() {
|
|
43
|
+
if (!this._modelBytes || !this._tokenizerJson) {
|
|
44
|
+
throw new Error('ParallelEmbedder requires modelBytes and tokenizerJson');
|
|
45
|
+
}
|
|
46
|
+
// Share model bytes across all workers via a single SharedArrayBuffer.
|
|
47
|
+
const sab = new SharedArrayBuffer(this._modelBytes.length);
|
|
48
|
+
new Uint8Array(sab).set(this._modelBytes);
|
|
49
|
+
|
|
50
|
+
const workerUrl = new URL('./embed-worker.mjs', import.meta.url);
|
|
51
|
+
const readies = [];
|
|
52
|
+
|
|
53
|
+
for (let i = 0; i < this.numWorkers; i++) {
|
|
54
|
+
const w = new Worker(workerUrl, {
|
|
55
|
+
workerData: { modelSab: sab, tokenizerJson: this._tokenizerJson, maxLength: this._maxLength },
|
|
56
|
+
});
|
|
57
|
+
w.on('message', (m) => this._onMessage(m));
|
|
58
|
+
// If a worker dies (uncaught error or unexpected exit), fail every request
|
|
59
|
+
// currently routed to it instead of letting those promises hang forever.
|
|
60
|
+
w.on('error', (e) => this._failWorker(w, e instanceof Error ? e : new Error(String(e))));
|
|
61
|
+
w.on('exit', (code) => {
|
|
62
|
+
if (!this._shuttingDown && code !== 0) {
|
|
63
|
+
this._failWorker(w, new Error(`embed worker exited unexpectedly (code ${code})`));
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
this._workers.push(w);
|
|
67
|
+
readies.push(new Promise((resolve, reject) => {
|
|
68
|
+
const onReady = (m) => {
|
|
69
|
+
if (m.type === 'ready') { cleanup(); resolve(); }
|
|
70
|
+
else if (m.type === 'init-error') { cleanup(); reject(new Error('worker init failed: ' + m.error)); }
|
|
71
|
+
};
|
|
72
|
+
const onErr = (e) => { cleanup(); reject(e); };
|
|
73
|
+
const cleanup = () => { w.off('message', onReady); w.off('error', onErr); };
|
|
74
|
+
w.on('message', onReady);
|
|
75
|
+
w.once('error', onErr);
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
await Promise.all(readies);
|
|
80
|
+
// Drop the main-thread reference; the SAB keeps the shared copy alive.
|
|
81
|
+
this._modelBytes = null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
_settle(id, fn) {
|
|
85
|
+
const p = this._pending.get(id);
|
|
86
|
+
if (!p) return;
|
|
87
|
+
this._pending.delete(id);
|
|
88
|
+
if (p.timer) clearTimeout(p.timer);
|
|
89
|
+
fn(p);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
_onMessage(m) {
|
|
93
|
+
if (m.type !== 'result' && m.type !== 'error') return;
|
|
94
|
+
this._settle(m.id, (p) => {
|
|
95
|
+
if (m.type === 'error') p.reject(new Error(m.error));
|
|
96
|
+
else p.resolve({ dim: m.dim, count: m.count, flat: new Float32Array(m.buffer) });
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Reject every in-flight request routed to a dead worker. */
|
|
101
|
+
_failWorker(worker, err) {
|
|
102
|
+
for (const [id, p] of this._pending) {
|
|
103
|
+
if (p.worker === worker) this._settle(id, () => p.reject(err));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
_send(worker, texts) {
|
|
108
|
+
const id = ++this._seq;
|
|
109
|
+
return new Promise((resolve, reject) => {
|
|
110
|
+
const entry = { resolve, reject, worker, timer: null };
|
|
111
|
+
if (this._requestTimeoutMs > 0) {
|
|
112
|
+
entry.timer = setTimeout(() => {
|
|
113
|
+
this._settle(id, () =>
|
|
114
|
+
reject(new Error(`embed request timed out after ${this._requestTimeoutMs}ms`)));
|
|
115
|
+
}, this._requestTimeoutMs);
|
|
116
|
+
// Don't keep the event loop alive solely for this timer.
|
|
117
|
+
if (typeof entry.timer.unref === 'function') entry.timer.unref();
|
|
118
|
+
}
|
|
119
|
+
this._pending.set(id, entry);
|
|
120
|
+
worker.postMessage({ type: 'embed', id, texts });
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Embed many texts, sharded across workers. Returns number[][] in input order.
|
|
126
|
+
*/
|
|
127
|
+
async embedBatch(texts) {
|
|
128
|
+
if (!texts || texts.length === 0) return [];
|
|
129
|
+
const n = this._workers.length;
|
|
130
|
+
const shard = Math.ceil(texts.length / n);
|
|
131
|
+
const tasks = [];
|
|
132
|
+
const starts = [];
|
|
133
|
+
for (let i = 0; i < n; i++) {
|
|
134
|
+
const start = i * shard;
|
|
135
|
+
if (start >= texts.length) break;
|
|
136
|
+
const end = Math.min(texts.length, start + shard);
|
|
137
|
+
starts.push(start);
|
|
138
|
+
tasks.push(this._send(this._workers[i], texts.slice(start, end)));
|
|
139
|
+
}
|
|
140
|
+
const results = await Promise.all(tasks);
|
|
141
|
+
const out = new Array(texts.length);
|
|
142
|
+
for (let r = 0; r < results.length; r++) {
|
|
143
|
+
const { dim, count, flat } = results[r];
|
|
144
|
+
const start = starts[r];
|
|
145
|
+
for (let j = 0; j < count; j++) {
|
|
146
|
+
out[start + j] = Array.from(flat.subarray(j * dim, (j + 1) * dim));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return out;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async shutdown() {
|
|
153
|
+
this._shuttingDown = true;
|
|
154
|
+
// Reject anything still in flight so callers don't hang on shutdown.
|
|
155
|
+
for (const [id, p] of this._pending) {
|
|
156
|
+
this._settle(id, () => p.reject(new Error('ParallelEmbedder shut down')));
|
|
157
|
+
}
|
|
158
|
+
const ws = this._workers;
|
|
159
|
+
this._workers = [];
|
|
160
|
+
await Promise.all(ws.map((w) => w.terminate()));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export default ParallelEmbedder;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker-thread entry for the bundled-WASM parallel embedder (issue #523 SOTA).
|
|
3
|
+
*
|
|
4
|
+
* Each worker loads its own instance of the bundled ONNX WASM embedder from the
|
|
5
|
+
* SAME model bytes (shared via SharedArrayBuffer — no per-worker download) and
|
|
6
|
+
* the SAME config, so the vectors it produces are identical to the single-thread
|
|
7
|
+
* path (cosine-equivalent by construction).
|
|
8
|
+
*
|
|
9
|
+
* Protocol:
|
|
10
|
+
* workerData: { modelSab: SharedArrayBuffer, tokenizerJson: string, maxLength: number }
|
|
11
|
+
* → posts { type: 'ready' } once the WASM embedder is constructed
|
|
12
|
+
* message { type: 'embed', id, texts: string[] }
|
|
13
|
+
* → posts { type: 'result', id, dim, count, buffer } (Float32Array buffer, transferred)
|
|
14
|
+
* errors → { type: 'error', id, error }
|
|
15
|
+
*/
|
|
16
|
+
import { parentPort, workerData } from 'node:worker_threads';
|
|
17
|
+
import { pathToFileURL, fileURLToPath } from 'node:url';
|
|
18
|
+
import * as path from 'node:path';
|
|
19
|
+
import * as fs from 'node:fs';
|
|
20
|
+
|
|
21
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
22
|
+
|
|
23
|
+
let embedder = null;
|
|
24
|
+
|
|
25
|
+
async function init() {
|
|
26
|
+
const bgJsPath = path.join(__dirname, 'pkg', 'ruvector_onnx_embeddings_wasm_bg.js');
|
|
27
|
+
const wasmPath = path.join(__dirname, 'pkg', 'ruvector_onnx_embeddings_wasm_bg.wasm');
|
|
28
|
+
|
|
29
|
+
const wasmModule = await import(pathToFileURL(bgJsPath).href);
|
|
30
|
+
const wasmBytes = fs.readFileSync(wasmPath);
|
|
31
|
+
const wasmResult = await WebAssembly.instantiate(wasmBytes, {
|
|
32
|
+
'./ruvector_onnx_embeddings_wasm_bg.js': wasmModule,
|
|
33
|
+
});
|
|
34
|
+
const wasmExports = wasmResult.instance.exports;
|
|
35
|
+
if (typeof wasmModule.__wbg_set_wasm === 'function') wasmModule.__wbg_set_wasm(wasmExports);
|
|
36
|
+
if (typeof wasmExports.__wbindgen_start === 'function') wasmExports.__wbindgen_start();
|
|
37
|
+
|
|
38
|
+
// Reconstruct model bytes from the shared buffer (zero-copy view, then handed
|
|
39
|
+
// to wasm-bindgen which copies into WASM linear memory).
|
|
40
|
+
const modelBytes = new Uint8Array(workerData.modelSab);
|
|
41
|
+
|
|
42
|
+
const cfg = new wasmModule.WasmEmbedderConfig()
|
|
43
|
+
.setMaxLength(workerData.maxLength || 256)
|
|
44
|
+
.setNormalize(true)
|
|
45
|
+
.setPooling(0); // Mean pooling — matches the single-thread path.
|
|
46
|
+
|
|
47
|
+
embedder = wasmModule.WasmEmbedder.withConfig(modelBytes, workerData.tokenizerJson, cfg);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
parentPort.on('message', (msg) => {
|
|
51
|
+
if (msg.type !== 'embed') return;
|
|
52
|
+
try {
|
|
53
|
+
const dim = embedder.dimension();
|
|
54
|
+
const flat = embedder.embedBatch(msg.texts); // length = texts.length * dim
|
|
55
|
+
const arr = Float32Array.from(flat);
|
|
56
|
+
parentPort.postMessage(
|
|
57
|
+
{ type: 'result', id: msg.id, dim, count: msg.texts.length, buffer: arr.buffer },
|
|
58
|
+
[arr.buffer],
|
|
59
|
+
);
|
|
60
|
+
} catch (e) {
|
|
61
|
+
parentPort.postMessage({ type: 'error', id: msg.id, error: e?.message || String(e) });
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
init()
|
|
66
|
+
.then(() => parentPort.postMessage({ type: 'ready' }))
|
|
67
|
+
.catch((e) => parentPort.postMessage({ type: 'init-error', error: e?.message || String(e) }));
|
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model Loader for RuVector ONNX Embeddings WASM
|
|
3
|
+
*
|
|
4
|
+
* Provides easy loading of pre-trained models from HuggingFace Hub
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Pre-configured models with their HuggingFace URLs
|
|
9
|
+
*/
|
|
10
|
+
export const MODELS = {
|
|
11
|
+
// Sentence Transformers - Small & Fast
|
|
12
|
+
'all-MiniLM-L6-v2': {
|
|
13
|
+
name: 'all-MiniLM-L6-v2',
|
|
14
|
+
dimension: 384,
|
|
15
|
+
maxLength: 256,
|
|
16
|
+
size: '23MB',
|
|
17
|
+
description: 'Fast, general-purpose embeddings',
|
|
18
|
+
model: 'https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/onnx/model.onnx',
|
|
19
|
+
tokenizer: 'https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/tokenizer.json',
|
|
20
|
+
},
|
|
21
|
+
'all-MiniLM-L12-v2': {
|
|
22
|
+
name: 'all-MiniLM-L12-v2',
|
|
23
|
+
dimension: 384,
|
|
24
|
+
maxLength: 256,
|
|
25
|
+
size: '33MB',
|
|
26
|
+
description: 'Better quality, balanced speed',
|
|
27
|
+
model: 'https://huggingface.co/sentence-transformers/all-MiniLM-L12-v2/resolve/main/onnx/model.onnx',
|
|
28
|
+
tokenizer: 'https://huggingface.co/sentence-transformers/all-MiniLM-L12-v2/resolve/main/tokenizer.json',
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
// BGE Models - State of the art
|
|
32
|
+
'bge-small-en-v1.5': {
|
|
33
|
+
name: 'bge-small-en-v1.5',
|
|
34
|
+
dimension: 384,
|
|
35
|
+
maxLength: 512,
|
|
36
|
+
size: '33MB',
|
|
37
|
+
description: 'State-of-the-art small model',
|
|
38
|
+
model: 'https://huggingface.co/BAAI/bge-small-en-v1.5/resolve/main/onnx/model.onnx',
|
|
39
|
+
tokenizer: 'https://huggingface.co/BAAI/bge-small-en-v1.5/resolve/main/tokenizer.json',
|
|
40
|
+
},
|
|
41
|
+
'bge-base-en-v1.5': {
|
|
42
|
+
name: 'bge-base-en-v1.5',
|
|
43
|
+
dimension: 768,
|
|
44
|
+
maxLength: 512,
|
|
45
|
+
size: '110MB',
|
|
46
|
+
description: 'Best overall quality',
|
|
47
|
+
model: 'https://huggingface.co/BAAI/bge-base-en-v1.5/resolve/main/onnx/model.onnx',
|
|
48
|
+
tokenizer: 'https://huggingface.co/BAAI/bge-base-en-v1.5/resolve/main/tokenizer.json',
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
// E5 Models - Microsoft
|
|
52
|
+
'e5-small-v2': {
|
|
53
|
+
name: 'e5-small-v2',
|
|
54
|
+
dimension: 384,
|
|
55
|
+
maxLength: 512,
|
|
56
|
+
size: '33MB',
|
|
57
|
+
description: 'Excellent for search & retrieval',
|
|
58
|
+
model: 'https://huggingface.co/intfloat/e5-small-v2/resolve/main/onnx/model.onnx',
|
|
59
|
+
tokenizer: 'https://huggingface.co/intfloat/e5-small-v2/resolve/main/tokenizer.json',
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
// GTE Models - Alibaba
|
|
63
|
+
'gte-small': {
|
|
64
|
+
name: 'gte-small',
|
|
65
|
+
dimension: 384,
|
|
66
|
+
maxLength: 512,
|
|
67
|
+
size: '33MB',
|
|
68
|
+
description: 'Good multilingual support',
|
|
69
|
+
model: 'https://huggingface.co/thenlper/gte-small/resolve/main/onnx/model.onnx',
|
|
70
|
+
tokenizer: 'https://huggingface.co/thenlper/gte-small/resolve/main/tokenizer.json',
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Default model for quick start
|
|
76
|
+
*/
|
|
77
|
+
export const DEFAULT_MODEL = 'all-MiniLM-L6-v2';
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* In-memory memo of loaded models, keyed by model name. Deduplicates the
|
|
81
|
+
* (re-)download + decode when multiple embedder instances load the same model
|
|
82
|
+
* in one process. In Node there is no Cache API, so without this every
|
|
83
|
+
* ModelLoader.loadModel() re-fetches the model from HuggingFace (issue #523).
|
|
84
|
+
*/
|
|
85
|
+
const _inMemoryModelCache = new Map();
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Model loader with caching support
|
|
89
|
+
*/
|
|
90
|
+
export class ModelLoader {
|
|
91
|
+
constructor(options = {}) {
|
|
92
|
+
this.cache = options.cache ?? true;
|
|
93
|
+
this.cacheStorage = options.cacheStorage ?? 'ruvector-models';
|
|
94
|
+
this.onProgress = options.onProgress ?? null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Load a pre-configured model by name
|
|
99
|
+
* @param {string} modelName - Model name from MODELS
|
|
100
|
+
* @returns {Promise<{modelBytes: Uint8Array, tokenizerJson: string, config: object}>}
|
|
101
|
+
*/
|
|
102
|
+
async loadModel(modelName = DEFAULT_MODEL) {
|
|
103
|
+
const modelConfig = MODELS[modelName];
|
|
104
|
+
if (!modelConfig) {
|
|
105
|
+
throw new Error(`Unknown model: ${modelName}. Available: ${Object.keys(MODELS).join(', ')}`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// In-memory memo: a second load of the same model in this process reuses
|
|
109
|
+
// the already-downloaded bytes instead of re-fetching (issue #523).
|
|
110
|
+
if (this.cache && _inMemoryModelCache.has(modelName)) {
|
|
111
|
+
return _inMemoryModelCache.get(modelName);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// On-disk cache (Node only): models persist across processes so they are
|
|
115
|
+
// downloaded once, not every run. The browser has the Cache API instead
|
|
116
|
+
// (handled in fetchWithCache). See issue #523.
|
|
117
|
+
if (this.cache) {
|
|
118
|
+
const disk = await this._loadFromDisk(modelName);
|
|
119
|
+
if (disk) {
|
|
120
|
+
const cached = { ...disk, config: modelConfig };
|
|
121
|
+
_inMemoryModelCache.set(modelName, cached);
|
|
122
|
+
return cached;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
console.log(`Loading model: ${modelConfig.name} (${modelConfig.size})`);
|
|
127
|
+
|
|
128
|
+
const [modelBytes, tokenizerJson] = await Promise.all([
|
|
129
|
+
this.fetchWithCache(modelConfig.model, `${modelName}-model.onnx`, 'arraybuffer'),
|
|
130
|
+
this.fetchWithCache(modelConfig.tokenizer, `${modelName}-tokenizer.json`, 'text'),
|
|
131
|
+
]);
|
|
132
|
+
|
|
133
|
+
const result = {
|
|
134
|
+
modelBytes: new Uint8Array(modelBytes),
|
|
135
|
+
tokenizerJson,
|
|
136
|
+
config: modelConfig,
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
if (this.cache) {
|
|
140
|
+
_inMemoryModelCache.set(modelName, result);
|
|
141
|
+
await this._saveToDisk(modelName, result.modelBytes, tokenizerJson);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Resolve the Node on-disk cache dir for a model (null in non-Node envs).
|
|
149
|
+
* Uses dynamic import so this module stays loadable in browsers/bundlers.
|
|
150
|
+
*/
|
|
151
|
+
async _diskCacheDir(modelName) {
|
|
152
|
+
if (typeof process === 'undefined' || !process.versions?.node) return null;
|
|
153
|
+
const home = process.env.RUVECTOR_CACHE_DIR
|
|
154
|
+
|| process.env.HOME || process.env.USERPROFILE || '/tmp';
|
|
155
|
+
const path = await import('node:path');
|
|
156
|
+
return path.join(home, '.ruvector', 'models', modelName);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Load model bytes + tokenizer from the Node disk cache, or null if absent. */
|
|
160
|
+
async _loadFromDisk(modelName) {
|
|
161
|
+
const dir = await this._diskCacheDir(modelName);
|
|
162
|
+
if (!dir) return null;
|
|
163
|
+
try {
|
|
164
|
+
const fs = await import('node:fs');
|
|
165
|
+
const path = await import('node:path');
|
|
166
|
+
const modelPath = path.join(dir, 'model.onnx');
|
|
167
|
+
const tokPath = path.join(dir, 'tokenizer.json');
|
|
168
|
+
if (!fs.existsSync(modelPath) || !fs.existsSync(tokPath)) return null;
|
|
169
|
+
const modelBytes = new Uint8Array(fs.readFileSync(modelPath));
|
|
170
|
+
const tokenizerJson = fs.readFileSync(tokPath, 'utf8');
|
|
171
|
+
if (modelBytes.length === 0 || tokenizerJson.length === 0) return null;
|
|
172
|
+
console.log(` Disk cache hit: ${modelName}`);
|
|
173
|
+
return { modelBytes, tokenizerJson };
|
|
174
|
+
} catch {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Persist model bytes + tokenizer to the Node disk cache (best-effort). */
|
|
180
|
+
async _saveToDisk(modelName, modelBytes, tokenizerJson) {
|
|
181
|
+
const dir = await this._diskCacheDir(modelName);
|
|
182
|
+
if (!dir) return;
|
|
183
|
+
try {
|
|
184
|
+
const fs = await import('node:fs');
|
|
185
|
+
const path = await import('node:path');
|
|
186
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
187
|
+
// Write to temp files then rename, so a crash mid-write can't leave a
|
|
188
|
+
// truncated cache entry that later reads would trust.
|
|
189
|
+
const mTmp = path.join(dir, 'model.onnx.tmp');
|
|
190
|
+
const tTmp = path.join(dir, 'tokenizer.json.tmp');
|
|
191
|
+
fs.writeFileSync(mTmp, Buffer.from(modelBytes));
|
|
192
|
+
fs.writeFileSync(tTmp, tokenizerJson);
|
|
193
|
+
fs.renameSync(mTmp, path.join(dir, 'model.onnx'));
|
|
194
|
+
fs.renameSync(tTmp, path.join(dir, 'tokenizer.json'));
|
|
195
|
+
} catch {
|
|
196
|
+
// Cache write is best-effort; embedding still works without it.
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Load model from custom URLs
|
|
202
|
+
* @param {string} modelUrl - URL to ONNX model
|
|
203
|
+
* @param {string} tokenizerUrl - URL to tokenizer.json
|
|
204
|
+
* @returns {Promise<{modelBytes: Uint8Array, tokenizerJson: string}>}
|
|
205
|
+
*/
|
|
206
|
+
async loadFromUrls(modelUrl, tokenizerUrl) {
|
|
207
|
+
const [modelBytes, tokenizerJson] = await Promise.all([
|
|
208
|
+
this.fetchWithCache(modelUrl, null, 'arraybuffer'),
|
|
209
|
+
this.fetchWithCache(tokenizerUrl, null, 'text'),
|
|
210
|
+
]);
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
modelBytes: new Uint8Array(modelBytes),
|
|
214
|
+
tokenizerJson,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Load model from local files (Node.js)
|
|
220
|
+
* @param {string} modelPath - Path to ONNX model
|
|
221
|
+
* @param {string} tokenizerPath - Path to tokenizer.json
|
|
222
|
+
* @returns {Promise<{modelBytes: Uint8Array, tokenizerJson: string}>}
|
|
223
|
+
*/
|
|
224
|
+
async loadFromFiles(modelPath, tokenizerPath) {
|
|
225
|
+
// Node.js environment
|
|
226
|
+
if (typeof process !== 'undefined' && process.versions?.node) {
|
|
227
|
+
const fs = await import('fs/promises');
|
|
228
|
+
const [modelBytes, tokenizerJson] = await Promise.all([
|
|
229
|
+
fs.readFile(modelPath),
|
|
230
|
+
fs.readFile(tokenizerPath, 'utf8'),
|
|
231
|
+
]);
|
|
232
|
+
return {
|
|
233
|
+
modelBytes: new Uint8Array(modelBytes),
|
|
234
|
+
tokenizerJson,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
throw new Error('loadFromFiles is only available in Node.js');
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Fetch with optional caching (uses Cache API in browsers)
|
|
242
|
+
*/
|
|
243
|
+
async fetchWithCache(url, cacheKey, responseType) {
|
|
244
|
+
// Try cache first (browser only)
|
|
245
|
+
if (this.cache && typeof caches !== 'undefined' && cacheKey) {
|
|
246
|
+
try {
|
|
247
|
+
const cache = await caches.open(this.cacheStorage);
|
|
248
|
+
const cached = await cache.match(cacheKey);
|
|
249
|
+
if (cached) {
|
|
250
|
+
console.log(` Cache hit: ${cacheKey}`);
|
|
251
|
+
return responseType === 'arraybuffer'
|
|
252
|
+
? await cached.arrayBuffer()
|
|
253
|
+
: await cached.text();
|
|
254
|
+
}
|
|
255
|
+
} catch (e) {
|
|
256
|
+
// Cache API not available, continue with fetch
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Fetch from network
|
|
261
|
+
console.log(` Downloading: ${url}`);
|
|
262
|
+
const response = await this.fetchWithProgress(url);
|
|
263
|
+
|
|
264
|
+
if (!response.ok) {
|
|
265
|
+
throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Clone for caching
|
|
269
|
+
const responseClone = response.clone();
|
|
270
|
+
|
|
271
|
+
// Cache the response (browser only)
|
|
272
|
+
if (this.cache && typeof caches !== 'undefined' && cacheKey) {
|
|
273
|
+
try {
|
|
274
|
+
const cache = await caches.open(this.cacheStorage);
|
|
275
|
+
await cache.put(cacheKey, responseClone);
|
|
276
|
+
} catch (e) {
|
|
277
|
+
// Cache write failed, continue
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return responseType === 'arraybuffer'
|
|
282
|
+
? await response.arrayBuffer()
|
|
283
|
+
: await response.text();
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Fetch with progress reporting
|
|
288
|
+
*/
|
|
289
|
+
async fetchWithProgress(url) {
|
|
290
|
+
const response = await fetch(url);
|
|
291
|
+
|
|
292
|
+
if (!this.onProgress || !response.body) {
|
|
293
|
+
return response;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const contentLength = response.headers.get('content-length');
|
|
297
|
+
if (!contentLength) {
|
|
298
|
+
return response;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const total = parseInt(contentLength, 10);
|
|
302
|
+
let loaded = 0;
|
|
303
|
+
|
|
304
|
+
const reader = response.body.getReader();
|
|
305
|
+
const chunks = [];
|
|
306
|
+
|
|
307
|
+
while (true) {
|
|
308
|
+
const { done, value } = await reader.read();
|
|
309
|
+
if (done) break;
|
|
310
|
+
|
|
311
|
+
chunks.push(value);
|
|
312
|
+
loaded += value.length;
|
|
313
|
+
|
|
314
|
+
this.onProgress({
|
|
315
|
+
loaded,
|
|
316
|
+
total,
|
|
317
|
+
percent: Math.round((loaded / total) * 100),
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const body = new Uint8Array(loaded);
|
|
322
|
+
let position = 0;
|
|
323
|
+
for (const chunk of chunks) {
|
|
324
|
+
body.set(chunk, position);
|
|
325
|
+
position += chunk.length;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return new Response(body, {
|
|
329
|
+
headers: response.headers,
|
|
330
|
+
status: response.status,
|
|
331
|
+
statusText: response.statusText,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Clear cached models
|
|
337
|
+
*/
|
|
338
|
+
async clearCache() {
|
|
339
|
+
if (typeof caches !== 'undefined') {
|
|
340
|
+
await caches.delete(this.cacheStorage);
|
|
341
|
+
console.log('Model cache cleared');
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* List available models
|
|
347
|
+
*/
|
|
348
|
+
static listModels() {
|
|
349
|
+
return Object.entries(MODELS).map(([key, config]) => ({
|
|
350
|
+
id: key,
|
|
351
|
+
...config,
|
|
352
|
+
}));
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Quick helper to create an embedder with a pre-configured model
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* ```javascript
|
|
361
|
+
* import { createEmbedder } from './loader.js';
|
|
362
|
+
*
|
|
363
|
+
* const embedder = await createEmbedder('all-MiniLM-L6-v2');
|
|
364
|
+
* const embedding = embedder.embedOne("Hello world");
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
367
|
+
export async function createEmbedder(modelName = DEFAULT_MODEL, wasmModule = null) {
|
|
368
|
+
// Import WASM module if not provided
|
|
369
|
+
if (!wasmModule) {
|
|
370
|
+
wasmModule = await import('./pkg/ruvector_onnx_embeddings_wasm.js');
|
|
371
|
+
await wasmModule.default();
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const loader = new ModelLoader();
|
|
375
|
+
const { modelBytes, tokenizerJson, config } = await loader.loadModel(modelName);
|
|
376
|
+
|
|
377
|
+
const embedderConfig = new wasmModule.WasmEmbedderConfig()
|
|
378
|
+
.setMaxLength(config.maxLength)
|
|
379
|
+
.setNormalize(true)
|
|
380
|
+
.setPooling(0); // Mean pooling
|
|
381
|
+
|
|
382
|
+
const embedder = wasmModule.WasmEmbedder.withConfig(
|
|
383
|
+
modelBytes,
|
|
384
|
+
tokenizerJson,
|
|
385
|
+
embedderConfig
|
|
386
|
+
);
|
|
387
|
+
|
|
388
|
+
return embedder;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Quick helper for one-off embedding (loads model, embeds, returns)
|
|
393
|
+
*
|
|
394
|
+
* @example
|
|
395
|
+
* ```javascript
|
|
396
|
+
* import { embed } from './loader.js';
|
|
397
|
+
*
|
|
398
|
+
* const embedding = await embed("Hello world");
|
|
399
|
+
* const embeddings = await embed(["Hello", "World"]);
|
|
400
|
+
* ```
|
|
401
|
+
*/
|
|
402
|
+
export async function embed(text, modelName = DEFAULT_MODEL) {
|
|
403
|
+
const embedder = await createEmbedder(modelName);
|
|
404
|
+
|
|
405
|
+
if (Array.isArray(text)) {
|
|
406
|
+
return embedder.embedBatch(text);
|
|
407
|
+
}
|
|
408
|
+
return embedder.embedOne(text);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Quick helper for similarity comparison
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* ```javascript
|
|
416
|
+
* import { similarity } from './loader.js';
|
|
417
|
+
*
|
|
418
|
+
* const score = await similarity("I love dogs", "I adore puppies");
|
|
419
|
+
* console.log(score); // ~0.85
|
|
420
|
+
* ```
|
|
421
|
+
*/
|
|
422
|
+
export async function similarity(text1, text2, modelName = DEFAULT_MODEL) {
|
|
423
|
+
const embedder = await createEmbedder(modelName);
|
|
424
|
+
return embedder.similarity(text1, text2);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
export default {
|
|
428
|
+
MODELS,
|
|
429
|
+
DEFAULT_MODEL,
|
|
430
|
+
ModelLoader,
|
|
431
|
+
createEmbedder,
|
|
432
|
+
embed,
|
|
433
|
+
similarity,
|
|
434
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 rUv
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|