Skip to content

APiGen Recommendation

ML-based recommendation engine module for APiGen applications.

Purpose

Provides a complete recommendation system with collaborative filtering, content-based filtering, hybrid algorithms, A/B testing, and ML model training infrastructure.

Features

Recommendation Algorithms

  • Collaborative Filtering
    • User-based collaborative filtering
    • Item-based collaborative filtering
    • Matrix factorization (SVD/ALS)
    • Popularity-based (cold start fallback)
  • Content-Based Filtering
    • Feature-based similarity
    • TF-IDF text content filtering
  • Hybrid Algorithms
    • Weighted hybrid (combines multiple algorithms)
    • Switching hybrid (selects algorithm based on user profile)
    • Cascade hybrid (refines results through multiple stages)

Similarity Metrics

  • Cosine similarity
  • Pearson correlation
  • Jaccard similarity

A/B Testing Framework

  • Experiment creation and management
  • Variant traffic allocation
  • User bucketing (deterministic assignment)
  • Statistical analysis (CTR, conversion rate, revenue per user)
  • Real-time metrics tracking

ML Model Training

  • Training data provider
  • Model versioning and storage
  • Async training pipeline
  • Event-driven architecture

Caching

  • Caffeine-based caching
  • Per-user cache invalidation
  • Configurable TTL

Usage

Gradle:

groovy
dependencies {
    implementation 'com.jnzader:apigen-recommendation:1.0.0-SNAPSHOT'
}

Maven:

xml
<dependency>
    <groupId>com.jnzader</groupId>
    <artifactId>apigen-recommendation</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

Recommendation Endpoints

MethodEndpointDescription
POST/api/v1/recommendationsGet personalized recommendations
GET/api/v1/recommendations/user/{userId}Get recommendations for user
POST/api/v1/recommendations/asyncGet recommendations asynchronously
POST/api/v1/recommendations/similar-itemsFind similar items
GET/api/v1/recommendations/similar-items/{itemId}Find similar items (GET)
POST/api/v1/recommendations/clicksRecord recommendation click
POST/api/v1/recommendations/conversionsRecord conversion
GET/api/v1/recommendations/statisticsGet performance statistics
POST/api/v1/recommendations/cache/invalidate/user/{userId}Invalidate user cache
POST/api/v1/recommendations/cache/invalidateInvalidate all caches

Experiment Endpoints

MethodEndpointDescription
POST/api/v1/experimentsCreate experiment
GET/api/v1/experiments/runningGet running experiments
GET/api/v1/experiments/{experimentId}Get experiment by ID
GET/api/v1/experiments/name/{name}Get experiment by name
POST/api/v1/experiments/{experimentId}/startStart experiment
POST/api/v1/experiments/{experimentId}/pausePause experiment
POST/api/v1/experiments/{experimentId}/resumeResume experiment
POST/api/v1/experiments/{experimentId}/stopStop experiment
POST/api/v1/experiments/{experimentId}/completeComplete experiment
GET/api/v1/experiments/{experimentName}/user/{userId}/variantGet user's variant
GET/api/v1/experiments/user/{userId}/variantsGet all user variants
POST/api/v1/experiments/{experimentName}/user/{userId}/impressionRecord impression
POST/api/v1/experiments/{experimentName}/user/{userId}/clickRecord click
POST/api/v1/experiments/{experimentName}/user/{userId}/conversionRecord conversion
GET/api/v1/experiments/{experimentId}/analysisGet statistical analysis
GET/api/v1/experiments/{experimentId}/statisticsGet variant statistics
POST/api/v1/experiments/{experimentId}/resultSet experiment result

Algorithm Types

java
public enum AlgorithmType {
    COLLABORATIVE_USER_BASED,      // User-based CF
    COLLABORATIVE_ITEM_BASED,      // Item-based CF
    COLLABORATIVE_MATRIX_FACTORIZATION, // SVD/ALS
    CONTENT_BASED,                 // Feature-based
    CONTENT_BASED_TFIDF,           // TF-IDF text
    HYBRID_WEIGHTED,               // Weighted combination
    HYBRID_SWITCHING,              // Dynamic selection
    HYBRID_CASCADE,                // Multi-stage refinement
    POPULARITY_BASED,              // Popular items
    RANDOM,                        // A/B testing baseline
    ASSOCIATION_RULES,             // Frequently bought together
    KNOWLEDGE_BASED,               // Rule-based
    DEEP_LEARNING                  // Neural network based
}

Entities

  • Recommendation - Stored recommendation with metadata
  • UserInteraction - User interaction event (view, click, purchase)
  • ItemFeature - Item attributes for content-based filtering
  • Experiment - A/B testing experiment configuration
  • ExperimentVariant - Experiment variant with algorithm and traffic
  • RecommendationModel - Trained ML model metadata

Example Usage

Getting Recommendations

java
@Autowired
private RecommendationService recommendationService;

// Get recommendations with context
RecommendationContext context = RecommendationContext.builder()
    .userId("user123")
    .itemType("product")
    .maxResults(10)
    .build();

RecommendationResult result = recommendationService.getRecommendations(context);

// Get recommendations with specific algorithm
RecommendationResult result = recommendationService.getRecommendations(
    context,
    AlgorithmType.HYBRID_WEIGHTED
);

Creating an A/B Test

java
@Autowired
private ExperimentService experimentService;

// Create experiment
Experiment experiment = experimentService.createExperiment(
    "recommendation_algorithm_test",
    "Testing user-based vs item-based CF",
    "Item-based CF will have higher CTR",
    BigDecimal.valueOf(100),  // 100% traffic
    "data-science-team"
);

// Add control variant
experimentService.addVariant(
    experiment.getId(),
    "control",
    "User-based CF",
    BigDecimal.valueOf(50),
    true,  // isControl
    AlgorithmType.COLLABORATIVE_USER_BASED,
    null
);

// Add treatment variant
experimentService.addVariant(
    experiment.getId(),
    "treatment",
    "Item-based CF",
    BigDecimal.valueOf(50),
    false,
    AlgorithmType.COLLABORATIVE_ITEM_BASED,
    null
);

// Start experiment
experimentService.startExperiment(experiment.getId());

Recording Interactions

java
@Autowired
private UserInteractionService interactionService;

// Record a view
interactionService.recordView("user123", "product456");

// Record a purchase
interactionService.recordPurchase("user123", "product456", BigDecimal.valueOf(99.99));

Dependencies

This module requires:

  • apigen-core - Base services and Result pattern
  • Spring Boot 4.0.2+
  • Java 25+
  • SMILE ML library for matrix factorization
  • Apache Commons Text for TF-IDF
  • Caffeine for caching

See main README for complete documentation.

Released under the MIT License.