BankBot-AI / docs /ARCHITECTURE.md
mohsin-devs's picture
Deploy to HF
a282d4b
# BankBot AI β€” System Architecture
## 1. High-Level Architecture
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ CLIENT BROWSER β”‚
β”‚ Next.js 14 (React, TypeScript) β”‚
β”‚ β”‚
β”‚ Pages: β”‚
β”‚ / Dashboard β”‚
β”‚ /chat AI Assistant (WS) β”‚
β”‚ /analytics Spending Intel β”‚
β”‚ /simulator What-If Engine β”‚
β”‚ /transactions History β”‚
β”‚ /status Observability β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ HTTPS / WSS
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ NGINX REVERSE PROXY β”‚
β”‚ β€’ TLS termination β”‚
β”‚ β€’ Rate limiting (30r/m API) β”‚
β”‚ β€’ Auth rate limit (10r/m) β”‚
β”‚ β€’ WebSocket upgrade proxy β”‚
β”‚ β€’ Static asset caching β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ FastAPI Backend β”‚ β”‚ Next.js Standalone β”‚
β”‚ Python 3.11 β”‚ β”‚ Node.js 20 β”‚
β”‚ Uvicorn (2 workers) β”‚ β”‚ Port 3000 β”‚
β”‚ Port 8000 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚
β”‚ Routers: β”‚
β”‚ /api/auth β”‚
β”‚ /api/dashboard β”‚
β”‚ /api/ai/* β”‚
β”‚ /api/ai/chat/ws (WS) β”‚
β”‚ /api/transactions β”‚
β”‚ /api/notifications β”‚
β”‚ /api/metrics β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β” β”Œβ”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ PostgreSQL 15 β”‚ β”‚ Redis 7 β”‚
β”‚ (Primary DB) β”‚ β”‚ (Cache Layer) β”‚
β”‚ β”‚ β”‚ β”‚
β”‚ Tables: β”‚ β”‚ Keys: β”‚
β”‚ users β”‚ β”‚ dashboard:* β”‚
β”‚ accounts β”‚ β”‚ ai:coaching:* β”‚
β”‚ transactions β”‚ β”‚ ai:behavior:* β”‚
β”‚ goals β”‚ β”‚ ai:twin:* β”‚
β”‚ investments β”‚ β”‚ ai:subs:* β”‚
β”‚ subscriptions β”‚ β”‚ β”‚
β”‚ notifications β”‚ β”‚ TTLs: β”‚
β”‚ fraud_logs β”‚ β”‚ dashboard: 2min β”‚
β”‚ ai_insights β”‚ β”‚ score: 10min β”‚
β”‚ analytics_ β”‚ β”‚ briefing: 1hr β”‚
β”‚ snapshots β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ AI ORCHESTRATION LAYER β”‚
β”‚ β”‚
β”‚ Priority Chain: β”‚
β”‚ 1. OpenAI (gpt-4o-mini) ← fastest β”‚
β”‚ ↓ if unavailable β”‚
β”‚ 2. Groq (llama-3.3-70b) ← free tier β”‚
β”‚ ↓ if unavailable β”‚
β”‚ 3. Ollama (llama3:latest) ← local β”‚
β”‚ ↓ if unavailable β”‚
β”‚ 4. Rule-based fallback ← always on β”‚
β”‚ β”‚
β”‚ Modules: β”‚
β”‚ β€’ chat.py β€” contextual chat β”‚
β”‚ β€’ coaching.py β€” health score β”‚
β”‚ β€’ forecasting.py β€” balance prediction β”‚
β”‚ β€’ simulation.py β€” what-if engine β”‚
β”‚ β€’ fraud.py β€” anomaly detection β”‚
β”‚ β€’ behavior.py β€” spending patterns β”‚
β”‚ β€’ subscriptions.py β€” sub optimization β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```
---
## 2. Data Flow β€” Dashboard Load
```
Browser Next.js FastAPI DB/Cache
β”‚ β”‚ β”‚ β”‚
│── GET / β”‚ β”‚ β”‚
β”‚ │── fetch /api/ β”‚ β”‚
β”‚ β”‚ dashboard/ β”‚ β”‚
β”‚ β”‚ overview β”‚ β”‚
β”‚ β”‚ │── check cache ──► β”‚
β”‚ β”‚ │◄── cache miss ── β”‚
β”‚ β”‚ │── query accounts β”‚
β”‚ β”‚ │── query txns β”‚
β”‚ │── JSON response ◄──│── query fraud β”‚
│◄── render dashboard ──│ │── set cache(2min) β”‚
β”‚ β”‚ β”‚ β”‚
β”‚ [2nd request] β”‚ β”‚ β”‚
β”‚ │── fetch /api/ β”‚ β”‚
β”‚ β”‚ dashboard/ β”‚ β”‚
β”‚ β”‚ overview β”‚ β”‚
β”‚ β”‚ │── check cache ──► β”‚
β”‚ β”‚ │◄── cache HIT ─── β”‚
│◄── render (22ms) ─────│◄── JSON (22ms) ────│ β”‚
```
---
## 3. Data Flow β€” WebSocket Chat
```
Browser FastAPI AI Backend
β”‚ β”‚ β”‚
│── WS connect ─────────► β”‚
│◄── WS accepted ────────│ β”‚
β”‚ β”‚ β”‚
│── { type: "chat", β”‚ β”‚
β”‚ message: "..." } ──►│ β”‚
β”‚ │── build context ──►│
β”‚ β”‚ (user profile, β”‚
β”‚ β”‚ history, goals) β”‚
β”‚ β”‚ │── stream tokens
│◄── { type: "chat_start" } β”‚
│◄── { type: "chat_chunk", content: "He" } β”‚
│◄── { type: "chat_chunk", content: "re" } β”‚
│◄── { type: "chat_chunk", content: " is" } β”‚
β”‚ ... (streaming) β”‚
│◄── { type: "chat_end" } β”‚
β”‚ β”‚ β”‚
│── { type: "ping" } ───►│ (heartbeat 25s) β”‚
│◄── { type: "pong" } ───│ β”‚
```
---
## 4. AI Context Construction
Every chat message is enriched with full user financial context:
```python
system_prompt = f"""
You are BankBot, an elite AI Financial Analyst.
CURRENT USER PORTFOLIO:
- Name: {user.name}
- Financial Personality: {user.financial_personality}
- Health Score: {score}/100
- Total Balance: ${total_balance:,.2f}
- Accounts: {account_details}
- Goals: {goals_details}
- Investments: {investments_details}
- Subscriptions: {subs_details}
- Behavioral Insights: {behavior_insights}
PRINCIPLES:
1. Never give generic advice β€” use real numbers
2. Respond like a Bloomberg Terminal analyst
3. Keep answers brief, actionable, financially meaningful
"""
```
---
## 5. Fraud Detection Algorithm
```
Transaction received
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Load last 30 transactions β”‚
β”‚ for this user β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Amount spike? β”‚ > 3.5x avg β†’ +40 pts
β”‚ β”‚ > 2.0x avg β†’ +20 pts
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Timing anomaly?β”‚ 11PM–4AM β†’ +25 pts
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Rapid fire? β”‚ < 3 min gap β†’ +20 pts
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Duplicate? β”‚ Same merchant+amount
β”‚ β”‚ within 10 min β†’ +30 pts
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Score β‰₯ 30? β”‚ β†’ Log to fraud_logs
β”‚ Score β‰₯ 50? β”‚ β†’ Status: "flagged"
β”‚ Score < 30? β”‚ β†’ Status: "verified"
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```
---
## 6. Caching Strategy
| Data | Cache Key | TTL | Reason |
|------|-----------|-----|--------|
| Dashboard overview | `dashboard:overview:{uid}` | 2 min | High-frequency, DB-heavy |
| AI health score | `ai:coaching:score:{uid}` | 10 min | AI call expensive |
| AI daily briefing | `ai:coaching:briefing:{uid}` | 1 hr | LLM cost control |
| Behavior insights | `ai:behavior:insights:{uid}` | 10 min | Computation heavy |
| Twin prediction | `ai:twin:predict:{uid}` | 5 min | Moderate cost |
| Subscriptions | `ai:subs:optimize:{uid}` | 10 min | Stable data |
Cache backend: Redis β†’ in-memory dict fallback (automatic, no config needed).
---
## 7. Security Architecture
```
Request β†’ Nginx (rate limit) β†’ FastAPI middleware stack:
1. Rate limiter (120 req/min per IP)
2. Security headers (X-Frame-Options, CSP, etc.)
3. Request logger (structured JSON)
4. Process time header
5. CORS validation
6. Route handler
└── JWT validation (if protected route)
└── Business logic
└── DB query / AI call / Cache lookup
```
**JWT Flow:**
```
Login β†’ access_token (60min) + refresh_token (7 days)
β”‚
β–Ό
Request with Authorization: Bearer {access_token}
β”‚
β–Ό
Token expired? β†’ POST /api/auth/refresh with refresh_token
β”‚
β–Ό
New access_token issued (refresh_token unchanged)
β”‚
β–Ό
Logout β†’ client clears tokens (stateless)
```
---
## 8. Deployment Architecture
```
Internet
β”‚
β–Ό
Cloudflare (DNS + DDoS protection)
β”‚
β–Ό
Nginx (SSL termination, rate limiting)
β”‚
β”œβ”€β”€β–Ί Next.js Frontend (Vercel / Docker port 3000)
β”‚
└──► FastAPI Backend (Render / Docker port 8000)
β”‚
β”œβ”€β”€β–Ί PostgreSQL (Render managed / Docker)
β”œβ”€β”€β–Ί Redis (Render managed / Docker)
└──► AI Provider (OpenAI API / Groq API)
```