ruvector 0.2.16 → 0.2.18

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,127 @@
1
+ /**
2
+ * CommonJS-compatible WASM loader for Node.js
3
+ *
4
+ * This file provides a way to load the WASM module without requiring
5
+ * the --experimental-wasm-modules flag by manually loading the WASM bytes.
6
+ *
7
+ * Usage:
8
+ * const wasm = require('./ruvector_onnx_embeddings_wasm_cjs.js');
9
+ * await wasm.init(); // or wasm.initSync(wasmBytes)
10
+ */
11
+
12
+ const fs = require('fs');
13
+ const path = require('path');
14
+
15
+ // Re-export everything from the JS bindings
16
+ const bindings = require('./ruvector_onnx_embeddings_wasm_bg.js');
17
+
18
+ // Track initialization state
19
+ let initialized = false;
20
+ let initPromise = null;
21
+
22
+ /**
23
+ * Initialize the WASM module asynchronously
24
+ * Automatically loads the .wasm file from the same directory
25
+ */
26
+ async function init(wasmInput) {
27
+ if (initialized) return bindings;
28
+
29
+ if (initPromise) {
30
+ await initPromise;
31
+ return bindings;
32
+ }
33
+
34
+ initPromise = (async () => {
35
+ let wasmBytes;
36
+
37
+ if (wasmInput instanceof WebAssembly.Module) {
38
+ // Already compiled module
39
+ const instance = await WebAssembly.instantiate(wasmInput, getImports());
40
+ bindings.__wbg_set_wasm(instance.exports);
41
+ finishInit();
42
+ return;
43
+ } else if (wasmInput instanceof ArrayBuffer || wasmInput instanceof Uint8Array) {
44
+ // Raw bytes provided
45
+ wasmBytes = wasmInput;
46
+ } else if (typeof wasmInput === 'string') {
47
+ // Path to WASM file
48
+ wasmBytes = fs.readFileSync(wasmInput);
49
+ } else {
50
+ // Auto-detect WASM file location
51
+ const wasmPath = path.join(__dirname, 'ruvector_onnx_embeddings_wasm_bg.wasm');
52
+ wasmBytes = fs.readFileSync(wasmPath);
53
+ }
54
+
55
+ const wasmModule = await WebAssembly.compile(wasmBytes);
56
+ const instance = await WebAssembly.instantiate(wasmModule, getImports());
57
+
58
+ bindings.__wbg_set_wasm(instance.exports);
59
+ finishInit();
60
+ })();
61
+
62
+ await initPromise;
63
+ return bindings;
64
+ }
65
+
66
+ /**
67
+ * Initialize the WASM module synchronously
68
+ * Requires the WASM bytes to be provided
69
+ */
70
+ function initSync(wasmBytes) {
71
+ if (initialized) return bindings;
72
+
73
+ if (!wasmBytes) {
74
+ const wasmPath = path.join(__dirname, 'ruvector_onnx_embeddings_wasm_bg.wasm');
75
+ wasmBytes = fs.readFileSync(wasmPath);
76
+ }
77
+
78
+ const wasmModule = new WebAssembly.Module(wasmBytes);
79
+ const instance = new WebAssembly.Instance(wasmModule, getImports());
80
+
81
+ bindings.__wbg_set_wasm(instance.exports);
82
+ finishInit();
83
+
84
+ return bindings;
85
+ }
86
+
87
+ /**
88
+ * Get the WASM import object
89
+ */
90
+ function getImports() {
91
+ return {
92
+ './ruvector_onnx_embeddings_wasm_bg.js': bindings,
93
+ };
94
+ }
95
+
96
+ /**
97
+ * Finalize initialization
98
+ */
99
+ function finishInit() {
100
+ if (typeof bindings.__wbindgen_init_externref_table === 'function') {
101
+ bindings.__wbindgen_init_externref_table();
102
+ }
103
+ initialized = true;
104
+ }
105
+
106
+ /**
107
+ * Check if initialized
108
+ */
109
+ function isInitialized() {
110
+ return initialized;
111
+ }
112
+
113
+ // Export init functions and all bindings
114
+ module.exports = {
115
+ init,
116
+ initSync,
117
+ isInitialized,
118
+ default: init,
119
+ // Re-export all bindings
120
+ WasmEmbedder: bindings.WasmEmbedder,
121
+ WasmEmbedderConfig: bindings.WasmEmbedderConfig,
122
+ PoolingStrategy: bindings.PoolingStrategy,
123
+ cosineSimilarity: bindings.cosineSimilarity,
124
+ normalizeL2: bindings.normalizeL2,
125
+ simd_available: bindings.simd_available,
126
+ version: bindings.version,
127
+ };
@@ -0,0 +1,206 @@
1
+ /**
2
+ * ONNX LLM Text Generation for RuVector
3
+ *
4
+ * Provides real local LLM inference using ONNX Runtime via transformers.js
5
+ * Supports small models that run efficiently on CPU:
6
+ * - SmolLM 135M - Smallest, fast (~135MB)
7
+ * - SmolLM 360M - Better quality (~360MB)
8
+ * - TinyLlama 1.1B - Best small model quality (~1GB quantized)
9
+ * - Qwen2.5 0.5B - Good balance (~500MB)
10
+ *
11
+ * Features:
12
+ * - Automatic model downloading and caching
13
+ * - Quantized INT4/INT8 models for efficiency
14
+ * - Streaming generation support
15
+ * - Temperature, top-k, top-p sampling
16
+ * - KV cache for efficient multi-turn conversations
17
+ */
18
+ export interface OnnxLLMConfig {
19
+ /** Model ID (default: 'Xenova/smollm-135m-instruct') */
20
+ modelId?: string;
21
+ /** Cache directory for models */
22
+ cacheDir?: string;
23
+ /** Use quantized model (default: true) */
24
+ quantized?: boolean;
25
+ /** Device: 'cpu' | 'webgpu' (default: 'cpu') */
26
+ device?: 'cpu' | 'webgpu';
27
+ /** Maximum context length */
28
+ maxLength?: number;
29
+ }
30
+ export interface GenerationConfig {
31
+ /** Maximum new tokens to generate (default: 128) */
32
+ maxNewTokens?: number;
33
+ /** Temperature for sampling (default: 0.7) */
34
+ temperature?: number;
35
+ /** Top-p nucleus sampling (default: 0.9) */
36
+ topP?: number;
37
+ /** Top-k sampling (default: 50) */
38
+ topK?: number;
39
+ /** Repetition penalty (default: 1.1) */
40
+ repetitionPenalty?: number;
41
+ /** Stop sequences */
42
+ stopSequences?: string[];
43
+ /** System prompt for chat models */
44
+ systemPrompt?: string;
45
+ /** Enable streaming (callback for each token) */
46
+ onToken?: (token: string) => void;
47
+ }
48
+ export interface GenerationResult {
49
+ /** Generated text */
50
+ text: string;
51
+ /** Number of tokens generated */
52
+ tokensGenerated: number;
53
+ /** Time taken in milliseconds */
54
+ timeMs: number;
55
+ /** Tokens per second */
56
+ tokensPerSecond: number;
57
+ /** Model used */
58
+ model: string;
59
+ /** Whether model was loaded from cache */
60
+ cached: boolean;
61
+ }
62
+ export declare const AVAILABLE_MODELS: {
63
+ readonly 'trm-tinystories': {
64
+ readonly id: "Xenova/TinyStories-33M";
65
+ readonly name: "TinyStories 33M (TRM)";
66
+ readonly size: "~65MB";
67
+ readonly description: "Ultra-tiny model for stories and basic generation";
68
+ readonly contextLength: 512;
69
+ };
70
+ readonly 'trm-gpt2-tiny': {
71
+ readonly id: "Xenova/gpt2";
72
+ readonly name: "GPT-2 124M (TRM)";
73
+ readonly size: "~250MB";
74
+ readonly description: "Classic GPT-2 tiny for general text";
75
+ readonly contextLength: 1024;
76
+ };
77
+ readonly 'trm-distilgpt2': {
78
+ readonly id: "Xenova/distilgpt2";
79
+ readonly name: "DistilGPT-2 (TRM)";
80
+ readonly size: "~82MB";
81
+ readonly description: "Distilled GPT-2, fastest general model";
82
+ readonly contextLength: 1024;
83
+ };
84
+ readonly 'smollm-135m': {
85
+ readonly id: "HuggingFaceTB/SmolLM-135M-Instruct";
86
+ readonly name: "SmolLM 135M";
87
+ readonly size: "~135MB";
88
+ readonly description: "Smallest instruct model, very fast";
89
+ readonly contextLength: 2048;
90
+ };
91
+ readonly 'smollm-360m': {
92
+ readonly id: "HuggingFaceTB/SmolLM-360M-Instruct";
93
+ readonly name: "SmolLM 360M";
94
+ readonly size: "~360MB";
95
+ readonly description: "Small model, fast, better quality";
96
+ readonly contextLength: 2048;
97
+ };
98
+ readonly 'smollm2-135m': {
99
+ readonly id: "HuggingFaceTB/SmolLM2-135M-Instruct";
100
+ readonly name: "SmolLM2 135M";
101
+ readonly size: "~135MB";
102
+ readonly description: "Latest SmolLM v2, improved capabilities";
103
+ readonly contextLength: 2048;
104
+ };
105
+ readonly 'smollm2-360m': {
106
+ readonly id: "HuggingFaceTB/SmolLM2-360M-Instruct";
107
+ readonly name: "SmolLM2 360M";
108
+ readonly size: "~360MB";
109
+ readonly description: "Latest SmolLM v2, better quality";
110
+ readonly contextLength: 2048;
111
+ };
112
+ readonly 'qwen2.5-0.5b': {
113
+ readonly id: "Qwen/Qwen2.5-0.5B-Instruct";
114
+ readonly name: "Qwen2.5 0.5B";
115
+ readonly size: "~300MB quantized";
116
+ readonly description: "Good balance of speed and quality, multilingual";
117
+ readonly contextLength: 4096;
118
+ };
119
+ readonly tinyllama: {
120
+ readonly id: "TinyLlama/TinyLlama-1.1B-Chat-v1.0";
121
+ readonly name: "TinyLlama 1.1B";
122
+ readonly size: "~600MB quantized";
123
+ readonly description: "Best small model quality, slower";
124
+ readonly contextLength: 2048;
125
+ };
126
+ readonly 'codegemma-2b': {
127
+ readonly id: "google/codegemma-2b";
128
+ readonly name: "CodeGemma 2B";
129
+ readonly size: "~1GB quantized";
130
+ readonly description: "Code generation specialist";
131
+ readonly contextLength: 8192;
132
+ };
133
+ readonly 'deepseek-coder-1.3b': {
134
+ readonly id: "deepseek-ai/deepseek-coder-1.3b-instruct";
135
+ readonly name: "DeepSeek Coder 1.3B";
136
+ readonly size: "~700MB quantized";
137
+ readonly description: "Excellent for code tasks";
138
+ readonly contextLength: 4096;
139
+ };
140
+ readonly 'phi-2': {
141
+ readonly id: "microsoft/phi-2";
142
+ readonly name: "Phi-2 2.7B";
143
+ readonly size: "~1.5GB quantized";
144
+ readonly description: "High quality small model";
145
+ readonly contextLength: 2048;
146
+ };
147
+ readonly 'phi-3-mini': {
148
+ readonly id: "microsoft/Phi-3-mini-4k-instruct";
149
+ readonly name: "Phi-3 Mini";
150
+ readonly size: "~2GB quantized";
151
+ readonly description: "Best quality tiny model";
152
+ readonly contextLength: 4096;
153
+ };
154
+ };
155
+ export type ModelKey = keyof typeof AVAILABLE_MODELS;
156
+ /**
157
+ * Check if transformers.js is available
158
+ */
159
+ export declare function isTransformersAvailable(): Promise<boolean>;
160
+ /**
161
+ * Initialize the ONNX LLM with specified model
162
+ */
163
+ export declare function initOnnxLLM(config?: OnnxLLMConfig): Promise<boolean>;
164
+ /**
165
+ * Generate text using ONNX LLM
166
+ */
167
+ export declare function generate(prompt: string, config?: GenerationConfig): Promise<GenerationResult>;
168
+ /**
169
+ * Generate with streaming (token by token)
170
+ */
171
+ export declare function generateStream(prompt: string, config?: GenerationConfig): Promise<AsyncGenerator<string, GenerationResult, undefined>>;
172
+ /**
173
+ * Chat completion with conversation history
174
+ */
175
+ export declare function chat(messages: Array<{
176
+ role: 'system' | 'user' | 'assistant';
177
+ content: string;
178
+ }>, config?: GenerationConfig): Promise<GenerationResult>;
179
+ /**
180
+ * Get model information
181
+ */
182
+ export declare function getModelInfo(): {
183
+ model: string | null;
184
+ ready: boolean;
185
+ availableModels: typeof AVAILABLE_MODELS;
186
+ };
187
+ /**
188
+ * Unload the current model to free memory
189
+ */
190
+ export declare function unload(): Promise<void>;
191
+ export declare class OnnxLLM {
192
+ private config;
193
+ private initialized;
194
+ constructor(config?: OnnxLLMConfig);
195
+ init(): Promise<boolean>;
196
+ generate(prompt: string, config?: GenerationConfig): Promise<GenerationResult>;
197
+ chat(messages: Array<{
198
+ role: 'system' | 'user' | 'assistant';
199
+ content: string;
200
+ }>, config?: GenerationConfig): Promise<GenerationResult>;
201
+ unload(): Promise<void>;
202
+ get ready(): boolean;
203
+ get model(): string | null;
204
+ }
205
+ export default OnnxLLM;
206
+ //# sourceMappingURL=onnx-llm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"onnx-llm.d.ts","sourceRoot":"","sources":["../../src/core/onnx-llm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAaH,MAAM,WAAW,aAAa;IAC5B,wDAAwD;IACxD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iCAAiC;IACjC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,0CAA0C;IAC1C,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,gDAAgD;IAChD,MAAM,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC;IAC1B,6BAA6B;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,gBAAgB;IAC/B,oDAAoD;IACpD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,8CAA8C;IAC9C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,4CAA4C;I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