ruvector 0.2.13 → 0.2.15
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/mcp-server.js +1 -1
- package/dist/core/core/agentdb-fast.d.ts +148 -0
- package/dist/core/core/agentdb-fast.js +301 -0
- package/dist/core/core/intelligence-engine.d.ts +257 -0
- package/dist/core/core/intelligence-engine.js +1030 -0
- package/dist/core/core/onnx-embedder.d.ts +104 -0
- package/dist/core/core/onnx-embedder.js +410 -0
- package/dist/core/core/parallel-intelligence.d.ts +108 -0
- package/dist/core/core/parallel-intelligence.js +340 -0
- package/dist/core/core/sona-wrapper.d.ts +214 -0
- package/dist/core/core/sona-wrapper.js +258 -0
- package/dist/core/intelligence-engine.d.ts.map +1 -1
- package/dist/core/intelligence-engine.js +2 -1
- package/dist/core/onnx/loader.js +348 -0
- package/dist/core/onnx/pkg/LICENSE +21 -0
- package/dist/core/onnx/pkg/loader.js +348 -0
- package/dist/core/onnx/pkg/package.json +3 -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/types.d.ts +144 -0
- package/dist/core/types.js +2 -0
- package/dist/workers/native-worker.d.ts.map +1 -1
- package/dist/workers/native-worker.js +46 -3
- package/package.json +4 -2
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* SONA Wrapper - Self-Optimizing Neural Architecture
|
|
4
|
+
*
|
|
5
|
+
* Provides a safe, flexible interface to @ruvector/sona with:
|
|
6
|
+
* - Automatic array type conversion (Array <-> Float64Array)
|
|
7
|
+
* - Graceful handling when sona is not installed
|
|
8
|
+
* - TypeScript types for all APIs
|
|
9
|
+
*
|
|
10
|
+
* SONA Features:
|
|
11
|
+
* - Micro-LoRA: Ultra-fast rank-1/2 adaptations (~0.1ms)
|
|
12
|
+
* - Base-LoRA: Deeper adaptations for complex patterns
|
|
13
|
+
* - EWC++: Elastic Weight Consolidation to prevent catastrophic forgetting
|
|
14
|
+
* - ReasoningBank: Pattern storage and retrieval
|
|
15
|
+
* - Trajectory tracking: Record and learn from execution paths
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.Sona = exports.SonaEngine = void 0;
|
|
19
|
+
exports.isSonaAvailable = isSonaAvailable;
|
|
20
|
+
// ============================================================================
|
|
21
|
+
// Helper Functions
|
|
22
|
+
// ============================================================================
|
|
23
|
+
/** Convert any array-like to regular Array (SONA expects number[]) */
|
|
24
|
+
function toArray(input) {
|
|
25
|
+
if (Array.isArray(input))
|
|
26
|
+
return input;
|
|
27
|
+
return Array.from(input);
|
|
28
|
+
}
|
|
29
|
+
// ============================================================================
|
|
30
|
+
// Lazy Loading
|
|
31
|
+
// ============================================================================
|
|
32
|
+
let sonaModule = null;
|
|
33
|
+
let sonaLoadError = null;
|
|
34
|
+
function getSonaModule() {
|
|
35
|
+
if (sonaModule)
|
|
36
|
+
return sonaModule;
|
|
37
|
+
if (sonaLoadError)
|
|
38
|
+
throw sonaLoadError;
|
|
39
|
+
try {
|
|
40
|
+
sonaModule = require('@ruvector/sona');
|
|
41
|
+
return sonaModule;
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
sonaLoadError = new Error(`@ruvector/sona is not installed. Install it with:\n` +
|
|
45
|
+
` npm install @ruvector/sona\n\n` +
|
|
46
|
+
`Original error: ${e.message}`);
|
|
47
|
+
throw sonaLoadError;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Check if sona is available */
|
|
51
|
+
function isSonaAvailable() {
|
|
52
|
+
try {
|
|
53
|
+
getSonaModule();
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// ============================================================================
|
|
61
|
+
// SONA Engine Wrapper
|
|
62
|
+
// ============================================================================
|
|
63
|
+
/**
|
|
64
|
+
* SONA Engine - Self-Optimizing Neural Architecture
|
|
65
|
+
*
|
|
66
|
+
* Provides runtime-adaptive learning with:
|
|
67
|
+
* - Micro-LoRA for instant adaptations
|
|
68
|
+
* - Base-LoRA for deeper learning
|
|
69
|
+
* - EWC++ for preventing forgetting
|
|
70
|
+
* - ReasoningBank for pattern storage
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* import { Sona } from 'ruvector';
|
|
75
|
+
*
|
|
76
|
+
* // Create engine with hidden dimension
|
|
77
|
+
* const engine = new Sona.Engine(256);
|
|
78
|
+
*
|
|
79
|
+
* // Or with custom config
|
|
80
|
+
* const engine = Sona.Engine.withConfig({
|
|
81
|
+
* hiddenDim: 256,
|
|
82
|
+
* microLoraRank: 2,
|
|
83
|
+
* patternClusters: 100
|
|
84
|
+
* });
|
|
85
|
+
*
|
|
86
|
+
* // Record a trajectory
|
|
87
|
+
* const trajId = engine.beginTrajectory([0.1, 0.2, ...]);
|
|
88
|
+
* engine.addStep(trajId, activations, attentionWeights, 0.8);
|
|
89
|
+
* engine.endTrajectory(trajId, 0.9);
|
|
90
|
+
*
|
|
91
|
+
* // Apply learned adaptations
|
|
92
|
+
* const adapted = engine.applyMicroLora(input);
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
class SonaEngine {
|
|
96
|
+
/**
|
|
97
|
+
* Create a new SONA engine
|
|
98
|
+
* @param hiddenDim Hidden dimension size (e.g., 256, 512, 768)
|
|
99
|
+
*/
|
|
100
|
+
constructor(hiddenDim) {
|
|
101
|
+
const mod = getSonaModule();
|
|
102
|
+
this._native = new mod.SonaEngine(hiddenDim);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Create engine with custom configuration
|
|
106
|
+
* @param config SONA configuration options
|
|
107
|
+
*/
|
|
108
|
+
static withConfig(config) {
|
|
109
|
+
const mod = getSonaModule();
|
|
110
|
+
const engine = new SonaEngine(config.hiddenDim);
|
|
111
|
+
// Replace native with configured version
|
|
112
|
+
engine._native = mod.SonaEngine.withConfig(config);
|
|
113
|
+
return engine;
|
|
114
|
+
}
|
|
115
|
+
// -------------------------------------------------------------------------
|
|
116
|
+
// Trajectory Recording
|
|
117
|
+
// -------------------------------------------------------------------------
|
|
118
|
+
/**
|
|
119
|
+
* Begin recording a new trajectory
|
|
120
|
+
* @param queryEmbedding Initial query embedding
|
|
121
|
+
* @returns Trajectory ID for subsequent operations
|
|
122
|
+
*/
|
|
123
|
+
beginTrajectory(queryEmbedding) {
|
|
124
|
+
return this._native.beginTrajectory(toArray(queryEmbedding));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Add a step to an active trajectory
|
|
128
|
+
* @param trajectoryId Trajectory ID from beginTrajectory
|
|
129
|
+
* @param activations Layer activations
|
|
130
|
+
* @param attentionWeights Attention weights
|
|
131
|
+
* @param reward Reward signal for this step (0.0 - 1.0)
|
|
132
|
+
*/
|
|
133
|
+
addStep(trajectoryId, activations, attentionWeights, reward) {
|
|
134
|
+
this._native.addTrajectoryStep(trajectoryId, toArray(activations), toArray(attentionWeights), reward);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Alias for addStep for API compatibility
|
|
138
|
+
*/
|
|
139
|
+
addTrajectoryStep(trajectoryId, activations, attentionWeights, reward) {
|
|
140
|
+
this.addStep(trajectoryId, activations, attentionWeights, reward);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Set the model route for a trajectory
|
|
144
|
+
* @param trajectoryId Trajectory ID
|
|
145
|
+
* @param route Model route identifier (e.g., "gpt-4", "claude-3")
|
|
146
|
+
*/
|
|
147
|
+
setRoute(trajectoryId, route) {
|
|
148
|
+
this._native.setTrajectoryRoute(trajectoryId, route);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Add context to a trajectory
|
|
152
|
+
* @param trajectoryId Trajectory ID
|
|
153
|
+
* @param contextId Context identifier
|
|
154
|
+
*/
|
|
155
|
+
addContext(trajectoryId, contextId) {
|
|
156
|
+
this._native.addTrajectoryContext(trajectoryId, contextId);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Complete a trajectory and submit for learning
|
|
160
|
+
* @param trajectoryId Trajectory ID
|
|
161
|
+
* @param quality Final quality score (0.0 - 1.0)
|
|
162
|
+
*/
|
|
163
|
+
endTrajectory(trajectoryId, quality) {
|
|
164
|
+
this._native.endTrajectory(trajectoryId, quality);
|
|
165
|
+
}
|
|
166
|
+
// -------------------------------------------------------------------------
|
|
167
|
+
// LoRA Transformations
|
|
168
|
+
// -------------------------------------------------------------------------
|
|
169
|
+
/**
|
|
170
|
+
* Apply micro-LoRA transformation (ultra-fast, ~0.1ms)
|
|
171
|
+
* @param input Input vector
|
|
172
|
+
* @returns Transformed output vector
|
|
173
|
+
*/
|
|
174
|
+
applyMicroLora(input) {
|
|
175
|
+
return this._native.applyMicroLora(toArray(input));
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Apply base-LoRA transformation to a specific layer
|
|
179
|
+
* @param layerIdx Layer index
|
|
180
|
+
* @param input Input vector
|
|
181
|
+
* @returns Transformed output vector
|
|
182
|
+
*/
|
|
183
|
+
applyBaseLora(layerIdx, input) {
|
|
184
|
+
return this._native.applyBaseLora(layerIdx, toArray(input));
|
|
185
|
+
}
|
|
186
|
+
// -------------------------------------------------------------------------
|
|
187
|
+
// Learning Control
|
|
188
|
+
// -------------------------------------------------------------------------
|
|
189
|
+
/**
|
|
190
|
+
* Run background learning cycle if due
|
|
191
|
+
* Call this periodically (e.g., every few seconds)
|
|
192
|
+
* @returns Status message if learning occurred, null otherwise
|
|
193
|
+
*/
|
|
194
|
+
tick() {
|
|
195
|
+
return this._native.tick();
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Force immediate background learning cycle
|
|
199
|
+
* @returns Status message with learning results
|
|
200
|
+
*/
|
|
201
|
+
forceLearn() {
|
|
202
|
+
return this._native.forceLearn();
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Flush pending instant loop updates
|
|
206
|
+
*/
|
|
207
|
+
flush() {
|
|
208
|
+
this._native.flush();
|
|
209
|
+
}
|
|
210
|
+
// -------------------------------------------------------------------------
|
|
211
|
+
// Pattern Retrieval
|
|
212
|
+
// -------------------------------------------------------------------------
|
|
213
|
+
/**
|
|
214
|
+
* Find similar learned patterns to a query
|
|
215
|
+
* @param queryEmbedding Query embedding
|
|
216
|
+
* @param k Number of patterns to return
|
|
217
|
+
* @returns Array of similar patterns
|
|
218
|
+
*/
|
|
219
|
+
findPatterns(queryEmbedding, k) {
|
|
220
|
+
return this._native.findPatterns(toArray(queryEmbedding), k);
|
|
221
|
+
}
|
|
222
|
+
// -------------------------------------------------------------------------
|
|
223
|
+
// Engine Control
|
|
224
|
+
// -------------------------------------------------------------------------
|
|
225
|
+
/**
|
|
226
|
+
* Get engine statistics
|
|
227
|
+
* @returns Statistics object
|
|
228
|
+
*/
|
|
229
|
+
getStats() {
|
|
230
|
+
const statsJson = this._native.getStats();
|
|
231
|
+
return JSON.parse(statsJson);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Enable or disable the engine
|
|
235
|
+
* @param enabled Whether to enable
|
|
236
|
+
*/
|
|
237
|
+
setEnabled(enabled) {
|
|
238
|
+
this._native.setEnabled(enabled);
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Check if engine is enabled
|
|
242
|
+
*/
|
|
243
|
+
isEnabled() {
|
|
244
|
+
return this._native.isEnabled();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
exports.SonaEngine = SonaEngine;
|
|
248
|
+
// ============================================================================
|
|
249
|
+
// Convenience Exports
|
|
250
|
+
// ============================================================================
|
|
251
|
+
/**
|
|
252
|
+
* SONA namespace with all exports
|
|
253
|
+
*/
|
|
254
|
+
exports.Sona = {
|
|
255
|
+
Engine: SonaEngine,
|
|
256
|
+
isAvailable: isSonaAvailable,
|
|
257
|
+
};
|
|
258
|
+
exports.default = exports.Sona;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"intelligence-engine.d.ts","sourceRoot":"","sources":["../../src/core/intelligence-engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAoC,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACvF,OAAO,EAAc,UAAU,EAAE,cAAc,EAA8B,MAAM,gBAAgB,CAAC;AAEpG,OAAO,EAAwB,cAAc,EAAE,YAAY,EAA2B,MAAM,yBAAyB,CAAC;AAMtH,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,cAAc,EAAE,CAAC;IAC5B,UAAU,CAAC,EAAE,KAAK,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CAC3D;AAED,MAAM,WAAW,aAAa;IAE5B,aAAa,EAAE,MAAM,CAAC;IACtB,gBAAgB,EAAE,MAAM,CAAC;IAGzB,aAAa,EAAE,MAAM,CAAC;IACtB,iBA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AG,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAc1F;;OAEG;IACH,cAAc,CAAC,YAAY,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI;IAUvD;;OAEG;IACH,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE;IA6B1C;;OAEG;IACH,IAAI,IAAI,MAAM,GAAG,IAAI;IAWrB;;OAEG;IACH,UAAU,IAAI,MAAM,GAAG,IAAI;IAe3B;;OAEG;IACH,QAAQ,IAAI,aAAa;IA0DzB;;OAEG;IACH,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAkC7B;;OAEG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,KAAK,GAAE,OAAe,GAAG,IAAI;IA4E/D;;OAEG;IACH,KAAK,IAAI,IAAI;IAcb,8BAA8B;IAC9B,IAAI,QAAQ,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAMrD;IAED,mCAAmC;IACnC,IAAI,cAAc,IAAI,MAAM,EAAE,EAAE,CAW/B;IAED,4BAA4B;IAC5B,IAAI,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAErC;CACF;AAMD;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,CAAC,EAAE,kBAAkB,GAAG,kBAAkB,CAExF;AAED;;GAEG;AACH,wBAAgB,2BAA2B,IAAI,kBAAkB,CAchE;AAED;;GAEG;AACH,wBAAgB,uBAAuB,IAAI,kBAAkB,CAQ5D;AAED,eAAe,kBAAkB,CAAC"}
|
|
@@ -417,7 +417,8 @@ class IntelligenceEngine {
|
|
|
417
417
|
async route(task, file) {
|
|
418
418
|
const ext = file ? this.getExtension(file) : '';
|
|
419
419
|
const state = this.getState(task, ext);
|
|
420
|
-
|
|
420
|
+
// Use async ONNX embeddings for semantic routing (critical fix)
|
|
421
|
+
const taskEmbed = await this.embedAsync(task + ' ' + (file || ''));
|
|
421
422
|
// Apply SONA micro-LoRA transformation if available
|
|
422
423
|
let adaptedEmbed = taskEmbed;
|
|
423
424
|
if (this.sona) {
|
|
@@ -0,0 +1,348 @@
|
|
|
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
|
+
* Model loader with caching support
|
|
81
|
+
*/
|
|
82
|
+
export class ModelLoader {
|
|
83
|
+
constructor(options = {}) {
|
|
84
|
+
this.cache = options.cache ?? true;
|
|
85
|
+
this.cacheStorage = options.cacheStorage ?? 'ruvector-models';
|
|
86
|
+
this.onProgress = options.onProgress ?? null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Load a pre-configured model by name
|
|
91
|
+
* @param {string} modelName - Model name from MODELS
|
|
92
|
+
* @returns {Promise<{modelBytes: Uint8Array, tokenizerJson: string, config: object}>}
|
|
93
|
+
*/
|
|
94
|
+
async loadModel(modelName = DEFAULT_MODEL) {
|
|
95
|
+
const modelConfig = MODELS[modelName];
|
|
96
|
+
if (!modelConfig) {
|
|
97
|
+
throw new Error(`Unknown model: ${modelName}. Available: ${Object.keys(MODELS).join(', ')}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
console.log(`Loading model: ${modelConfig.name} (${modelConfig.size})`);
|
|
101
|
+
|
|
102
|
+
const [modelBytes, tokenizerJson] = await Promise.all([
|
|
103
|
+
this.fetchWithCache(modelConfig.model, `${modelName}-model.onnx`, 'arraybuffer'),
|
|
104
|
+
this.fetchWithCache(modelConfig.tokenizer, `${modelName}-tokenizer.json`, 'text'),
|
|
105
|
+
]);
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
modelBytes: new Uint8Array(modelBytes),
|
|
109
|
+
tokenizerJson,
|
|
110
|
+
config: modelConfig,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Load model from custom URLs
|
|
116
|
+
* @param {string} modelUrl - URL to ONNX model
|
|
117
|
+
* @param {string} tokenizerUrl - URL to tokenizer.json
|
|
118
|
+
* @returns {Promise<{modelBytes: Uint8Array, tokenizerJson: string}>}
|
|
119
|
+
*/
|
|
120
|
+
async loadFromUrls(modelUrl, tokenizerUrl) {
|
|
121
|
+
const [modelBytes, tokenizerJson] = await Promise.all([
|
|
122
|
+
this.fetchWithCache(modelUrl, null, 'arraybuffer'),
|
|
123
|
+
this.fetchWithCache(tokenizerUrl, null, 'text'),
|
|
124
|
+
]);
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
modelBytes: new Uint8Array(modelBytes),
|
|
128
|
+
tokenizerJson,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Load model from local files (Node.js)
|
|
134
|
+
* @param {string} modelPath - Path to ONNX model
|
|
135
|
+
* @param {string} tokenizerPath - Path to tokenizer.json
|
|
136
|
+
* @returns {Promise<{modelBytes: Uint8Array, tokenizerJson: string}>}
|
|
137
|
+
*/
|
|
138
|
+
async loadFromFiles(modelPath, tokenizerPath) {
|
|
139
|
+
// Node.js environment
|
|
140
|
+
if (typeof process !== 'undefined' && process.versions?.node) {
|
|
141
|
+
const fs = await import('fs/promises');
|
|
142
|
+
const [modelBytes, tokenizerJson] = await Promise.all([
|
|
143
|
+
fs.readFile(modelPath),
|
|
144
|
+
fs.readFile(tokenizerPath, 'utf8'),
|
|
145
|
+
]);
|
|
146
|
+
return {
|
|
147
|
+
modelBytes: new Uint8Array(modelBytes),
|
|
148
|
+
tokenizerJson,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
throw new Error('loadFromFiles is only available in Node.js');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Fetch with optional caching (uses Cache API in browsers)
|
|
156
|
+
*/
|
|
157
|
+
async fetchWithCache(url, cacheKey, responseType) {
|
|
158
|
+
// Try cache first (browser only)
|
|
159
|
+
if (this.cache && typeof caches !== 'undefined' && cacheKey) {
|
|
160
|
+
try {
|
|
161
|
+
const cache = await caches.open(this.cacheStorage);
|
|
162
|
+
const cached = await cache.match(cacheKey);
|
|
163
|
+
if (cached) {
|
|
164
|
+
console.log(` Cache hit: ${cacheKey}`);
|
|
165
|
+
return responseType === 'arraybuffer'
|
|
166
|
+
? await cached.arrayBuffer()
|
|
167
|
+
: await cached.text();
|
|
168
|
+
}
|
|
169
|
+
} catch (e) {
|
|
170
|
+
// Cache API not available, continue with fetch
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Fetch from network
|
|
175
|
+
console.log(` Downloading: ${url}`);
|
|
176
|
+
const response = await this.fetchWithProgress(url);
|
|
177
|
+
|
|
178
|
+
if (!response.ok) {
|
|
179
|
+
throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Clone for caching
|
|
183
|
+
const responseClone = response.clone();
|
|
184
|
+
|
|
185
|
+
// Cache the response (browser only)
|
|
186
|
+
if (this.cache && typeof caches !== 'undefined' && cacheKey) {
|
|
187
|
+
try {
|
|
188
|
+
const cache = await caches.open(this.cacheStorage);
|
|
189
|
+
await cache.put(cacheKey, responseClone);
|
|
190
|
+
} catch (e) {
|
|
191
|
+
// Cache write failed, continue
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return responseType === 'arraybuffer'
|
|
196
|
+
? await response.arrayBuffer()
|
|
197
|
+
: await response.text();
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Fetch with progress reporting
|
|
202
|
+
*/
|
|
203
|
+
async fetchWithProgress(url) {
|
|
204
|
+
const response = await fetch(url);
|
|
205
|
+
|
|
206
|
+
if (!this.onProgress || !response.body) {
|
|
207
|
+
return response;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const contentLength = response.headers.get('content-length');
|
|
211
|
+
if (!contentLength) {
|
|
212
|
+
return response;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const total = parseInt(contentLength, 10);
|
|
216
|
+
let loaded = 0;
|
|
217
|
+
|
|
218
|
+
const reader = response.body.getReader();
|
|
219
|
+
const chunks = [];
|
|
220
|
+
|
|
221
|
+
while (true) {
|
|
222
|
+
const { done, value } = await reader.read();
|
|
223
|
+
if (done) break;
|
|
224
|
+
|
|
225
|
+
chunks.push(value);
|
|
226
|
+
loaded += value.length;
|
|
227
|
+
|
|
228
|
+
this.onProgress({
|
|
229
|
+
loaded,
|
|
230
|
+
total,
|
|
231
|
+
percent: Math.round((loaded / total) * 100),
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const body = new Uint8Array(loaded);
|
|
236
|
+
let position = 0;
|
|
237
|
+
for (const chunk of chunks) {
|
|
238
|
+
body.set(chunk, position);
|
|
239
|
+
position += chunk.length;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return new Response(body, {
|
|
243
|
+
headers: response.headers,
|
|
244
|
+
status: response.status,
|
|
245
|
+
statusText: response.statusText,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Clear cached models
|
|
251
|
+
*/
|
|
252
|
+
async clearCache() {
|
|
253
|
+
if (typeof caches !== 'undefined') {
|
|
254
|
+
await caches.delete(this.cacheStorage);
|
|
255
|
+
console.log('Model cache cleared');
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* List available models
|
|
261
|
+
*/
|
|
262
|
+
static listModels() {
|
|
263
|
+
return Object.entries(MODELS).map(([key, config]) => ({
|
|
264
|
+
id: key,
|
|
265
|
+
...config,
|
|
266
|
+
}));
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Quick helper to create an embedder with a pre-configured model
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* ```javascript
|
|
275
|
+
* import { createEmbedder } from './loader.js';
|
|
276
|
+
*
|
|
277
|
+
* const embedder = await createEmbedder('all-MiniLM-L6-v2');
|
|
278
|
+
* const embedding = embedder.embedOne("Hello world");
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
export async function createEmbedder(modelName = DEFAULT_MODEL, wasmModule = null) {
|
|
282
|
+
// Import WASM module if not provided
|
|
283
|
+
if (!wasmModule) {
|
|
284
|
+
wasmModule = await import('./pkg/ruvector_onnx_embeddings_wasm.js');
|
|
285
|
+
await wasmModule.default();
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const loader = new ModelLoader();
|
|
289
|
+
const { modelBytes, tokenizerJson, config } = await loader.loadModel(modelName);
|
|
290
|
+
|
|
291
|
+
const embedderConfig = new wasmModule.WasmEmbedderConfig()
|
|
292
|
+
.setMaxLength(config.maxLength)
|
|
293
|
+
.setNormalize(true)
|
|
294
|
+
.setPooling(0); // Mean pooling
|
|
295
|
+
|
|
296
|
+
const embedder = wasmModule.WasmEmbedder.withConfig(
|
|
297
|
+
modelBytes,
|
|
298
|
+
tokenizerJson,
|
|
299
|
+
embedderConfig
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
return embedder;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Quick helper for one-off embedding (loads model, embeds, returns)
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```javascript
|
|
310
|
+
* import { embed } from './loader.js';
|
|
311
|
+
*
|
|
312
|
+
* const embedding = await embed("Hello world");
|
|
313
|
+
* const embeddings = await embed(["Hello", "World"]);
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
export async function embed(text, modelName = DEFAULT_MODEL) {
|
|
317
|
+
const embedder = await createEmbedder(modelName);
|
|
318
|
+
|
|
319
|
+
if (Array.isArray(text)) {
|
|
320
|
+
return embedder.embedBatch(text);
|
|
321
|
+
}
|
|
322
|
+
return embedder.embedOne(text);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Quick helper for similarity comparison
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* ```javascript
|
|
330
|
+
* import { similarity } from './loader.js';
|
|
331
|
+
*
|
|
332
|
+
* const score = await similarity("I love dogs", "I adore puppies");
|
|
333
|
+
* console.log(score); // ~0.85
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
export async function similarity(text1, text2, modelName = DEFAULT_MODEL) {
|
|
337
|
+
const embedder = await createEmbedder(modelName);
|
|
338
|
+
return embedder.similarity(text1, text2);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export default {
|
|
342
|
+
MODELS,
|
|
343
|
+
DEFAULT_MODEL,
|
|
344
|
+
ModelLoader,
|
|
345
|
+
createEmbedder,
|
|
346
|
+
embed,
|
|
347
|
+
similarity,
|
|
348
|
+
};
|
|
@@ -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.
|