@memberjunction/ai-vectordb 2.32.2 → 2.33.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 +238 -0
- package/package.json +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
# @memberjunction/ai-vectordb
|
|
2
|
+
|
|
3
|
+
The MemberJunction AI Vector Database package provides a standardized interface and base classes for working with vector databases in the MemberJunction ecosystem. This package serves as a common abstraction layer that allows different vector database implementations to be used interchangeably.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Abstract Base Class**: Common API for all vector database implementations
|
|
8
|
+
- **Index Management**: Create, list, edit, and delete vector indexes
|
|
9
|
+
- **Record Operations**: Comprehensive CRUD operations for vector records
|
|
10
|
+
- **Query Capabilities**: Flexible vector similarity search with filtering
|
|
11
|
+
- **Type Definitions**: Comprehensive TypeScript type definitions
|
|
12
|
+
- **Provider Agnostic**: Works with any vector database that implements the interface
|
|
13
|
+
- **Standardized Response Format**: Consistent response format across providers
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @memberjunction/ai-vectordb
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Core Components
|
|
22
|
+
|
|
23
|
+
### VectorDBBase
|
|
24
|
+
|
|
25
|
+
The abstract base class that all vector database providers must implement:
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
export abstract class VectorDBBase {
|
|
29
|
+
constructor(apiKey: string);
|
|
30
|
+
|
|
31
|
+
// Index operations
|
|
32
|
+
abstract listIndexes(): IndexList | Promise<IndexList>;
|
|
33
|
+
abstract getIndex(params: BaseRequestParams): BaseResponse | Promise<BaseResponse>;
|
|
34
|
+
abstract createIndex(params: CreateIndexParams): BaseResponse | Promise<BaseResponse>;
|
|
35
|
+
abstract deleteIndex(params: BaseRequestParams): BaseResponse | Promise<BaseResponse>;
|
|
36
|
+
abstract editIndex(params: EditIndexParams): BaseResponse | Promise<BaseResponse>;
|
|
37
|
+
abstract queryIndex(params: QueryOptions): BaseResponse | Promise<BaseResponse>;
|
|
38
|
+
|
|
39
|
+
// Record operations
|
|
40
|
+
abstract createRecord(record: VectorRecord): BaseResponse | Promise<BaseResponse>;
|
|
41
|
+
abstract createRecords(record: VectorRecord[]): BaseResponse | Promise<BaseResponse>;
|
|
42
|
+
abstract getRecord(param: BaseRequestParams): BaseResponse | Promise<BaseResponse>;
|
|
43
|
+
abstract getRecords(params: BaseRequestParams): BaseResponse | Promise<BaseResponse>;
|
|
44
|
+
abstract updateRecord(record: UpdateOptions): BaseResponse | Promise<BaseResponse>;
|
|
45
|
+
abstract updateRecords(records: UpdateOptions): BaseResponse | Promise<BaseResponse>;
|
|
46
|
+
abstract deleteRecord(record: VectorRecord): BaseResponse | Promise<BaseResponse>;
|
|
47
|
+
abstract deleteRecords(records: VectorRecord[]): BaseResponse | Promise<BaseResponse>;
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Key Types and Interfaces
|
|
52
|
+
|
|
53
|
+
#### Vector Records
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
export type VectorRecord<T extends RecordMetadata = RecordMetadata> = {
|
|
57
|
+
id: string; // Unique identifier for the record
|
|
58
|
+
values: number[]; // Vector embedding values (e.g., from an embedding model)
|
|
59
|
+
sparseValues?: { // Optional sparse representation for hybrid search
|
|
60
|
+
indices: number[];
|
|
61
|
+
values: number[];
|
|
62
|
+
};
|
|
63
|
+
metadata?: T; // Optional metadata for filtering and identification
|
|
64
|
+
};
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
#### Index Description
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
export type IndexDescription = {
|
|
71
|
+
name: string; // Name of the index
|
|
72
|
+
dimension: number; // Vector dimension size
|
|
73
|
+
metric: IndexModelMetricEnum; // Distance metric: 'cosine', 'euclidean', or 'dotproduct'
|
|
74
|
+
host: string; // Host where the index is located
|
|
75
|
+
};
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
#### Query Options
|
|
79
|
+
|
|
80
|
+
For similarity search in vector databases:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
// Base query parameters
|
|
84
|
+
export type QueryParamsBase = {
|
|
85
|
+
topK: number; // Number of results to return
|
|
86
|
+
includeValues?: boolean; // Whether to include vector values in results
|
|
87
|
+
includeMetadata?: boolean; // Whether to include metadata in results
|
|
88
|
+
filter?: object; // Metadata filter to apply
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// Query by vector values
|
|
92
|
+
export type QueryByVectorValues = QueryParamsBase & {
|
|
93
|
+
vector: number[]; // The query vector to find similar vectors for
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// Query by record ID
|
|
97
|
+
export type QueryByRecordId = QueryParamsBase & {
|
|
98
|
+
id: string; // Use an existing record's vector to query
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// Combined query options type
|
|
102
|
+
export type QueryOptions = QueryByRecordId | QueryByVectorValues;
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### Query Response
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
export type QueryResponse<T extends RecordMetadata = RecordMetadata> = {
|
|
109
|
+
matches: Array<ScoredRecord<T>>; // Results sorted by similarity
|
|
110
|
+
namespace: string; // Namespace where query was executed
|
|
111
|
+
usage?: OperationUsage; // Usage information
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export interface ScoredRecord<T extends RecordMetadata = RecordMetadata> extends VectorRecord<T> {
|
|
115
|
+
score?: number; // Similarity score
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Usage Examples
|
|
120
|
+
|
|
121
|
+
While this package primarily provides interfaces and base classes, here's how it would be used with a concrete implementation:
|
|
122
|
+
|
|
123
|
+
### Implementing a Vector Database Provider
|
|
124
|
+
|
|
125
|
+
Create a provider by implementing the `VectorDBBase` abstract class:
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
import { VectorDBBase, VectorRecord, BaseResponse, CreateIndexParams } from '@memberjunction/ai-vectordb';
|
|
129
|
+
|
|
130
|
+
class MyVectorDBProvider extends VectorDBBase {
|
|
131
|
+
constructor(apiKey: string) {
|
|
132
|
+
super(apiKey);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async listIndexes() {
|
|
136
|
+
// Implementation for listing indexes
|
|
137
|
+
return {
|
|
138
|
+
indexes: [
|
|
139
|
+
// Index descriptions
|
|
140
|
+
]
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async createIndex(params: CreateIndexParams) {
|
|
145
|
+
// Implementation for creating an index
|
|
146
|
+
try {
|
|
147
|
+
// Provider-specific code
|
|
148
|
+
return {
|
|
149
|
+
success: true,
|
|
150
|
+
message: 'Index created successfully',
|
|
151
|
+
data: { /* index info */ }
|
|
152
|
+
};
|
|
153
|
+
} catch (error) {
|
|
154
|
+
return {
|
|
155
|
+
success: false,
|
|
156
|
+
message: error.message,
|
|
157
|
+
data: null
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Implement remaining methods...
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Using a Vector Database Provider
|
|
167
|
+
|
|
168
|
+
Once a provider is implemented, you can use it with a consistent API:
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import { VectorDBBase, VectorRecord } from '@memberjunction/ai-vectordb';
|
|
172
|
+
import { MyVectorDBProvider } from './my-vector-db-provider';
|
|
173
|
+
|
|
174
|
+
async function workWithVectors() {
|
|
175
|
+
// Initialize the provider
|
|
176
|
+
const vectorDB: VectorDBBase = new MyVectorDBProvider('your-api-key');
|
|
177
|
+
|
|
178
|
+
// Create an index
|
|
179
|
+
const createResult = await vectorDB.createIndex({
|
|
180
|
+
id: 'my-index',
|
|
181
|
+
dimension: 1536,
|
|
182
|
+
metric: 'cosine'
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
if (createResult.success) {
|
|
186
|
+
console.log('Index created:', createResult.data);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Insert a vector
|
|
190
|
+
const insertResult = await vectorDB.createRecord({
|
|
191
|
+
id: 'record-1',
|
|
192
|
+
values: [0.1, 0.2, 0.3, /* ... */],
|
|
193
|
+
metadata: {
|
|
194
|
+
category: 'document',
|
|
195
|
+
title: 'Sample Document'
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// Query for similar vectors
|
|
200
|
+
const queryResult = await vectorDB.queryIndex({
|
|
201
|
+
vector: [0.1, 0.2, 0.3, /* ... */],
|
|
202
|
+
topK: 5,
|
|
203
|
+
includeMetadata: true,
|
|
204
|
+
filter: {
|
|
205
|
+
category: 'document'
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
if (queryResult.success) {
|
|
210
|
+
console.log('Similar vectors:', queryResult.data.matches);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Provider Implementations
|
|
216
|
+
|
|
217
|
+
MemberJunction provides implementations for popular vector databases:
|
|
218
|
+
|
|
219
|
+
- `@memberjunction/ai-vectors-pinecone` - Implementation for Pinecone vector database
|
|
220
|
+
|
|
221
|
+
You can also create your own implementation for any vector database service by extending the `VectorDBBase` class.
|
|
222
|
+
|
|
223
|
+
## Integration with MemberJunction Ecosystem
|
|
224
|
+
|
|
225
|
+
This package integrates with the broader MemberJunction AI vector ecosystem:
|
|
226
|
+
|
|
227
|
+
- `@memberjunction/ai-vectors` - Core vector functionality
|
|
228
|
+
- `@memberjunction/ai-vectors-sync` - Synchronize entity data to vector databases
|
|
229
|
+
- `@memberjunction/ai-vectors-dupe` - Duplicate detection using vector similarity
|
|
230
|
+
|
|
231
|
+
## Dependencies
|
|
232
|
+
|
|
233
|
+
- `@memberjunction/core`: MemberJunction core library
|
|
234
|
+
- `@memberjunction/global`: MemberJunction global utilities
|
|
235
|
+
|
|
236
|
+
## License
|
|
237
|
+
|
|
238
|
+
ISC
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai-vectordb",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.33.0",
|
|
4
4
|
"description": "MemberJunction: AI Vector Database Module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
"author": "MemberJunction.com",
|
|
16
16
|
"license": "ISC",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@memberjunction/core": "2.
|
|
19
|
-
"@memberjunction/global": "2.
|
|
18
|
+
"@memberjunction/core": "2.33.0",
|
|
19
|
+
"@memberjunction/global": "2.33.0",
|
|
20
20
|
"dotenv": "^16.4.1"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|