2.48.0 • Published 6 months ago

@memberjunction/ai-recommendations v2.48.0

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

@memberjunction/ai-recommendations

The MemberJunction Recommendations Engine provides a flexible and extensible framework for integrating with AI-powered recommendation systems. It handles the orchestration of recommendation requests, provider management, and the storage of recommendation results within the MemberJunction ecosystem.

Features

  • Extensible Provider Framework: Support for multiple recommendation providers through a unified API
  • Built-in Tracking: Automatic tracking of recommendation runs and results
  • Entity Integration: Seamless integration with MemberJunction entities and records
  • List Support: Generate recommendations for records in a list
  • Error Handling: Comprehensive error tracking and reporting
  • Batch Processing: Process multiple recommendation requests efficiently
  • Metadata Management: Automatic management of recommendation metadata

Installation

npm install @memberjunction/ai-recommendations

Core Components

RecommendationEngineBase

The main engine class that coordinates recommendation requests:

import { RecommendationEngineBase, RecommendationRequest } from '@memberjunction/ai-recommendations';

// Initialize and load the engine
await RecommendationEngineBase.Instance.Config();

// Create a recommendation request
const request = new RecommendationRequest();

// Configure the request
request.EntityAndRecordsInfo = {
  EntityName: 'Customers',
  RecordIDs: ['CUST001', 'CUST002']
};

// Execute the recommendation
const result = await RecommendationEngineBase.Instance.Recommend(request);

RecommendationProviderBase

Abstract base class for implementing recommendation providers:

import { RecommendationProviderBase, RecommendationRequest, RecommendationResult } from '@memberjunction/ai-recommendations';
import { UserInfo } from '@memberjunction/core';
import { RecommendationItemEntity } from '@memberjunction/core-entities';

export class MyRecommendationProvider extends RecommendationProviderBase {
  constructor(contextUser: UserInfo) {
    super(contextUser);
  }

  public async Recommend(request: RecommendationRequest): Promise<RecommendationResult> {
    const result = new RecommendationResult(request);
    
    try {
      // Process each recommendation
      for (const recommendation of request.Recommendations) {
        // Generate items for this recommendation
        const items: RecommendationItemEntity[] = [];
        
        // Your recommendation logic here
        // ...
        
        // Save the recommendation and its items
        await this.SaveRecommendation(recommendation, request.RunID, items);
      }
    } catch (error) {
      result.AppendError(error.message);
    }
    
    return result;
  }
}

Usage Examples

Generate Recommendations for Specific Records

import { RecommendationEngineBase, RecommendationRequest } from '@memberjunction/ai-recommendations';

async function getCustomerRecommendations(customerIds: string[]) {
  // Ensure the engine is configured
  await RecommendationEngineBase.Instance.Config();
  
  // Create a request for specific customer records
  const request = new RecommendationRequest();
  request.EntityAndRecordsInfo = {
    EntityName: 'Customers',
    RecordIDs: customerIds
  };
  
  // Optionally specify a provider
  // request.Provider = RecommendationEngineBase.Instance.RecommendationProviders.find(p => p.Name === 'MyProvider');
  
  // Execute the recommendation
  const result = await RecommendationEngineBase.Instance.Recommend(request);
  
  if (result.Success) {
    console.log('Recommendations generated successfully!');
    return result.RecommendationItems;
  } else {
    console.error('Error generating recommendations:', result.ErrorMessage);
    return null;
  }
}

Generate Recommendations from a List

import { RecommendationEngineBase, RecommendationRequest } from '@memberjunction/ai-recommendations';

async function getRecommendationsFromList(listId: string) {
  await RecommendationEngineBase.Instance.Config();
  
  // Create a request for records in a list
  const request = new RecommendationRequest();
  request.ListID = listId;
  request.CreateErrorList = true; // Create a list to track errors
  
  // Execute the recommendation
  const result = await RecommendationEngineBase.Instance.Recommend(request);
  
  if (result.Success) {
    console.log('Recommendations generated successfully!');
    console.log('Error list ID (if needed):', result.Request.ErrorListID);
    return result.RecommendationItems;
  } else {
    console.error('Error generating recommendations:', result.ErrorMessage);
    return null;
  }
}

Using Advanced Options

import { RecommendationEngineBase, RecommendationRequest } from '@memberjunction/ai-recommendations';

// Define a custom options interface for your provider
interface MyProviderOptions {
  similarityThreshold: number;
  maxRecommendations: number;
  includeRatings: boolean;
}

async function getCustomizedRecommendations(customerId: string) {
  await RecommendationEngineBase.Instance.Config();
  
  // Create a request with custom options
  const request = new RecommendationRequest<MyProviderOptions>();
  request.EntityAndRecordsInfo = {
    EntityName: 'Customers',
    RecordIDs: [customerId]
  };
  
  // Add provider-specific options
  request.Options = {
    similarityThreshold: 0.75,
    maxRecommendations: 5,
    includeRatings: true
  };
  
  // Execute the recommendation
  return await RecommendationEngineBase.Instance.Recommend(request);
}

Recommendation Flow

The recommendation process follows these steps:

  1. Request Creation: A RecommendationRequest is created with entity records or a list
  2. Run Tracking: A RecommendationRun entity is created to track the process
  3. Provider Selection: The appropriate recommendation provider is selected
  4. Recommendation Generation: The provider generates recommendations for each requested record
  5. Result Storage: Recommendations and items are saved to the database
  6. Status Update: The run status is updated (completed or error)
  7. Result Return: The RecommendationResult is returned to the caller

Data Model

The recommendation engine works with these key entities:

  • RecommendationProviderEntity: Configuration for recommendation providers
  • RecommendationRunEntity: Tracks each recommendation execution
  • RecommendationEntity: Represents a recommendation for a source record
  • RecommendationItemEntity: Individual recommendation items (products, content, etc.)

Provider Implementation

To create a custom recommendation provider:

  1. Create a class that extends RecommendationProviderBase
  2. Implement the Recommend method to generate recommendations
  3. Register your provider implementation:
// Register your provider with MemberJunction's class factory
import { MJGlobal } from '@memberjunction/global';
import { RecommendationProviderBase } from '@memberjunction/ai-recommendations';

MJGlobal.Instance.ClassFactory.RegisterClass(
  RecommendationProviderBase,
  'MyRecommendationProvider',
  MyRecommendationProvider
);

Integration with MemberJunction

This package integrates with the MemberJunction ecosystem:

  • Uses MemberJunction entities for data storage and retrieval
  • Works with MemberJunction lists for batch processing
  • Leverages MemberJunction's metadata system

Dependencies

  • @memberjunction/global: MemberJunction global utilities
  • @memberjunction/core: MemberJunction core library
  • @memberjunction/core-entities: MemberJunction entity definitions
  • @memberjunction/ai: MemberJunction AI abstractions

License

ISC

2.27.1

9 months ago

2.23.2

10 months ago

2.46.0

6 months ago

2.23.1

10 months ago

2.27.0

9 months ago

2.34.0

7 months ago

2.30.0

9 months ago

2.19.4

10 months ago

2.19.5

10 months ago

2.19.2

10 months ago

2.19.3

10 months ago

2.19.0

10 months ago

2.19.1

10 months ago

2.15.2

11 months ago

2.34.2

7 months ago

2.15.0

11 months ago

2.34.1

7 months ago

2.38.0

7 months ago

2.45.0

6 months ago

2.22.1

10 months ago

2.22.0

10 months ago

2.41.0

6 months ago

2.22.2

10 months ago

2.26.1

9 months ago

2.26.0

9 months ago

2.33.0

7 months ago

2.18.3

11 months ago

2.18.1

11 months ago

2.18.2

11 months ago

2.18.0

11 months ago

2.37.1

7 months ago

2.37.0

7 months ago

2.14.0

11 months ago

2.21.0

10 months ago

2.44.0

6 months ago

2.40.0

7 months ago

2.29.0

9 months ago

2.29.2

9 months ago

2.29.1

9 months ago

2.25.0

10 months ago

2.48.0

6 months ago

2.32.0

8 months ago

2.32.2

8 months ago

2.32.1

8 months ago

2.17.0

11 months ago

2.13.4

11 months ago

2.36.0

7 months ago

2.13.2

12 months ago

2.13.3

11 months ago

2.13.0

1 year ago

2.36.1

7 months ago

2.13.1

1 year ago

2.43.0

6 months ago

2.20.2

10 months ago

2.20.3

10 months ago

2.20.0

10 months ago

2.20.1

10 months ago

2.28.0

9 months ago

2.47.0

6 months ago

2.24.1

10 months ago

2.24.0

10 months ago

2.31.0

8 months ago

2.12.0

1 year ago

2.39.0

7 months ago

2.16.1

11 months ago

2.35.1

7 months ago

2.35.0

7 months ago

2.16.0

11 months ago

2.42.1

6 months ago

2.42.0

6 months ago

2.23.0

10 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.7.0

1 year ago

2.6.1

1 year ago

2.5.2

1 year ago

2.6.0

1 year ago

2.7.1

1 year ago

2.5.1

1 year ago

2.5.0

1 year ago

2.4.1

1 year ago

2.4.0

1 year ago

2.3.3

1 year ago

2.3.2

1 year ago

2.3.1

1 year ago

2.3.0

1 year ago

2.2.1

1 year ago

2.2.0

1 year ago

2.1.5

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.0.0

1 year ago

1.8.1

1 year ago

1.8.0

1 year ago

1.7.1

1 year ago

1.7.0

1 year ago

1.6.1

1 year ago

1.6.0

1 year ago

1.5.3

1 year ago