@memberjunction/ai-vectors-pinecone 2.43.0 → 2.44.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/README.md +228 -17
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -11,6 +11,7 @@ A MemberJunction implementation of vector database services using Pinecone as th
|
|
|
11
11
|
- **Namespace Support**: Work with namespaces for organization within indexes
|
|
12
12
|
- **Metadata Storage**: Store and query with metadata alongside vector embeddings
|
|
13
13
|
- **Configuration Management**: Environment-based configuration options
|
|
14
|
+
- **Auto-registration**: Automatically registers with MemberJunction's class factory system
|
|
14
15
|
|
|
15
16
|
## Installation
|
|
16
17
|
|
|
@@ -36,6 +37,13 @@ PINECONE_DEFAULT_INDEX=your-default-index-name
|
|
|
36
37
|
|
|
37
38
|
# Optional: For embedding generation
|
|
38
39
|
OPENAI_API_KEY=your-openai-api-key
|
|
40
|
+
|
|
41
|
+
# Optional: Database configuration (if using with MemberJunction sync)
|
|
42
|
+
DB_HOST=your-database-host
|
|
43
|
+
DB_PORT=1433
|
|
44
|
+
DB_USERNAME=your-db-username
|
|
45
|
+
DB_PASSWORD=your-db-password
|
|
46
|
+
DB_DATABASE=your-database-name
|
|
39
47
|
```
|
|
40
48
|
|
|
41
49
|
## Usage
|
|
@@ -60,12 +68,13 @@ console.log('Available indexes:', indexList.indexes.map(idx => idx.name));
|
|
|
60
68
|
### Create a New Index
|
|
61
69
|
|
|
62
70
|
```typescript
|
|
63
|
-
// Create a new vector index
|
|
71
|
+
// Create a new vector index with serverless configuration
|
|
64
72
|
const createResult = await pineconeDB.createIndex({
|
|
65
73
|
id: 'my-new-index',
|
|
66
74
|
dimension: 1536, // Dimension for OpenAI embeddings
|
|
67
|
-
metric: 'cosine',
|
|
75
|
+
metric: 'cosine', // 'cosine' | 'euclidean' | 'dotproduct'
|
|
68
76
|
additionalParams: {
|
|
77
|
+
// Pinecone spec object
|
|
69
78
|
serverless: {
|
|
70
79
|
cloud: 'aws',
|
|
71
80
|
region: 'us-west-2'
|
|
@@ -78,12 +87,16 @@ if (createResult.success) {
|
|
|
78
87
|
} else {
|
|
79
88
|
console.error('Failed to create index:', createResult.message);
|
|
80
89
|
}
|
|
90
|
+
|
|
91
|
+
// Get index description
|
|
92
|
+
const description = await pineconeDB.getIndexDescription({ id: 'my-new-index' });
|
|
93
|
+
console.log('Index details:', description);
|
|
81
94
|
```
|
|
82
95
|
|
|
83
96
|
### Insert Vector Records
|
|
84
97
|
|
|
85
98
|
```typescript
|
|
86
|
-
import { VectorRecord } from '@memberjunction/ai-
|
|
99
|
+
import { VectorRecord } from '@memberjunction/ai-vectors';
|
|
87
100
|
|
|
88
101
|
// Create a single vector record
|
|
89
102
|
const vectorRecord: VectorRecord = {
|
|
@@ -151,14 +164,43 @@ if (queryResult.success) {
|
|
|
151
164
|
### Retrieve Specific Records
|
|
152
165
|
|
|
153
166
|
```typescript
|
|
154
|
-
// Fetch
|
|
167
|
+
// Fetch specific records by IDs
|
|
155
168
|
const getRecordResult = await pineconeDB.getRecord({
|
|
156
169
|
id: 'my-index-name', // Optional - uses default if not specified
|
|
157
|
-
data: 'record-123'
|
|
170
|
+
data: ['record-123'] // Array of record IDs to fetch
|
|
158
171
|
});
|
|
159
172
|
|
|
160
173
|
if (getRecordResult.success) {
|
|
161
|
-
console.log('Retrieved
|
|
174
|
+
console.log('Retrieved records:', getRecordResult.data);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Fetch multiple records at once
|
|
178
|
+
const getMultipleResult = await pineconeDB.getRecords({
|
|
179
|
+
data: ['record-123', 'record-124', 'record-125']
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
if (getMultipleResult.success) {
|
|
183
|
+
console.log('Retrieved records:', getMultipleResult.data);
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Update Records
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
// Update a record's values and/or metadata
|
|
191
|
+
const updateResult = await pineconeDB.updateRecord({
|
|
192
|
+
data: {
|
|
193
|
+
id: 'record-123',
|
|
194
|
+
values: [0.2, 0.3, 0.4, /* ... new vector values */],
|
|
195
|
+
setMetadata: {
|
|
196
|
+
// Metadata fields to update
|
|
197
|
+
TemplateID: 'updated-template'
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
if (updateResult.success) {
|
|
203
|
+
console.log('Record updated successfully');
|
|
162
204
|
}
|
|
163
205
|
```
|
|
164
206
|
|
|
@@ -183,6 +225,45 @@ const deleteAllResult = await pineconeDB.deleteAllRecords({
|
|
|
183
225
|
});
|
|
184
226
|
```
|
|
185
227
|
|
|
228
|
+
### Working with Namespaces
|
|
229
|
+
|
|
230
|
+
Pinecone supports namespaces for organizing vectors within an index. This is useful for multi-tenancy or logical separation of data:
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
// Query within a specific namespace
|
|
234
|
+
const namespaceQuery = await pineconeDB.queryIndex({
|
|
235
|
+
vector: [/* query vector */],
|
|
236
|
+
topK: 10,
|
|
237
|
+
namespace: 'customer-data',
|
|
238
|
+
includeMetadata: true
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// Delete all records in a specific namespace
|
|
242
|
+
const deleteNamespace = await pineconeDB.deleteAllRecords({
|
|
243
|
+
data: 'customer-data' // Deletes only in this namespace
|
|
244
|
+
});
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### Advanced Metadata Filtering
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
// Query with complex metadata filters
|
|
251
|
+
const filteredQuery = await pineconeDB.queryIndex({
|
|
252
|
+
vector: [/* query vector */],
|
|
253
|
+
topK: 5,
|
|
254
|
+
filter: {
|
|
255
|
+
// Pinecone filter syntax
|
|
256
|
+
$and: [
|
|
257
|
+
{ Entity: { $eq: 'Customer' } },
|
|
258
|
+
{ TemplateID: { $in: ['template-1', 'template-2'] } },
|
|
259
|
+
{ RecordID: { $gte: '100' } }
|
|
260
|
+
]
|
|
261
|
+
},
|
|
262
|
+
includeMetadata: true,
|
|
263
|
+
includeValues: false // Don't return vector values to save bandwidth
|
|
264
|
+
});
|
|
265
|
+
```
|
|
266
|
+
|
|
186
267
|
## API Reference
|
|
187
268
|
|
|
188
269
|
### PineconeDatabase Class
|
|
@@ -212,9 +293,9 @@ new PineconeDatabase(apiKey: string)
|
|
|
212
293
|
##### Record Operations
|
|
213
294
|
- `createRecord(params)`: Insert a single vector record
|
|
214
295
|
- `createRecords(records)`: Insert multiple vector records
|
|
215
|
-
- `getRecord(params)`: Retrieve
|
|
216
|
-
- `getRecords(params)`: Retrieve multiple records by ID
|
|
217
|
-
- `updateRecord(params)`: Update a single record
|
|
296
|
+
- `getRecord(params)`: Retrieve records by ID(s)
|
|
297
|
+
- `getRecords(params)`: Retrieve multiple records by ID(s)
|
|
298
|
+
- `updateRecord(params)`: Update a single record's values and/or metadata
|
|
218
299
|
- `deleteRecord(record)`: Delete a specific record
|
|
219
300
|
- `deleteRecords(records)`: Delete multiple records
|
|
220
301
|
- `deleteAllRecords(params)`: Delete all records in an index or namespace
|
|
@@ -222,6 +303,10 @@ new PineconeDatabase(apiKey: string)
|
|
|
222
303
|
##### Querying
|
|
223
304
|
- `queryIndex(params)`: Query vectors by similarity
|
|
224
305
|
|
|
306
|
+
##### Not Implemented
|
|
307
|
+
- `editIndex(params)`: Edit index configuration (throws error)
|
|
308
|
+
- `updateRecords(params)`: Batch update records (throws error)
|
|
309
|
+
|
|
225
310
|
### Key Interfaces
|
|
226
311
|
|
|
227
312
|
#### VectorRecord
|
|
@@ -251,7 +336,30 @@ interface CreateIndexParams {
|
|
|
251
336
|
id: string; // Index name
|
|
252
337
|
dimension: number; // Vector dimension
|
|
253
338
|
metric?: string; // Distance metric (cosine, euclidean, dotproduct)
|
|
254
|
-
additionalParams?: any; // Additional Pinecone-specific parameters
|
|
339
|
+
additionalParams?: any; // Additional Pinecone-specific parameters (spec object)
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
#### QueryOptions (Pinecone)
|
|
344
|
+
|
|
345
|
+
```typescript
|
|
346
|
+
interface QueryOptions {
|
|
347
|
+
vector: number[]; // Query vector
|
|
348
|
+
topK: number; // Number of results to return
|
|
349
|
+
filter?: object; // Metadata filter
|
|
350
|
+
includeValues?: boolean; // Include vector values in response
|
|
351
|
+
includeMetadata?: boolean;// Include metadata in response
|
|
352
|
+
namespace?: string; // Namespace to query
|
|
353
|
+
}
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
#### BaseMetadata
|
|
357
|
+
|
|
358
|
+
```typescript
|
|
359
|
+
type BaseMetadata = {
|
|
360
|
+
RecordID: string;
|
|
361
|
+
Entity: string;
|
|
362
|
+
TemplateID: string;
|
|
255
363
|
}
|
|
256
364
|
```
|
|
257
365
|
|
|
@@ -259,20 +367,30 @@ interface CreateIndexParams {
|
|
|
259
367
|
|
|
260
368
|
| Variable | Description |
|
|
261
369
|
|----------|-------------|
|
|
262
|
-
| PINECONE_API_KEY | Your Pinecone API key |
|
|
263
|
-
| PINECONE_HOST | Your Pinecone host URL |
|
|
264
|
-
| PINECONE_DEFAULT_INDEX | Default index name to use if not specified |
|
|
370
|
+
| PINECONE_API_KEY | Your Pinecone API key (required) |
|
|
371
|
+
| PINECONE_HOST | Your Pinecone host URL (optional) |
|
|
372
|
+
| PINECONE_DEFAULT_INDEX | Default index name to use if not specified (optional) |
|
|
265
373
|
| OPENAI_API_KEY | Optional: OpenAI API key for generating embeddings |
|
|
374
|
+
| DB_HOST | Optional: Database host for MemberJunction sync |
|
|
375
|
+
| DB_PORT | Optional: Database port (defaults to 1433) |
|
|
376
|
+
| DB_USERNAME | Optional: Database username |
|
|
377
|
+
| DB_PASSWORD | Optional: Database password |
|
|
378
|
+
| DB_DATABASE | Optional: Database name |
|
|
379
|
+
| CURRENT_USER_EMAIL | Optional: Current user email for context |
|
|
380
|
+
| MISTRAL_API_KEY | Optional: Mistral API key for embeddings |
|
|
266
381
|
|
|
267
382
|
## Integration with MemberJunction Vectors
|
|
268
383
|
|
|
269
384
|
This package works seamlessly with other MemberJunction vector packages:
|
|
270
385
|
|
|
271
386
|
```typescript
|
|
272
|
-
import { PineconeDatabase } from '@memberjunction/ai-vectors-pinecone';
|
|
387
|
+
import { PineconeDatabase, LoadPineconeVectorDB } from '@memberjunction/ai-vectors-pinecone';
|
|
273
388
|
import { OpenAIEmbedding } from '@memberjunction/ai-openai';
|
|
274
389
|
import { VectorSync } from '@memberjunction/ai-vectors-sync';
|
|
275
390
|
|
|
391
|
+
// Ensure the PineconeDatabase class is registered
|
|
392
|
+
LoadPineconeVectorDB();
|
|
393
|
+
|
|
276
394
|
// Set up your vector database
|
|
277
395
|
const vectorDB = new PineconeDatabase(pineconeAPIKey);
|
|
278
396
|
|
|
@@ -287,13 +405,106 @@ const vectorSync = new VectorSync({
|
|
|
287
405
|
});
|
|
288
406
|
```
|
|
289
407
|
|
|
408
|
+
### Class Registration
|
|
409
|
+
|
|
410
|
+
The PineconeDatabase class automatically registers itself with MemberJunction's class factory system using the `@RegisterClass` decorator. This allows it to be dynamically instantiated by the MemberJunction framework when needed.
|
|
411
|
+
|
|
412
|
+
To ensure the class is not removed by tree shaking, import the `LoadPineconeVectorDB` function in your application initialization:
|
|
413
|
+
|
|
414
|
+
```typescript
|
|
415
|
+
import { LoadPineconeVectorDB } from '@memberjunction/ai-vectors-pinecone';
|
|
416
|
+
|
|
417
|
+
// Call this in your app initialization
|
|
418
|
+
LoadPineconeVectorDB();
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
## Error Handling and Best Practices
|
|
422
|
+
|
|
423
|
+
### Error Handling
|
|
424
|
+
|
|
425
|
+
All methods return a `BaseResponse` object with success/failure status:
|
|
426
|
+
|
|
427
|
+
```typescript
|
|
428
|
+
const result = await pineconeDB.createRecord(vectorRecord);
|
|
429
|
+
|
|
430
|
+
if (result.success) {
|
|
431
|
+
console.log('Operation successful:', result.data);
|
|
432
|
+
} else {
|
|
433
|
+
console.error('Operation failed:', result.message);
|
|
434
|
+
// Handle error appropriately
|
|
435
|
+
}
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
### Best Practices
|
|
439
|
+
|
|
440
|
+
1. **Index Management**
|
|
441
|
+
- Always check if an index exists before creating it
|
|
442
|
+
- Use meaningful index names that reflect their purpose
|
|
443
|
+
- Consider using namespaces for multi-tenant applications
|
|
444
|
+
|
|
445
|
+
2. **Vector Dimensions**
|
|
446
|
+
- Ensure vector dimensions match your embedding model
|
|
447
|
+
- OpenAI embeddings typically use 1536 dimensions
|
|
448
|
+
- Mistral and other models may use different dimensions
|
|
449
|
+
|
|
450
|
+
3. **Metadata Design**
|
|
451
|
+
- Keep metadata lightweight to optimize performance
|
|
452
|
+
- Use the BaseMetadata type as a foundation
|
|
453
|
+
- Add indexes on frequently queried metadata fields
|
|
454
|
+
|
|
455
|
+
4. **Batch Operations**
|
|
456
|
+
- Use `createRecords()` for bulk inserts (more efficient than individual inserts)
|
|
457
|
+
- Pinecone supports up to 100 vectors per batch operation
|
|
458
|
+
- Consider chunking larger datasets
|
|
459
|
+
|
|
460
|
+
5. **Query Optimization**
|
|
461
|
+
- Use metadata filters to reduce search space
|
|
462
|
+
- Set `includeValues: false` if you don't need vector values
|
|
463
|
+
- Adjust `topK` based on your use case
|
|
464
|
+
|
|
465
|
+
6. **Connection Management**
|
|
466
|
+
- Reuse PineconeDatabase instances rather than creating new ones
|
|
467
|
+
- The default index is cached after first retrieval
|
|
468
|
+
|
|
290
469
|
## Dependencies
|
|
291
470
|
|
|
292
|
-
- `@memberjunction/ai-
|
|
471
|
+
- `@memberjunction/ai-vectors`: Base abstractions for vector databases
|
|
293
472
|
- `@memberjunction/aiengine`: MemberJunction AI Engine
|
|
294
473
|
- `@memberjunction/core`: MemberJunction core library
|
|
295
|
-
- `@memberjunction/global`: MemberJunction global utilities
|
|
296
|
-
- `@pinecone-database/pinecone`: Official Pinecone client
|
|
474
|
+
- `@memberjunction/global`: MemberJunction global utilities and class registration
|
|
475
|
+
- `@pinecone-database/pinecone`: Official Pinecone client (v2.2.2)
|
|
476
|
+
- `dotenv`: Environment variable management
|
|
477
|
+
- `openai`: OpenAI SDK for embeddings
|
|
478
|
+
- `rxjs`: Reactive programming library
|
|
479
|
+
- `typeorm`: TypeORM for database operations
|
|
480
|
+
|
|
481
|
+
## Troubleshooting
|
|
482
|
+
|
|
483
|
+
### Common Issues
|
|
484
|
+
|
|
485
|
+
1. **"PINECONE_API_KEY not found"**
|
|
486
|
+
- Ensure your `.env` file is in the project root
|
|
487
|
+
- Check that the environment variable is properly set
|
|
488
|
+
- Verify the API key is valid in your Pinecone dashboard
|
|
489
|
+
|
|
490
|
+
2. **"Index not found"**
|
|
491
|
+
- Verify the index exists using `listIndexes()`
|
|
492
|
+
- Check that PINECONE_DEFAULT_INDEX matches an existing index
|
|
493
|
+
- Ensure the index has finished initializing after creation
|
|
494
|
+
|
|
495
|
+
3. **"Dimension mismatch"**
|
|
496
|
+
- Ensure your vector dimensions match the index configuration
|
|
497
|
+
- OpenAI ada-002 embeddings are 1536 dimensions
|
|
498
|
+
- Check your embedding model's output dimensions
|
|
499
|
+
|
|
500
|
+
4. **Connection timeouts**
|
|
501
|
+
- Verify your network connectivity
|
|
502
|
+
- Check Pinecone service status
|
|
503
|
+
- Ensure your API key has proper permissions
|
|
504
|
+
|
|
505
|
+
5. **Tree shaking removes the class**
|
|
506
|
+
- Always call `LoadPineconeVectorDB()` in your initialization code
|
|
507
|
+
- This prevents the class from being removed during optimization
|
|
297
508
|
|
|
298
509
|
## License
|
|
299
510
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai-vectors-pinecone",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.44.0",
|
|
4
4
|
"description": "MemberJunction: Pinecone Implementation for AI Vectors",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,10 +15,10 @@
|
|
|
15
15
|
"author": "MemberJunction.com",
|
|
16
16
|
"license": "ISC",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@memberjunction/aiengine": "2.
|
|
19
|
-
"@memberjunction/core": "2.
|
|
20
|
-
"@memberjunction/global": "2.
|
|
21
|
-
"@memberjunction/ai-vectors": "2.
|
|
18
|
+
"@memberjunction/aiengine": "2.44.0",
|
|
19
|
+
"@memberjunction/core": "2.44.0",
|
|
20
|
+
"@memberjunction/global": "2.44.0",
|
|
21
|
+
"@memberjunction/ai-vectors": "2.44.0",
|
|
22
22
|
"@pinecone-database/pinecone": "2.2.2",
|
|
23
23
|
"dotenv": "^16.4.1",
|
|
24
24
|
"openai": "^4.28.4",
|