vecbox 0.2.1 → 0.2.2

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/CHANGELOG.md ADDED
@@ -0,0 +1,40 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.2.2] - 2026-02-14
9
+
10
+ ### Added
11
+ - Native N-API integration for Llama.cpp (10x faster performance)
12
+ - Auto-detection of best available provider
13
+ - Support for GGUF models with direct native loading
14
+ - Smart fallback system between providers
15
+ - File input support for direct text file embedding
16
+ - Batch processing capabilities
17
+
18
+ ### Changed
19
+ - Simplified installation - zero setup required
20
+ - Updated README with modern usage examples
21
+ - Improved error handling and logging
22
+ - Better TypeScript support with comprehensive types
23
+
24
+ ### Fixed
25
+ - Native module compilation issues
26
+ - Provider detection and fallback logic
27
+ - Memory management for native embeddings
28
+
29
+ ### Providers
30
+ - **OpenAI**: text-embedding-3-small, text-embedding-3-large
31
+ - **Google Gemini**: gemini-embedding-001
32
+ - **Mistral**: mistral-embed
33
+ - **Llama.cpp**: Native N-API with GGUF support
34
+
35
+ ## [0.2.1] - Previous
36
+
37
+ ### Added
38
+ - Multi-provider support
39
+ - Basic embedding functionality
40
+ - TypeScript definitions
package/README.md CHANGED
@@ -1,42 +1,49 @@
1
- # vecbox v0.1.0
1
+ # Vecbox
2
2
 
3
3
  ![vecbox](./src/images/vecbox.png)
4
- [![npm version](https://img.shields.io/npm/v/vecbox.svg)](https://www.npmjs.com/package/vecbox)
4
+ [![npm version](https://img.shields.io/npm/v/vecbox.svg)](https://www.npmjs.org/package/vecbox)
5
5
  [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
6
6
 
7
- ## Why vecbox?
7
+ **One API, multiple providers.** Switch between OpenAI, Gemini, Mistral, or run locally with Llama.cpp using native N-API performance.
8
8
 
9
- **One API, multiple providers.** Switch between OpenAI, Gemini, or run locally with Llama.cpp without changing code.
10
9
  ```typescript
11
- // Works with any provider
10
+ import { autoEmbed } from 'vecbox';
11
+
12
+ // Works with any provider - auto-detects the best available
12
13
  const result = await autoEmbed({ text: 'Hello, world!' });
13
14
  console.log(result.embedding); // [0.1, 0.2, ...]
15
+ console.log(result.provider); // 'llamacpp' | 'openai' | 'gemini' | 'mistral'
14
16
  ```
15
17
 
16
18
  ## Installation
19
+
17
20
  ```bash
18
- npm install vecbox
19
- pnpm add vecbox
21
+ npm install vecbox
22
+ # or
23
+ pnpm add vecbox
20
24
  ```
21
25
 
22
- **Zero setup required!** Everything is included - no need to download Llama.cpp or compile anything.
26
+ **Zero setup required!** Everything is included - native N-API module automatically compiled during installation.
23
27
 
24
28
  ## Quick Start
25
29
 
26
- ### Auto-detect (Recommended)
30
+ ### Auto-detection (Recommended)
31
+
27
32
  ```typescript
28
33
  import { autoEmbed } from 'vecbox';
29
34
 
30
- // Just works - automatically picks the best available provider
35
+ // Automatically picks the best available provider
31
36
  const result = await autoEmbed({ text: 'Your text' });
32
37
  console.log(result.embedding); // [0.1, 0.2, ...]
33
38
  console.log(result.provider); // 'llamacpp' | 'openai' | 'gemini' | 'mistral'
34
39
  ```
35
40
 
36
41
  ### Specific Provider
42
+
37
43
  ```typescript
38
44
  import { embed } from 'vecbox';
39
45
 
46
+ // Use specific provider
40
47
  const result = await embed(
41
48
  { provider: 'openai', apiKey: process.env.OPENAI_API_KEY },
42
49
  { text: 'Your text' }
@@ -44,17 +51,19 @@ const result = await embed(
44
51
  ```
45
52
 
46
53
  ### File Input
54
+
47
55
  ```typescript
48
56
  import { embed } from 'vecbox';
49
57
 
50
58
  // Embed text from files
51
59
  const result = await embed(
52
- { provider: 'gemini', apiKey: process.env.GEMINI_API_KEY },
60
+ { provider: 'gemini', apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY },
53
61
  { filePath: './document.txt' }
54
62
  );
55
63
  ```
56
64
 
57
65
  ### Batch Processing
66
+
58
67
  ```typescript
59
68
  import { embed } from 'vecbox';
60
69
 
@@ -72,10 +81,49 @@ const result = await embed(
72
81
  console.log(result.embeddings.length); // 3
73
82
  ```
74
83
 
75
- ## Providers
84
+ ## 🚀 Local Llama.cpp with Native N-API
76
85
 
77
- <details>
78
- <summary><b>OpenAI</b></summary>
86
+ **Automatic Native Detection:**
87
+ ```typescript
88
+ import { autoEmbed } from 'vecbox';
89
+
90
+ // Automatically uses native N-API when available
91
+ const result = await autoEmbed({ text: 'Your text' });
92
+ console.log(result.provider); // 'llamacpp' (native)
93
+ ```
94
+
95
+ **Manual Native Configuration:**
96
+ ```typescript
97
+ import { embed } from 'vecbox';
98
+
99
+ // Force native module usage
100
+ const result = await embed(
101
+ { provider: 'llamacpp', model: './models/nomic-embed-text-v1.5.Q4_K_M.gguf' },
102
+ { text: 'Your text' }
103
+ );
104
+ ```
105
+
106
+ **Setup for Local Models:**
107
+ ```bash
108
+ # Download a GGUF embedding model
109
+ wget https://huggingface.co/nomic-ai/nomic-embed-text-v1.5-GGUF/resolve/main/nomic-embed-text-v1.5.Q4_K_M.gguf
110
+
111
+ # Place it in your project directory
112
+ mkdir models && mv nomic-embed-text-v1.5.Q4_K_M.gguf models/
113
+ ```
114
+
115
+ ## 🌍 Environment Variables
116
+
117
+ ```bash
118
+ # .env file
119
+ OPENAI_API_KEY=sk-...
120
+ GOOGLE_GENERATIVE_AI_API_KEY=...
121
+ MISTRAL_API_KEY=...
122
+ ```
123
+
124
+ ## 📚 Providers
125
+
126
+ ### OpenAI
79
127
  ```typescript
80
128
  await embed(
81
129
  {
@@ -86,13 +134,9 @@ await embed(
86
134
  { text: 'Your text' }
87
135
  );
88
136
  ```
89
-
90
137
  **Setup:** Get API key at [platform.openai.com](https://platform.openai.com)
91
138
 
92
- </details>
93
-
94
- <details>
95
- <summary><b>Google Gemini</b></summary>
139
+ ### Google Gemini
96
140
  ```typescript
97
141
  await embed(
98
142
  {
@@ -103,37 +147,9 @@ await embed(
103
147
  { text: 'Your text' }
104
148
  );
105
149
  ```
106
-
107
150
  **Setup:** Get API key at [aistudio.google.com](https://aistudio.google.com)
108
151
 
109
- </details>
110
-
111
- <details>
112
- <summary><b>Llama.cpp (Local)</b></summary>
113
- ```typescript
114
- await embed(
115
- { provider: 'llamacpp', model: 'nomic-embed-text-v1.5.Q4_K_M.gguf' },
116
- { text: 'Your text' }
117
- );
118
- ```
119
-
120
- **Setup:**
121
- ```bash
122
- # 1. Install
123
- git clone https://github.com/ggerganov/llama.cpp
124
- cd llama.cpp && make llama-server
125
-
126
- # 2. Download model
127
- wget https://huggingface.co/nomic-ai/nomic-embed-text-v1.5-GGUF/resolve/main/nomic-embed-text-v1.5.Q4_K_M.gguf
128
-
129
- # 3. Run server
130
- ./llama-server -m nomic-embed-text-v1.5.Q4_K_M.gguf --embedding --port 8080
131
- ```
132
-
133
- </details>
134
-
135
- <details>
136
- <summary><b>Mistral</b></summary>
152
+ ### Mistral
137
153
  ```typescript
138
154
  await embed(
139
155
  {
@@ -144,10 +160,16 @@ await embed(
144
160
  { text: 'Your text' }
145
161
  );
146
162
  ```
147
-
148
163
  **Setup:** Get API key at [mistral.ai](https://mistral.ai)
149
164
 
150
- </details>
165
+ ### Llama.cpp (Local)
166
+ ```typescript
167
+ await embed(
168
+ { provider: 'llamacpp', model: './models/nomic-embed-text-v1.5.Q4_K_M.gguf' },
169
+ { text: 'Your text' }
170
+ );
171
+ ```
172
+ **Setup:** Download GGUF model and place in your project directory
151
173
 
152
174
  ## 🚀 Features
153
175
 
@@ -160,25 +182,48 @@ await embed(
160
182
  - **🛡️ Type Safe** - Full TypeScript support
161
183
  - **🌍 Zero Dependencies** - No external downloads or setup required
162
184
 
163
- ## 🏆 Why Vecbox?
185
+ ## 📖 API Reference
186
+
187
+ ### `autoEmbed(input)`
188
+
189
+ Auto-detects best provider in priority order:
190
+ 1. **Llama.cpp** (Local & Free)
191
+ 2. **OpenAI** (if API key available)
192
+ 3. **Gemini** (if API key available)
193
+ 4. **Mistral** (if API key available)
194
+
195
+ ```typescript
196
+ await autoEmbed({ text: string } | { filePath: string })
197
+ ```
198
+
199
+ ### `embed(config, input)`
164
200
 
165
- **vs Other Libraries:**
166
- - ✅ **Native Llama.cpp** - Others use HTTP, we use direct C++ integration
167
- - ✅ **Auto-Detection** - Others require manual provider selection
168
- - **Zero Setup** - Others need external downloads and configuration
169
- - **Multiple Providers** - Others are limited to one provider
170
- - ✅ **Smart Fallbacks** - Others fail when a provider is unavailable
201
+ Explicit provider selection.
202
+ ```typescript
203
+ await embed(
204
+ { provider, model?, apiKey?, baseUrl?, timeout?, maxRetries? },
205
+ { text: string } | { filePath: string } | Array
206
+ )
207
+ ```
171
208
 
172
- **Performance:**
173
- - **Llama.cpp Native**: ~50ms per embedding
174
- - **Cloud Providers**: ~100-300ms per embedding
175
- - **HTTP Llama.cpp**: ~500ms+ per embedding
209
+ **Returns:**
210
+ ```typescript
211
+ {
212
+ embedding: number[],
213
+ dimensions: number,
214
+ provider: string,
215
+ model: string,
216
+ usage?: {
217
+ promptTokens?: number;
218
+ totalTokens?: number;
219
+ }
220
+ }
221
+ ```
176
222
 
177
- ## Common Use Cases
223
+ ## 🧪 Examples
178
224
 
179
225
  ### Semantic Search
180
226
  ```typescript
181
- // Helper function for cosine similarity
182
227
  function cosineSimilarity(vecA: number[], vecB: number[]): number {
183
228
  const dotProduct = vecA.reduce((sum, val, i) => sum + val * vecB[i], 0);
184
229
  const magnitudeA = Math.sqrt(vecA.reduce((sum, val) => sum + val * val, 0));
@@ -199,40 +244,6 @@ const mostSimilar = scores.indexOf(Math.max(...scores));
199
244
  console.log(`Best match: ${documents[mostSimilar]}`);
200
245
  ```
201
246
 
202
- ### Text Similarity
203
- ```typescript
204
- function cosineSimilarity(vecA: number[], vecB: number[]): number {
205
- const dotProduct = vecA.reduce((sum, val, i) => sum + val * vecB[i], 0);
206
- const magnitudeA = Math.sqrt(vecA.reduce((sum, val) => sum + val * val, 0));
207
- const magnitudeB = Math.sqrt(vecB.reduce((sum, val) => sum + val * val, 0));
208
- return dotProduct / (magnitudeA * magnitudeB);
209
- }
210
-
211
- const [emb1, emb2] = await Promise.all([
212
- autoEmbed({ text: 'cat sleeping' }),
213
- autoEmbed({ text: 'cat napping' })
214
- ]);
215
-
216
- const similarity = cosineSimilarity(emb1.embedding, emb2.embedding);
217
- console.log(`Similarity: ${similarity.toFixed(3)}`); // → 0.95 (very similar)
218
- ```
219
-
220
- ### Batch Processing
221
- ```typescript
222
- const results = await embed(
223
- { provider: 'openai', apiKey: 'key' },
224
- [
225
- { text: 'Text 1' },
226
- { text: 'Text 2' },
227
- { filePath: './doc.txt' }
228
- ]
229
- );
230
- // → { embeddings: [[...], [...], [...]], dimensions: 1536 }
231
-
232
- console.log(`Processed ${results.embeddings.length} texts`);
233
- console.log(`Dimensions: ${results.dimensions}`);
234
- ```
235
-
236
247
  ### File Processing
237
248
  ```typescript
238
249
  import { readdir } from 'fs/promises';
@@ -261,151 +272,28 @@ const embeddings = await embedAllFiles('./documents');
261
272
  console.log(`Processed ${embeddings.length} files`);
262
273
  ```
263
274
 
264
- ## API
265
-
266
- ### `autoEmbed(input)`
267
-
268
- Auto-detects best provider in priority order:
269
- 1. **Llama.cpp** (Local & Free)
270
- 2. **OpenAI** (if API key available)
271
- 3. **Gemini** (if API key available)
272
- 4. **Mistral** (if API key available)
273
-
274
- ```typescript
275
- await autoEmbed({ text: string } | { filePath: string })
276
- ```
277
-
278
- ### `embed(config, input)`
279
-
280
- Explicit provider selection.
281
- ```typescript
282
- await embed(
283
- { provider, model?, apiKey?, baseUrl?, timeout?, maxRetries? },
284
- { text: string } | { filePath: string } | Array
285
- )
286
- ```
287
-
288
- **Returns:**
289
- ```typescript
290
- {
291
- embedding: number[],
292
- dimensions: number,
293
- provider: string,
294
- model: string,
295
- usage?: {
296
- promptTokens?: number;
297
- totalTokens?: number;
298
- }
299
- }
300
- ```
301
-
302
- ### `getSupportedProviders()`
303
-
304
- Returns available providers.
305
- ```typescript
306
- import { getSupportedProviders } from 'embedbox';
307
-
308
- const providers = getSupportedProviders();
309
- // → ['openai', 'gemini', 'mistral', 'llamacpp']
310
- ```
311
-
312
- ### `createProvider(config)`
313
-
314
- Create provider instance for advanced usage.
315
- ```typescript
316
- import { createProvider } from 'embedbox';
317
-
318
- const provider = createProvider({
319
- provider: 'openai',
320
- model: 'text-embedding-3-small',
321
- apiKey: 'your-key'
322
- });
275
+ ## 🐛 Troubleshooting
323
276
 
324
- const isReady = await provider.isReady();
325
- if (isReady) {
326
- const result = await provider.embed({ text: 'Hello' });
327
- }
328
- ```
277
+ ### Native Module Issues
329
278
 
330
- ## Environment Variables
279
+ **Problem:** `binding.createModel is not a function`
331
280
  ```bash
332
- # .env file
333
- OPENAI_API_KEY=sk-...
334
- GOOGLE_GENERATIVE_AI_API_KEY=...
335
- ANTHROPIC_API_KEY=sk-ant-...
336
- MISTRAL_API_KEY=...
337
- ```
338
-
339
- ## Error Handling
340
-
341
- ```typescript
342
- import { autoEmbed } from 'embedbox';
343
-
344
- try {
345
- const result = await autoEmbed({ text: 'Hello' });
346
- console.log(result.embedding);
347
- } catch (error) {
348
- if (error.message.includes('API key')) {
349
- console.error('Please set up your API keys in .env');
350
- } else if (error.message.includes('not ready')) {
351
- console.error('Provider is not available');
352
- } else if (error.message.includes('network')) {
353
- console.error('Network connection failed');
354
- } else {
355
- console.error('Embedding failed:', error.message);
356
- }
357
- }
281
+ # Solution: Rebuild native module
282
+ npm run build:native
358
283
  ```
359
284
 
360
- ## TypeScript Support
361
-
362
- Full TypeScript support with type definitions:
363
- ```typescript
364
- import {
365
- autoEmbed,
366
- embed,
367
- getSupportedProviders,
368
- createProvider,
369
- type EmbedConfig,
370
- type EmbedInput,
371
- type EmbedResult
372
- } from 'embedbox';
373
-
374
- // Full type safety
375
- const config: EmbedConfig = {
376
- provider: 'openai',
377
- model: 'text-embedding-3-small'
378
- };
379
-
380
- const input: EmbedInput = {
381
- text: 'Your text here'
382
- };
383
-
384
- const result: EmbedResult = await embed(config, input);
285
+ **Problem:** Model file not found
286
+ ```bash
287
+ # Solution: Check model path
288
+ ls -la models/ # Verify model exists
385
289
  ```
386
290
 
387
- ## 📚 Documentation
388
-
389
- - **[API Reference](./API.md)** - Complete API documentation
390
- - **[Contributing Guide](./CONTRIBUTING.md)** - How to contribute to Vecbox
391
- - **[Troubleshooting](./TROUBLESHOOTING.md)** - Common issues and solutions
392
- - **[Examples](./examples/)** - Code examples and tutorials
393
-
394
- ## 🤝 Contributing
291
+ ### Performance Issues
395
292
 
396
- We welcome contributions! See our [Contributing Guide](./CONTRIBUTING.md) for:
397
- - Adding new providers
398
- - Improving performance
399
- - Bug fixes and features
400
- - Documentation improvements
401
-
402
- ## 🐛 Troubleshooting
403
-
404
- Having issues? Check our [Troubleshooting Guide](./TROUBLESHOOTING.md) for:
405
- - Installation problems
406
- - Runtime errors
407
- - Performance issues
408
- - Common solutions
293
+ **Slow embeddings:**
294
+ - Check model size (smaller = faster)
295
+ - Use batch processing for multiple texts
296
+ - Ensure native module is being used (not HTTP fallback)
409
297
 
410
298
  ## 📄 License
411
299
 
@@ -418,14 +306,8 @@ MIT License - see [LICENSE](LICENSE) file for details.
418
306
  - [Google Gemini](https://ai.google.dev/) - Embedding API
419
307
  - [Mistral AI](https://mistral.ai/) - Embedding API
420
308
 
421
- ## 📞 Support
422
-
423
- - **GitHub Issues**: [Report bugs](https://github.com/box-safe/vecbox/issues)
424
- - **GitHub Discussions**: [Ask questions](https://github.com/box-safe/vecbox/discussions)
425
- - **Documentation**: [API Reference](./API.md)
426
-
427
309
  ---
428
310
 
429
311
  **⭐ Star us on GitHub!** [github.com/box-safe/vecbox](https://github.com/box-safe/vecbox)
430
312
 
431
- **Made with ❤️ by the Vecbox Team**
313
+ **Made with ❤️ by the Vecbox Team**