ruvector 0.1.99 → 0.2.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/LICENSE +21 -0
- package/bin/cli.js +820 -47
- package/bin/mcp-server.js +71 -18
- package/package.json +28 -4
- package/HOOKS.md +0 -221
- package/PACKAGE_SUMMARY.md +0 -409
- package/examples/api-usage.js +0 -211
- package/examples/cli-demo.sh +0 -85
package/PACKAGE_SUMMARY.md
DELETED
|
@@ -1,409 +0,0 @@
|
|
|
1
|
-
# ruvector Package Summary
|
|
2
|
-
|
|
3
|
-
## Overview
|
|
4
|
-
|
|
5
|
-
The main `ruvector` package provides a unified interface for high-performance vector database operations in Node.js, with automatic platform detection and smart fallback between native (Rust) and WASM implementations.
|
|
6
|
-
|
|
7
|
-
## Package Structure
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
/workspaces/ruvector/npm/packages/ruvector/
|
|
11
|
-
├── src/ # TypeScript source
|
|
12
|
-
│ ├── index.ts # Smart loader with platform detection
|
|
13
|
-
│ └── types.ts # TypeScript type definitions
|
|
14
|
-
├── dist/ # Compiled JavaScript and types
|
|
15
|
-
│ ├── index.js # Main entry point
|
|
16
|
-
│ ├── index.d.ts # Type definitions
|
|
17
|
-
│ ├── types.js # Compiled types
|
|
18
|
-
│ └── types.d.ts # Type definitions
|
|
19
|
-
├── bin/
|
|
20
|
-
│ └── cli.js # CLI tool
|
|
21
|
-
├── test/
|
|
22
|
-
│ ├── mock-implementation.js # Mock VectorDB for testing
|
|
23
|
-
│ ├── standalone-test.js # Package structure tests
|
|
24
|
-
│ └── integration.js # Integration tests
|
|
25
|
-
├── examples/
|
|
26
|
-
│ ├── api-usage.js # API usage examples
|
|
27
|
-
│ └── cli-demo.sh # CLI demonstration
|
|
28
|
-
├── package.json # NPM package configuration
|
|
29
|
-
├── tsconfig.json # TypeScript configuration
|
|
30
|
-
└── README.md # Package documentation
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
## Key Features
|
|
34
|
-
|
|
35
|
-
### 1. Smart Platform Detection
|
|
36
|
-
|
|
37
|
-
The package automatically detects and loads the best available implementation:
|
|
38
|
-
|
|
39
|
-
```typescript
|
|
40
|
-
// Tries to load in this order:
|
|
41
|
-
// 1. @ruvector/core (native Rust, fastest)
|
|
42
|
-
// 2. @ruvector/wasm (WebAssembly, universal fallback)
|
|
43
|
-
|
|
44
|
-
import { VectorDB, getImplementationType, isNative, isWasm } from 'ruvector';
|
|
45
|
-
|
|
46
|
-
console.log(getImplementationType()); // 'native' or 'wasm'
|
|
47
|
-
console.log(isNative()); // true if using native
|
|
48
|
-
console.log(isWasm()); // true if using WASM
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
### 2. Complete TypeScript Support
|
|
52
|
-
|
|
53
|
-
Full type definitions for all APIs:
|
|
54
|
-
|
|
55
|
-
```typescript
|
|
56
|
-
interface VectorEntry {
|
|
57
|
-
id: string;
|
|
58
|
-
vector: number[];
|
|
59
|
-
metadata?: Record<string, any>;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
interface SearchQuery {
|
|
63
|
-
vector: number[];
|
|
64
|
-
k?: number;
|
|
65
|
-
filter?: Record<string, any>;
|
|
66
|
-
threshold?: number;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
interface SearchResult {
|
|
70
|
-
id: string;
|
|
71
|
-
score: number;
|
|
72
|
-
vector: number[];
|
|
73
|
-
metadata?: Record<string, any>;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
interface DbOptions {
|
|
77
|
-
dimension: number;
|
|
78
|
-
metric?: 'cosine' | 'euclidean' | 'dot';
|
|
79
|
-
path?: string;
|
|
80
|
-
autoPersist?: boolean;
|
|
81
|
-
hnsw?: {
|
|
82
|
-
m?: number;
|
|
83
|
-
efConstruction?: number;
|
|
84
|
-
efSearch?: number;
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
### 3. VectorDB API
|
|
90
|
-
|
|
91
|
-
Comprehensive vector database operations:
|
|
92
|
-
|
|
93
|
-
```typescript
|
|
94
|
-
const db = new VectorDB({
|
|
95
|
-
dimension: 384,
|
|
96
|
-
metric: 'cosine'
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
// Insert operations
|
|
100
|
-
db.insert({ id: 'doc1', vector: [...], metadata: {...} });
|
|
101
|
-
db.insertBatch([...entries]);
|
|
102
|
-
|
|
103
|
-
// Search operations
|
|
104
|
-
const results = db.search({
|
|
105
|
-
vector: [...],
|
|
106
|
-
k: 10,
|
|
107
|
-
threshold: 0.7
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
// CRUD operations
|
|
111
|
-
const entry = db.get('doc1');
|
|
112
|
-
db.updateMetadata('doc1', { updated: true });
|
|
113
|
-
db.delete('doc1');
|
|
114
|
-
|
|
115
|
-
// Database management
|
|
116
|
-
const stats = db.stats();
|
|
117
|
-
db.save('./mydb.vec');
|
|
118
|
-
db.load('./mydb.vec');
|
|
119
|
-
db.buildIndex();
|
|
120
|
-
db.optimize();
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
### 4. CLI Tools
|
|
124
|
-
|
|
125
|
-
Command-line interface for database operations:
|
|
126
|
-
|
|
127
|
-
```bash
|
|
128
|
-
# Create database
|
|
129
|
-
ruvector create mydb.vec --dimension 384 --metric cosine
|
|
130
|
-
|
|
131
|
-
# Insert vectors
|
|
132
|
-
ruvector insert mydb.vec vectors.json --batch-size 1000
|
|
133
|
-
|
|
134
|
-
# Search
|
|
135
|
-
ruvector search mydb.vec --vector "[0.1,0.2,...]" --top-k 10
|
|
136
|
-
|
|
137
|
-
# Statistics
|
|
138
|
-
ruvector stats mydb.vec
|
|
139
|
-
|
|
140
|
-
# Benchmark
|
|
141
|
-
ruvector benchmark --num-vectors 10000 --num-queries 1000
|
|
142
|
-
|
|
143
|
-
# Info
|
|
144
|
-
ruvector info
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
## API Reference
|
|
148
|
-
|
|
149
|
-
### Constructor
|
|
150
|
-
|
|
151
|
-
```typescript
|
|
152
|
-
new VectorDB(options: DbOptions): VectorDB
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
### Methods
|
|
156
|
-
|
|
157
|
-
- `insert(entry: VectorEntry): void` - Insert single vector
|
|
158
|
-
- `insertBatch(entries: VectorEntry[]): void` - Batch insert
|
|
159
|
-
- `search(query: SearchQuery): SearchResult[]` - Search similar vectors
|
|
160
|
-
- `get(id: string): VectorEntry | null` - Get by ID
|
|
161
|
-
- `delete(id: string): boolean` - Delete vector
|
|
162
|
-
- `updateMetadata(id: string, metadata: Record<string, any>): void` - Update metadata
|
|
163
|
-
- `stats(): DbStats` - Get database statistics
|
|
164
|
-
- `save(path?: string): void` - Save to disk
|
|
165
|
-
- `load(path: string): void` - Load from disk
|
|
166
|
-
- `clear(): void` - Clear all vectors
|
|
167
|
-
- `buildIndex(): void` - Build HNSW index
|
|
168
|
-
- `optimize(): void` - Optimize database
|
|
169
|
-
|
|
170
|
-
### Utility Functions
|
|
171
|
-
|
|
172
|
-
- `getImplementationType(): 'native' | 'wasm'` - Get current implementation
|
|
173
|
-
- `isNative(): boolean` - Check if using native
|
|
174
|
-
- `isWasm(): boolean` - Check if using WASM
|
|
175
|
-
- `getVersion(): { version: string, implementation: string }` - Get version info
|
|
176
|
-
|
|
177
|
-
## Dependencies
|
|
178
|
-
|
|
179
|
-
### Production Dependencies
|
|
180
|
-
|
|
181
|
-
- `commander` (^11.1.0) - CLI framework
|
|
182
|
-
- `chalk` (^4.1.2) - Terminal styling
|
|
183
|
-
- `ora` (^5.4.1) - Spinners and progress
|
|
184
|
-
|
|
185
|
-
### Optional Dependencies
|
|
186
|
-
|
|
187
|
-
- `@ruvector/core` (^0.1.1) - Native Rust bindings (when available)
|
|
188
|
-
- `@ruvector/wasm` (^0.1.1) - WebAssembly module (fallback)
|
|
189
|
-
|
|
190
|
-
### Dev Dependencies
|
|
191
|
-
|
|
192
|
-
- `typescript` (^5.3.3) - TypeScript compiler
|
|
193
|
-
- `@types/node` (^20.10.5) - Node.js type definitions
|
|
194
|
-
|
|
195
|
-
## Package.json Configuration
|
|
196
|
-
|
|
197
|
-
```json
|
|
198
|
-
{
|
|
199
|
-
"name": "ruvector",
|
|
200
|
-
"version": "0.1.1",
|
|
201
|
-
"main": "dist/index.js",
|
|
202
|
-
"types": "dist/index.d.ts",
|
|
203
|
-
"bin": {
|
|
204
|
-
"ruvector": "./bin/cli.js"
|
|
205
|
-
},
|
|
206
|
-
"scripts": {
|
|
207
|
-
"build": "tsc",
|
|
208
|
-
"test": "node test/standalone-test.js"
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
## Build Process
|
|
214
|
-
|
|
215
|
-
```bash
|
|
216
|
-
# Install dependencies
|
|
217
|
-
npm install
|
|
218
|
-
|
|
219
|
-
# Build TypeScript
|
|
220
|
-
npm run build
|
|
221
|
-
|
|
222
|
-
# Run tests
|
|
223
|
-
npm test
|
|
224
|
-
|
|
225
|
-
# Package for NPM
|
|
226
|
-
npm pack
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
## Testing
|
|
230
|
-
|
|
231
|
-
The package includes comprehensive tests:
|
|
232
|
-
|
|
233
|
-
### 1. Standalone Test (`test/standalone-test.js`)
|
|
234
|
-
|
|
235
|
-
Tests package structure and API using mock implementation:
|
|
236
|
-
- Package structure validation
|
|
237
|
-
- TypeScript type definitions
|
|
238
|
-
- VectorDB API functionality
|
|
239
|
-
- CLI structure
|
|
240
|
-
- Smart loader logic
|
|
241
|
-
|
|
242
|
-
### 2. Integration Test (`test/integration.js`)
|
|
243
|
-
|
|
244
|
-
Tests integration with real implementations when available.
|
|
245
|
-
|
|
246
|
-
### 3. Mock Implementation (`test/mock-implementation.js`)
|
|
247
|
-
|
|
248
|
-
JavaScript-based VectorDB implementation for testing and demonstration purposes.
|
|
249
|
-
|
|
250
|
-
## Examples
|
|
251
|
-
|
|
252
|
-
### API Usage (`examples/api-usage.js`)
|
|
253
|
-
|
|
254
|
-
Demonstrates:
|
|
255
|
-
- Basic CRUD operations
|
|
256
|
-
- Batch operations
|
|
257
|
-
- Semantic search
|
|
258
|
-
- Different distance metrics
|
|
259
|
-
- Performance benchmarking
|
|
260
|
-
- Persistence
|
|
261
|
-
|
|
262
|
-
### CLI Demo (`examples/cli-demo.sh`)
|
|
263
|
-
|
|
264
|
-
Bash script demonstrating CLI tools.
|
|
265
|
-
|
|
266
|
-
## Usage Examples
|
|
267
|
-
|
|
268
|
-
### Simple Vector Search
|
|
269
|
-
|
|
270
|
-
```javascript
|
|
271
|
-
const { VectorDB } = require('ruvector');
|
|
272
|
-
|
|
273
|
-
const db = new VectorDB({ dimension: 3 });
|
|
274
|
-
|
|
275
|
-
db.insertBatch([
|
|
276
|
-
{ id: 'cat', vector: [0.9, 0.1, 0.1], metadata: { animal: 'cat' } },
|
|
277
|
-
{ id: 'dog', vector: [0.1, 0.9, 0.1], metadata: { animal: 'dog' } },
|
|
278
|
-
{ id: 'tiger', vector: [0.8, 0.2, 0.15], metadata: { animal: 'tiger' } }
|
|
279
|
-
]);
|
|
280
|
-
|
|
281
|
-
const results = db.search({
|
|
282
|
-
vector: [0.9, 0.1, 0.1],
|
|
283
|
-
k: 2
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
console.log(results);
|
|
287
|
-
// [
|
|
288
|
-
// { id: 'cat', score: 1.0, ... },
|
|
289
|
-
// { id: 'tiger', score: 0.97, ... }
|
|
290
|
-
// ]
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
### Semantic Document Search
|
|
294
|
-
|
|
295
|
-
```javascript
|
|
296
|
-
const db = new VectorDB({ dimension: 768, metric: 'cosine' });
|
|
297
|
-
|
|
298
|
-
// Insert documents with embeddings (from your embedding model)
|
|
299
|
-
db.insertBatch([
|
|
300
|
-
{ id: 'doc1', vector: embedding1, metadata: { title: 'AI Guide' } },
|
|
301
|
-
{ id: 'doc2', vector: embedding2, metadata: { title: 'Web Dev' } }
|
|
302
|
-
]);
|
|
303
|
-
|
|
304
|
-
// Search with query embedding
|
|
305
|
-
const results = db.search({
|
|
306
|
-
vector: queryEmbedding,
|
|
307
|
-
k: 10,
|
|
308
|
-
threshold: 0.7
|
|
309
|
-
});
|
|
310
|
-
```
|
|
311
|
-
|
|
312
|
-
### Persistence
|
|
313
|
-
|
|
314
|
-
```javascript
|
|
315
|
-
const db = new VectorDB({
|
|
316
|
-
dimension: 384,
|
|
317
|
-
path: './vectors.db',
|
|
318
|
-
autoPersist: true
|
|
319
|
-
});
|
|
320
|
-
|
|
321
|
-
// Changes automatically saved
|
|
322
|
-
db.insert({ id: 'doc1', vector: [...] });
|
|
323
|
-
|
|
324
|
-
// Or manual save
|
|
325
|
-
db.save('./backup.db');
|
|
326
|
-
|
|
327
|
-
// Load from disk
|
|
328
|
-
db.load('./vectors.db');
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
## Performance Characteristics
|
|
332
|
-
|
|
333
|
-
### Mock Implementation (JavaScript)
|
|
334
|
-
- Insert: ~1M vectors/sec (batch)
|
|
335
|
-
- Search: ~400 queries/sec (1000 vectors, k=10)
|
|
336
|
-
|
|
337
|
-
### Native Implementation (Rust)
|
|
338
|
-
- Insert: ~10M+ vectors/sec (batch)
|
|
339
|
-
- Search: ~100K+ queries/sec with HNSW index
|
|
340
|
-
- 150x faster than pgvector
|
|
341
|
-
|
|
342
|
-
### WASM Implementation
|
|
343
|
-
- Insert: ~1M+ vectors/sec (batch)
|
|
344
|
-
- Search: ~10K+ queries/sec with HNSW index
|
|
345
|
-
- ~10x faster than pure JavaScript
|
|
346
|
-
|
|
347
|
-
## Integration with Other Packages
|
|
348
|
-
|
|
349
|
-
This package serves as the main interface and coordinates between:
|
|
350
|
-
|
|
351
|
-
1. **@ruvector/core** - Native Rust bindings (napi-rs)
|
|
352
|
-
- Platform-specific native modules
|
|
353
|
-
- Maximum performance
|
|
354
|
-
- Optional dependency
|
|
355
|
-
|
|
356
|
-
2. **@ruvector/wasm** - WebAssembly module
|
|
357
|
-
- Universal compatibility
|
|
358
|
-
- Near-native performance
|
|
359
|
-
- Fallback implementation
|
|
360
|
-
|
|
361
|
-
## Error Handling
|
|
362
|
-
|
|
363
|
-
The package provides clear error messages when implementations are unavailable:
|
|
364
|
-
|
|
365
|
-
```
|
|
366
|
-
Failed to load ruvector: Neither native nor WASM implementation available.
|
|
367
|
-
Native error: Cannot find module '@ruvector/core'
|
|
368
|
-
WASM error: Cannot find module '@ruvector/wasm'
|
|
369
|
-
```
|
|
370
|
-
|
|
371
|
-
## Environment Variables
|
|
372
|
-
|
|
373
|
-
- `RUVECTOR_DEBUG=1` - Enable debug logging for implementation loading
|
|
374
|
-
|
|
375
|
-
## Next Steps
|
|
376
|
-
|
|
377
|
-
To complete the package ecosystem:
|
|
378
|
-
|
|
379
|
-
1. **Create @ruvector/core**
|
|
380
|
-
- napi-rs bindings to Rust code
|
|
381
|
-
- Platform-specific builds (Linux, macOS, Windows)
|
|
382
|
-
- Native module packaging
|
|
383
|
-
|
|
384
|
-
2. **Create @ruvector/wasm**
|
|
385
|
-
- wasm-pack build from Rust code
|
|
386
|
-
- WebAssembly module
|
|
387
|
-
- Universal compatibility layer
|
|
388
|
-
|
|
389
|
-
3. **Update Dependencies**
|
|
390
|
-
- Add @ruvector/core as optionalDependency
|
|
391
|
-
- Add @ruvector/wasm as dependency
|
|
392
|
-
- Configure proper fallback chain
|
|
393
|
-
|
|
394
|
-
4. **Publishing**
|
|
395
|
-
- Publish all three packages to npm
|
|
396
|
-
- Set up CI/CD for builds
|
|
397
|
-
- Create platform-specific releases
|
|
398
|
-
|
|
399
|
-
## Version
|
|
400
|
-
|
|
401
|
-
Current version: **0.1.1**
|
|
402
|
-
|
|
403
|
-
## License
|
|
404
|
-
|
|
405
|
-
MIT
|
|
406
|
-
|
|
407
|
-
## Repository
|
|
408
|
-
|
|
409
|
-
https://github.com/ruvnet/ruvector
|
package/examples/api-usage.js
DELETED
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* ruvector API Usage Examples
|
|
5
|
-
*
|
|
6
|
-
* This demonstrates how to use ruvector in your Node.js applications
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
// For this demo, we use the mock implementation
|
|
10
|
-
// In production, you would use: const { VectorDB } = require('ruvector');
|
|
11
|
-
const { VectorDB } = require('../test/mock-implementation.js');
|
|
12
|
-
|
|
13
|
-
console.log('ruvector API Examples\n');
|
|
14
|
-
console.log('='.repeat(60));
|
|
15
|
-
|
|
16
|
-
// Show info
|
|
17
|
-
console.log('\nUsing: Mock implementation (for demo purposes)');
|
|
18
|
-
console.log('In production: npm install ruvector\n');
|
|
19
|
-
|
|
20
|
-
// Example 1: Basic usage
|
|
21
|
-
console.log('Example 1: Basic Vector Operations');
|
|
22
|
-
console.log('-'.repeat(60));
|
|
23
|
-
|
|
24
|
-
const db = new VectorDB({
|
|
25
|
-
dimension: 3,
|
|
26
|
-
metric: 'cosine'
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
// Insert some vectors
|
|
30
|
-
db.insert({
|
|
31
|
-
id: 'doc1',
|
|
32
|
-
vector: [1, 0, 0],
|
|
33
|
-
metadata: { title: 'First Document', category: 'A' }
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
db.insertBatch([
|
|
37
|
-
{ id: 'doc2', vector: [0, 1, 0], metadata: { title: 'Second Document', category: 'B' } },
|
|
38
|
-
{ id: 'doc3', vector: [0, 0, 1], metadata: { title: 'Third Document', category: 'C' } },
|
|
39
|
-
{ id: 'doc4', vector: [0.7, 0.7, 0], metadata: { title: 'Fourth Document', category: 'A' } }
|
|
40
|
-
]);
|
|
41
|
-
|
|
42
|
-
console.log('✓ Inserted 4 vectors');
|
|
43
|
-
|
|
44
|
-
// Get stats
|
|
45
|
-
const stats = db.stats();
|
|
46
|
-
console.log(`✓ Database has ${stats.count} vectors, dimension ${stats.dimension}`);
|
|
47
|
-
|
|
48
|
-
// Search
|
|
49
|
-
const results = db.search({
|
|
50
|
-
vector: [1, 0, 0],
|
|
51
|
-
k: 3
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
console.log(`✓ Search returned ${results.length} results:`);
|
|
55
|
-
results.forEach((result, i) => {
|
|
56
|
-
console.log(` ${i + 1}. ${result.id} (score: ${result.score.toFixed(4)}) - ${result.metadata.title}`);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
// Get by ID
|
|
60
|
-
const doc = db.get('doc2');
|
|
61
|
-
console.log(`✓ Retrieved document: ${doc.metadata.title}`);
|
|
62
|
-
|
|
63
|
-
// Update metadata
|
|
64
|
-
db.updateMetadata('doc1', { updated: true, timestamp: Date.now() });
|
|
65
|
-
console.log('✓ Updated metadata');
|
|
66
|
-
|
|
67
|
-
// Delete
|
|
68
|
-
db.delete('doc3');
|
|
69
|
-
console.log('✓ Deleted doc3');
|
|
70
|
-
console.log(`✓ Database now has ${db.stats().count} vectors\n`);
|
|
71
|
-
|
|
72
|
-
// Example 2: Semantic Search Simulation
|
|
73
|
-
console.log('Example 2: Semantic Search Simulation');
|
|
74
|
-
console.log('-'.repeat(60));
|
|
75
|
-
|
|
76
|
-
const semanticDb = new VectorDB({
|
|
77
|
-
dimension: 5,
|
|
78
|
-
metric: 'cosine'
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// Simulate document embeddings
|
|
82
|
-
const documents = [
|
|
83
|
-
{ id: 'machine-learning', vector: [0.9, 0.8, 0.1, 0.2, 0.1], metadata: { title: 'Introduction to Machine Learning', topic: 'AI' } },
|
|
84
|
-
{ id: 'deep-learning', vector: [0.85, 0.9, 0.15, 0.25, 0.1], metadata: { title: 'Deep Learning Fundamentals', topic: 'AI' } },
|
|
85
|
-
{ id: 'web-dev', vector: [0.1, 0.2, 0.9, 0.8, 0.1], metadata: { title: 'Web Development Guide', topic: 'Web' } },
|
|
86
|
-
{ id: 'react', vector: [0.15, 0.2, 0.85, 0.9, 0.1], metadata: { title: 'React Tutorial', topic: 'Web' } },
|
|
87
|
-
{ id: 'database', vector: [0.2, 0.3, 0.3, 0.4, 0.9], metadata: { title: 'Database Design', topic: 'Data' } }
|
|
88
|
-
];
|
|
89
|
-
|
|
90
|
-
semanticDb.insertBatch(documents);
|
|
91
|
-
console.log(`✓ Indexed ${documents.length} documents`);
|
|
92
|
-
|
|
93
|
-
// Search for AI-related content
|
|
94
|
-
const aiQuery = [0.9, 0.85, 0.1, 0.2, 0.1];
|
|
95
|
-
const aiResults = semanticDb.search({ vector: aiQuery, k: 2 });
|
|
96
|
-
|
|
97
|
-
console.log('\nQuery: AI-related content');
|
|
98
|
-
console.log('Results:');
|
|
99
|
-
aiResults.forEach((result, i) => {
|
|
100
|
-
console.log(` ${i + 1}. ${result.metadata.title} (score: ${result.score.toFixed(4)})`);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
// Search for Web-related content
|
|
104
|
-
const webQuery = [0.1, 0.2, 0.9, 0.85, 0.1];
|
|
105
|
-
const webResults = semanticDb.search({ vector: webQuery, k: 2 });
|
|
106
|
-
|
|
107
|
-
console.log('\nQuery: Web-related content');
|
|
108
|
-
console.log('Results:');
|
|
109
|
-
webResults.forEach((result, i) => {
|
|
110
|
-
console.log(` ${i + 1}. ${result.metadata.title} (score: ${result.score.toFixed(4)})`);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
// Example 3: Different Distance Metrics
|
|
114
|
-
console.log('\n\nExample 3: Distance Metrics Comparison');
|
|
115
|
-
console.log('-'.repeat(60));
|
|
116
|
-
|
|
117
|
-
const metrics = ['cosine', 'euclidean', 'dot'];
|
|
118
|
-
const testVectors = [
|
|
119
|
-
{ id: 'v1', vector: [1, 0, 0] },
|
|
120
|
-
{ id: 'v2', vector: [0.7, 0.7, 0] },
|
|
121
|
-
{ id: 'v3', vector: [0, 1, 0] }
|
|
122
|
-
];
|
|
123
|
-
|
|
124
|
-
metrics.forEach(metric => {
|
|
125
|
-
const metricDb = new VectorDB({ dimension: 3, metric });
|
|
126
|
-
metricDb.insertBatch(testVectors);
|
|
127
|
-
|
|
128
|
-
const results = metricDb.search({ vector: [1, 0, 0], k: 3 });
|
|
129
|
-
|
|
130
|
-
console.log(`\n${metric.toUpperCase()} metric:`);
|
|
131
|
-
results.forEach((result, i) => {
|
|
132
|
-
console.log(` ${i + 1}. ${result.id}: ${result.score.toFixed(4)}`);
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
// Example 4: Batch Operations Performance
|
|
137
|
-
console.log('\n\nExample 4: Batch Operations Performance');
|
|
138
|
-
console.log('-'.repeat(60));
|
|
139
|
-
|
|
140
|
-
const perfDb = new VectorDB({ dimension: 128, metric: 'cosine' });
|
|
141
|
-
|
|
142
|
-
// Generate random vectors
|
|
143
|
-
const numVectors = 1000;
|
|
144
|
-
const vectors = [];
|
|
145
|
-
for (let i = 0; i < numVectors; i++) {
|
|
146
|
-
vectors.push({
|
|
147
|
-
id: `vec_${i}`,
|
|
148
|
-
vector: Array.from({ length: 128 }, () => Math.random()),
|
|
149
|
-
metadata: { index: i, batch: Math.floor(i / 100) }
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
console.log(`Inserting ${numVectors} vectors...`);
|
|
154
|
-
const insertStart = Date.now();
|
|
155
|
-
perfDb.insertBatch(vectors);
|
|
156
|
-
const insertTime = Date.now() - insertStart;
|
|
157
|
-
|
|
158
|
-
console.log(`✓ Inserted ${numVectors} vectors in ${insertTime}ms`);
|
|
159
|
-
console.log(`✓ Rate: ${Math.round(numVectors / (insertTime / 1000))} vectors/sec`);
|
|
160
|
-
|
|
161
|
-
// Search performance
|
|
162
|
-
const numQueries = 100;
|
|
163
|
-
console.log(`\nRunning ${numQueries} searches...`);
|
|
164
|
-
const searchStart = Date.now();
|
|
165
|
-
|
|
166
|
-
for (let i = 0; i < numQueries; i++) {
|
|
167
|
-
const query = {
|
|
168
|
-
vector: Array.from({ length: 128 }, () => Math.random()),
|
|
169
|
-
k: 10
|
|
170
|
-
};
|
|
171
|
-
perfDb.search(query);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const searchTime = Date.now() - searchStart;
|
|
175
|
-
console.log(`✓ Completed ${numQueries} searches in ${searchTime}ms`);
|
|
176
|
-
console.log(`✓ Rate: ${Math.round(numQueries / (searchTime / 1000))} queries/sec`);
|
|
177
|
-
console.log(`✓ Avg latency: ${(searchTime / numQueries).toFixed(2)}ms`);
|
|
178
|
-
|
|
179
|
-
// Example 5: Persistence (conceptual, would need real implementation)
|
|
180
|
-
console.log('\n\nExample 5: Persistence');
|
|
181
|
-
console.log('-'.repeat(60));
|
|
182
|
-
|
|
183
|
-
const persistDb = new VectorDB({
|
|
184
|
-
dimension: 3,
|
|
185
|
-
metric: 'cosine',
|
|
186
|
-
path: './my-vectors.db',
|
|
187
|
-
autoPersist: true
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
persistDb.insertBatch([
|
|
191
|
-
{ id: 'p1', vector: [1, 0, 0], metadata: { name: 'First' } },
|
|
192
|
-
{ id: 'p2', vector: [0, 1, 0], metadata: { name: 'Second' } }
|
|
193
|
-
]);
|
|
194
|
-
|
|
195
|
-
console.log('✓ Created database with auto-persist enabled');
|
|
196
|
-
console.log('✓ Insert operations will automatically save to disk');
|
|
197
|
-
console.log('✓ Use db.save(path) for manual saves');
|
|
198
|
-
console.log('✓ Use db.load(path) to restore from disk');
|
|
199
|
-
|
|
200
|
-
// Summary
|
|
201
|
-
console.log('\n' + '='.repeat(60));
|
|
202
|
-
console.log('\n✅ All examples completed successfully!');
|
|
203
|
-
console.log('\nKey Features Demonstrated:');
|
|
204
|
-
console.log(' • Basic CRUD operations (insert, search, get, update, delete)');
|
|
205
|
-
console.log(' • Batch operations for better performance');
|
|
206
|
-
console.log(' • Multiple distance metrics (cosine, euclidean, dot)');
|
|
207
|
-
console.log(' • Semantic search simulation');
|
|
208
|
-
console.log(' • Performance benchmarking');
|
|
209
|
-
console.log(' • Metadata filtering and updates');
|
|
210
|
-
console.log(' • Persistence (save/load)');
|
|
211
|
-
console.log('\nFor more examples, see: /workspaces/ruvector/npm/packages/ruvector/examples/');
|
package/examples/cli-demo.sh
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
#!/bin/bash
|
|
2
|
-
|
|
3
|
-
# ruvector CLI Demo
|
|
4
|
-
# This demonstrates the CLI functionality with a simple example
|
|
5
|
-
|
|
6
|
-
echo "🚀 ruvector CLI Demo"
|
|
7
|
-
echo "===================="
|
|
8
|
-
echo ""
|
|
9
|
-
|
|
10
|
-
# 1. Show version info
|
|
11
|
-
echo "1. Checking ruvector info..."
|
|
12
|
-
ruvector info
|
|
13
|
-
echo ""
|
|
14
|
-
|
|
15
|
-
# 2. Create a database
|
|
16
|
-
echo "2. Creating a new database..."
|
|
17
|
-
ruvector create demo.vec --dimension 3 --metric cosine
|
|
18
|
-
echo ""
|
|
19
|
-
|
|
20
|
-
# 3. Create sample data
|
|
21
|
-
echo "3. Creating sample vectors..."
|
|
22
|
-
cat > demo-vectors.json << 'EOF'
|
|
23
|
-
[
|
|
24
|
-
{
|
|
25
|
-
"id": "cat",
|
|
26
|
-
"vector": [0.9, 0.1, 0.1],
|
|
27
|
-
"metadata": {"animal": "cat", "category": "feline"}
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
"id": "dog",
|
|
31
|
-
"vector": [0.1, 0.9, 0.1],
|
|
32
|
-
"metadata": {"animal": "dog", "category": "canine"}
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
"id": "tiger",
|
|
36
|
-
"vector": [0.8, 0.2, 0.15],
|
|
37
|
-
"metadata": {"animal": "tiger", "category": "feline"}
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
"id": "wolf",
|
|
41
|
-
"vector": [0.2, 0.8, 0.15],
|
|
42
|
-
"metadata": {"animal": "wolf", "category": "canine"}
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
"id": "lion",
|
|
46
|
-
"vector": [0.85, 0.15, 0.1],
|
|
47
|
-
"metadata": {"animal": "lion", "category": "feline"}
|
|
48
|
-
}
|
|
49
|
-
]
|
|
50
|
-
EOF
|
|
51
|
-
echo " Created demo-vectors.json with 5 animals"
|
|
52
|
-
echo ""
|
|
53
|
-
|
|
54
|
-
# 4. Insert vectors
|
|
55
|
-
echo "4. Inserting vectors into database..."
|
|
56
|
-
ruvector insert demo.vec demo-vectors.json
|
|
57
|
-
echo ""
|
|
58
|
-
|
|
59
|
-
# 5. Show statistics
|
|
60
|
-
echo "5. Database statistics..."
|
|
61
|
-
ruvector stats demo.vec
|
|
62
|
-
echo ""
|
|
63
|
-
|
|
64
|
-
# 6. Search for cat-like animals
|
|
65
|
-
echo "6. Searching for cat-like animals (vector: [0.9, 0.1, 0.1])..."
|
|
66
|
-
ruvector search demo.vec --vector "[0.9, 0.1, 0.1]" --top-k 3
|
|
67
|
-
echo ""
|
|
68
|
-
|
|
69
|
-
# 7. Search for dog-like animals
|
|
70
|
-
echo "7. Searching for dog-like animals (vector: [0.1, 0.9, 0.1])..."
|
|
71
|
-
ruvector search demo.vec --vector "[0.1, 0.9, 0.1]" --top-k 3
|
|
72
|
-
echo ""
|
|
73
|
-
|
|
74
|
-
# 8. Run benchmark
|
|
75
|
-
echo "8. Running performance benchmark..."
|
|
76
|
-
ruvector benchmark --dimension 128 --num-vectors 1000 --num-queries 100
|
|
77
|
-
echo ""
|
|
78
|
-
|
|
79
|
-
# Cleanup
|
|
80
|
-
echo "9. Cleanup (removing demo files)..."
|
|
81
|
-
rm -f demo.vec demo-vectors.json
|
|
82
|
-
echo " ✓ Demo files removed"
|
|
83
|
-
echo ""
|
|
84
|
-
|
|
85
|
-
echo "✅ Demo complete!"
|