2.48.0 • Published 5 months ago

@memberjunction/ai-vectors v2.48.0

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

@memberjunction/ai-vectors

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.

Overview

This package serves as the core foundation for vector-related operations in MemberJunction, providing:

  • Abstract interfaces for vector database and embedding operations
  • Base class implementation with entity integration
  • Type definitions for pagination and data handling
  • Seamless integration with MemberJunction's metadata system and AI engine

Features

  • Core Interfaces: Fundamental interface definitions for vector operations
  • Base Classes: Base implementation patterns for vector-related functionality
  • Database Abstraction: Interface for vector database operations
  • Embedding Abstraction: Interface for text embedding generation
  • Entity Integration: Seamless integration with MemberJunction entities
  • Pagination Support: Efficiently retrieve and process large sets of records
  • Type Definitions: Comprehensive TypeScript types for vector operations
  • AI Model Integration: Built-in support for accessing embedding models
  • User Context Management: Automatic handling of user context for entity operations

Installation

npm install @memberjunction/ai-vectors

Core Components

Interfaces

The package defines several key interfaces for vector operations:

IEmbedding

Interface for text embedding generation:

interface IEmbedding {
  createEmbedding(text: string, options?: any): any;
  createBatchEmbedding(text: string[], options?: any): any;
}

IVectorDatabase

Interface for vector database management:

interface IVectorDatabase {
  listIndexes(options?: any): any;
  createIndex(options: any): any;
  deleteIndex(indexID: any, options?: any): any;
  editIndex(indexID: any, options?: any): any;
}

IVectorIndex

Interface for vector index operations:

interface IVectorIndex {
  createRecord(record: any, options?: any): any;
  createRecords(records: any[], options?: any): any;
  getRecord(recordID: any, options?: any): any;
  getRecords(recordIDs: any[], options?: any): any;
  updateRecord(record: any, options?: any): any;
  updateRecords(records: any[], options?: any): any;
  deleteRecord(recordID: any, options?: any): any;
  deleteRecords(recordIDs: any[], options?: any): any;
}

VectorBase Class

The VectorBase class serves as the foundation for vector operations, providing:

  • Integration with MemberJunction's metadata system
  • Entity record retrieval and manipulation
  • Pagination support for handling large datasets
  • Helper methods for AI model and vector database access
  • Automatic user context management for entity operations
  • Built-in RunView integration for flexible data querying

Key Methods

  • GetRecordsByEntityID(entityID: string, recordIDs?: CompositeKey[]): Promise<BaseEntity[]> - Retrieve entity records with optional filtering
  • PageRecordsByEntityID<T>(params: PageRecordsParams): Promise<T[]> - Paginated entity record retrieval
  • GetAIModel(id?: string): AIModelEntityExtended - Access configured embedding models
  • GetVectorDatabase(id?: string): VectorDatabaseEntity - Access configured vector databases
  • RunViewForSingleValue<T>(entityName: string, extraFilter: string): Promise<T | null> - Query for single entity records
  • SaveEntity(entity: BaseEntity): Promise<boolean> - Save entities with proper user context

Type Definitions

PageRecordsParams

Type definition for paginated record retrieval:

type PageRecordsParams = {
  EntityID: string | number;        // The ID of the entity to get records from
  PageNumber: number;               // Page number (1-based)
  PageSize: number;                 // Number of records per page
  ResultType: "entity_object" | "simple" | "count_only";  // Type of result
  Filter?: string;                  // Optional SQL filter
}

Usage

Extending the Base Class

Create specialized vector operation classes by extending VectorBase:

import { VectorBase, PageRecordsParams } from '@memberjunction/ai-vectors';
import { BaseEntity } from '@memberjunction/core';

export class MyVectorProcessor extends VectorBase {
  async processEntityRecords(entityId: string): Promise<void> {
    // Get all entity records
    const records = await this.GetRecordsByEntityID(entityId);
    
    // Process each record
    for (const record of records) {
      // Your vector processing logic here
      console.log(`Processing ${record.EntityInfo.Name} record ${record.ID}`);
    }
  }
  
  async paginatedProcess(entityId: string): Promise<void> {
    // Process records page by page
    const params: PageRecordsParams = {
      EntityID: entityId,
      PageNumber: 1,
      PageSize: 100,
      ResultType: 'entity_object' as const
    };
    
    const records = await this.PageRecordsByEntityID<BaseEntity>(params);
    // Process paged records
    console.log(`Retrieved ${records.length} records`);
  }
}

Implementing the Interfaces

Implement the interfaces to create specific provider implementations:

import { IEmbedding } from '@memberjunction/ai-vectors';

export class OpenAIEmbedding implements IEmbedding {
  constructor(private apiKey: string) {}
  
  async createEmbedding(text: string): Promise<number[]> {
    // Implementation using OpenAI's embedding API
  }
  
  async createBatchEmbedding(texts: string[]): Promise<number[][]> {
    // Batch implementation
  }
}

Working with Entity Records

The package provides utilities for working with MemberJunction entities:

import { VectorBase, PageRecordsParams } from '@memberjunction/ai-vectors';
import { BaseEntity } from '@memberjunction/core';
import { AIModelEntityExtended, VectorDatabaseEntity } from '@memberjunction/core-entities';

class EntityVectorizer extends VectorBase {
  async vectorizeEntities(entityId: string): Promise<void> {
    // Get AI model for embeddings (defaults to first embedding model if no ID provided)
    const embeddingModel: AIModelEntityExtended = this.GetAIModel();
    console.log(`Using embedding model: ${embeddingModel.Name}`);
    
    // Get vector database (defaults to first configured vector DB)
    const vectorDb: VectorDatabaseEntity = this.GetVectorDatabase();
    console.log(`Using vector database: ${vectorDb.Name}`);
    
    // Process records in pages for memory efficiency
    let pageNumber = 1;
    let hasMoreRecords = true;
    
    while (hasMoreRecords) {
      const params: PageRecordsParams = {
        EntityID: entityId,
        PageNumber: pageNumber,
        PageSize: 50,
        ResultType: 'entity_object' as const,
        Filter: "IsActive = 1"  // Optional: add custom filtering
      };
      
      const records = await this.PageRecordsByEntityID<BaseEntity>(params);
      hasMoreRecords = records.length === params.PageSize;
      pageNumber++;
      
      // Process the current page of records
      for (const record of records) {
        // Your vectorization logic here
        // Example: Generate embeddings for record content
      }
    }
  }
  
  async saveVectorizedEntity(entity: BaseEntity): Promise<boolean> {
    // SaveEntity automatically adds user context
    return await this.SaveEntity(entity);
  }
}

Advanced Entity Filtering

Use composite keys for complex filtering scenarios:

import { VectorBase } from '@memberjunction/ai-vectors';
import { CompositeKey } from '@memberjunction/core';

class AdvancedVectorProcessor extends VectorBase {
  async getSpecificRecords(entityId: string): Promise<void> {
    // Build composite keys for specific records
    const compositeKeys: CompositeKey[] = [
      {
        KeyValuePairs: [
          { FieldName: 'Status', Value: 'Active' },
          { FieldName: 'CategoryID', Value: '123' }
        ]
      },
      {
        KeyValuePairs: [
          { FieldName: 'Status', Value: 'Pending' },
          { FieldName: 'CategoryID', Value: '456' }
        ]
      }
    ];
    
    // This will generate: (Status = 'Active' AND CategoryID = '123') OR (Status = 'Pending' AND CategoryID = '456')
    const records = await this.GetRecordsByEntityID(entityId, compositeKeys);
    console.log(`Found ${records.length} matching records`);
  }
}

Integration with MemberJunction

This package works in harmony with other MemberJunction packages:

import { VectorBase } from '@memberjunction/ai-vectors';
import { AIEngine } from '@memberjunction/aiengine';
import { Metadata, RunView } from '@memberjunction/core';

// Your vector processing will have access to:
// - Entity metadata
// - AI models configuration
// - Vector database configuration
// - User context

Package Ecosystem

This core package serves as the foundation for a suite of vector-related packages:

  • @memberjunction/ai-vectors - Core abstractions (this package)
  • @memberjunction/ai-vectordb - Vector database interface
  • @memberjunction/ai-vectors-sync - Entity synchronization with vector databases
  • @memberjunction/ai-vectors-pinecone - Pinecone vector database implementation
  • @memberjunction/ai-vectors-dupe - Duplicate detection using vector similarity

Configuration

The package automatically integrates with MemberJunction's configuration system:

  1. AI Models: Embedding models are configured in the MemberJunction metadata and accessed via AIEngine.Instance
  2. Vector Databases: Vector databases are configured similarly and accessed through the AI engine
  3. User Context: The current user context is automatically managed and passed to entity operations

Best Practices

  1. Always extend VectorBase for custom vector operations to ensure proper integration
  2. Use pagination when processing large datasets to avoid memory issues
  3. Handle errors gracefully - the base class methods throw exceptions that should be caught
  4. Set user context - The base class automatically manages user context for entity operations
  5. Use type-safe generics - When using PageRecordsByEntityID, specify the expected type

Error Handling

The package provides clear error messages for common scenarios:

try {
  const model = this.GetAIModel('specific-model-id');
} catch (error) {
  // Will throw if no embedding model is configured
  console.error('No AI Model Entity found');
}

try {
  const records = await this.GetRecordsByEntityID('invalid-id');
} catch (error) {
  // Will throw if entity ID doesn't exist
  console.error(`Entity with ID invalid-id not found.`);
}

Dependencies

  • @memberjunction/core: ^2.43.0 - Core MemberJunction functionality
  • @memberjunction/global: ^2.43.0 - Global utilities
  • @memberjunction/core-entities: ^2.43.0 - Entity definitions
  • @memberjunction/aiengine: ^2.43.0 - AI engine integration
  • @memberjunction/ai: ^2.43.0 - AI abstractions
  • @memberjunction/ai-vectordb: ^2.43.0 - Vector database interfaces
  • openai: ^4.28.4 - OpenAI SDK (for embedding implementations)
  • dotenv: ^16.4.1 - Environment configuration

Development

Building

npm run build

Development Mode

npm run start

Contributing

When contributing to this package:

  1. Follow the MemberJunction coding standards
  2. Ensure all interfaces remain generic and implementation-agnostic
  3. Add comprehensive TypeScript types
  4. Update this README with any new features or changes
  5. Test integration with dependent packages

License

ISC

2.27.1

8 months ago

2.23.2

9 months ago

2.46.0

5 months ago

2.23.1

9 months ago

2.27.0

8 months ago

2.34.0

6 months ago

2.30.0

7 months ago

2.19.4

9 months ago

2.19.5

9 months ago

2.19.2

9 months ago

2.19.3

9 months ago

2.19.0

9 months ago

2.19.1

9 months ago

2.15.2

9 months ago

2.34.2

6 months ago

2.15.0

9 months ago

2.34.1

6 months ago

2.15.1

9 months ago

2.38.0

5 months ago

2.45.0

5 months ago

2.22.1

9 months ago

2.22.0

9 months ago

2.41.0

5 months ago

2.22.2

9 months ago

2.26.1

8 months ago

2.26.0

8 months ago

2.33.0

6 months ago

2.18.3

9 months ago

2.18.1

9 months ago

2.18.2

9 months ago

2.18.0

9 months ago

2.37.1

6 months ago

2.37.0

6 months ago

2.14.0

10 months ago

2.21.0

9 months ago

2.44.0

5 months ago

2.40.0

5 months ago

2.29.0

8 months ago

2.29.2

8 months ago

2.29.1

8 months ago

2.25.0

8 months ago

2.48.0

5 months ago

2.32.0

7 months ago

2.32.2

7 months ago

2.32.1

7 months ago

2.17.0

9 months ago

2.13.4

10 months ago

2.36.0

6 months ago

2.13.2

11 months ago

2.13.3

10 months ago

2.13.0

11 months ago

2.36.1

6 months ago

2.13.1

11 months ago

2.43.0

5 months ago

2.20.2

9 months ago

2.20.3

9 months ago

2.20.0

9 months ago

2.20.1

9 months ago

2.28.0

8 months ago

2.47.0

5 months ago

2.24.1

8 months ago

2.24.0

8 months ago

2.31.0

7 months ago

2.12.0

12 months ago

2.39.0

5 months ago

2.16.1

9 months ago

2.35.1

6 months ago

2.35.0

6 months ago

2.16.0

9 months ago

2.42.1

5 months ago

2.42.0

5 months ago

2.23.0

9 months ago

2.11.0

1 year ago

2.10.0

1 year ago

2.9.0

1 year ago

2.8.0

1 year ago

2.6.1

1 year ago

2.6.0

1 year ago

2.7.0

1 year ago

2.5.2

1 year ago

2.7.1

1 year ago

1.8.1

1 year ago

1.8.0

1 year ago

1.6.1

1 year ago

1.6.0

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

2.2.1

1 year ago

2.2.0

1 year ago

2.4.1

1 year ago

2.4.0

1 year ago

2.0.0

1 year ago

1.7.1

1 year ago

1.5.3

1 year ago

1.7.0

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.3.3

1 year ago

1.5.0

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

2.3.0

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.5.0

1 year ago

2.3.2

1 year ago

2.1.4

1 year ago

2.3.1

1 year ago

2.1.3

1 year ago

2.5.1

1 year ago

2.3.3

1 year ago

2.1.5

1 year ago

2.1.0

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.0.11

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.8-next.6

2 years ago

1.0.8-next.5

2 years ago

1.0.8-next.4

2 years ago

1.0.8-next.3

2 years ago

1.0.8-next.2

2 years ago

1.0.8-beta.0

2 years ago

1.0.8-next.1

2 years ago

1.0.8-next.0

2 years ago

1.0.7-next.0

2 years ago

1.0.2

2 years ago

1.0.6

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.9.20

2 years ago

0.9.18

2 years ago

0.9.19

2 years ago

0.9.16

2 years ago

0.9.17

2 years ago

0.9.15

2 years ago

0.9.12

2 years ago

0.9.13

2 years ago

0.9.14

2 years ago

0.9.11

2 years ago

0.9.10

2 years ago

0.9.8

2 years ago

0.9.7

2 years ago

0.9.9

2 years ago

0.9.5

2 years ago

0.9.4

2 years ago