AI Model Manager & Integration
Relevant source files
The following files were used as context for generating this wiki page:
The AI Model Manager & Integration system serves as the intelligence backbone of MarketSage. It orchestrates multiple machine learning models and heuristic engines to provide real-time scam detection, price forecasting, and natural language shopping assistance. By leveraging TensorFlow.js and custom heuristic analyzers, the system processes marketplace data to protect users and optimize purchasing decisions.
This module is responsible for the lifecycle of AI assets, including model loading, training, caching, and execution. It integrates directly with the background service worker for efficient resource management and provides unified interfaces for various frontend components.
Sources: src/common/models/scamDetectionModel.ts, src/core/ai/PricePredictionEngine.ts:1-20, src/service-worker.ts:1-15
Architecture and Core Engines
The system utilizes a multi-layered analysis approach. High-level services like the SecurityAnalyzer and SmartShoppingAssistant call upon specialized engines to process specific data facets such as price history, image quality, and seller reputation.
Scam Detection Framework
The ScamDetectionModel acts as an orchestrator for several specialized detection engines. It aggregates scores from these engines based on user-defined weights and global sensitivity thresholds.
The diagram above shows the orchestration flow where ScamDetectionModel aggregates data from multiple specialized heuristic engines.
Sources: src/common/models/scamDetectionModel.ts:15-50, src/background/services/protect/securityAnalyzer.ts:35-50
Price Prediction Engine
The PricePredictionEngine utilizes a TensorFlow.js sequential model to forecast market trends. It extracts 15 distinct features, including 7-day price changes, market volatility, and seasonal factors, to generate buy/wait recommendations.
| Feature | Description |
|---|---|
| Model Type | Dense Neural Network (Sequential) |
| Input Shape | [15] (Price history, market trends, etc.) |
| Activation | ReLU (Hidden layers), Linear (Output) |
| Optimizer | Adam (Learning rate: 0.001) |
| Loss Function | Mean Squared Error |
Sources: src/core/ai/PricePredictionEngine.ts:60-100, src/core/ai/PricePredictionEngine.ts:156-175
AI Resource Management
Models are managed via a specialized lifecycle that includes pre-caching and background training.
Model Caching and Loading
The service worker manages a specific MODEL_CACHE to store model.json files and weights. This ensures that heavy AI assets are available offline and do not block the main execution thread.
Continuous Training
The PricePredictionEngine supports on-device training. It prepares training data from local storage and performs 50 epochs of training with a 0.2 validation split, provided at least 100 data points are available.
Sources: src/core/ai/PricePredictionEngine.ts:225-255
Data Processing and Heuristics
The system converts raw marketplace data into actionable insights through various heuristic analyzers.
Review Pattern Analysis
The ReviewAnalyzer detects synthetic feedback by identifying:
- Review Spikes: Unusual volume increases in short timeframes.
- Sentiment Inconsistency: Mismatches between text sentiment and numerical ratings.
- Repetitive Text: Multiple reviews sharing identical or near-identical content.
- Verified Purchase Ratio: The proportion of reviews from confirmed buyers.
Sources: src/common/models/scamDetection/reviewAnalyzer.ts:4-35
Security Metrics Calculation
The SecurityAnalyzer calculates an overall risk score using weighted factors:
- Seller History: 40% weight
- Price Anomaly: 30% weight
- Listing Quality: 30% weight (includes text scam keyword detection)
private static readonly RISK_WEIGHTS = {
SELLER_HISTORY: 0.4,
PRICE_ANOMALY: 0.3,
LISTING_QUALITY: 0.3,
TEXT_SCAM_KEYWORDS: 0.3,
};
Sources: src/background/services/protect/securityAnalyzer.ts:13-30
Smart Shopping Assistant Integration
The SmartShoppingAssistant provides a natural language interface for the AI systems. It uses Regex-based Natural Language Processing (NLP) to extract user intent and entities, which are then passed to the specialized engines.
| Intent Name | Pattern Matchers | Target Action |
|---|---|---|
find_product | find, search, look for, buy | handleProductSearch |
price_inquiry | price, cost, budget, cheap | handlePriceInquiry |
safety_check | safe, trust, scam, fake | handleSafetyCheck |
compare_products | compare, vs, difference | handleProductComparison |
Sources: src/core/ai/SmartShoppingAssistant.ts:98-130, src/core/ai/SmartShoppingAssistant.ts:165-190
Conclusion
The AI Model Manager & Integration module provides a robust foundation for marketplace intelligence. By combining neural networks for price prediction with diverse heuristic engines for scam detection, it ensures that MarketSage users receive accurate, data-driven insights. The modular design allows for the independent scaling of analysis engines while maintaining a unified interface for the end-user via the Smart Shopping Assistant.