A sophisticated multi-agent system leveraging serverless architecture and ensemble learning for intelligent price predictions and deal discovery.
- Introduction
- System Architecture
- Core Components
- Technical Implementation
- Setup & Installation
- Usage Guide
Agentic AI is a cutting-edge price intelligence system that combines multiple specialized AI agents working in harmony. The system employs:
- 🧠 Fine-tuned LLaMA 3.1 model using QLoRA
- 🔍 RAG (Retrieval Augmented Generation) with ChromaDB
- 🌲 Random Forest ML with vector embeddings
- 🤝 Weighted ensemble learning
- 🔄 Real-time deal scanning and analysis
- 📊 Beautiful Gradio-based UI
Multi-Agent System Architecture
The system is built on a modular agent framework where each agent specializes in specific tasks:
- Base Agent Class
class Agent: def __init__(self): self.name = self.__class__.__name__ self.logger = setup_logger(self.name) async def process(self, message): try: return await self._handle_message(message) except Exception as e: self.logger.error(f"Error processing message: {e}") raise
The system follows a sophisticated data flow pattern:
- Input Processing
Loading
graph LR A[Product Description] --> B[Scanner Agent] B --> C[Vector Embedding] C --> D[Price Analysis] D --> E[Deal Detection]
The RAG pipeline enhances price predictions with contextual information:
-
Vector Database
- ChromaDB for similarity search
- 384-dimensional embeddings
- Price and metadata storage
-
Retrieval Process
def retrieve_similar(description: str, k: int = 5): embedding = encode_text(description) results = vector_db.query( embedding, n_results=k, include=['metadata', 'distance'] ) return process_results(results)
LLaMA 3.1 Training Process with QLoRA
The Specialist Agent is built on a fine-tuned LLaMA 3.1 model, optimized specifically for price prediction tasks. The fine-tuning process employs QLoRA (Quantized Low-Rank Adaptation), a technique that enables efficient model adaptation while maintaining high performance.
Theory & Implementation:
-
QLoRA Fine-tuning
- Uses 4-bit quantization to reduce memory footprint
- Applies low-rank adaptation matrices to key model layers
- Maintains model quality while reducing training resources
- Enables efficient deployment on consumer GPUs
-
Training Process
- Custom prompt engineering for price prediction
- Structured output format for consistent pricing
- Context window optimization for product descriptions
- Training on curated e-commerce dataset
class SpecialistAgent(Agent):
def __init__(self):
self.model = load_quantized_model(
"llama-3.1",
quantization="4bit",
device_map="auto"
)
def predict_price(self, description: str) -> float:
prompt = self.create_price_prompt(description)
response = self.model.generate(
prompt,
max_new_tokens=50,
temperature=0.7
)
return self.extract_price(response)
Retrieval Augmented Generation Pipeline
The Frontier Agent implements a sophisticated RAG (Retrieval Augmented Generation) pipeline that combines GPT-4's reasoning capabilities with a vector database of historical product data.
Theory & Implementation:
-
Vector Similarity Search
- Uses Sentence Transformers for embedding generation
- ChromaDB for efficient similarity matching
- Cosine similarity for semantic matching
- Top-k retrieval with dynamic filtering
-
Context Engineering
- Intelligent prompt construction with similar products
- Price-aware context formatting
- Confidence scoring for retrieved matches
- Dynamic context window optimization
def create_context(self, similar_products):
context = []
for product in similar_products:
context.append({
"description": product.description,
"price": product.price,
"similarity": product.score
})
return format_context(context)
def predict_with_context(self, description: str, context: str):
prompt = f"""Given the following similar products and their prices:
{context}
Estimate the price for: {description}
Consider the similarities and differences between the products."""
return self.gpt4_client.complete(prompt)
Vector Space Representation of Products
The Random Forest Agent employs traditional machine learning techniques enhanced with modern NLP embeddings to provide robust price predictions based on historical data.
Theory & Implementation:
-
Feature Engineering
- Sentence transformer embeddings (384 dimensions)
- Product metadata incorporation
- Categorical feature encoding
- Numerical feature normalization
-
Model Architecture
- Random Forest with 100 estimators
- Optimized tree depth for generalization
- Feature importance analysis
- Cross-validation for robustness
class RandomForestAgent(Agent):
def __init__(self):
self.embedder = SentenceTransformer('all-MiniLM-L6-v2')
self.model = RandomForestRegressor(
n_estimators=100,
max_depth=None,
min_samples_split=2
)
def create_features(self, description: str):
# Generate embeddings
embedding = self.embedder.encode(description)
# Add additional features
features = np.concatenate([
embedding,
self.extract_metadata_features(description)
])
return features
Multi-Model Ensemble Architecture
The Ensemble Agent combines predictions from all three specialized agents using a sophisticated weighting mechanism that accounts for each model's strengths and confidence levels.
Theory & Implementation:
-
Model Weighting
- Dynamic weight assignment
- Confidence-based adjustment
- Historical performance tracking
- Specialized domain expertise
-
Aggregation Strategy
- Weighted average computation
- Outlier detection and handling
- Confidence threshold filtering
- Error analysis and correction
def ensemble_predict(self, description: str):
predictions = {
'specialist': self.specialist.predict(description),
'frontier': self.frontier.predict(description),
'random_forest': self.rf.predict(description)
}
confidences = self.calculate_confidences(description)
return self.weighted_combine(predictions, confidences)
Deal Detection and Analysis Pipeline
The Scanner Agent continuously monitors various data sources for potential deals, implementing a sophisticated pipeline for deal validation and analysis.
Theory & Implementation:
-
Data Collection
- RSS feed monitoring
- Web scraping integration
- Real-time price tracking
- Data normalization
-
Deal Analysis
- Price comparison logic
- Historical price tracking
- Discount calculation
- Deal validation rules
async def process_deals(self):
deals = await self.fetch_deals()
for deal in deals:
if self.is_valid_deal(deal):
price_estimate = await self.get_price_estimate(deal)
if self.calculate_discount(deal, price_estimate) > 40:
await self.notify_deal(deal)
def calculate_discount(self, deal, estimated_price):
return max(0, estimated_price - deal.price)
Multi-Agent Workflow Orchestration
The Planning Agent acts as the system's orchestrator, coordinating the activities of all other agents and managing the overall workflow of price prediction and deal discovery.
Theory & Implementation:
-
Workflow Management
- Task scheduling and prioritization
- Agent coordination
- State management
- Error handling and recovery
-
Decision Making
- Task decomposition
- Resource allocation
- Pipeline optimization
- Result aggregation
class PlanningAgent(Agent):
async def execute_workflow(self, task):
# Create execution plan
plan = self.create_execution_plan(task)
# Execute steps in sequence
for step in plan:
result = await self.execute_step(step)
self.update_state(result)
return self.summarize_results()
def create_execution_plan(self, task):
return [
Step("scan", self.scanner_agent),
Step("price", self.ensemble_agent),
Step("analyze", self.analyzer_agent),
Step("notify", self.notification_agent)
]The complete system workflow follows these steps:
-
Input Processing
Loadinggraph TD A[User Input] --> B[Planning Agent] B --> C[Scanner Agent] C --> D[Price Analysis] D --> E[Deal Detection] E --> F[Notification]
-
Price Prediction Flow
Loadinggraph LR A[Product] --> B[Vector Embedding] B --> C[Specialist Agent] B --> D[Frontier Agent] B --> E[Random Forest] C & D & E --> F[Ensemble Agent] F --> G[Final Price]
-
Deal Processing
Loadinggraph TD A[New Deal] --> B[Scanner] B --> C[Price Estimation] C --> D[Discount Calculation] D --> E{Threshold Check} E -->|Above $40| F[Notify] E -->|Below $40| G[Skip]
- Environment Setup
conda create -n agentic-ai python=3.8
conda activate agentic-ai
pip install -r requirements.txt- Model Deployment
modal deploy pricer_service.py
modal deploy agent_framework.py-
Basic Usage
from agents import EnsembleAgent agent = EnsembleAgent() price = agent.predict("iPhone 13 Pro Max 256GB") print(f"Estimated Price: ${price:.2f}")
-
Deal Monitoring
from agents import ScannerAgent
scanner = ScannerAgent()
scanner.start_monitoring(
threshold=40,
interval="1h"
)
