@memberjunction/ai-vectors 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 +167 -19
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
The MemberJunction AI Vectors Core package provides the foundational abstractions and base classes for working with vector embeddings, vector databases, and vector operations within the MemberJunction ecosystem.
|
|
4
4
|
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package serves as the core foundation for vector-related operations in MemberJunction, providing:
|
|
8
|
+
- Abstract interfaces for vector database and embedding operations
|
|
9
|
+
- Base class implementation with entity integration
|
|
10
|
+
- Type definitions for pagination and data handling
|
|
11
|
+
- Seamless integration with MemberJunction's metadata system and AI engine
|
|
12
|
+
|
|
5
13
|
## Features
|
|
6
14
|
|
|
7
15
|
- **Core Interfaces**: Fundamental interface definitions for vector operations
|
|
@@ -11,6 +19,8 @@ The MemberJunction AI Vectors Core package provides the foundational abstraction
|
|
|
11
19
|
- **Entity Integration**: Seamless integration with MemberJunction entities
|
|
12
20
|
- **Pagination Support**: Efficiently retrieve and process large sets of records
|
|
13
21
|
- **Type Definitions**: Comprehensive TypeScript types for vector operations
|
|
22
|
+
- **AI Model Integration**: Built-in support for accessing embedding models
|
|
23
|
+
- **User Context Management**: Automatic handling of user context for entity operations
|
|
14
24
|
|
|
15
25
|
## Installation
|
|
16
26
|
|
|
@@ -73,6 +83,33 @@ The `VectorBase` class serves as the foundation for vector operations, providing
|
|
|
73
83
|
- Entity record retrieval and manipulation
|
|
74
84
|
- Pagination support for handling large datasets
|
|
75
85
|
- Helper methods for AI model and vector database access
|
|
86
|
+
- Automatic user context management for entity operations
|
|
87
|
+
- Built-in RunView integration for flexible data querying
|
|
88
|
+
|
|
89
|
+
#### Key Methods
|
|
90
|
+
|
|
91
|
+
- `GetRecordsByEntityID(entityID: string, recordIDs?: CompositeKey[]): Promise<BaseEntity[]>` - Retrieve entity records with optional filtering
|
|
92
|
+
- `PageRecordsByEntityID<T>(params: PageRecordsParams): Promise<T[]>` - Paginated entity record retrieval
|
|
93
|
+
- `GetAIModel(id?: string): AIModelEntityExtended` - Access configured embedding models
|
|
94
|
+
- `GetVectorDatabase(id?: string): VectorDatabaseEntity` - Access configured vector databases
|
|
95
|
+
- `RunViewForSingleValue<T>(entityName: string, extraFilter: string): Promise<T | null>` - Query for single entity records
|
|
96
|
+
- `SaveEntity(entity: BaseEntity): Promise<boolean>` - Save entities with proper user context
|
|
97
|
+
|
|
98
|
+
### Type Definitions
|
|
99
|
+
|
|
100
|
+
#### PageRecordsParams
|
|
101
|
+
|
|
102
|
+
Type definition for paginated record retrieval:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
type PageRecordsParams = {
|
|
106
|
+
EntityID: string | number; // The ID of the entity to get records from
|
|
107
|
+
PageNumber: number; // Page number (1-based)
|
|
108
|
+
PageSize: number; // Number of records per page
|
|
109
|
+
ResultType: "entity_object" | "simple" | "count_only"; // Type of result
|
|
110
|
+
Filter?: string; // Optional SQL filter
|
|
111
|
+
}
|
|
112
|
+
```
|
|
76
113
|
|
|
77
114
|
## Usage
|
|
78
115
|
|
|
@@ -81,30 +118,33 @@ The `VectorBase` class serves as the foundation for vector operations, providing
|
|
|
81
118
|
Create specialized vector operation classes by extending `VectorBase`:
|
|
82
119
|
|
|
83
120
|
```typescript
|
|
84
|
-
import { VectorBase } from '@memberjunction/ai-vectors';
|
|
121
|
+
import { VectorBase, PageRecordsParams } from '@memberjunction/ai-vectors';
|
|
122
|
+
import { BaseEntity } from '@memberjunction/core';
|
|
85
123
|
|
|
86
124
|
export class MyVectorProcessor extends VectorBase {
|
|
87
125
|
async processEntityRecords(entityId: string): Promise<void> {
|
|
88
|
-
// Get entity records
|
|
126
|
+
// Get all entity records
|
|
89
127
|
const records = await this.GetRecordsByEntityID(entityId);
|
|
90
128
|
|
|
91
129
|
// Process each record
|
|
92
130
|
for (const record of records) {
|
|
93
131
|
// Your vector processing logic here
|
|
132
|
+
console.log(`Processing ${record.EntityInfo.Name} record ${record.ID}`);
|
|
94
133
|
}
|
|
95
134
|
}
|
|
96
135
|
|
|
97
136
|
async paginatedProcess(entityId: string): Promise<void> {
|
|
98
137
|
// Process records page by page
|
|
99
|
-
const params = {
|
|
138
|
+
const params: PageRecordsParams = {
|
|
100
139
|
EntityID: entityId,
|
|
101
140
|
PageNumber: 1,
|
|
102
141
|
PageSize: 100,
|
|
103
|
-
ResultType: 'entity_object'
|
|
142
|
+
ResultType: 'entity_object' as const
|
|
104
143
|
};
|
|
105
144
|
|
|
106
|
-
const records = await this.PageRecordsByEntityID(params);
|
|
145
|
+
const records = await this.PageRecordsByEntityID<BaseEntity>(params);
|
|
107
146
|
// Process paged records
|
|
147
|
+
console.log(`Retrieved ${records.length} records`);
|
|
108
148
|
}
|
|
109
149
|
}
|
|
110
150
|
```
|
|
@@ -135,16 +175,20 @@ The package provides utilities for working with MemberJunction entities:
|
|
|
135
175
|
|
|
136
176
|
```typescript
|
|
137
177
|
import { VectorBase, PageRecordsParams } from '@memberjunction/ai-vectors';
|
|
178
|
+
import { BaseEntity } from '@memberjunction/core';
|
|
179
|
+
import { AIModelEntityExtended, VectorDatabaseEntity } from '@memberjunction/core-entities';
|
|
138
180
|
|
|
139
181
|
class EntityVectorizer extends VectorBase {
|
|
140
182
|
async vectorizeEntities(entityId: string): Promise<void> {
|
|
141
|
-
// Get AI model for embeddings
|
|
142
|
-
const embeddingModel = this.GetAIModel();
|
|
183
|
+
// Get AI model for embeddings (defaults to first embedding model if no ID provided)
|
|
184
|
+
const embeddingModel: AIModelEntityExtended = this.GetAIModel();
|
|
185
|
+
console.log(`Using embedding model: ${embeddingModel.Name}`);
|
|
143
186
|
|
|
144
|
-
// Get vector database
|
|
145
|
-
const vectorDb = this.GetVectorDatabase();
|
|
187
|
+
// Get vector database (defaults to first configured vector DB)
|
|
188
|
+
const vectorDb: VectorDatabaseEntity = this.GetVectorDatabase();
|
|
189
|
+
console.log(`Using vector database: ${vectorDb.Name}`);
|
|
146
190
|
|
|
147
|
-
// Process records in pages
|
|
191
|
+
// Process records in pages for memory efficiency
|
|
148
192
|
let pageNumber = 1;
|
|
149
193
|
let hasMoreRecords = true;
|
|
150
194
|
|
|
@@ -153,17 +197,59 @@ class EntityVectorizer extends VectorBase {
|
|
|
153
197
|
EntityID: entityId,
|
|
154
198
|
PageNumber: pageNumber,
|
|
155
199
|
PageSize: 50,
|
|
156
|
-
ResultType: 'entity_object'
|
|
200
|
+
ResultType: 'entity_object' as const,
|
|
201
|
+
Filter: "IsActive = 1" // Optional: add custom filtering
|
|
157
202
|
};
|
|
158
203
|
|
|
159
|
-
const records = await this.PageRecordsByEntityID(params);
|
|
204
|
+
const records = await this.PageRecordsByEntityID<BaseEntity>(params);
|
|
160
205
|
hasMoreRecords = records.length === params.PageSize;
|
|
161
206
|
pageNumber++;
|
|
162
207
|
|
|
163
208
|
// Process the current page of records
|
|
164
|
-
|
|
209
|
+
for (const record of records) {
|
|
210
|
+
// Your vectorization logic here
|
|
211
|
+
// Example: Generate embeddings for record content
|
|
212
|
+
}
|
|
165
213
|
}
|
|
166
214
|
}
|
|
215
|
+
|
|
216
|
+
async saveVectorizedEntity(entity: BaseEntity): Promise<boolean> {
|
|
217
|
+
// SaveEntity automatically adds user context
|
|
218
|
+
return await this.SaveEntity(entity);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Advanced Entity Filtering
|
|
224
|
+
|
|
225
|
+
Use composite keys for complex filtering scenarios:
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
import { VectorBase } from '@memberjunction/ai-vectors';
|
|
229
|
+
import { CompositeKey } from '@memberjunction/core';
|
|
230
|
+
|
|
231
|
+
class AdvancedVectorProcessor extends VectorBase {
|
|
232
|
+
async getSpecificRecords(entityId: string): Promise<void> {
|
|
233
|
+
// Build composite keys for specific records
|
|
234
|
+
const compositeKeys: CompositeKey[] = [
|
|
235
|
+
{
|
|
236
|
+
KeyValuePairs: [
|
|
237
|
+
{ FieldName: 'Status', Value: 'Active' },
|
|
238
|
+
{ FieldName: 'CategoryID', Value: '123' }
|
|
239
|
+
]
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
KeyValuePairs: [
|
|
243
|
+
{ FieldName: 'Status', Value: 'Pending' },
|
|
244
|
+
{ FieldName: 'CategoryID', Value: '456' }
|
|
245
|
+
]
|
|
246
|
+
}
|
|
247
|
+
];
|
|
248
|
+
|
|
249
|
+
// This will generate: (Status = 'Active' AND CategoryID = '123') OR (Status = 'Pending' AND CategoryID = '456')
|
|
250
|
+
const records = await this.GetRecordsByEntityID(entityId, compositeKeys);
|
|
251
|
+
console.log(`Found ${records.length} matching records`);
|
|
252
|
+
}
|
|
167
253
|
}
|
|
168
254
|
```
|
|
169
255
|
|
|
@@ -193,14 +279,76 @@ This core package serves as the foundation for a suite of vector-related package
|
|
|
193
279
|
- `@memberjunction/ai-vectors-pinecone` - Pinecone vector database implementation
|
|
194
280
|
- `@memberjunction/ai-vectors-dupe` - Duplicate detection using vector similarity
|
|
195
281
|
|
|
282
|
+
## Configuration
|
|
283
|
+
|
|
284
|
+
The package automatically integrates with MemberJunction's configuration system:
|
|
285
|
+
|
|
286
|
+
1. **AI Models**: Embedding models are configured in the MemberJunction metadata and accessed via `AIEngine.Instance`
|
|
287
|
+
2. **Vector Databases**: Vector databases are configured similarly and accessed through the AI engine
|
|
288
|
+
3. **User Context**: The current user context is automatically managed and passed to entity operations
|
|
289
|
+
|
|
290
|
+
## Best Practices
|
|
291
|
+
|
|
292
|
+
1. **Always extend VectorBase** for custom vector operations to ensure proper integration
|
|
293
|
+
2. **Use pagination** when processing large datasets to avoid memory issues
|
|
294
|
+
3. **Handle errors gracefully** - the base class methods throw exceptions that should be caught
|
|
295
|
+
4. **Set user context** - The base class automatically manages user context for entity operations
|
|
296
|
+
5. **Use type-safe generics** - When using `PageRecordsByEntityID`, specify the expected type
|
|
297
|
+
|
|
298
|
+
## Error Handling
|
|
299
|
+
|
|
300
|
+
The package provides clear error messages for common scenarios:
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
try {
|
|
304
|
+
const model = this.GetAIModel('specific-model-id');
|
|
305
|
+
} catch (error) {
|
|
306
|
+
// Will throw if no embedding model is configured
|
|
307
|
+
console.error('No AI Model Entity found');
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
try {
|
|
311
|
+
const records = await this.GetRecordsByEntityID('invalid-id');
|
|
312
|
+
} catch (error) {
|
|
313
|
+
// Will throw if entity ID doesn't exist
|
|
314
|
+
console.error(`Entity with ID invalid-id not found.`);
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
196
318
|
## Dependencies
|
|
197
319
|
|
|
198
|
-
- `@memberjunction/core`: MemberJunction
|
|
199
|
-
- `@memberjunction/global`:
|
|
200
|
-
- `@memberjunction/core-entities`:
|
|
201
|
-
- `@memberjunction/aiengine`:
|
|
202
|
-
- `@memberjunction/ai`:
|
|
203
|
-
- `@memberjunction/ai-vectordb`: Vector database interfaces
|
|
320
|
+
- `@memberjunction/core`: ^2.43.0 - Core MemberJunction functionality
|
|
321
|
+
- `@memberjunction/global`: ^2.43.0 - Global utilities
|
|
322
|
+
- `@memberjunction/core-entities`: ^2.43.0 - Entity definitions
|
|
323
|
+
- `@memberjunction/aiengine`: ^2.43.0 - AI engine integration
|
|
324
|
+
- `@memberjunction/ai`: ^2.43.0 - AI abstractions
|
|
325
|
+
- `@memberjunction/ai-vectordb`: ^2.43.0 - Vector database interfaces
|
|
326
|
+
- `openai`: ^4.28.4 - OpenAI SDK (for embedding implementations)
|
|
327
|
+
- `dotenv`: ^16.4.1 - Environment configuration
|
|
328
|
+
|
|
329
|
+
## Development
|
|
330
|
+
|
|
331
|
+
### Building
|
|
332
|
+
|
|
333
|
+
```bash
|
|
334
|
+
npm run build
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
### Development Mode
|
|
338
|
+
|
|
339
|
+
```bash
|
|
340
|
+
npm run start
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
## Contributing
|
|
344
|
+
|
|
345
|
+
When contributing to this package:
|
|
346
|
+
|
|
347
|
+
1. Follow the MemberJunction coding standards
|
|
348
|
+
2. Ensure all interfaces remain generic and implementation-agnostic
|
|
349
|
+
3. Add comprehensive TypeScript types
|
|
350
|
+
4. Update this README with any new features or changes
|
|
351
|
+
5. Test integration with dependent packages
|
|
204
352
|
|
|
205
353
|
## License
|
|
206
354
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai-vectors",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.44.0",
|
|
4
4
|
"description": "MemberJunction: AI Vectors Module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,12 +15,12 @@
|
|
|
15
15
|
"author": "MemberJunction.com",
|
|
16
16
|
"license": "ISC",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@memberjunction/core": "2.
|
|
19
|
-
"@memberjunction/global": "2.
|
|
20
|
-
"@memberjunction/core-entities": "2.
|
|
21
|
-
"@memberjunction/aiengine": "2.
|
|
22
|
-
"@memberjunction/ai": "2.
|
|
23
|
-
"@memberjunction/ai-vectordb": "2.
|
|
18
|
+
"@memberjunction/core": "2.44.0",
|
|
19
|
+
"@memberjunction/global": "2.44.0",
|
|
20
|
+
"@memberjunction/core-entities": "2.44.0",
|
|
21
|
+
"@memberjunction/aiengine": "2.44.0",
|
|
22
|
+
"@memberjunction/ai": "2.44.0",
|
|
23
|
+
"@memberjunction/ai-vectordb": "2.44.0",
|
|
24
24
|
"dotenv": "^16.4.1",
|
|
25
25
|
"openai": "^4.28.4"
|
|
26
26
|
},
|