Skip to main content

Hugging Face Integration with Adaptive Model Selection

This document outlines the integration of Hugging Face's machine learning services into the Kai platform. The implementation includes an innovative adaptive model selection system that works alongside existing AI providers (OpenAI, Anthropic) to automatically select the best-performing model for each specific task.

Overview

The Hugging Face integration extends the Kai platform's AI capabilities beyond dataset management, providing comprehensive ML services that include:

  1. Text Generation: Access to a wide range of transformer models
  2. Embedding Generation: Vector representations for search and similarity
  3. Image Analysis: Classification, object detection, and segmentation
  4. Classification: Category prediction for materials and metadata
  5. Provider-Agnostic Interface: Seamless operation with other AI providers

Architecture

The implementation follows a provider-agnostic architecture with several key components:

Core Components

  1. HuggingFaceProvider Service

    • Interfaces with Hugging Face's APIs
    • Handles authentication and configuration
    • Provides unified access to HF's ML capabilities
    • Manages caching and performance optimization
  2. ModelRegistry Service

    • Tracks performance metrics for all models across providers
    • Maintains historical performance data
    • Provides model selection based on performance criteria
    • Supports different selection strategies (accuracy, latency, cost)
  3. ModelRouter System

    • Routes AI requests to the optimal model
    • Implements rotation-based evaluation
    • Provides fallback mechanisms for service unavailability
    • Handles cross-provider compatibility

Rotation-Based Evaluation System

The implementation includes an innovative model evaluation mechanism:

  1. Standard Operation - During normal operation, each task is routed to the historically best-performing model
  2. Evaluation Mode - After every 10 tasks of a particular type, the system enters evaluation mode
  3. Multi-Model Testing - The next 3 tasks are executed across all available models simultaneously
  4. Comparative Analysis - Results are compared, metrics updated, and model rankings adjusted

This creates a self-improving system that continuously identifies the best models for each specific task type without requiring manual intervention.

Implementation Details

1. HuggingFaceProvider Service

The provider service exposes various ML capabilities through a unified interface:

Text Generation

interface TextGenerationOptions {
model?: string;
maxLength?: number;
temperature?: number;
topK?: number;
topP?: number;
repetitionPenalty?: number;
}

interface TextGenerationResult {
text: string;
model: string;
finishReason: string;
processingTime: number;
}

Embedding Generation

interface EmbeddingOptions {
model?: string;
truncate?: boolean;
normalize?: boolean;
encoderType?: 'text' | 'image' | 'multimodal';
}

interface EmbeddingResult {
embedding: number[];
dimensions: number;
model: string;
processingTime: number;
}

Image Analysis

interface ImageAnalysisOptions {
model?: string;
task?: 'object-detection' | 'image-classification' | 'image-segmentation';
}

2. ModelRegistry Service

The registry tracks and analyzes model performance:

interface ModelPerformanceMetrics {
// Quality metrics
accuracy?: number;
relevance?: number;

// Operational metrics
latencyMs: number;
processingTimeMs: number;
tokenCount?: number;
costPerRequest?: number;

// Usage metrics
requestCount: number;
errorCount: number;
userFeedbackScore?: number;

// Timestamp
updatedAt: Date;
}

3. ModelRouter System

The router implements the logic for model selection and evaluation:

interface ModelRoutingOptions {
taskType: string;
prioritize?: 'speed' | 'quality' | 'cost' | 'balanced';
maxLatencyMs?: number;
minQualityScore?: number;
maxCost?: number;
userContext?: any;
}

API Endpoints

The Hugging Face integration is exposed through the following RESTful API endpoints:

EndpointMethodDescription
/api/ai/text/generatePOSTGenerate text using optimal model
/api/ai/embedding/generatePOSTGenerate embeddings for vector search
/api/ai/image/analyzePOSTAnalyze images (classification, detection)
/api/ai/models/listGETList available AI models
/api/ai/models/metricsGETGet performance metrics for AI models
/api/ai/evaluation/setPOSTConfigure the evaluation system

Usage Examples

Text Generation

// Using standard route that selects best model automatically
const response = await fetch('/api/ai/text/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: 'Explain the properties of porcelain tiles compared to ceramic tiles',
maxLength: 300,
temperature: 0.7
})
});

const result = await response.json();
console.log(result.text);

Embedding Generation

// Generate embeddings for vector search
const response = await fetch('/api/ai/embedding/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: 'White marble with subtle grey veining',
encoderType: 'text',
normalize: true
})
});

const result = await response.json();
// Use the embedding vector for similarity search
const embedding = result.embedding;

Image Analysis

// Analyze an image for material properties
const formData = new FormData();
formData.append('image', fileInput.files[0]);
formData.append('task', 'image-classification');
formData.append('detailLevel', 'high');

const response = await fetch('/api/ai/image/analyze', {
method: 'POST',
body: formData
});

const result = await response.json();
console.log(result.classifications);

Configuration

Environment Variables

The Hugging Face integration can be configured using the following environment variables:

# Hugging Face Configuration
HF_API_KEY=your_huggingface_api_key
HF_ORGANIZATION_ID=your_organization_id (optional)
HF_DEFAULT_TEXT_MODEL=google/flan-t5-xxl
HF_DEFAULT_EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L6-v2
HF_DEFAULT_IMAGE_MODEL=google/vit-base-patch16-224
HF_MODEL_TIMEOUT=30000
HF_USE_FAST_MODELS=true

# Evaluation System Configuration
MODEL_EVALUATION_STANDARD_CYCLE=10
MODEL_EVALUATION_TEST_CYCLE=3
MODEL_SELECTION_METRICS_WEIGHTS=

Model Selection Strategy

The system supports different model selection strategies:

  1. Balanced (default): Considers accuracy, latency, and cost equally
  2. Quality-First: Prioritizes accuracy and relevance over performance
  3. Speed-First: Prioritizes low latency for time-sensitive applications
  4. Cost-Efficient: Prioritizes lower cost models when appropriate

These strategies can be set globally or specified per request.

Integration with Other Platform Components

Vector Search Integration

The Hugging Face embedding models can be used with the platform's vector search capabilities:

// Generate embedding using Hugging Face
const embeddingResult = await fetch('/api/ai/embedding/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify()
});

const = await embeddingResult.json();

// Use embedding for vector search
const searchResult = await fetch('/api/materials/vector-search', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
vector: embedding,
limit: 5,
threshold: 0.7
})
});

const similarMaterials = await searchResult.json();

Agent System Integration

The Hugging Face models can be used by the agent system for various tasks:

import  from '@kai/agents';

// Initialize agent system with Hugging Face support
await initializeAgentSystem({
providers: {
huggingface: {
apiKey: process.env.HF_API_KEY,
defaultModels: {
text: 'google/flan-t5-xxl',
embedding: 'sentence-transformers/all-MiniLM-L6-v2'
}
},
openai: {
apiKey: process.env.OPENAI_API_KEY
}
},
modelSelection: {
enableAdaptiveSelection: true,
standardCycleLength: 10,
evaluationCycleLength: 3
}
});

Benefits and Impact

The Hugging Face integration with adaptive model selection provides several advantages:

  1. Provider Flexibility: Reduced dependency on any single AI provider
  2. Specialized Models: Access to domain-specific models not available in general-purpose APIs
  3. Cost Optimization: Ability to use more cost-effective models for specific tasks
  4. Performance Optimization: Automatic selection of best-performing models for each task
  5. Continuous Improvement: Self-improving system that adapts to model changes
  6. Open Source Options: Ability to use open-source models for flexibility and control

Extension Points

The system is designed for easy extension:

  1. New Providers: Implement the provider interface to add new AI services
  2. Custom Tasks: Extend the task type definition for specialized use cases
  3. Evaluation Metrics: Add custom metrics for domain-specific evaluation
  4. Selection Strategies: Implement custom strategy classes for specific needs
  5. Caching Mechanisms: Configure different caching strategies by task type

Installation and Setup

For installation and setup instructions, see the Deployment Guide.