How rohit-dhade/pre-delinquency-intervention-engine works
1. Repository split
- This repo is a two-service system: a Python Delinquency_Engine for scoring risk, and a Node.js Intervention_Engine for deciding and sending customer outreach.
- They communicate over HTTP, while both rely on the same PostgreSQL-backed operational data.
2. Delinquency Engine boundary
- The FastAPI service is the ML inference layer.
- Its job is to fetch features, run the XGBoost classifier, generate SHAP-based explanations, and optionally trigger intervention downstream.
3. Prediction data flow
- Customer data originates in PostgreSQL tables such as customers, transactions, and payment_events.
- Daily feature SQL produces engineered features, which are materialized into a Feast online store.
- The FastAPI endpoint /predict/{customer_id} reads from Feast, scores delinquency probability, and returns the prediction plus explanations.
4. Modeling and explainability
- The trained artifacts are packaged directly in the backend: xgb_model_latest.joblib, shap_explainer.joblib, and model_feature_names.joblib.
- SHAP identifies the main drivers of risk, and those top reasons are converted into human-readable text via Mistral-backed explanation generation.
5. Feature store subsystem
- Feast is the feature-serving boundary between raw transactional data and model inference.
- The offline source is a Parquet snapshot under feast_repo/data/, while the online store is a local SQLite DB used for low-latency point lookups by customer_id.
- The Feast repo also defines the entity and feature views that keep training and serving aligned.
6. Feature pipeline and orchestration
- The scripts/ directory contains the data and ML pipeline: synthetic data generation, SQL feature computation, Feast materialization, traffic simulation, and weekly retraining.
- Airflow runs the daily feature pipeline DAG, while weekly_retrain.py handles retraining gates and model hot-reload.
7. Intervention Engine boundary
- The Node.js service is the business-action layer.
- It receives a delinquency signal, applies policy rules, chooses an offer, generates messaging, sends email, and logs the outcome.
8. Intervention decision pipeline
- tierRouter converts probability into risk tiers like stable, watch, moderate, and critical.
- offerEngine maps the tier plus customer context to a relief product.
- channelRouter determines urgency and routing, and messageGenerator produces the customer-facing message.
9. Messaging and delivery
- Intervention emails are generated with Mistral AI, then wrapped in branded HTML and delivered through Nodemailer using Gmail SMTP.
- If AI generation fails, the service falls back to predefined templates, so email delivery is not dependent on the model being available.
10. Shared persistence
- PostgreSQL is the system’s state backbone.
- The Delinquency Engine owns the source and feature tables plus retraining audit logs.
- The Intervention Engine writes to intervention_log and outcome_log, which track outreach actions and customer responses.
11. Service-to-service coupling
- The coupling is intentionally narrow: FastAPI calls the Intervention Engine only when the delinquency probability crosses the trigger threshold.
- FastAPI also exposes proxy routes for intervention stats, history, and health, so the prediction API acts as the primary integration point for the platform.
12. External dependencies that matter architecturally
- Mistral AI is used in both services, but for different concerns: explainability in the prediction service and message generation in the intervention service.
- Gmail SMTP is the actual outbound delivery channel, and Docker is used for PostgreSQL and the Python deployment path.
- The whole system is designed around batch feature computation plus low-latency online scoring and automated outbound intervention.
Main entry points: main.py FastAPI API · messageGenerator.js Message Generator · intervention.js Intervention Route · feature_store.yaml Feature Store
Components 19
Delinquency Engine
Python scoring service, feature pipeline, and model artifacts.
- FastAPI API · Prediction endpoint and service boundary for delinquency scoring.
Delinquency_Engine/FastApi_Backend/main.py - Feature Store · Online/offline feature access layer for customer scoring.
Delinquency_Engine/feast_repo/feature_store.yaml - Entity Views · Feature entities and views that align training and serving.
Delinquency_Engine/feast_repo/features.py - Feature Pipeline · Daily feature SQL, materialization, synthetic data, and retraining scripts.
Delinquency_Engine/scripts/feature_sql.sql - Airflow DAG · Scheduled daily feature pipeline execution.
Delinquency_Engine/airflow/dags/delinquency_feature_pipeline.py - XGBoost Model · Latest delinquency classifier used for online inference.
Delinquency_Engine/FastApi_Backend/xgb_model_latest.joblib - SHAP Explainer · Produces feature attributions for risk explanations.
Delinquency_Engine/FastApi_Backend/shap_explainer.joblib - Explanation LLM · Turns SHAP drivers into human-readable delinquency explanations.
Delinquency_Engine/FastApi_Backend/app/llm_response.py
Intervention Engine
Node.js outreach service, decisioning, and email delivery.
- Intervention Route · HTTP boundary for delinquency-triggered outreach actions.
Intervention_Engine/src/routes/intervention.js - Tier Router · Maps delinquency probability to risk tiers.
Intervention_Engine/src/services/tierRouter.js - Offer Engine · Selects the relief product based on tier and customer context.
Intervention_Engine/src/services/offerEngine.js - Channel Router · Determines urgency and outreach routing.
Intervention_Engine/src/services/channelRouter.js - Message Generator · Creates customer-facing outreach copy with AI fallback.
Intervention_Engine/src/services/messageGenerator.js - Mailer · Wraps messages and sends email via SMTP.
Intervention_Engine/src/email/mailer.js - Templates · Predefined email copy used when AI generation fails.
Intervention_Engine/src/email/templates.js - HTML Wrapper · Applies branded HTML around generated outreach content.
Intervention_Engine/src/email/htmlWrapper.js
Shared Systems
Operational datastore and third-party services used by both engines.
- PostgreSQL · Shared source-of-truth database for customer, feature, and intervention state.
- Mistral AI · Used for explanation generation and outreach message drafting.
- Gmail SMTP · Outbound email transport for intervention delivery.
Connections 21
- PostgreSQL → Feature Pipeline: raw data · Customer, transaction, and payment data feed feature SQL.
- Feature Pipeline → Airflow DAG: scheduled by · Daily pipeline is orchestrated through Airflow.
- Airflow DAG → Feature Store: materializes · Computed features are loaded into Feast online serving storage.
- Entity Views → Feature Store: defines · Entity and feature views keep serving aligned.
- Feature Store → FastAPI API: serves features · FastAPI reads online features by customer_id.
- XGBoost Model → FastAPI API: scores · Model artifact powers delinquency probability prediction.
- SHAP Explainer → FastAPI API: explains · SHAP attributions are generated during prediction.
- Explanation LLM → Mistral AI: uses · LLM-backed explanation text generation.
- FastAPI API → Explanation LLM: summarizes risk · Top SHAP drivers are converted to readable reasons.
- FastAPI API → Intervention Route: triggers · High-risk predictions can invoke intervention downstream.
- FastAPI API → PostgreSQL: reads/writes · Uses operational data and may persist scoring-related state.
- Intervention Route → Tier Router: routes by risk · Incoming probability is mapped to a tier.
- Tier Router → Offer Engine: selects offer · Tier and customer context determine the relief product.
- Offer Engine → Channel Router: prepares outreach · Offer choice informs urgency and channel routing.
- Channel Router → Message Generator: requests message · Routing decision passes into content generation.
- Message Generator → Mistral AI: drafts with · AI generates outreach copy when available.
- Message Generator → Templates: fallbacks to · Predefined templates are used if AI generation fails.
- Message Generator → HTML Wrapper: formats · Generated content is wrapped for branded email.
- HTML Wrapper → Mailer: delivers · Wrapped email is sent through the mailer.
- Mailer → Gmail SMTP: sends via · Nodemailer uses Gmail SMTP for delivery.
- Intervention Route → PostgreSQL: logs · Intervention and outcome records are persisted.