Architecture
System architecture and data flow overview
Architecture
OptimaAi follows a layered, service-oriented architecture. Each concern — data ingestion, ML inference, LLM generation, caching, persistence — is isolated into its own module.
High-Level Diagram
┌─────────────────────────────────────────────────────────┐
│ FRONTEND (Next.js) │
│ Dashboard · Charts · KPIs · Recommendations │
└────────────────────────┬────────────────────────────────┘
│ HTTP / REST
┌────────────────────────▼────────────────────────────────┐
│ FASTAPI BACKEND │
│ │
│ /api/v1/predict/* /api/v1/kpis /api/v1/recommend │
│ /api/v1/bmc /api/v1/datasets /api/v1/kb │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌───────────────┐ │
│ │ Inference │ │ LLM Layer │ │ Knowledge │ │
│ │ Service │ │ (LangChain │ │ Base (RAG) │ │
│ │ (XGBoost) │ │ OpenRouter) │ │ (ChromaDB) │ │
│ └──────┬───────┘ └──────┬───────┘ └───────┬───────┘ │
│ ┌──────▼─────────────────▼───────────────────▼───────┐ │
│ │ Redis Cache Layer │ │
│ └──────────────────────┬──────────────────────────────┘ │
│ ┌──────────────────────▼──────────────────────────────┐ │
│ │ PostgreSQL Database │ │
│ └─────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────┘
│
┌────────────────────────▼────────────────────────────────┐
│ ODOO ERP (Data Source) │
│ Sales Orders · CRM · Invoices · Products │
└─────────────────────────────────────────────────────────┘
Data Flow
Training Flow (offline)
- Raw CSV data is exported from Odoo (sales, churn, growth datasets)
train.pyrunsOptimaAiMLTrainerV3, which preprocesses, trains, and evaluates all models- Trained model files (
.pkl) and evaluation metrics (evaluation_results_v*.json) are saved tooptimaai_artefacts/
Inference Flow (real-time)
- API receives a prediction request
InferenceServicelazy-loads model artefacts viaOptimaAiInferenceV3- Features are preprocessed and passed through the appropriate model
- Response returned directly to the caller
KPI & LLM Flow (cached)
/api/v1/kpischecks Redis first (5-min TTL)- On cache miss,
ml_bridgeloads the latestevaluation_resultsJSON - Metrics are persisted to
kpi_snapshotsin PostgreSQL and written to Redis /api/v1/recommendand/api/v1/bmcuse these KPIs as LLM context
RAG Knowledge Base Flow
- Users upload documents via
/api/v1/datasets/upload DynamicPreprocessingPipelinecleans and quality-scores the file- Cleaned data is stored in PostgreSQL; text is embedded into ChromaDB
/api/v1/kb/queryretrieves relevant chunks and generates grounded LLM answers
Service Responsibilities
| Service | File | Responsibility |
|---|---|---|
InferenceService | inference_service.py | Load ML artefacts, expose predict functions |
MLBridge | ml_bridge.py | Find and load latest evaluation JSON |
RecommendationService | recommendation_service.py | Build LLM prompt, call OpenRouter |
BMCService | bmc_service.py | Generate 9-block Business Model Canvas |
KnowledgeBase | knowledge_base.py | ChromaDB ingestion and RAG query |
Cache | cache.py | Redis get/set with per-entity TTLs |
Database | database.py | SQLAlchemy models and session management |
