Expand description
AI service integration for intelligent subtitle matching and content analysis.
This module provides a comprehensive AI service abstraction layer for SubX’s intelligent content analysis capabilities. It enables AI-powered subtitle-video file matching through semantic analysis, content understanding, and confidence scoring across multiple AI service providers.
§Architecture Overview
The AI service layer is built around a provider pattern that supports:
- Multi-Provider Support: OpenAI, Anthropic, and other AI backends
- Content Analysis: Deep understanding of video and subtitle content
- Semantic Matching: Intelligent file pairing beyond filename similarity
- Confidence Scoring: Quantitative match quality assessment
- Caching Layer: Persistent caching of expensive AI analysis results
- Retry Logic: Robust error handling with exponential backoff
§Core Capabilities
§Content Analysis Engine
- Video Metadata Extraction: Title, series, episode, language detection
- Subtitle Content Analysis: Dialogue patterns, character names, themes
- Cross-Reference Matching: Semantic similarity between content types
- Language Identification: Automatic detection and verification
- Quality Assessment: Content quality scoring and recommendations
§Intelligent Matching Algorithm
- Content Sampling: Extract representative samples from subtitle files
- Metadata Analysis: Parse video filenames and directory structures
- Semantic Analysis: AI-powered content understanding and comparison
- Confidence Scoring: Multi-factor confidence calculation
- Conflict Resolution: Resolve ambiguous matches with user preferences
- Verification: Optional human-in-the-loop verification workflow
§Provider Management
- Dynamic Provider Selection: Choose optimal provider based on content type
- Automatic Failover: Seamless fallback between service providers
- Cost Optimization: Smart routing to minimize API usage costs
- Rate Limiting: Respect provider-specific rate limits and quotas
- Usage Tracking: Detailed usage statistics and cost monitoring
§Usage Examples
§Basic Content Analysis
ⓘ
use subx_cli::core::ComponentFactory;
use subx_cli::config::ProductionConfigService;
use subx_cli::Result;
use std::sync::Arc;
async fn analyze_content() -> Result<()> {
// Create AI client using component factory
let config_service = Arc::new(ProductionConfigService::new()?);
let factory = ComponentFactory::new(config_service.as_ref())?;
let ai_client = factory.create_ai_provider()?;
// AI client is ready for content analysis
println!("AI client created and configured");
Ok(())
}§Match Verification Workflow
ⓘ
use subx_cli::services::ai::{AIProvider, VerificationRequest};
async fn verify_matches(ai_client: Box<dyn AIProvider>) -> Result<()> {
let verification = VerificationRequest {
video_file: "movie.mp4".to_string(),
subtitle_file: "movie_subtitles.srt".to_string(),
match_factors: vec![
"title_similarity".to_string(),
"content_correlation".to_string(),
],
};
let confidence = ai_client.verify_match(verification).await?;
if confidence.score > 0.9 {
println!("Verification successful: {:.2}%", confidence.score * 100.0);
} else {
println!("Verification failed. Factors: {:?}", confidence.factors);
}
Ok(())
}§Advanced Provider Configuration
ⓘ
use subx_cli::core::ComponentFactory;
use subx_cli::config::ProductionConfigService;
use std::sync::Arc;
async fn configure_ai_services() -> Result<()> {
// Create component factory with configuration service
let config_service = Arc::new(ProductionConfigService::new()?);
let factory = ComponentFactory::new(config_service.as_ref())?;
// Create AI client with factory-injected configuration
let client = factory.create_ai_provider()?;
// Use configured client...
println!("AI client configured with all settings from config service");
Ok(())
}§Performance Characteristics
§Processing Speed
- Analysis Time: 2-5 seconds per content analysis request
- Batch Processing: Concurrent processing of multiple file pairs
- Caching Benefits: 10-100x speedup for cached results
- Network Latency: Optimized for high-latency connections
§Resource Usage
- Memory Footprint: ~50-200MB for typical analysis sessions
- API Costs: $0.001-0.01 per analysis depending on content size
- Cache Storage: ~1-10KB per cached analysis result
- Network Bandwidth: 1-50KB per API request
§Accuracy Metrics
- Match Accuracy: >95% for properly named content
- False Positive Rate: <2% with confidence threshold >0.8
- Language Detection: >99% accuracy for supported languages
- Content Understanding: Context-aware matching for complex scenarios
§Error Handling and Recovery
The AI service layer provides comprehensive error handling:
- Network Failures: Automatic retry with exponential backoff
- API Rate Limits: Intelligent backoff and queue management
- Service Unavailability: Graceful fallback to alternative providers
- Invalid Responses: Response validation and error recovery
- Timeout Handling: Configurable timeout with partial result recovery
§Security and Privacy
- Data Privacy: Content samples are processed with privacy-focused prompts
- API Key Management: Secure credential storage and rotation
- Content Filtering: No permanent storage of user content on AI providers
- Request Sanitization: Input validation and safe prompt construction
Re-exports§
pub use cache::AICache;pub use openai::OpenAIClient;pub use retry::RetryConfig;pub use retry::retry_with_backoff;
Modules§
- azure_
openai - Azure OpenAI service provider client implementation
- cache
- Caching functionality for AI analysis results AI analysis result caching system for performance optimization.
- openai
- OpenAI integration and client implementation
- openrouter
- OpenRouter AI service provider client implementation
- prompts
- AI prompt templates and management
- retry
- Retry logic and backoff strategies for AI services
Structs§
- AiResponse
- AI response content and usage statistics.
- AiUsage
Stats - AI usage statistics.
- Analysis
Request - Analysis request structure for AI content analysis.
- Confidence
Score - Confidence score for AI matching decisions.
- Content
Sample - Subtitle content sample for AI analysis.
- File
Match - Individual file match information using unique file IDs.
- Match
Result - AI analysis result containing potential file matches.
- Verification
Request - Verification request structure for AI validation.
Traits§
- AIProvider
- AI provider trait for content analysis and subtitle matching.