Comprehensive 3-Tier Enterprise Reference: Real-Time Ingestion, Two-Stage Predictive ML & RAG Core, and Multi-Agent Autonomous Execution
- SECTION I: SYSTEM ARCHITECTURE & UNIFIED 3-TIER TOPOLOGY
- SECTION II: TIER 1 — REAL-TIME SENSORY INGESTION & DOCUMENT SYNTHESIS
- SECTION III: TIER 2 — SAP FEATURE STORE, TWO-STAGE PREDICTIVE ML & HYBRID RAG
- 3.1 Relational ERP Schema & Data Mapping Architecture (10 SAP Tables)
- 3.2 Machine Learning Feature Store & Training Engineering (19 Features)
- 3.3 Mathematical Formulations of Key Engineered Features
- 3.4 Two-Stage Hurdle Machine Learning Pipeline & Benchmark Metrics
- 3.5 The Role of Live Environmental Signals (Weather & Strike): Dynamic Post-ML Modifiers
- 3.6 Detailed Function Breakdown: Feature Store & RAG Modules (Modules 7–10)
- 3.7 Tier 2 Data Summary Matrix & Dual-Engine Synergy
- SECTION IV: TIER 3 — MULTI-AGENT SPECIALIST REASONING, ERP ACTIONS & ORCHESTRATION
- SECTION V: COMPLETE END-TO-END SYSTEM INTEGRATION
Note
Databricks Optimized Edition: This document has been specially configured for Databricks Workspaces, Databricks Repos, and Databricks Notebook %md markdown cells. All architectural topologies, decision flowcharts, and sequence traces use Databricks Native Monospace Unicode Architecture Blocks (text) that render crisply and reliably across all cloud cluster environments without external JavaScript dependencies, CDNs, or rendering failures.
The Order-to-Cash (O2C) Delivery Risk Copilot is an enterprise AI platform engineered to proactively detect, quantify, legally adjudicate, and autonomously mitigate shipment delays across complex, multi-modal supply chain networks.
In modern global logistics operations, delayed shipments trigger severe operational and financial liabilities:
- Contractual Late Delivery Penalties: Platinum clinics and premier enterprise accounts enforce strict Service Level Agreements (SLAs), levying $500/day penalties or 5%/day discounts.
- Receiving Window Dock Breaches: Unscheduled deliveries arriving after facility operating hours incur $150 redelivery fees.
-
Product Spoilage & Patient Risk: For veterinary therapeutics, biologic vaccines, and clinical diets, multi-day delays during severe heatwaves (
$>40^\circ ext{C}$ ) or transport strikes cause catastrophic inventory write-offs and patient care crises. - Carrier Chargeback Disputes: Without verifiable telematics audits and meteorological evidence, claims against third-party logistics (3PL) carriers collapse during contract arbitration.
The O2C Copilot resolves these enterprise challenges by fusing:
- Tier 1 (Real-Time Ingestion): Continuous Open-Meteo weather telemetry and Google News RSS web scraping, transformed into 23 high-density Word regulatory policy protocols with discrete rule IDs (
[RULE-W-*],[RULE-S-*]). - Tier 2 (Predictive ML & RAG Core): A 10-table SAP relational feature store feeding an enterprise Two-Stage Hurdle ML Architecture (97.10% accuracy, 0.9958 ROC-AUC, 5.63h MAE) and a Hybrid Dense/Sparse RAG Engine (82 documents, 909 chunks) operating on FAISS Cosine Similarity and Okapi BM25 Reciprocal Rank Fusion (RRF).
- Tier 3 (Multi-Agent Specialist Reasoning & Execution): A 4-agent collaborative reasoning graph (
RouteSupervisorAgent,ContractAdjudicatorAgent,QualityMitigationAgent,LLMReasoningEngine) executing simulated write-backs to SAP ERP tables (VBAK-LIFSK = '01',VBAK-VDATU,SAP_BKPFcarrier AP debit memos) and dispatching interactive Microsoft Teams Adaptive Cards (v1.4) with a 2-hour executive approval SLA.
┌────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ TIER 1: REAL-TIME SENSORY INGESTION & DOCUMENT SYNTHESIS │
└────────────────────────────────────────────────────────────────────────────────────────────────────────┘
[Open-Meteo & OWM Telemetry] ──► [WeatherService] ──► [DatabaseManager] ──► weather_readings (130+ rows)
[Google News RSS Feed] ──► [NewsService] ──► [DatabaseManager] ──► strike_news (396+ rows)
│
▼
[WeatherPolicyGenerator] ──► 6 Word Weather Protocols ([RULE-W-*])
[StrikeIntelGenerator] ──► 17 Word Strike Briefs ([RULE-S-*])
│
▼
┌────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ TIER 2: SAP FEATURE STORE, TWO-STAGE ML & HYBRID RAG │
└────────────────────────────────────────────────────────────────────────────────────────────────────────┘
[10 SAP ERP CSV Exports] ──► [MLDatabaseExtension] ──► 19-Feature Vector Space
│
┌──────────────────────────────────────────────────────┴───────────────────────────────────┐
▼ ▼
[Stage 1: RandomForestClassifier Gate] [82 Corpus Policy Documents]
(100 Trees, Depth 6, Class-Weighted) │
│ ▼
├───────────────────────────────────┐ [DocumentLoader & ClauseChunker]
▼ (If P(delay) < 0.40) ▼ (If P(delay) >= 0.40) │
[PREDICTED ON TIME] [Stage 2: Conditional Huber Regressor] ▼
• Delay: 0.00h (Ghost Eliminated) (GradientBoostingRegressor, loss='huber') [909 Semantic Chunks]
• Risk: $0.00 │ │
▼ ┌───────────┴───────────┐
[Estimated Delay: >= 12.0h] ▼ ▼
│ [Dense FAISS FlatIP] [Sparse BM25]
▼ (384-dim Vectors) (Exact Terms)
[Live Environmental Modifiers] │ │
• Live Heat/Rain: +12h, +0.10 P(delay) └───────────┬───────────┘
• Live Strikes: +12h, +0.10 P(delay) ▼
│ [Hybrid RRF Fusion Search]
└───────────────────────────────┬────────────────────────┘
▼
[Enriched Multi-Engine Risk Payload]
│
▼
┌────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ TIER 3: MULTI-AGENT REASONING & PHYSICAL EXECUTION │
└────────────────────────────────────────────────────────────────────────────────────────────────────────┘
[AgenticOrchestrator]
│
┌───────────────────────────────┬───────────────────────┴───────────────────────┬────────────────────────┐
▼ ▼ ▼ ▼
[RouteSupervisorAgent] [ClinicNotificationDispatcher] [ContractAdjudicatorAgent] [QualityMitigationAgent]
• GPS Telematics Audit • Proactive 12h Early Warning • Platinum: $500/day SLA • $1,000 Air Freight Pallet
• $200 Penalty if Dead • Secures Statutory Force Majeure • Post-17:00: $150 Dock Fee • QA Hold LIFSK='01'
│ • 72h Force Majeure Waiver • $500 Director Approval Gate
└───────────────────────┬───────────────────────┘ │
▼ ▼
[LLMReasoningEngine Synthesis] ◄───────────────────────────────┘
(Qwen2.5 / Databricks / Local)
│
┌───────────────────────┴───────────────────────┐
▼ ▼
[SAPActionExecutor Write-Backs] [MSTeamsDispatcher Cards]
• Delivery Block: VBAK-LIFSK='01' • Adaptive Cards v1.4
• Delivery Date: VBAK-VDATU • Interactive Action Buttons
• Carrier AP: SAP_BKPF Debit Memo • 2-Hour Response SLA
│ │
└───────────────────────┬───────────────────────┘
▼
[Daily Executive Audit Report]
(daily_agent_report_YYYY-MM-DD.json)
💡 Databricks Interactive Rendering Tip: To render this topology graphically in a Databricks Notebook Python cell:
displayHTML(''' <script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script> <div class="mermaid"> graph TD subgraph "TIER 1: INGESTION" A1["Open-Meteo & OWM Live Telemetry"] --> A2["WeatherService"] B1["Google News RSS Disruption Stream"] --> B2["NewsService"] A2 & B2 --> C1["DatabaseManager (SQLite)"] C1 --> D1["weather_readings"] & D2["strike_news"] D1 --> E1["WeatherPolicyGenerator"] --> F1["6 Word Weather Protocols"] D2 --> E2["StrikeIntelligenceGenerator"] --> F2["17 Word Strike Briefs"] end subgraph "TIER 2: PREDICTIVE ML & RAG" G1["10 SAP ERP Tables"] --> G2["MLDatabaseExtension"] --> G3["19-Feature Vector Space"] G3 --> H1["Two-Stage Hurdle ML Engine"] --> H2["P(delay), Delay Hours, XAI Attributions"] F1 & F2 & J1["82 Corpus Documents"] --> J2["DocumentLoader & ClauseChunker"] --> J3["909 Chunks"] J3 --> K1["FAISS Dense Vectors"] & K2["Okapi BM25 Lexicon"] --> K3["Hybrid RRF Search"] H2 & K3 --> L1["Enriched Multi-Engine Risk Payload"] end subgraph "TIER 3: AGENTS & EXECUTION" L1 --> M1["AgenticOrchestrator"] M1 --> N1["RouteSupervisorAgent ($200 Penalty)"] M1 --> N2["ClinicNotificationDispatcher (12h Early Warning)"] M1 --> N3["ContractAdjudicatorAgent (SLA Math & Waivers)"] M1 --> N4["QualityMitigationAgent ($1,000 Air Freight)"] N1 & N2 & N3 & N4 --> P1["LLMReasoningEngine"] P1 --> Q1["SAPActionExecutor (VBAK-LIFSK, VDATU, Debit Memos)"] P1 --> Q2["MSTeamsDispatcher (Adaptive Cards v1.4)"] P1 --> Q3["Daily Executive Audit Report (daily_agent_report.json)"] end </div> ''')
The unified platform comprises 13 dedicated Python modules across 3 architectural tiers:
| Tier | Module # | File Location | Class Name / Component | Key Responsibility | Functions Validated |
|---|---|---|---|---|---|
| Tier 1 | Module 1 | modules/config.py |
Configuration Core | Absolute root resolution, city coordinates, RAG constants | 1 |
| Tier 1 | Module 2 | modules/database_manager.py |
DatabaseManager |
ACID transactions, WAL mode SQLite, ingestion table CRUD | 12 |
| Tier 1 | Module 3 | modules/weather_service.py |
WeatherService |
Dual-backend weather telemetry (Open-Meteo & OWM) | 6 |
| Tier 1 | Module 4 | modules/news_service.py |
NewsService |
Google News RSS scraper, NLP entity & modality extraction | 9 |
| Tier 1 | Module 5 | modules/weather_policy_generator.py |
WeatherPolicyGenerator |
Generates 6 Word protocols with [RULE-W-*] IDs |
6 |
| Tier 1 | Module 6 | modules/strike_intelligence_generator.py |
StrikeIntelligenceGenerator |
Generates 17 Word briefs with [RULE-S-*] IDs |
8 |
| Tier 2 | Module 7 | modules/ml_db_extension.py |
MLDatabaseExtension |
10-table SAP join, Haversine geospatial vectors, 19 features | 8 |
| Tier 2 | Module 8 | modules/predictive_engine.py |
PredictiveEngine |
Two-Stage Hurdle ML models, Huber regressor, XAI attributions | 10 |
| Tier 2 | Module 9 | modules/rag_engine.py |
RAGEngine (6 Classes) |
Document loading, chunking, FAISS + BM25 hybrid RRF retrieval | 27 |
| Tier 2 | Module 10 | modules/ollama_service.py |
OllamaService |
Local Qwen2.5:7b daemon interface with anti-hallucination prompt | 3 |
| Tier 3 | Module 11 | modules/agent_specialists.py |
4 Specialist Agents | Route supervisor, contract adjudicator, quality mitigation, LLM core | 6 |
| Tier 3 | Module 12 | modules/action_execution_engine.py |
3 Dispatchers / Executors | SAP ERP write-backs, MS Teams Adaptive Cards, 12h clinic notice | 10 |
| Tier 3 | Module 13 | modules/agentic_orchestrator.py |
AgenticOrchestrator |
6-stage autonomous daily lifecycle, report publishing | 6 |
| TOTAL | 13 Modules | Unified Core | Enterprise Copilot | Autonomous Order-to-Cash Logistics Governance | 112 / 112 Validated |
| Data Artifact | Physical File Path | Format | Description |
|---|---|---|---|
| Raw ERP Exports | Input Files/*.csv |
CSV (UTF-8) | 10 SAP ECC/S4HANA enterprise tables (VBAK, VBAP, LIKP, LIPS, VTTK, VTTP, KNA1, KNVV, LFA1, MARA). |
| Relational Database | india_monitor_data/database/india_monitor.db |
SQLite (WAL Mode) | Master relational database containing 6 core operational tables (scrape_sessions, weather_readings, strike_news, weather_alerts, daily_summaries, rag_analyses) and 10 mirrored SAP tables. |
| Dynamic Weather Docs | india_monitor_data/rag/documents/Weather_Policies/ |
DOCX | City-specific & master severe weather protocols generated from live sensor alerts. |
| Dynamic Strike Briefs | india_monitor_data/rag/documents/Strike_Intelligence/ |
DOCX | Regional & modality-specific transport disruption briefs generated from scraped RSS news. |
| Daily Decision Reports | india_monitor_data/reports/daily_agent_report_YYYY-MM-DD.json |
JSON | Daily executive and multi-agent synthesized delay risk & SLA penalty decisions. |
| System Event Logs | india_monitor_data/logs/monitor_YYYYMMDD.log |
Text/Log | Timestamped audit log of all database transactions, API fetches, and ETL sessions. |
- Open-Meteo Global Historical & Live Forecast API (
https://api.open-meteo.com/v1/forecast,https://archive-api.open-meteo.com/v1/archive)- Authentication: Zero-key public API.
- Data Fetched: Surface temperature (°C), apparent temperature, 1-hour precipitation (mm), weather condition WMO codes, 10m wind speed (m/s), relative humidity (%).
- OpenWeatherMap REST API (
https://api.openweathermap.org/data/2.5/weather)- Authentication:
OPENWEATHER_API_KEY(falls back gracefully to Open-Meteo if missing/invalid). - Data Fetched: Live visibility (m), barometric pressure (hPa), cloud cover (%), real-time wind gusts.
- Authentication:
- Google News RSS Feed (
https://news.google.com/rss/search)- Data Fetched: Transportation strikes, highway blockades, port congestion, trucker protests, and regional bandh/hartal alerts.
-- 1. Ingestion Execution Sessions
CREATE TABLE scrape_sessions (
session_id INTEGER PRIMARY KEY AUTOINCREMENT,
session_type TEXT NOT NULL, -- 'full', 'weather', 'news'
started_at TEXT NOT NULL,
completed_at TEXT,
status TEXT DEFAULT 'running',
cities_fetched INTEGER DEFAULT 0,
articles_found INTEGER DEFAULT 0,
error_message TEXT
);
-- 2. Environmental Weather Readings
CREATE TABLE weather_readings (
reading_id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id INTEGER,
city_name TEXT NOT NULL,
state TEXT,
recorded_at TEXT NOT NULL,
date_only TEXT NOT NULL,
hour_of_day INTEGER NOT NULL,
temperature REAL,
feels_like REAL,
temp_min REAL,
temp_max REAL,
humidity INTEGER,
pressure REAL,
visibility_km REAL,
cloudiness INTEGER,
weather_main TEXT,
weather_description TEXT,
wind_speed REAL,
wind_direction INTEGER,
rain_1h REAL DEFAULT 0,
snow_1h REAL DEFAULT 0,
data_source TEXT DEFAULT 'OpenWeatherMap',
UNIQUE(city_name, date_only, hour_of_day)
);
-- 3. Scraped Transportation Strikes & Disruption News
CREATE TABLE strike_news (
news_id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id INTEGER,
title TEXT NOT NULL,
description TEXT,
url TEXT,
source_name TEXT,
keyword_matched TEXT,
city_mentioned TEXT,
state_mentioned TEXT,
severity TEXT, -- 'HIGH', 'MEDIUM', 'LOW'
strike_type TEXT, -- 'bus', 'truck', 'railway', 'auto', 'taxi', 'bandh', 'general'
published_date TEXT,
scraped_at TEXT NOT NULL,
UNIQUE(title, source_name)
);
-- 4. Severe Weather Alerts Exceeding Safety Thresholds
CREATE TABLE weather_alerts (
alert_id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id INTEGER,
city_name TEXT NOT NULL,
state TEXT,
alert_type TEXT NOT NULL,
alert_message TEXT NOT NULL,
severity TEXT,
triggered_at TEXT NOT NULL
);
-- 5. Daily Aggregated City Metrics
CREATE TABLE daily_summaries (
summary_id INTEGER PRIMARY KEY AUTOINCREMENT,
summary_date TEXT NOT NULL,
city_name TEXT NOT NULL,
state TEXT,
avg_temperature REAL,
max_temperature REAL,
min_temperature REAL,
avg_humidity REAL,
total_rainfall REAL DEFAULT 0,
avg_wind_speed REAL,
dominant_weather TEXT,
strike_count INTEGER DEFAULT 0,
high_severity_count INTEGER DEFAULT 0,
weather_alert_count INTEGER DEFAULT 0,
updated_at TEXT,
UNIQUE(summary_date, city_name)
);
-- 6. Historical RAG Question-Answer Traces
CREATE TABLE rag_analyses (
analysis_id INTEGER PRIMARY KEY AUTOINCREMENT,
news_id INTEGER,
strike_title TEXT NOT NULL,
question TEXT NOT NULL,
answer TEXT NOT NULL,
sources TEXT,
confidence REAL,
analyzed_at TEXT NOT NULL,
FOREIGN KEY(news_id) REFERENCES strike_news(news_id)
); [Live Open-Meteo / OWM API] [Google News RSS Feeds]
│ (HTTP GET JSON) │ (HTTP GET XML)
▼ ▼
[modules/weather_service.py] [modules/news_service.py]
• fetch_current() • fetch()
• _owm_one() / _meteo_current_one() • _build_queries() / _classify_severity()
│ │
└────────────────────┬────────────────────┘
▼
[modules/database_manager.py]
• session_start("full_cycle")
• write_weather() / write_strikes()
│
▼
[SQLite Storage: monitor.db]
┌────────────────────────────────────────┐
│ • weather_readings (130+ records) │
│ • strike_news (396+ records) │
│ • ingestion_sessions (audit trail) │
└────────────────────────────────────────┘
│
┌─────────────────────────┴─────────────────────────┐
▼ ▼
[modules/weather_policy_generator.py] [modules/strike_intelligence_generator.py]
• 5 City Protocols (HYD, BOM, MAA, PNQ, BLR) • 9 City Strike Briefs
• 1 Master National Weather Protocol • 7 Transport Modality Briefs
• Emits [RULE-W-*] Regulation Codes • 1 Master Intelligence Brief
│ │
└─────────────────────────┬─────────────────────────┘
▼
[Output: 23 Word .docx Files]
(Preserved in RAG Knowledge Corpus)
File Location: d:\Progamming\O2C_AI\modules\config.py
Purpose (The Central Control Room): Acts as the central rulebook and settings hub for the entire system. It tells the software where all data folders live, sets standard safety limits (like heatwave temperatures and wind thresholds), and ensures the code runs smoothly whether it is hosted on cloud servers or a local office computer.
INDIA_CITIES (dict): Coordinate dictionary of 10 key Indian supply chain hubs:- Metros: Mumbai (
19.0760, 72.8777), Delhi (28.6139, 77.2090), Bangalore (12.9716, 77.5946), Chennai (13.0827, 80.2707), Kolkata (22.5726, 88.3639), Hyderabad (17.3850, 78.4867). - Tier-1 Hubs: Pune (
18.5204, 73.8567), Ahmedabad (23.0225, 72.5714), Jaipur (26.9124, 75.7873), Lucknow (26.8467, 80.9462).
- Metros: Mumbai (
STRIKE_KEYWORDS (list): 14 domain search phrases:"transport strike","truck strike","bandh","hartal","chakka jam","road blockade", etc.ALERT_THRESHOLDS (dict):rain_mm_per_hr: 20(Extreme precipitation trigger)temp_extreme_c: 42(Thermal cargo degradation trigger)wind_ms: 15(High-wind vehicle tipping hazard)visibility_km: 1(Fog/smog corridor slowdown)
RAG Settings (Hyperparameters):EMBEDDING_MODEL = "all-MiniLM-L6-v2": Specifies the HuggingFace transformer model producing 384-dimensional dense semantic vectors loaded byVectorStorein FAISS (IndexFlatIP(384)).CHUNK_SIZE = 500: The dynamic character accumulation ceiling. The chunker does not blindly slice text; it dynamically aggregates whole logical clauses/sections (\n\n,SECTION,TICKET, numbers) until reaching ~500 characters. If a legal clause exceeds 500 chars, it dynamically breaks at sentence boundaries ([.!?]).CHUNK_OVERLAP = 50: Character overlap preserved across adjacent sentence splits to ensure uninterrupted legal context.TOP_K_RESULTS = 5: The default retrieval depth forHybridRAG.search(), returning the top 5 most relevant policy clauses ranked by Reciprocal Rank Fusion (RRF).
- Purpose: Acts as the system's internal GPS. It automatically detects where the software is running (in the cloud, on a remote server, or on a local laptop) so all folders and files are located seamlessly without requiring manual configuration.
- Input Parameters: None (Inspects environment variables
O2C_PROJECT_ROOT,DATABRICKS_RUNTIME_VERSION, and file system ancestry). - Output Return Type:
pathlib.Path - How it helps the data: Guarantees that all paths (
india_monitor_data/,Input Files/,models/) resolve to the exact absolute disk path regardless of whether the code is run via terminal CLI, Databricks job, or Jupyter notebook.
File Location: d:\Progamming\O2C_AI\modules\database_manager.py
Class: DatabaseManager
Purpose (The Digital Filing Vault): Serves as the system's secure electronic safe. It organizes and protects all incoming live weather reports, news articles, and audit logs, making sure records are saved cleanly and can be pulled up in milliseconds without crashing.
- Purpose: Prepares the database storage vault, sets up daily activity logbooks, and builds the necessary data tables so everything is ready to record incoming information safely.
- Input Parameters:
db_path (str | Path)— Path to SQLite.dbfile. - Output Return Type: None.
- How it helps the data: Ensures tables and indices exist before any write operations occur.
- Purpose: Creates a daily activity journal that records every system action and error, giving managers a transparent, unalterable paper trail for system audits.
- Input Parameters: None.
- Output Return Type:
logging.Logger - How it helps the data: Provides an immutable audit trail of data transactions and operational errors.
- Purpose: Provides a safe, dedicated key to access the database vault, ensuring multiple processes can read and write data simultaneously without locking each other out or corrupting records.
- Input Parameters: None.
- Output Return Type:
Generator[sqlite3.Connection] - How it helps the data: Enables
PRAGMA journal_mode=WALandPRAGMA foreign_keys=ON, preventing database locking during high-frequency concurrent operations.
- Purpose: Builds the digital filing cabinets (database tables and lightning-fast search indexes) required to organize weather readings, news alerts, and legal analysis records.
- Input Parameters: None.
- Output Return Type: None.
- How it helps the data: Establishes table structures, composite uniqueness constraints (
UNIQUE(city_name, date_only, hour_of_day)), and index optimizations.
- Purpose: Punches a digital timecard at the start of a data-collection run, stamping an official tracking ticket on every incoming piece of weather and news data.
- Input Parameters:
session_type (str)— Type of run ("full","weather", or"news"). - Output Return Type:
int— Auto-incrementedsession_id. - How it helps the data: Assigns a session ID to tag every incoming weather reading and news article for data lineage.
- Purpose: Closes out the active tracking ticket when data collection finishes, recording how many rows were collected, whether the run succeeded, and any errors encountered.
- Input Parameters:
sid (int),status (str),cities (int),articles (int),error (str | None). - Output Return Type: None.
- How it helps the data: Tracks ETL health and monitors API pipeline success rates.
- Purpose: Safely files away incoming city weather reports while automatically skipping duplicate entries so the database stays clean and organized.
- Input Parameters:
records (list[dict])— Cleaned weather dicts,session_id (int). - Output Return Type:
tuple[int, int]—(inserted_count, skipped_duplicate_count). - How it helps the data: Deduplicates incoming weather readings so multiple runs on the same day/hour never create duplicate rows.
- Purpose: Saves incoming transport strike news and protest reports, automatically filtering out repeat articles so only fresh disruptions are logged.
- Input Parameters:
articles (list[dict])— Cleaned news dicts,session_id (int). - Output Return Type:
tuple[int, int]—(inserted_count, skipped_duplicate_count). - How it helps the data: Deduplicates news by
UNIQUE(title, source_name), preserving historical transport disruption records.
9. write_rag_analysis(self, news_id: int, strike_title: str, question: str, answer: str, sources: list, confidence: float = None) -> int
- Purpose: Archives the AI's legal analysis of strike events, linking news headlines directly to the contractual clauses and policy rules cited.
- Input Parameters:
news_id (int),strike_title (str),question (str),answer (str),sources (list),confidence (float). - Output Return Type:
int—analysis_id. - How it helps the data: Maintains traceability between live news events and Copilot legal/logistics interpretations.
- Purpose: Pulls historical weather records out of storage and formats them into clean, structured tables that the predictive machine learning models can study.
- Input Parameters:
date (str | None)(YYYY-MM-DD),city (str | None). - Output Return Type:
pd.DataFrame - How it helps the data: Converts SQL records into structured DataFrames for downstream feature engineering in Engine A.
- Purpose: Gathers historical strike and protest records from the database so the route planning agents can review ongoing road and rail disruptions.
- Input Parameters:
date (str | None),city (str | None). - Output Return Type:
pd.DataFrame - How it helps the data: Supplies disruption intelligence to the Multi-Agent Route Supervisor.
- Purpose: Summarizes total records, operational health, and system activity across all database tables for executive health-check dashboards.
- Input Parameters: None.
- Output Return Type:
dict— High-level dataset summary metrics. - How it helps the data: Used by validation test suites and health-check monitoring dashboards.
File Location: d:\Progamming\O2C_AI\modules\weather_service.py
Class: WeatherService
Purpose (24/7 Weather Radar): Keeps an eye on the skies across India's 10 busiest shipping hubs. It continuously tracks extreme heat, downpours, high winds, and dense fog to warn logistics managers before storms delay shipments.
CITIES: Dictionary mapping 10 major supply chain hubs (Mumbai,Delhi,Bangalore,Chennai,Kolkata,Hyderabad,Ahmedabad,Pune,Jaipur,Lucknow) to lat/long coordinates.WMO_CODES: Standard WMO code mapping table decoding numeric weather codes (e.g.95-> Thunderstorm,65-> Heavy Rain) into human-readable descriptions.
- Purpose: Connects the application to commercial and open-source weather satellite networks, linking geographic coordinates for all 10 major logistics delivery cities.
- Input Parameters:
api_key (str)— OpenWeatherMap API key (optional),cities (dict)— Coordinate mapping dict. - Output Return Type: None.
- How it helps the data: Establishes communication channels with external meteorological providers.
- Purpose: Checks live weather conditions (temperature, rainfall, storm winds, and fog) across all 10 logistics hubs, automatically switching to a free backup provider if the primary service is unavailable.
- Input Parameters: None.
- Output Return Type:
list[dict]— List of standardized weather dictionaries. - How it helps the data: Provides real-time weather readings for cold-chain temperature control and transit delay forecasting.
- Purpose: Pulls historical weather records for any past date so data scientists can test whether severe past storms explain delayed historical deliveries.
- Input Parameters:
date (str)— Target date inYYYY-MM-DDformat. - Output Return Type:
list[dict]— List of historical weather dictionaries. - How it helps the data: Enables backtesting ML models against historical weather events on specific dispatch dates.
- Purpose: Contacts the primary commercial weather provider (OpenWeatherMap) for a single city, extracting exact measurements for wind speed, humidity, cloud cover, and atmospheric pressure.
- Input Parameters:
city (str),coords (dict)—{"lat": float, "lon": float}. - Output Return Type:
dict | None— Normalized weather reading dict orNoneon failure. - How it helps the data: Extracts exact visibility, pressure, and cloudiness metrics from primary commercial telemetry.
- Purpose: Acts as an automatic safety net by fetching current weather from Open-Meteo if the primary commercial weather provider runs out of credits or encounters an error.
- Input Parameters:
city (str),coords (dict). - Output Return Type:
dict | None— Normalized weather reading dict. - How it helps the data: Ensures 100% continuous data availability even without paid API keys.
- Purpose: Retrieves and condenses 24 hours of historical hourly weather data into a single representative midday snapshot for model training.
- Input Parameters:
city (str),coords (dict),date (str). - Output Return Type:
dict | None— Daily aggregated weather observation. - How it helps the data: Normalizes hourly timeseries arrays into representative daily dispatch conditions.
File Location: d:\Progamming\O2C_AI\modules\news_service.py
Class: NewsService
Purpose (Highway Intelligence Scout): Scans internet news around the clock like a dedicated traffic scout, flagging trucker strikes, highway blockades, port strikes, and regional shutdowns that could strand freight on the road.
- Purpose: Programs the news scout with targeted logistics keywords (such as 'truck strike', 'chakka jam', and 'bandh') and city boundaries across major freight corridors.
- Input Parameters:
keywords (list[str])— 14 strike terms,cities (dict)— 10 Indian hub cities. - Output Return Type: None.
- How it helps the data: Configures the geographic and semantic boundaries for external web scraping.
- Purpose: Scours live Google News feeds across all cities and keywords, weeding out duplicate headlines and labeling each disruption with its severity and transport type.
- Input Parameters:
date (str | None),city (str | None). - Output Return Type:
list[dict]— Enriched article objects. - How it helps the data: Converts unorganized XML web news into structured risk data tagged with severity, location, and transport modality.
- Purpose: Combines target cities and strike keywords into smart search phrases (like 'truck strike Mumbai India') to pinpoint relevant disruption news.
- Input Parameters:
city (str | None). - Output Return Type:
list[str]— Formatted query strings. - How it helps the data: Focuses search scope specifically on Indian logistics corridors.
- Purpose: Narrows web searches to the exact operational shipping window so the system only collects news that actually impacts current shipments.
- Input Parameters:
date (str). - Output Return Type:
str— Search date filter fragment. - How it helps the data: Restricts scraping strictly to the active dispatch operational window.
- Purpose: Downloads and reads Google News XML feeds, gently spacing out web requests so servers are not overloaded, and extracts article titles, links, and publication dates.
- Input Parameters:
query (str). - Output Return Type:
list[dict]— Extracted article dictionaries. - How it helps the data: Extracts clean article metadata while enforcing a
time.sleep(0.5)rate-limiting delay.
- Purpose: Reads article headlines and body text to identify which specific Indian logistics hub or delivery route is being disrupted.
- Input Parameters:
text (str). - Output Return Type:
str— Matched city name or"Unknown". - How it helps the data: Spatially tags news articles to regional warehouse and customer delivery locations.
- Purpose: Identifies which state a city belongs to so the system can determine whether a protest is a local municipal issue or a state-wide highway shutdown.
- Input Parameters:
city (str). - Output Return Type:
str— State name (e.g."Maharashtra"for"Mumbai"). - How it helps the data: Maps municipal strikes to state-level regulatory jurisdictions.
- Purpose: Grades disruption severity like a veteran logistics dispatcher, classifying events into Red (total shutdowns / national bandhs), Yellow (regional 24-hour strikes), or Green (minor local delays):
🔴 HIGH: If text contains"bharat bandh","national strike","indefinite", or"complete shutdown".🟡 MEDIUM: If text contains"state bandh","24-hour","48-hour", or"city strike".🟢 LOW: All other local transport advisories.
- Input Parameters:
text (str). - Output Return Type:
str— Categorical severity rating ("🔴 HIGH","🟡 MEDIUM","🟢 LOW"). - How it helps the data: Enables downstream multi-agent specialists to trigger immediate Force Majeure and route diversions for high-severity events.
- Purpose: Determines what kind of transport is affected—identifying whether a strike halts long-haul road trucks, freight trains, port containers, or city delivery vans:
"bus","truck","railway","auto","taxi","metro","bandh", or"general".
- Input Parameters:
text (str). - Output Return Type:
str— Modality tag string. - How it helps the data: Allows the system to assess whether a strike impacts FTL road freight, intermodal rail, or local final-mile courier delivery.
File Location: d:\Progamming\O2C_AI\modules\weather_policy_generator.py
Class: WeatherPolicyGenerator
Purpose (Automated Safety Officer): Turns raw weather numbers into official, easy-to-read Word policy documents. It specifies exact safety rules—such as pulling high-sided trucks off the road in gale winds or requiring potency lab checks for heat-exposed pet diets.
- Purpose: Verifies access to local AI language models and prepares the regulatory storage folder where official weather rulebooks are saved.
- Input Parameters:
db_path (str | Path),output_dir (Path | None). - Output Return Type: None.
- How it helps the data: Establishes the generation output directory and verifies LLM synthesis availability.
- Purpose: Reviews all extreme weather alerts across the nation and publishes official Word policy rulebooks for impacted delivery cities and national logistics corridors.
- Input Parameters: None.
- Output Return Type:
list[str]— List of 6 generated.docxfile paths. - How it helps the data: Converts raw numbers (e.g. 42°C, 32.9 m/s wind, 25mm rain) into high-density structured tables and discrete rule blocks that the RAG engine can cite during agentic adjudication.
- Purpose: Scans weather records to isolate genuine safety hazards—filtering for extreme heat (>40°C), heavy rain (>20mm/hr), gale winds (>15m/s), or dense fog (<1km):
WHERE temperature > 40 OR wind_speed > 15 OR rain_1h > 20 OR visibility_km < 1
- Input Parameters: None.
- Output Return Type:
list[dict]— List of extreme weather alert records. - How it helps the data: Filters out benign telemetry to isolate critical environmental disruptions.
- Purpose: Sorts scattered weather warnings into organized city-by-city bundles so each regional dispatch team receives a customized local protocol.
- Input Parameters:
alerts (list[dict]). - Output Return Type:
dict[str, list[dict]]— Dictionary mapping city names to alert lists. - How it helps the data: Organizes national telemetry into city-level regional clusters.
-
Purpose: Drafts an official, plain-English logistics rulebook for a city, detailing critical highway bottlenecks, truck safety suspensions during 55+ km/h winds, and quality holds for heat-sensitive drugs:
- Header & Telemetry Metadata: Exact observed extremes (Peak Temp, Peak Wind, Peak Rain, Min Visibility).
- Section 1: Extracted Hazard Profile (AI Synthesis): Qwen2.5 extracts Primary Hazard Vector (e.g. Gale Force Crosswinds 32.9 m/s), Secondary Hazard Vector (ambient humidity/drizzle), and Critical Risk Window (linehaul speed reduction).
- Section 2: Logistics Corridor & Choke Point Risk Matrix (Table): Word Table detailing major freight arteries (e.g. Hyderabad ORR, Mumbai NH-48, Chennai NH-16) with specific hazard ratings and mandatory fleet directives.
-
Section 3: Binding Operational & QA Directives (Discrete Rules):
-
[RULE-W-{CITY}-01]: Trailer equipment suspension (wind$\ge 15\text{ m/s}$ / rain$\ge 20\text{ mm/h}$ ). -
[RULE-W-{CITY}-02]: Force Majeure Clause 4.2 penalty waiver ($500/day $ \to$ $0.00). -
[RULE-W-{CITY}-03]: Cold-Chain HPLC testing ($>40^\circ\text{C}$ for$>4\text{h}$ ) and moisture probe threshold ($>12%$ ). -
[RULE-W-{CITY}-04]: Dynamic ETA safety buffer (+4h to +8h) and $150 redelivery fee waiver.
-
- Section 4: Copilot Deterministic Action Checklist: Step-by-step verification checklist for downstream agents.
-
Input Parameters:
city (str),alerts (list[dict]). -
Output Return Type:
pathlib.Path— Path to generated Word document. - How it helps the data: Encodes legal rules and operational constraints into structured, citeable text documents for the RAG engine.
- Purpose: Combines regional storm data into a nationwide master weather protocol, establishing overarching corporate liability and force majeure guidelines across all transport routes.
- Input Parameters:
alerts (list[dict]). - Output Return Type:
pathlib.Path— Path to master protocol document. - How it helps the data: Synthesizes nationwide multi-corridor weather impacts into a single sovereign operational guide.
File Location: d:\Progamming\O2C_AI\modules\strike_intelligence_generator.py
Class: StrikeIntelligenceGenerator
Purpose (Disruption Security Analyst): Converts scattered strike news reports into actionable security briefings. It maps out bypass detours around blocked highways and sets contract rules like capping daily truck delay fees at $100.
- Purpose: Connects the intelligence generator to local AI reasoning services and prepares the document directory where strike intelligence briefs are stored.
- Input Parameters:
db_path (str | Path),output_dir (Path | None). - Output Return Type: None.
- How it helps the data: Prepares file storage directories and verifies local LLM connection.
- Purpose: Processes hundreds of scattered news reports to generate comprehensive disruption briefs for every impacted city and transport modality (truck, rail, port).
- Input Parameters: None.
- Output Return Type:
list[str]— List of 17 generated.docxdocument paths. - How it helps the data: Synthesizes hundreds of isolated web articles into actionable intelligence briefs with structured tables and discrete rule blocks.
- Purpose: Gathers verified strike and protest reports from the database into active memory for AI analysis and document authoring.
- Input Parameters: None.
- Output Return Type:
list[dict]— Raw strike news records. - How it helps the data: Loads verified transport disruption articles from SQLite storage into RAM.
- Purpose: Organizes news reports by metropolitan area so local dispatchers can see every active protest occurring along their delivery routes.
- Input Parameters:
articles (list[dict]). - Output Return Type:
dict[str, list[dict]]— Dictionary mapping cities to lists of disruption articles. - How it helps the data: Aggregates isolated incidents into city-specific incident registries.
- Purpose: Groups disruption reports by freight modality (truck, train, port, or general bandh) to measure the risk to specific shipping methods.
- Input Parameters:
articles (list[dict]). - Output Return Type:
dict[str, list[dict]]— Dictionary mapping transport modalities to articles. - How it helps the data: Categorizes incidents into modal vulnerability vectors for rail, road, and port transit.
-
Purpose: Compiles a detailed city intelligence brief featuring incident tables, highway bypass directives (like diverting trucks onto peripheral expressways), and carrier detention fee caps ($100/day):
- Header & Severity Counts: Exact incident count and severity breakdown (🔴 High / 🟡 Medium / 🟢 Low).
-
Section 1: Extracted Disruption Incident Registry (Table): High-density Word Table with columns:
Incident ID & Date,Source,Disruption Modality,Stated Trigger / Demand,Freight Impact Severity. - Section 2: Critical Bottlenecks & Highway Bypass Directives: Primary national highway choke points (e.g. NH-44 Kundli Border, NH-48 Bhiwandi) and recommended FTL bypasses (e.g. KMP Expressway).
-
Section 3: Autonomous Copilot Adjudication & Legal Rules (Discrete Rules):
-
[RULE-S-{CITY}-01]: Force Majeure SLA penalty waiver (Section 8.4) for blockades$>12\text{h}$ . -
[RULE-S-{CITY}-02]: Emergency $1,000 Air Freight replacement for specialty diets (sap_mara.specialty_diet_flag = 1). -
[RULE-S-{CITY}-03]: Carrier truck detention liability cap ($100.00/day per vehicle). -
[RULE-S-{CITY}-04]: Dynamic transit buffer (+12h to +24h) and clinic delivery rescheduling.
-
- Section 4: Copilot Deterministic Action Checklist: 4-step execution checklist for AI agents.
-
Input Parameters:
city (str),articles (list[dict]). -
Output Return Type:
pathlib.Path— Path to generated city brief. - How it helps the data: Converts disparate news headlines into standardized legal contracts and bypass directives for RAG indexing.
- Purpose: Produces a transport modality analysis detailing vulnerability trends, container demurrage caps ($500/day for rail), and warehouse lockdown procedures.
- Input Parameters:
category (str),articles (list[dict]). - Output Return Type:
pathlib.Path— Path to modal pattern brief. - How it helps the data: Generates modality-level carrier contract liability benchmarks.
- Purpose: Publishes an executive-level nationwide disruption brief outlining high-risk transport arteries and multi-agent coordination strategies.
- Input Parameters:
articles (list[dict]). - Output Return Type:
pathlib.Path— Path to master intelligence document. - How it helps the data: Synthesizes nationwide transport disruption patterns into an executive policy reference.
| Component / Module | Input Data | Transformation / Function | Output Data Artifact | Downstream Consumer |
|---|---|---|---|---|
modules/config.py |
Environment Variables & OS paths | Dynamic platform resolution (_resolve_project_root) |
Standardized Path objects & RAG constants |
All 12 project modules |
modules/weather_service.py |
Open-Meteo & OWM APIs | JSON payload normalization & WMO decoding | List of weather dictionaries | DatabaseManager.write_weather |
modules/news_service.py |
Google News RSS XML | NLP entity extraction, severity & modality classification | List of enriched news dictionaries | DatabaseManager.write_strikes |
modules/database_manager.py |
Sensor & News Dictionaries | ACID SQLite transactions with WAL concurrency | india_monitor.db tables |
MLDatabaseExtension & RAG Engine |
modules/weather_policy_generator.py |
SQLite weather_readings |
Threshold filtering, Qwen2.5 hazard extraction & Word tables | 6 .docx Weather Protocols |
modules/rag_engine.py (Hybrid RAG) |
modules/strike_intelligence_generator.py |
SQLite strike_news |
Regional aggregation, Qwen2.5 trigger extraction & Word tables | 17 .docx Strike Briefs |
modules/rag_engine.py (Hybrid RAG) |
5. 🧠 Architectural Rationale: Why Operational & Legal Rules are Embedded in Generated Knowledge Documents
A common question in enterprise AI architecture is:
“Why embed operational playbooks and contract rules inside generated .docx files instead of hardcoding everything in Python code?”
In an enterprise Order-to-Cash environment, an autonomous AI model (or Large Language Model) cannot make financial decisions (e.g. waiving a $500 SLA penalty, triggering a $1,000 emergency replacement air freight, or locking $25,000 of inventory in a QA quarantine hold) based purely on black-box heuristics or hallucinated prompt weights.
By structuring regulatory, operational, and contract rules directly into the RAG corpus:
- Zero Hallucination: When an order is evaluated (e.g. in Mumbai during a 42°C heatwave), Engine B RAG retrieves the exact excerpt from
Mumbai_Weather_Protocol.docxandPlatinum Tier Delivery Framework.docx. - Immutable Audit Trail: Every daily decision exported to
daily_agent_report.jsoncontains explicit citations (rag_sources) that legal, financial, and logistics directors can inspect and verify. - Multi-Agent Dispute Resolution: The 4 autonomous specialist agents (Route Supervisor, Contract Adjudicator, Quality Mitigation, and ERP Action Executor) use these retrieved clauses to resolve conflicting priorities (e.g. Contract Agent wants to fine the carrier, but the Policy Document proves the heatwave was a legally recognized Act of God under Section 4.2).
To prevent narrative filler essays while eliminating manual document editing, the system divides responsibilities between deterministic rules and local AI (Qwen2.5):
| Task / Document Component | Handled By | Why This Division is Used |
|---|---|---|
| Telemetry Extremes & Peak Figures | ⚙️ Deterministic Rule Engine | Queries SQLite directly (Peak Temp: 31.1°C, Peak Wind: 32.9 m/s). Guarantees 100% mathematical accuracy with zero AI drift. |
| Semantic Hazard Extraction | 🤖 AI (Qwen2.5 via Ollama) | Evaluates combined telemetry and extracts the core hazard vectors (e.g. identifying |
| Corridor Bottleneck Matrix Tables | 🤖 AI + ⚙️ Infrastructure Rules | Correlates city topography (ORR, port CFS, elevated expressways) against the active hazard to formulate specific road directives. |
| Unstructured Incident Entity Extraction | 🤖 AI (Qwen2.5 via Ollama) | Reads messy scraped web news descriptions and extracts clean Stated Triggers / Demands (e.g. "Diesel VAT & E-Way Bill Dispute"). |
| Binding Contract Caps & Discrete Rule IDs | ⚙️ Deterministic Rule Engine | Injects exact financial caps ($500/day, $1,000 air freight, $100/day detention) and discrete identifiers ([RULE-W-HYD-01], [RULE-S-DEL-02]). |
End of Part 1 Specification. Part 2 covers Knowledge Vectorization (Engine B Hybrid RAG) & Predictive Feature Store (Engine A ML).
Every file, table, serialized model artifact, and index used in Part 2 is mapped below:
sap_vbak: Sales Order Header (Order IDvbeln, Customerkunnr, Order Dateerdat, Requested Delivery Datevdatu, Net Valuenetwr).sap_vbap: Sales Order Items (Order IDvbeln, Lineposnr, SKUmatnr, Quantitykwmeng, Unit Pricenetpr).sap_likp: Outbound Delivery Header (Deliveryvbeln, Customerkunnr, Planned Goods Issuewadat, Shipping Pointvstel).sap_lips: Outbound Delivery Items (Deliveryvbeln, Lineposnr, Sales Ordervgbel, Gross Weightbrgew).sap_vttk: Shipment Linehaul Header (Shipmenttknum, Carrierlifnr, Transport Modevsart, Planned Departuredpabf, Statusstatus).sap_vttp: Shipment Items Bridge (Shipmenttknum, Itemtpnum, Deliveryvbeln).sap_kna1: Customer Master General (Customerkunnr, Namename1, Cityort01, Regionregio, Postal Codepstlz).sap_knvv: Customer Master Sales & SLAs (Customerkunnr, Tiercustomer_tier[Platinum/Gold/Silver], Receiving Dock Closing Timeclose_time).sap_lfa1: Carrier / Vendor Master (Carrierlifnr, Namename1, Cityort01, Contacttelf1).sap_mara: Material / SKU Master (Materialmatnr, Descriptionmaktx, Specialty Diet Flagspecialty_diet_flag, Shelf Life Monthsshelf_life_mos).ml_predictions: Persisted model inferences (Order ID, Delivery ID, Delay Probability, Delay Hours, Root Cause, Financial Risk USD, Timestamp).
rf_classifier.pkl: SerializedRandomForestClassifier(50 estimators, max depth 5) for binary delay probability scoring.gb_regressor.pkl: SerializedGradientBoostingRegressor(50 estimators, max depth 4) for continuous delay hour duration estimation.feature_importances.json: Global feature weights for Explainable AI (XAI) feature attribution percentages.
chunks/all_chunks.json: Human-readable JSON array containing all 909 vectorized text chunks with metadata (source document, category, chunk ID, char length, token count).vector_store/index.faiss: Dense FAISSIndexFlatIPvector index containing 909 normalized 384-dimensional embeddings.vector_store/bm25.pkl: Serialized Okapi BM25 sparse keyword lexicon (frequencies, inverted document frequenciesidf, average document length).vector_store/metadata.pkl: Serialized list of chunk metadata dictionaries matching FAISS internal vector IDs 1-to-1.vector_store/chunks.pkl: Serialized list of full raw chunk texts for sub-millisecond document reconstruction.
The Predictive Machine Learning engine (Engine A) is trained on 19 canonical engineered features extracted and synthesized from the 10 relational SAP ERP tables.
The table below outlines every single data point fed into RandomForestClassifier (Delay Probability) and GradientBoostingRegressor (Delay Hours):
| # | Feature Name | Source SAP Table & Column | Mathematical Transformation / Logic | Data Type & Range | Logistics Business Signal | Feature Importance (%) |
|---|---|---|---|---|---|---|
| 1 | order_to_delivery_days |
sap_vbak.vdatu - sap_vbak.erdat
|
|
Float ( |
Turnaround Window: Shorter windows ( |
14.2% |
| 2 | order_to_departure_days |
sap_vttk.dpabf - sap_vbak.erdat
|
|
Float ( |
Warehouse Dwell: Measures picking, staging, and carrier tender latency at the origin DC. | 8.6% |
| 3 | days_since_order |
sap_vbak.erdat vs |
Float ( |
Order Aging: Tracks how long an order has remained active in the ERP pipeline. | 4.1% | |
| 4 | days_until_delivery |
sap_vbak.vdatu vs |
Float ( |
SLA Imminence: Negative values indicate current delivery backlog or imminent SLA breach. | 5.3% | |
| 5 | total_quantity |
sap_vbap.kwmeng
|
Aggregate sum of all line item order quantities for order vbeln
|
Float ( |
Order Volume: High item quantities increase pallet building complexity. | 3.8% |
| 6 | total_weight |
sap_lips.brgew
|
Aggregate sum of gross line item weights in kilograms | Float ( |
Physical Payload: Heavy shipments require dedicated FTL equipment and mechanical loading docks. | 11.5% |
| 7 | weight_per_unit |
total_weight / total_quantity
|
Float ( |
Packaging Density: Differentiates heavy bulk bags (e.g. 20kg dry kibble) from lightweight pharmaceutical blister packs. | 4.7% | |
| 8 | is_heavy_shipment |
total_weight |
Binary ( |
Heavy Freight Flag: Identifies multi-pallet consignments requiring hydraulic tailgates or dock levelers. | 6.2% | |
| 9 | has_specialty_diet |
sap_mara.specialty_diet_flag |
Binary ( |
Product Fragility: Flags veterinary prescription diets, biologics, and clinical probiotics requiring thermal care. | 7.4% | |
| 10 | min_shelf_life |
sap_mara.shelf_life_mos
|
Minimum remaining shelf-life across all ordered SKUs in months | Integer ( |
Spoilage Vulnerability: Short-dated products ( |
3.5% |
| 11 | customer_tier_code |
sap_knvv.customer_tier |
Ordinal Int ( |
SLA Severity: Platinum clinics have strict $500/day penalties and mandatory pre-17:00 delivery slots. | 6.8% | |
| 12 | shipping_risk_code |
sap_vttk.vsart |
Ordinal Int ( |
Modality Risk: LTL multi-stop consolidation incurs high terminal dwell; Rush freight has high variance. | 9.1% | |
| 13 | status_code |
sap_vttk.status |
Ordinal Int ( |
Live Telematics State: Real-time indicator of active transit disruptions. | 15.8% | |
| 14 | haversine_distance_km |
sap_kna1.ort01 coordinates |
Great-circle Haversine distance from Mumbai Central DC ( |
Float ( |
Geospatial Corridor Length: Inter-state long-hauls cross multiple state toll plazas and weather zones. | 10.4% |
| 15 | required_transit_speed_kmh |
haversine_distance_km / order_to_delivery_days
|
Float ( |
Speed Feasibility: Measures the required linehaul velocity to satisfy the promised delivery date. | 12.9% | |
| 16 | is_unrealistic_speed |
required_transit_speed_kmh |
Binary ( |
Infeasible SLA Flag: Commercial trucks in India average 35–45 km/h; |
16.1% | |
| 17 | order_day_of_week |
sap_vbak.erdat |
Discrete Int ( |
Weekly Operational Rhythm: Captures carrier dispatch schedules and weekly freight volumes. | 2.3% | |
| 18 | is_weekend_order |
order_day_of_week |
Binary ( |
Weekend Dock Closure: Destination veterinary clinics are closed on Sundays, causing Monday delivery backlogs. | 7.2% | |
| 19 | is_month_end |
sap_vbak.erdat day |
Binary ( |
Month-End Congestion Surge: End-of-month commercial sales pushes create warehouse dock gridlock. | 5.9% |
To model real-world road corridor transit distances across India without requiring slow external routing APIs for 11,797+ orders, the engine computes the great-circle Haversine distance between the central distribution origin (
Logistics delays often occur not because of carrier breakdown, but because sales teams promise delivery windows that are physically impossible for commercial road freight:
During initial dataset preparation, a multi-signal risk heuristic establishes ground-truth labels for supervised training:
To solve the zero-inflation problem (79% on-time orders with 0h delay vs 21% delayed orders with 24–96h delays), the engine deploys an enterprise Two-Stage Hurdle (Classification-Gated) Architecture:
[Raw SAP Relational Data (62,299 records)]
│
▼
[Feature Engineering Pipeline (19 Features)]
│
▼
[Stratified Split: 80% Train, 20% Holdout Test]
│
┌─────────────────────────────────┴─────────────────────────────────┐
▼ ▼
[Stage 1: Classification Training] [Stage 2: Conditional Regression Training]
• Full Dataset (62,299 rows) • Delayed Subset ONLY (is_delayed == 1)
• Target: is_delayed (0 or 1) • Target: delay_hours (>= 12.0h)
• Model: RandomForestClassifier • Model: GradientBoostingRegressor
- n_estimators=100, max_depth=6 - loss='huber', learning_rate=0.08
- class_weight='balanced' - n_estimators=100, max_depth=5
│ │
▼ ▼
[Output: rf_classifier.pkl] [Output: gb_regressor.pkl]
• Accuracy: 97.10% • Regressor MAE: 5.63 Hours
• ROC-AUC: 0.9958 • Ghost False Alarms: 0.00 Hours
| Evaluation Metric | Model / Stage | Score | Logistics Operational Significance |
|---|---|---|---|
| Accuracy | Stage 1 (RandomForestClassifier) |
97.10% (0.9710) |
Overall proportion of correct on-time vs delayed gating decisions. |
| Precision | Stage 1 (RandomForestClassifier) |
97.49% (0.9749) |
When flagged for delay, the model is correct 97.5% of the time. |
| Recall | Stage 1 (RandomForestClassifier) |
86.45% (0.8645) |
Proactively captures 86.5% of all true supply chain bottlenecks. |
| F1-Score | Stage 1 (RandomForestClassifier) |
91.63% (0.9163) |
Balanced classification performance under 4:1 class imbalance. |
| ROC-AUC | Stage 1 (RandomForestClassifier) |
0.9958 (99.58%) |
Near-perfect probability ranking and class separation. |
| On-Time MAE | Two-Stage Gated Output | 0.00 hrs | Zero-delay hurdle gate completely eliminates false-alarm ghost delays on the 79% on-time orders. |
| Two-Stage MAE | Stage 1 + Stage 2 Combined | 5.63 hrs | Reduced overall mean absolute error by ~30% (down from 7.99 hrs). |
| R² Score ( |
Stage 2 Conditional Regressor |
0.8636 (86.36%) |
86.4% of variance in actual delay magnitude is captured by Huber loss gradient boosting. |
-
True Negatives (TN):
9,834(On-time shipments correctly identified with$0\text{h}$ delay) -
False Positives (FP):
41(On-time shipments flagged for review — 0.41% false alarm rate) -
False Negatives (FN):
355(Borderline shipments enriched downstream by live weather/strike RAG) -
True Positives (TP):
2,230(Delayed shipments gated to Stage 2 regressor for precise ETA calculation)
A vital architectural distinction in the O2C Copilot is the boundary between Static Supervised ML Features and Dynamic Streaming Environmental Signals:
┌────────────────────────────────────────────────────────┐
│ 1. STATIC SUPERVISED ML (19 Canonical ERP Features) │
└────────────────────────────────────────────────────────┘
Historical SAP ERP Tables (62,299 Orders)
│
▼
Two-Stage Hurdle ML Models (RandomForest + Huber Regressor)
│
▼
Baseline Predictions:
• P(delay) = 0.77
• Baseline Delay = 43.0 Hours
│
├───────────────────────────────────────────────────────┐
▼ ▼
┌──────────────────────────────────────────────┐ ┌──────────────────────────────────────────────┐
│ 2. DYNAMIC LIVE MODIFIERS (Zero-Leakage) │ │ 3. LEGAL RAG CONTEXT (Engine B) │
└──────────────────────────────────────────────┘ └──────────────────────────────────────────────┘
Live Weather & Strike Telemetry from SQLite Contract & Policy Retrieval
• Severe Heatwave (>40°C): +12.0h, +0.10 P(delay) • Clause 4.2: Force Majeure Relief
• Transport Strike Alert: +12.0h, +0.10 P(delay) • Section 8.4: 72h Penalty Waiver
│ │
└───────────────────────────────┬───────────────────────┘
▼
[FINAL ENRICHED PREDICTION]
• Calibrated P(delay) = 0.87 (87.1%)
• Calibrated Delay = 67.0 Hours
• Force Majeure Candidate = YES
• Financial SLA Penalty = WAIVED
- Temporal Asynchrony: Historical ERP orders from 6 months ago have no valid snapshot of today's live temperature sensor readings or this morning's flash highway protest. Baking live streaming data as static training columns would cause severe data leakage and synthetic distortion.
-
Three-Tier Operational Role:
-
Real-Time Duration & Risk Escalation: In
predict_delivery_delay(), if a live strike matches the destination corridor, the engine dynamically injects$+12.0\text{ hours}$ todelay_hoursand$+0.10$ todelay_prob. -
Contractual Force Majeure Gateway: If extreme heat (
$>40^\circ\text{C}$ ), gale winds ($>15\text{m/s}$ ), heavy rain ($>20\text{mm/hr}$ ), or verified bandhs are detected,force_majeure_applicableis set toTrue, triggering legal penalty waivers under Section 4.2 / Section 8.4. -
Quality Assurance Quarantine Trigger: Sustained temperatures
$>40^\circ\text{C}$ for$>4\text{h}$ automatically mandate HPLC assay testing and a 20% shelf-life reduction for veterinary therapeutics (QA Policy 2024-03).
-
Real-Time Duration & Risk Escalation: In
File Location: d:\Progamming\O2C_AI\modules\ml_db_extension.py
Class: MLDatabaseExtension
Purpose (SAP Enterprise Data Bridge): Takes messy business spreadsheets from SAP (orders, customer contracts, truck assignments, and warehouse dispatches) and weaves them into a single, clean tracking master sheet so the AI can predict delivery bottlenecks.
- Purpose: Establishes a high-performance database connection with fast memory caching so the AI can retrieve order features in less than one millisecond without lag.
- Input Parameters:
db_path (Path | None)— Target database path. - Output Return Type: None.
- How it helps the data: Guarantees thread-safe access and high-throughput zero-lock read/write operations during batch inference.
- Purpose: Creates normalized relational tables mimicking real-world SAP ERP systems (sales orders, delivery notes, shipping manifests, customer masters, and material masters).
- Input Parameters: None.
- Output Return Type: None.
- How it helps the data: Enforces relational integrity across sales orders, warehouse deliveries, linehaul shipments, and customer tiers.
- Purpose: Ingests raw business export files into the database, cleaning up messy text and whitespace to prepare enterprise data for AI training.
- Input Parameters:
input_dir (Path)— Directory containing SAP CSV files. - Output Return Type:
Dict[str, int]— Dictionary mapping table names to ingested row counts. - How it helps the data: Populates the ERP foundation of the AI system from flat enterprise exports.
- Purpose: Combines 10 separate SAP business spreadsheets into a unified master tracking sheet, computing key logistics metrics like travel distances, required road speeds, and weekend dock congestion.
-
Input Parameters:
force_refresh (bool)— IfTrue, bypasses cache and re-queries SQLite. -
Output Return Type:
pd.DataFrame— 62,299+ rows with all engineered feature columns. -
Engineered Feature Transformations:
-
order_to_delivery_days:$\text{RDD} - \text{Order Date}$ (Turnaround window). -
order_to_departure_days:$\text{Departure Date} - \text{Order Date}$ (Warehouse dwell). -
total_weight&weight_per_unit:$\frac{\text{Total Gross Weight}}{\max(\text{Total Quantity}, 1.0)}$ . -
is_heavy_shipment: Binary indicator$(1 \text{ if } \text{Total Weight} > 1000.0\text{ kg else } 0)$ . -
haversine_distance_km: Great-circle distance calculated from origin central distribution hub (Mumbai:$19.0760^\circ\text{N}, 72.8777^\circ\text{E}$ ) to destination city coordinates:$$\Delta\sigma = 2 \arcsin\left(\sqrt{\sin^2\left(\frac{\Delta\phi}{2}\right) + \cos\phi_1\cos\phi_2\sin^2\left(\frac{\Delta\lambda}{2}\right)}\right), \quad d = R \cdot \Delta\sigma$$ -
required_transit_speed_kmh:$\frac{\text{haversine_distance_km}}{\max(1.0, \text{order_to_delivery_days} \times 24.0)}$ . -
is_unrealistic_speed: Binary flag$(1 \text{ if } \text{required speed} > 55.0\text{ km/h else } 0)$ . -
is_weekend_order: Binary flag$(1 \text{ if day of week} \ge 4 \text{ [Fri/Sat/Sun] else } 0)$ . -
is_month_end: Binary flag$(1 \text{ if day of month} \ge 26 \text{ else } 0)$ to capture warehouse dispatch congestion surges. -
customer_tier_code: Categorical integer mapping:$\text{Platinum} \to 3, \text{Gold} \to 2, \text{Independent} \to 2, \text{Silver/Standard} \to 1$ . -
shipping_risk_code: Modal risk mapping:$\text{Rush} \to 3, \text{LTL} \to 2, \text{FTL/Rail/Intermodal} \to 1, \text{Air} \to 0$ .
-
- How it helps the data: Assembles disparate relational tables into a unified mathematical vector space optimized for supervised model training and sub-millisecond batch lookups.
- Purpose: Instantly pulls the complete business history and shipping profile for any specific order in less than one millisecond from memory.
- Input Parameters:
order_id (str)— SAP Sales Order Number (e.g."800000000000001"). - Output Return Type:
Optional[Dict[str, Any]]— Complete order feature dictionary orNone. - How it helps the data: Eliminates redundant disk I/O and SQL joins during real-time multi-agent order evaluation.
- Purpose: Permanently logs every AI prediction (delay probability, estimated hours late, and financial exposure) into an unalterable audit ledger.
- Input Parameters:
prediction (Dict[str, Any])— Inference result dictionary. - Output Return Type:
int— Auto-incrementedprediction_id. - How it helps the data: Maintains an immutable historical audit trail of all model predictions for model drift analysis.
- Purpose: Retrieves historical AI prediction records from the database, allowing managers to review past delay forecasts and audit model accuracy.
- Input Parameters:
limit (int)— Maximum number of records to return (default 100),delayed_only (bool)— IfTrue, filters strictly foris_delayed == 1. - Output Return Type:
List[Dict[str, Any]]— List of historical prediction dictionaries. - How it helps the data: Supplies historical baseline inferences to dashboard visualizers and audit engines.
- Purpose: Calculates high-level business summary metrics (total revenue volume, customer tier counts, and delay rates) to monitor overall supply chain health.
- Input Parameters: None.
- Output Return Type:
Dict[str, Any]— Key-value dictionary of high-level system telemetry. - How it helps the data: Provides pipeline health telemetry and data completeness verification before running daily inference batches.
File Location: d:\Progamming\O2C_AI\modules\predictive_engine.py
Class: PredictiveEngine
Purpose (The Predictive ML Brain): The mathematical heart of the AI. It uses historical shipping patterns to predict which orders will be late, exactly how many hours late they will be, and which real-world factors (like highway blockades or closed receiving docks) are causing the delay.
- Purpose: Sets up the predictive engine, defining the 19 standard logistics risk factors and loading live weather and strike alerts into fast memory.
- Input Parameters:
ml_db_extension (MLDatabaseExtension | None),rag_engine (RAGEngine | None),weather_service (WeatherService | None). - Output Return Type: None.
- How it helps the data: Binds relational data access, semantic RAG retrieval, and real-time sensor streams into a unified predictive runtime.
- Purpose: Pre-loads live city weather readings and highway strike news into computer memory, speeding up batch predictions from minutes to seconds.
- Input Parameters: None.
- Output Return Type: None.
- How it helps the data: Eliminates per-order disk I/O, reducing batch evaluation runtime across thousands of orders to sub-second speeds.
-
Purpose: Teaches the AI how to predict delays using historical shipment data with a two-step approach: first separating on-time orders to eliminate false alarms, then accurately calculating delay hours for high-risk shipments:
-
Stage 1 (Classification Gate): Fits
RandomForestClassifier(n_estimators=100, max_depth=6, random_state=42)on fullX_trainagainst binaryy_train_cls(Stratified). Evaluates gate accuracy ($97.10%$ , Precision$97.49%$ , ROC-AUC$0.9958$ ). -
Stage 2 (Conditional Hurdle Regressor): Filters training samples strictly to delayed orders (
y_train_cls == 1), fittingGradientBoostingRegressor(loss='huber', n_estimators=100, max_depth=5, learning_rate=0.08, random_state=42)on actual delay hours. -
Two-Stage Gated Evaluation: Evaluates test set using the classification gate:
$$\hat{y}{\text{hours}} = \begin{cases} \max(12.0, \hat{y}{\text{reg}}) & \text{if } P(\text{delay}) \ge 0.40 \ 0.0 & \text{if } P(\text{delay}) < 0.40 \end{cases}$$
Reduces overall MAE from
$7.99\text{h}$ to$5.63\text{h}$ (a 30% error reduction) and completely eliminates ghost delays on on-time orders. - Extracts Gini feature importances and calls
save_models().
-
Stage 1 (Classification Gate): Fits
-
Input Parameters:
df (pd.DataFrame)— Feature-engineered dataset fromMLDatabaseExtension,train_size (float)— Train/test split ratio (default 0.8). -
Output Return Type:
bool—Trueif training succeeded,Falseotherwise. - How it helps the data: Trains mathematically robust models that generalize without suffering from zero-inflation distortion.
- Purpose: Packages and locks in the trained AI models and importance weights to disk so the software can make instant predictions without needing to re-learn each time.
- Input Parameters:
model_dir (Path | None)— Directory to persist model files (defaultindia_monitor_data/models/). - Output Return Type:
bool—Trueif all 3 artifacts saved successfully. - How it helps the data: Locks in trained weights so the inference engine can execute instantly without retraining on each pipeline run.
- Purpose: Loads previously trained AI models from disk into active memory on startup in under 50 milliseconds for zero-delay operations.
- Input Parameters:
model_dir (Path | None)— Path to directory containing model files. - Output Return Type:
bool—Trueif artifacts loaded successfully. - How it helps the data: Restores decision boundaries and feature weights into memory in under 50 milliseconds.
- Purpose: Eliminates the 'black-box' mystery by calculating clear percentage attributions—showing managers exactly which real-world factors (e.g. 85% highway blockade, 10% dock closure) caused the delay forecast.
- Input Parameters:
order_data (Dict[str, Any])— Single order feature dictionary,delay_prob (float)— Predicted delay probability. - Output Return Type:
List[Dict[str, Any]]— Top contributing risk factors with human-readable explanations and percentage contributions. - How it helps the data: Transforms black-box ML probability scores into transparent root-cause narratives displayed in MS Teams Adaptive Cards and executive audit logs.
7. predict_delivery_delay(self, order_id: str, order_data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]
-
Purpose: Performs an end-to-end risk evaluation for an order, forecasting delay probability, calculating hours late, identifying root causes, assessing late penalties, and retrieving legal policy rules:
- Fetches feature vector from
MLDatabaseExtension. - Evaluates Two-Stage Hurdle models: Computes
delay_prob(Stage 1 Classifier). If$P \ge 0.40$ , evaluates Stage 2 Regressor fordelay_hours($\ge 12.0\text{h}$ ); otherwise sets `delay_hours = 0.0\text{h}$. - Checks live environmental caches (
self._weather_cacheandself._strike_cache) for real-time adjustments ($+12\text{h}$ delay and$+0.10$ probability for active strikes; thermal/moisture risk diagnoses for extreme weather). - Calculates financial exposure and contractual SLA penalties (Platinum Tier: $500/day; Gold/Independent: 5%/day capped at 25%; $150 after-hours clinic violation).
- Computes revised Estimated Time of Arrival (ETA).
- Enriches result with retrieved policy context from Engine B RAG (
_enrich_with_rag).
- Fetches feature vector from
-
Input Parameters:
order_id (str)— SAP Sales Order Number,order_data (Dict[str, Any] | None)— Pre-fetched feature dictionary (optional). -
Output Return Type:
Dict[str, Any]— Comprehensive prediction payload consumed by Multi-Agent Specialists in Part 3. - How it helps the data: Merges historical statistical predictions with real-time sensory data and legal contract exposure into a single actionable record.
-
Purpose: Investigates the order to identify the primary and secondary operational reasons for delay—such as extreme 40°C heat, active highway strikes, or unrealistic delivery deadlines:
- Extreme Heatwave (
$>40^\circ\text{C}$ ) / Monsoon Flooding ($>20\text{mm/hr}$ ) / Gale Winds ($>15\text{m/s}$ ). - Active Transport Strike or Highway Blockade in destination city.
- Multi-Stop LTL Terminal Consolidation Dwell (
$>1000\text{kg}$ LTL). - Unrealistic Transit Velocity Demand (
$>55\text{km/h}$ required linehaul speed). - Weekend Dispatch / Receiving Dock Closure (Delivery scheduled after clinic close time).
- Month-End Warehouse Dispatch Congestion.
- Extreme Heatwave (
-
Input Parameters:
order_data (Dict[str, Any])— Order feature dictionary,is_delayed (bool)— Delay flag. -
Output Return Type:
Tuple[str, str]—(primary_root_cause, secondary_root_cause). - How it helps the data: Isolates the root operational driver so downstream specialist agents know whether to re-route, invoke legal waivers, or trigger QA holds.
9. _calculate_financial_risk(self, order_data: Dict[str, Any], delay_hours: float, is_delayed: bool) -> Dict[str, float]
-
Purpose: Calculates contractual delay penalties based on customer importance—such as $500/day for VIP Platinum clinics or 5%/day for Gold accounts—and flags perishable product spoilage risks:
- Platinum Tier Customers: $$500.00$ per 24-hour delay increment beyond requested delivery date.
-
Gold Tier Customers:
$5%$ of total order value per 24-hour delay increment (capped at$25%$ ). - Silver / Standard Customers: $$150.00$ fixed late delivery penalty.
-
Carrier Chargeback:
$100%$ chargeback of delay penalty to carrier unless protected by Force Majeure. -
Perishable Spoilage Risk: If therapeutic wet food or biologics delay exceeds product shelf-life tolerance (
$>48\text{h}$ in extreme heat), flags$100%$ order value destruction risk.
-
Input Parameters:
order_data (Dict[str, Any]),delay_hours (float),is_delayed (bool). -
Output Return Type:
Dict[str, float]—{"sla_penalty_usd", "carrier_chargeback_usd", "total_financial_risk_usd"}. - How it helps the data: Quantifies the financial impact of the delivery delay for automated executive approval gates.
- Purpose: Searches the company's legal policy library using the order's specific customer tier and carrier to attach relevant contract clauses to the prediction.
- Input Parameters:
order_data (Dict[str, Any])— Order details,root_cause (str)— Diagnosed root cause. - Output Return Type:
Dict[str, Any]— Retrieved policy excerpts, source document names, and clause citations. - How it helps the data: Grounds mathematical ML predictions in legally binding enterprise contract clauses.
File Location: d:\Progamming\O2C_AI\modules\rag_engine.py
Classes: DocumentLoader, ClauseAwareChunker, BM25Index, VectorStore, RAGQueryEngine, RAGEngine
Purpose (Corporate Legal Research Assistant): Reads and memorizes the company's entire 82-document library of contracts, SLAs, and safety guidelines. When a crisis occurs, it instantly retrieves the exact contract clauses and legal rules needed to handle the situation.
[82 Raw Corpus Policy Documents (DOCX, PDF, XLSX, TXT)]
│
▼
[modules/rag_engine.py: DocumentLoader]
• Reads table cells, clause headers, and text formatting
│
▼
[modules/rag_engine.py: ClauseAwareChunker]
• Two-stage hierarchical splitting along legal sections
• Targets 500 chars with 50-char overlapping window
│
▼
[909 Semantic Chunks (all_chunks.json)]
│
┌─────────────────┴─────────────────┐
▼ ▼
[Dense FAISS FlatIP] [Sparse Okapi BM25]
• SentenceTransformer • Robertson-Spärck Jones BM25
• 384-dimensional dense vectors • k1=1.5, b=0.75
• L2-normalized Inner Product • Exact keyword & clause ID matching
│ │
└─────────────────┬─────────────────┘
▼
[Reciprocal Rank Fusion (RRF Search)]
• Score = 1/(60 + Rank_FAISS) + 1/(60 + Rank_BM25)
• Eliminates false negatives from pure vector search
│
▼
[Top-K Authoritative Legal Policy Chunks]
(Attached to order risk evaluation for multi-agent reasoning)
Purpose: Opens and reads all types of company files (Word contracts, PDF agreements, Excel penalty charts, and text manuals) so the AI can learn from them.
- Purpose: Sets the file boundary for the digital policy library where customer SLAs, carrier agreements, and packaging guidelines are kept.
- Input Parameters:
doc_dir (Path | None). - Output Return Type: None.
- How it helps the data: Establishes the authoritative filesystem boundary for regulatory and contractual policy ingestion.
- Purpose: Scans and opens all policy files across the corporate library, reading Word docs, PDFs, Excel tables, and text files into unified digital records.
- Input Parameters: None.
- Output Return Type:
List[Dict]— List of loaded document objects containingfilename,filepath,category,text,char_count, andfile_type. - How it helps the data: Converts unparsed, disparate multi-format disk files into uniform memory dictionaries for chunking.
- Purpose: Reads text and formatted data tables inside Microsoft Word contract files, preserving table layouts and clause headings.
- Input Parameters:
path (Path)— Absolute path to Word document. - Output Return Type:
str— Extracted, newline-delimited text. - How it helps the data: Preserves tabular corridor matrices and legal clause paragraphs in human-authored and AI-generated documents.
- Purpose: Reads through multi-page PDF regulatory documents and third-party carrier agreements, extracting readable text for the search index.
- Input Parameters:
path (Path)— Absolute path to PDF file. - Output Return Type:
str— Concatenated text from all document pages. - How it helps the data: Ingests third-party carrier contracts and external statutory transit guidelines into searchable text.
- Purpose: Converts corporate freight tariff sheets and penalty matrices in Excel into structured text that the search engine can understand.
- Input Parameters:
path (Path)— Absolute path to spreadsheet. - Output Return Type:
str— Tab-separated row-by-row string representation. - How it helps the data: Allows quantitative freight rate charts, detention penalty tiers, and historical ticket logs to be indexed semantically.
- Purpose: Ingests plain text runbooks, developer notes, and standard operating guidelines into the knowledge library.
- Input Parameters:
path (Path). - Output Return Type:
str. - How it helps the data: Ingests legacy configuration notes, SOP outlines, and developer runbooks.
Purpose: Slices long, complex legal documents into bite-sized paragraphs without cutting sentences or rules in half, ensuring the AI sees the complete context of every policy.
- Why Chunking is Needed: LLMs cannot read a 100-page carrier contract in a single prompt without confusion or extreme latency. Documents must be sliced into 300–600 character snippets ("chunks").
- Method 1: Fixed-Size Slicing ("The Meat Cleaver"): Cuts text every 500 characters blindly. If a sentence says "Carrier pays $500 penalty per day... unless an Act of God storm occurs, waiving all fines", it frequently cuts after "...per day", separating the penalty from the waiver! The AI sees only the fine and issues wrongful penalties during natural disasters.
- Method 2: Naive Paragraph Slicing ("The Blind Slicer"): Cuts on blank lines (
\n\n). Sub-clauses lose their parent headings, so the AI sees "Payment reduced by 25%" without knowing which customer tier or transport mode it governs. - Method 3: Clause-Aware Chunking ("The Intelligent Legal Paralegal"): Recognizes contract architecture (
Section 4.2,[RULE-W-HYD-02], numbered sub-clauses). It guarantees the Obligation + Penalty + Exception/Waiver remain in the exact same chunk, stamps document/section titles on every snippet, and maintains a 50-character sliding overlap.
| Evaluation Factor | Fixed-Size Slicing | Naive Paragraph Slicing | Clause-Aware Chunking (O2C Copilot) |
|---|---|---|---|
| Slicing Mechanism | Rigid character count | Splits on \n\n linebreaks |
Structural regex on legal clauses & rule IDs |
| Exception Preservation | ❌ Cuts waivers in half | ✅ 100% Preserved in same chunk | |
| Context Retention | ❌ Zero parent context | ❌ Loses governing section title | ✅ Document & section stamped on header |
| Risk of AI Hallucination | 🚨 Severe (amputated terms) | 🛡️ Near-Zero (complete legal thought) |
- Purpose: Sets the optimal paragraph slice size (500 characters) with a slight overlap so sentences and legal conditions are not cut off mid-thought.
- Input Parameters:
chunk_size (int),chunk_overlap (int). - Output Return Type: None.
- How it helps the data: Balances semantic granularity against sentence continuity for optimal dense embedding retrieval.
- Purpose: Slices long, multi-page legal documents into 909 digestible, bite-sized snippets that fit perfectly into AI reasoning prompts.
- Input Parameters:
documents (List[Dict])— List of loaded document dictionaries. - Output Return Type:
List[Dict]— List of 909 semantic chunk dictionaries with metadata and deterministic IDs. - How it helps the data: Transforms monolithic multi-page documents into digestible text snippets suitable for transformer token limits.
- Purpose: Intelligently cuts documents at natural legal boundaries (like 'Section 4.2', numbered clauses, or rule IDs) so penalties and conditions stay together:
- Primary Boundary Splitting: Splits at formal numbered clauses (
\n[0-9]+\.[0-9]*), ticket headers (TICKET\s+), section headers (SECTION\s+), and rule IDs ([RULE-). - Secondary Boundary Splitting: Accumulates sentences up to
chunk_sizecharacters withchunk_overlapcontinuity.
- Primary Boundary Splitting: Splits at formal numbered clauses (
- Input Parameters:
doc (Dict)— Single document dictionary. - Output Return Type:
List[Dict]— Chunk dictionaries containingchunk_id,text,filename,category, and token statistics. - How it helps the data: Guarantees that contractual conditions and their corresponding penalty dollar amounts remain bound together in the same chunk.
- Purpose: Generates a unique, tamper-proof digital fingerprint (hash ID) for every snippet, making every citation traceable and verifiable.
- Input Parameters:
text (str),filename (str),index (int). - Output Return Type:
str— 14-character unique chunk ID. - How it helps the data: Enables deduplication, immutable referencing, and exact citation tracking across pipeline runs.
- Purpose: Exports all 909 policy snippets to a clean JSON file so administrators can easily inspect and audit what the search engine has learned.
- Input Parameters:
chunks (List[Dict]),output_path (Path | None). - Output Return Type: None.
- How it helps the data: Provides human-readable JSON transparency and offline inspectability of the RAG knowledge base.
Purpose: Acts like an ultra-precise digital index, searching for exact words, section numbers (like 'Section 4.2'), and dollar penalties ($500) so specific contract terms are never overlooked.
- Purpose: Configures the exact-keyword search engine, tuning how aggressively it matches specific contract terms and section numbers.
- Input Parameters:
k1 (float),b (float). - Output Return Type: None.
- How it helps the data: Sets optimal sensitivity for legal acronyms and short contractual clauses.
- Purpose: Cleans search text into individual words while preserving critical symbols like section numbers ('4.2') and dollar signs ('$500').
-
Input Parameters:
text (str). -
Output Return Type:
List[str]. -
How it helps the data: Ensures clause citations (e.g.
4.2,8.4) and dollar penalties (e.g.$500,$1,000) are indexable terms.
-
Purpose: Catalogs how rare or common every word is across the policy library, ensuring that rare terms (like 'Force Majeure' or 'debit memo') receive high search priority:
$$\text{IDF}(q) = \ln\left(1.0 + \frac{N - df(q) + 0.5}{df(q) + 0.5}\right)$$ -
Input Parameters:
chunks (List[Dict])— Complete list of semantic chunks. - Output Return Type: None.
- How it helps the data: Establishes the sparse statistical lexicon across all 909 policy chunks.
-
Purpose: Searches the policy library for exact keyword matches, ensuring that specific section citations or contract codes are never missed:
$$\text{BM25}(D, Q) = \sum_{q \in Q} \text{IDF}(q) \cdot \frac{f(q, D) \cdot (k_1 + 1)}{f(q, D) + k_1 \cdot \left(1 - b + b \cdot \frac{|D|}{avgdl}\right)}$$ -
Input Parameters:
query (str)— Natural language search string,top_k (int)— Number of results,category (str | None)— Category filter. -
Output Return Type:
List[Dict]— Top-k scored chunk dictionaries withbm25_score. - How it helps the data: Guarantees exact keyword recall for specific contract codes (e.g. "Force Majeure 4.2" or "MHDRZ").
Purpose: Understands the conceptual meaning behind business questions—finding relevant policies even when the user searches with different wording than the contract.
- Purpose: Loads the deep-learning language model (SentenceTransformer) that understands the conceptual meaning of words beyond just keyword matching.
- Input Parameters:
model_name (str)— HuggingFace model identifier. - Output Return Type: None.
- How it helps the data: Instantiates dense neural semantic mapping capability.
- Purpose: Translates all 909 text snippets into mathematical concept maps (embeddings) and saves the search index to disk for sub-millisecond retrieval.
- Input Parameters:
chunks (List[Dict]). - Output Return Type: None.
- How it helps the data: Creates persistent vector structures supporting sub-millisecond similarity lookups.
- Purpose: Loads pre-built concept search indexes from disk into computer memory on startup, allowing the AI to search instantly without warm-up delays.
- Input Parameters: None.
- Output Return Type:
bool—Trueif indexes loaded successfully,Falseotherwise. - How it helps the data: Achieves zero-warmup cold-start latency for real-time agent queries.
-
Purpose: Executes a two-pronged search—combining exact keyword matching with conceptual AI understanding—and fuses the best results so the AI gets the most relevant legal clauses:
$$\text{RRF_Score}(d) = \frac{1}{60 + \text{Rank}{\text{dense}}(d)} + \frac{1}{60 + \text{Rank}{\text{BM25}}(d)}$$
Normalizes combined similarity score to
$[0.0, 1.0]$ . In-memory query cache ensures sub-millisecond retrieval. -
Input Parameters:
query (str),top_k (int),category (str | None). -
Output Return Type:
List[Dict]— Top-k fused chunks with similarity scores and source filenames. - How it helps the data: Combines semantic conceptual understanding (dense) with exact legal clause precision (sparse), eliminating retrieval blind spots.
Purpose: Formulates direct, human-friendly answers to business questions by citing exact policy clauses, section numbers, and monetary rules.
- Purpose: Connects the answer-generation engine to the hybrid search index so it can pull the right context to answer operational questions.
- Input Parameters:
vector_store (VectorStore). - Output Return Type: None.
- How it helps the data: Connects the query synthesis layer to the underlying hybrid index.
- Purpose: Takes an operational question, retrieves the top matching policy excerpts, and compiles a clean, cited answer payload for decision-makers.
- Input Parameters:
question (str),category (str | None),top_k (int). - Output Return Type:
Dict—{"question", "answer", "sources", "confidence", "chunks"}. - How it helps the data: Supplies self-contained knowledge payloads to the ML engine and multi-agent specialists.
- Purpose: Summarizes scattered legal clauses into a single, cohesive answer paragraph that cites specific section numbers and dollar amounts.
- Input Parameters:
question (str),context (str),chunks (List[Dict]). - Output Return Type:
str— Grounded textual answer with citations. - How it helps the data: Condenses multi-document search matches into actionable summaries.
4. query(self, question: str, category: Optional[str] = None, top_k: int = 5, verbose: bool = True) -> Dict
- Purpose: Formats search results with visual color coding for terminal debugging, allowing developers to inspect retrieval quality.
- Input Parameters:
question (str),category (str | None),top_k (int),verbose (bool). - Output Return Type:
Dict. - How it helps the data: Facilitates manual evaluation and developer debugging of knowledge retrieval.
Purpose: The main front door to the entire policy search library, allowing any part of the system to ask questions and receive cited answers with a single command.
- Purpose: Combines all document loading, chunking, and search tools into a single, easy-to-use master interface for the entire application.
- Input Parameters: None.
- Output Return Type: None.
- How it helps the data: Provides a single unified entry point for all RAG operations in the project.
- Purpose: Manages the lifecycle of the knowledge base, loading saved indexes from disk or rebuilding everything from scratch if new policies were added.
- Input Parameters:
force_rebuild (bool)— IfTrue, forces rebuild of FAISS and BM25 indexes. - Output Return Type:
bool—Trueon success. - How it helps the data: Guarantees that the knowledge base remains synchronized with any newly added documents.
- Purpose: The standard, programmatic method used by the predictive engine to fetch relevant contract clauses quietly in the background.
- Input Parameters:
question (str),category (str | None). - Output Return Type:
Dict. - How it helps the data: Standard method used by
PredictiveEngine._enrich_with_rag().
- Purpose: An interactive search tool for terminal testing, allowing users to type questions and see retrieved policy snippets directly on screen.
- Input Parameters:
question (str),category (str | None),verbose (bool). - Output Return Type:
Dict. - How it helps the data: Used for CLI diagnostic queries and manual verification.
File Location: d:\Progamming\O2C_AI\modules\ollama_service.py
Class: OllamaService
Purpose (Private On-Premise AI Model): Runs an advanced AI reasoning model directly on the company's internal computers, ensuring sensitive business data stays completely private and secure without relying on third-party cloud services.
1. __init__(self, host: str = "http://localhost:11434", model: str = "qwen2.5:7b", timeout: int = 45)
- Purpose: Sets the network connection address and timeout limits for communicating with the local AI model service.
- Input Parameters:
host (str)— Local daemon URL,model (str)— LLM model tag (qwen2.5:7b),timeout (int)— Request timeout in seconds. - Output Return Type: None.
- How it helps the data: Connects the system to the local GPU-accelerated language model without external API dependencies.
- Purpose: Sends a quick health-check ping to make sure the local AI model is running and ready to answer questions, triggering automatic fallbacks if it is offline.
- Input Parameters: None.
- Output Return Type:
bool—Trueif active and model exists,Falseotherwise. - How it helps the data: Ensures the pipeline never hangs if Ollama is offline, enabling immediate fallback to deterministic templates.
- Purpose: Prompts the local AI model with strict anti-hallucination guardrails, instructing it to rely solely on real facts and telemetry rather than guessing:
Applies the non-negotiable
{ "temperature": 0.1, "top_p": 0.85, "repeat_penalty": 1.15, "num_predict": 1024 }STRICT_SYSTEM_PROMPTmandating that the model only cite explicitly provided telemetry and facts. - Input Parameters:
prompt (str)— User prompt,system_prompt (str | None)— Custom system instructions (optional). - Output Return Type:
Optional[str]— Generated text completion orNoneon failure/timeout. - How it helps the data: Synthesizes unstructured weather observations and news headlines into structured insight blocks with zero hallucination.
| Module / Component | Primary Input Data | Core Transformation / Function | Output Artifact | Downstream Consumer |
|---|---|---|---|---|
modules/ml_db_extension.py |
10 SAP CSV Tables & SQLite database | Relational SQL Joins, Haversine Geospatial Vectors, Calendar Stress Signals | sap_* tables & in-memory ML DataFrame cache |
PredictiveEngine & AgenticOrchestrator |
modules/predictive_engine.py |
Feature-engineered order DataFrame | Supervised Random Forest Classifier & Gradient Boosting Regressor | rf_classifier.pkl, gb_regressor.pkl, feature_importances.json |
modules/agent_specialists.py |
modules/rag_engine.py (Chunker) |
82 Raw Word, PDF, Excel & Text Documents | Clause-Aware Regex Lookahead Splitting & Sentence Accumulation | india_monitor_data/rag/chunks/all_chunks.json (909 chunks) |
VectorStore |
modules/rag_engine.py (VectorStore) |
909 Semantic Chunks | SentenceTransformer Embedding (all-MiniLM-L6-v2) & Okapi BM25 Indexing |
index.faiss, bm25.pkl, metadata.pkl, chunks.pkl |
RAGQueryEngine & Multi-Agent Graph |
modules/rag_engine.py (Hybrid Search) |
Natural Language Query | Parallel Dense Vector Search + Sparse BM25 Search merged via RRF | Top 5 Ranked Chunks with Similarity Scores | PredictiveEngine._enrich_with_rag & Agent Specialists |
modules/ollama_service.py |
Live Telemetry & Incident Headlines | Low-temperature (0.1) Qwen2.5 synthesis with anti-hallucination prompt |
Executive Risk Advisories & Cold-Chain Directives | StrikeIntelligenceGenerator & WeatherPolicyGenerator |
A defining innovation of the O2C Delivery Risk Copilot is the mathematical coupling between Engine A (Predictive ML) and Engine B (Hybrid RAG):
Pipeline MLDatabaseExtension Engine A (ML) Engine B (RAG) LLMSynthesizer
│ │ │ │ │
│── get_order_details() ─►│ │ │ │
│◄─ 19-Feature Vector ────│ │ │ │
│ │ │ │ │
│── predict_delay() ─────────────────────────────►│ │ │
│ │ │── query_rag_rules() ───►│ │
│ │ │◄─ Exact SLA Clauses ────│ │
│◄─ Prediction + RAG Context ─────────────────────│ │ │
│ │
│── synthesize_decision() ─────────────────────────────────────────────────────────────────────────►│
│◄─ Consolidated Executive Resolution Artifact ─────────────────────────────────────────────────────│
-
Step 1:
MLDatabaseExtensiondelivers the complete 19-dimensional feature vector for the order. -
Step 2:
Engine Acomputes the empirical probability of failure ($88.3%$ ) and expected duration ($60.9\text{ hours}$ ), calculates financial SLA risk ($$1,000.00$), and identifies root causes. -
Step 3:
Engine Aautomatically triggersEngine Bwith targeted semantic search queries based on the diagnosed root causes, customer contract tier, and destination city. -
Step 4:
Engine Bextracts the binding legal clauses (Force Majeure Section 4.2, Cold-Chain HPLC quarantine rules, Carrier penalty waivers) and attaches them directly to the prediction payload. - Step 5: The enriched payload is handed over to the Multi-Agent Specialist Graph (Part 3) for autonomous ERP writebacks and MS Teams approvals.
End of Part 2 Specification. Part 3 covers Multi-Agent Specialist Reasoning, Conflict Resolution, and SAP ERP / MS Teams Action Execution.
The multi-agent framework replaces monolithic LLM prompts with collaborative specialist agents, each possessing dedicated domain knowledge, operational constraints, and statutory mandates:
[Prediction Payload: Order ID, Carrier, Tier, ETA, Root Cause, RAG Rules]
│
▼
[RouteSupervisorAgent]
│
┌────────────────┴────────────────┐
▼ (Lost > 12h) ▼ (Active)
[Charge $200 Penalty] [Verify Speed <= 55 km/h]
[Void Force Majeure] [Flag Corridor Hazards]
│ │
└────────────────┬────────────────┘
▼
[ClinicNotificationDispatcher]
• Sends Proactive 12h Early Warning
• Secures Statutory Force Majeure Legal Defense
│
▼
[ContractAdjudicatorAgent]
│
┌────────────────┴────────────────┐
▼ (FM Verified & 12h Notice) ▼ (Standard Late Delivery)
[Grant 72h Penalty Waiver] [Calculate Tiered Penalties]
[Waive $500/day SLA Penalty] • Platinum: $500/day
• Gold: 5%/day (Capped 25%)
│
┌────────────────┴────────────────┐
▼ (ETA >= 17:00) ▼ (ETA < 17:00)
[Assess $150 Dock Overtime Fee] [No Dock Fee Assessed]
│
▼
[QualityMitigationAgent]
│
┌────────────────┴────────────────┐
▼ (Specialty Diet & Delay > 48h) ▼ (Heatwave > 40°C or Shelf-Life < 6m)
[Authorize $1,000 Air Freight] [Mandate QA Quarantine Hold '01']
[Flag for HPLC Potency Lab Assay]
│
┌────────────────┴────────────────┐
▼ (Expense > $500 or Penalty > $1,000) ▼ (Expense <= $500)
[DIRECTOR_APPROVAL_REQUIRED] [AUTONOMOUSLY_APPROVED]
• Dispatch MS Teams Adaptive Card • Immediate Autonomous ERP Write-Back
• 2-Hour Response SLA
File Location: d:\Progamming\O2C_AI\modules\agent_specialists.py
Classes: RouteSupervisorAgent, ContractAdjudicatorAgent, QualityMitigationAgent, LLMReasoningEngine
Purpose (Virtual Supply Chain Board of Specialists): Houses 4 specialized AI agents that act like a collaborative team of human experts—a highway route manager, a contract lawyer, a quality assurance manager, and an executive brief writer.
Purpose: Acts as the virtual highway route manager, monitoring truck GPS signals, checking realistic driving speeds, and fining carriers if their tracking devices go dark.
1. analyze_route(self, prediction_payload: Dict[str, Any], order_data: Dict[str, Any]) -> Dict[str, Any]
- Purpose: Acts as a virtual highway operations manager, checking whether the truck driver's GPS tracking is active (charging a $200 penalty if disconnected for >12 hours) and verifying that the delivery speed is realistic.
-
Input Parameters:
-
prediction_payload (Dict[str, Any])— Comprehensive prediction dictionary from Engine A (dest_city,shipping_type,carrier_name,haversine_distance_km,required_transit_speed_kmh). -
order_data (Dict[str, Any])— Raw order feature dictionary from ERP (telematics_status).
-
-
Output Return Type:
Dict[str, Any]— Route analysis dictionary (telematics_active,telematics_penalty_usd,telematics_notes,route_hazards,corridor_distance_km,transit_speed_kmh,destination_city,shipping_mode). - How it helps the data: Detects GPS blind-tracking breaches (levying a mandatory $200 carrier chargeback) and flags high-velocity transit hazards before legal contract adjudication begins.
Purpose: Acts as the corporate contract lawyer, calculating customer late fees, waiving penalties during verified natural disasters, and billing carriers if they arrive after receiving docks close.
1. adjudicate_contract(self, prediction_payload: Dict[str, Any], order_data: Dict[str, Any], route_analysis: Dict[str, Any], notice_given_12h: bool = True) -> Dict[str, Any]
- Purpose: Acts as an automated corporate contract lawyer. It checks customer contracts to see if late fees apply (like /day for VIP Platinum clinics), waives penalties if an official 'Act of God' storm occurred and the clinic was notified early, and charges the carrier a fee if the truck arrives after the clinic is closed for the night.
-
Input Parameters:
-
prediction_payload (Dict[str, Any])— Engine A inference dictionary (customer_tier,order_value_usd,delay_hours,will_be_delayed,predicted_eta,root_causes). -
order_data (Dict[str, Any])— Receiving dock closing time (close_time). -
route_analysis (Dict[str, Any])— Telematics and route hazards fromRouteSupervisorAgent. -
notice_given_12h (bool)— Boolean flag indicating whether a proactive early warning was issued$\ge 12\text{h}$ before delivery.
-
-
Output Return Type:
Dict[str, Any]— Adjudication dictionary (customer_tier,force_majeure_status,force_majeure_waived,sla_delay_penalty_usd,after_hours_violation,after_hours_redelivery_fee_usd,total_carrier_chargeback_usd,penalty_clauses). - How it helps the data: Establishes exact, contractually grounded dollar liabilities, waiving penalties when statutory Force Majeure applies or passing liabilities through to carriers via debit memos.
Purpose: Protects delicate products (like critical veterinary pet diets and medications), approving emergency air freight replacements for stuck orders and holding heat-damaged goods for safety testing.
1. plan_mitigation(self, prediction_payload: Dict[str, Any], order_data: Dict[str, Any], contract_analysis: Dict[str, Any]) -> Dict[str, Any]
- Purpose: Protects product quality and animal health. If a truck is stuck for more than 2 days, it can automatically approve a $1,000 emergency replacement air shipment to keep clinics supplied, and places spoiled heat-exposed goods on hold for lab testing before clinics receive them.
- Input Parameters:
prediction_payload (Dict[str, Any])— Engine A risk payload (has_specialty_diet,delay_hours,will_be_delayed,root_causes).order_data (Dict[str, Any])— Material attributes (min_shelf_life,material_description).contract_analysis (Dict[str, Any])— Financial liability dictionary fromContractAdjudicatorAgent.
- Output Return Type:
Dict[str, Any]— Mitigation payload (has_specialty_diet,material_description,mitigation_actions,total_mitigation_cost_usd,qa_hold_required,qa_hold_reasons,approval_status,approval_gate,ms_teams_escalation_card). - How it helps the data: Prevents patient-critical veterinary therapy stock-outs and enforces enterprise financial governance before costs are incurred.
Purpose: Combines the predictions, legal clauses, and mitigation recommendations into a concise, professional executive briefing that business leaders can read in under 30 seconds.
- Purpose: Detects available AI language models across enterprise platforms (Databricks, Gemini, OpenAI, or local offline engines) so decision synthesis works in any cloud or local environment.
- Input Parameters: None (Inspects environment variables
DATABRICKS_HOST,GEMINI_API_KEY,OPENAI_API_KEY,OLLAMA_HOST). - Output Return Type: None.
- How it helps the data: Ensures 100% continuous decision synthesis across cloud enterprise clusters, local offline developer laptops, and air-gapped server environments.
2. build_synthesis_prompt(self, order_id: str, customer_name: str, customer_tier: str, carrier_name: str, shipping_type: str, delay_prob: float, will_delay: bool, delay_hours: float, predicted_eta: str, route_analysis: Dict[str, Any], contract_analysis: Dict[str, Any], quality_analysis: Dict[str, Any], rag_citations: List[str]) -> str
- Purpose: Combines raw order data, machine learning delay predictions, and retrieved contract clauses into a complete case brief for executive legal review.
- Input Parameters:
order_id (str),customer_name (str),customer_tier (str),carrier_name (str),shipping_type (str),delay_prob (float),will_delay (bool),delay_hours (float),predicted_eta (str),route_analysis (Dict),contract_analysis (Dict),quality_analysis (Dict),rag_citations (List[str]). - Output Return Type:
str— Structured synthesis prompt. - How it helps the data: Binds numerical ML predictions, discrete rule IDs, and contract clause citations into an unambiguous prompt for LLM adjudication.
3. synthesize_executive_decision(self, order_id: str, customer_name: str, customer_tier: str, carrier_name: str, shipping_type: str, delay_prob: float, will_delay: bool, delay_hours: float, predicted_eta: str, route_analysis: Dict[str, Any], contract_analysis: Dict[str, Any], quality_analysis: Dict[str, Any], rag_citations: List[str]) -> str
- Purpose: Writes an authoritative, plain-English executive summary paragraph explaining the root cause of the delay, financial liabilities, and recommended mitigation actions.
- Input Parameters: All 13 master data, prediction, specialist analysis, and RAG citation arguments.
- Output Return Type:
str— Executive decision brief string. - How it helps the data: Produces human-readable, legally grounded decision summaries written directly into the executive audit log and MS Teams notifications.
File Location: d:\Progamming\O2C_AI\modules\action_execution_engine.py
Classes: SAPActionExecutor, MSTeamsDispatcher, ClinicNotificationDispatcher
Purpose (Physical & Digital Execution Engine): The operational 'hands and feet' of the system. It puts the AI's decisions into action—updating SAP enterprise software, posting interactive cards in Microsoft Teams, and warning clinics ahead of time.
Purpose: Automatically updates the company's core SAP system—locking damaged shipments so they aren't delivered by mistake, updating delivery dates, and issuing penalty debit memos to carriers.
- Purpose: Prepares the database connection and ensures audit log tables exist before any automated ERP updates are attempted.
- Input Parameters:
db_path (Path)— SQLite database path. - Output Return Type: None.
- How it helps the data: Prepares the relational tables required to store automated enterprise write-backs.
- Purpose: Opens a safe, low-overhead database connection for recording simulated SAP transactions.
- Input Parameters: None.
- Output Return Type:
sqlite3.Connection. - How it helps the data: Provides low-overhead transactional access to local database tables.
- Purpose: Builds audit tables that track every simulated SAP change (old value vs. new value) and records carrier penalty debit memos for financial reconciliations.
- Input Parameters: None.
- Output Return Type: None.
- How it helps the data: Establishes immutable tables for ERP compliance audits and carrier billing reconciliations.
4. execute_sap_writebacks(self, order_id: str, predicted_eta: str, qa_hold_required: bool, qa_reasons: List[str], carrier_chargeback_usd: float, carrier_name: str, penalty_clauses: List[str]) -> List[Dict[str, Any]]
- Purpose: Executes simulated business write-backs to SAP tables—placing quality-risk shipments on delivery hold (LIFSK = '01'), updating delivery arrival dates (VDATU), and posting penalty debit memos against carrier payables:
- QA Quarantine Delivery Block: Updates
SAP_VBAK.LIFSK = '01'if cargo requires inspection. - Promise Date Synchronization: Updates
SAP_VBAK.VDATUto the ML predicted ETA date. - Carrier Accounts Payable Debit Memo: Posts financial penalty to
carrier_debit_memosand logs debit record inSAP_BKPF.DMBTR.
- QA Quarantine Delivery Block: Updates
- Input Parameters:
order_id (str),predicted_eta (str),qa_hold_required (bool),qa_reasons (List[str]),carrier_chargeback_usd (float),carrier_name (str),penalty_clauses (List[str]). - Output Return Type:
List[Dict[str, Any]]— List of executed action confirmation dictionaries. - How it helps the data: Closes the loop from AI prediction to enterprise operational reality by updating ERP records and financial ledgers.
Purpose: Delivers interactive notification cards directly into the Logistics Director's Microsoft Teams chat, allowing managers to approve or reject expensive mitigation actions with a single click.
- Purpose: Sets up the Microsoft Teams communications link and prepares the storage folder where executive notification cards are saved.
- Input Parameters:
webhook_url (str | None). - Output Return Type: None.
- How it helps the data: Sets up the communication channel to enterprise collaboration platforms.
- Purpose: Formats an interactive Microsoft Teams card with prominent order details, cost summaries, and clickable 'Approve' or 'Reject' buttons for human managers.
- Input Parameters:
escalation_data (Dict[str, Any])— Escalation card details fromQualityMitigationAgent. - Output Return Type:
Dict[str, Any]— Complete Adaptive Card JSON schema dictionary. - How it helps the data: Formats machine decisions into intuitive visual cards for executive decision-makers.
- Purpose: Saves the notification card to disk and instantly posts it to the Regional Logistics Director's Microsoft Teams channel for fast sign-off.
- Input Parameters:
escalation_data (Dict[str, Any]). - Output Return Type:
Dict[str, Any]—{"card_file", "dispatch_status", "card_payload"}. - How it helps the data: Guarantees that every high-value escalation is permanently recorded as an inspectable JSON artifact and transmitted to human managers.
Purpose: Proactively warns customer veterinary clinics at least 12 hours in advance if a storm or strike delays their order, keeping customers informed while legally protecting the company from late fees.
- Purpose: Connects the customer communications dispatcher to the database to track all early warnings sent to destination veterinary clinics.
- Input Parameters:
db_path (Path). - Output Return Type: None.
- How it helps the data: Connects the notification dispatcher to the central SQLite database.
- Purpose: Builds the notification log table that proves when proactive early warnings were dispatched to clinics.
- Input Parameters: None.
- Output Return Type: None.
- How it helps the data: Establishes an immutable record of external customer communications.
3. send_proactive_12h_notice(self, order_id: str, clinic_name: str, dest_city: str, predicted_eta: str, delay_reasons: List[str]) -> Dict[str, Any]
- Purpose: Automatically drafts and logs an early warning notification to the receiving clinic at least 12 hours before delivery, satisfying the legal requirement for Force Majeure late-fee waivers.
- Input Parameters:
order_id (str),clinic_name (str),dest_city (str),predicted_eta (str),delay_reasons (List[str]). - Output Return Type:
Dict[str, Any]—{"notice_status", "force_majeure_compliant", "notice_message", "sent_at"}. - How it helps the data: Provides the legally required early-warning evidence needed by
ContractAdjudicatorAgentto waive $500/day late delivery penalties under Act of God clauses.
File Location: d:\Progamming\O2C_AI\modules\agentic_orchestrator.py
Classes: LLMSynthesizer, AgenticOrchestrator
Purpose (The Master Conductor): Orchestrates the complete daily routine of the AI Copilot from start to finish—gathering data, running predictive models, coordinating specialist agents, updating SAP, and emailing daily executive reports.
Purpose: Directs the sequence of agent discussions for every troubled order, making sure the route manager, contract lawyer, quality planner, and SAP executor work together in the right order.
- Purpose: Coordinates all 4 specialist agents, the SAP executor, and the Teams dispatcher into a single unified decision-making workflow.
- Input Parameters:
enable_teams_dispatch (bool)— Flag to enable live webhook delivery. - Output Return Type: None.
- How it helps the data: Assembles the multi-agent graph into a cohesive in-memory pipeline.
2. synthesize(self, prediction_payload: Dict[str, Any], order_data: Dict[str, Any] = None) -> Dict[str, Any]
- Purpose: Coordinates the multi-agent review for a single shipment—running route checks, sending clinic notices, calculating penalties, planning mitigations, updating SAP, and writing executive summaries:
- Calls
RouteSupervisorAgent.analyze_route()to verify telematics and corridor hazards. - Calls
ClinicNotificationDispatcher.send_proactive_12h_notice()to satisfy Force Majeure compliance. - Calls
ContractAdjudicatorAgent.adjudicate_contract()to calculate SLA penalties and Force Majeure status. - Calls
QualityMitigationAgent.plan_mitigation()to authorize emergency air freight and QA holds. - Calls
SAPActionExecutor.execute_sap_writebacks()to apply simulated ERP updates and AP debit memos. - Dispatches MS Teams Adaptive Card if director approval is required.
- Calls
LLMReasoningEngine.synthesize_executive_decision()to draft the executive summary. - Returns consolidated decision JSON payload.
- Calls
- Input Parameters:
prediction_payload (Dict[str, Any]),order_data (Dict[str, Any] | None). - Output Return Type:
Dict[str, Any]— Complete structured decision artifact containing customer profile, ML prediction, specialist analyses, legal adjudication, emergency actions, ERP write-backs, RAG citations, and executive brief. - How it helps the data: Merges predictions, legal rules, quality policies, and ERP write-backs into a unified, audit-proof operational record.
Purpose: Drives the daily morning execution cycle across all 13 modules, ensuring the entire supply chain risk pipeline runs seamlessly every single day without requiring manual intervention.
- Purpose: Connects all 12 platform modules together upon startup, ensuring the pipeline is ready to execute its daily routine.
- Input Parameters: None.
- Output Return Type: None.
- How it helps the data: Binds all 12 project modules into a single executable system.
2. run_daily_agent_cycle(self, date: str = None, order_limit: int = 5, target_order: str = None, all_orders: bool = False, repredict: bool = False, rebuild_rag: bool = False, enable_teams_dispatch: bool = False) -> Dict[str, Any]
- Purpose: Executes the complete 6-stage morning routine: ingesting weather/strikes, updating the rulebook, training ML models, analyzing active shipments, updating SAP, and publishing the executive report:
- Step 1: Ingests live weather telemetry and strike RSS articles into SQLite (
session_start). - Step 2: Verifies and rebuilds regulatory Word policy documents and Hybrid RAG vector index.
- Step 3: Refreshes SAP ERP feature store and trains Two-Stage Hurdle ML models (
train_models). - Step 4: Vectorized batch inference across unpredicted orders (skipping previously evaluated orders unless
repredict=True). - Step 5: Multi-agent synthesis (
LLMSynthesizer.synthesize), ERP write-backs, and Teams card generation. - Step 6: Compiles and exports
daily_agent_report_{date}.json, exports CSV snapshots, and logssession_end.
- Step 1: Ingests live weather telemetry and strike RSS articles into SQLite (
- Input Parameters:
date (str | None)— Operational target date (YYYY-MM-DD).order_limit (int)— Max orders to evaluate (default 5).target_order (str | None)— Single order ID override.all_orders (bool)— IfTrue, evaluates entire dataset.repredict (bool)— IfTrue, forces re-prediction of already evaluated orders.rebuild_rag (bool)— IfTrue, forces rebuild of FAISS and BM25 indexes.enable_teams_dispatch (bool)— IfTrue, transmits cards to live webhooks.
- Output Return Type:
Dict[str, Any]—{"status": "success", "date": today_str, "report_file": str, "decisions": List[Dict]}. - How it helps the data: Orchestrates the continuous daily transformation of raw streaming data into concrete ERP transactions and executive reports.
- Purpose: Exports daily weather and strike tables into flat spreadsheet CSV files so business analysts can easily open and explore them in Excel or PowerBI.
- Input Parameters:
target_date (str). - Output Return Type: None.
- How it helps the data: Provides portable tabular snapshots for external business intelligence tools (PowerBI, Tableau, Excel).
- Purpose: Provides a command-line interface with customizable options (like processing specific dates, single orders, or forcing re-runs) for automated scheduling and testing.
- Input Parameters: None (Parses
sys.argv). - Output Return Type: None.
- How it helps the data: Enables cron scheduling, Databricks job execution, and terminal CLI testing.
| Module / Component | Primary Input Data | Core Transformation / Function | Output Artifact | Downstream Consumer |
|---|---|---|---|---|
modules/agent_specialists.py (RouteSupervisor) |
Prediction payload & telematics status | GPS signal loss detection & speed feasibility check |
telematics_penalty_usd ($200) & route_hazards
|
ContractAdjudicatorAgent |
modules/action_execution_engine.py (ClinicNotifier) |
Order ID, Clinic name, City, ETA, Root causes | Generates early warning & logs timestamped record |
clinic_early_warnings table & force_majeure_compliant
|
ContractAdjudicatorAgent |
modules/agent_specialists.py (ContractAdjudicator) |
Route analysis, clinic notice flag, SLA tier | Multi-tier penalty math & Force Majeure waiver check |
sla_delay_penalty_usd, total_carrier_chargeback_usd
|
QualityMitigationAgent & SAPActionExecutor
|
modules/agent_specialists.py (QualityMitigation) |
Specialty diet flag, delay hours, shelf-life | Authorizes $1,000 Air Freight & QA holds; checks $500 approval gate |
mitigation_actions, approval_status, Teams card data |
MSTeamsDispatcher & SAPActionExecutor
|
modules/agent_specialists.py (LLMReasoning) |
Math + Rules + Master Data + RAG citations | Multi-model legal prompt construction & LLM synthesis |
executive_decision_brief string |
daily_agent_report.json |
modules/action_execution_engine.py (SAPExecutor) |
Mitigation decisions, revised ETA, chargebacks | Executes simulated SQL write-backs & audit logging |
sap_action_audit_log & carrier_debit_memos
|
SAP ERP System & Financial Ledgers |
modules/action_execution_engine.py (TeamsDispatcher) |
High-value escalation data (> $500 expense) | Schema-compliant Adaptive Card JSON generation (v1.4) | reports/ms_teams_cards/teams_card_order_*.json |
Regional Logistics Director (MS Teams) |
modules/agentic_orchestrator.py (Orchestrator) |
CLI arguments & daily schedules | 6-step autonomous operational cycle coordination | daily_agent_report_YYYY-MM-DD.json |
Executive Leadership & Operations Desks |
To see how the entire architecture functions as a unified system, follow the lifecycle of distressed Order 800000000000001 from raw ingestion to physical execution:
External Feeds Part 1: Ingestion Part 2: ML & RAG Part 3: Orchestrator Part 3: Specialists Part 3: Execution
│ │ │ │ │ │
│── 41.5°C Heat ───►│ │ │ │ │
│── Highway Strike ─►│ │ │ │ │
│ │── Generate DOCX ────►│ │ │ │
│ │ (Hyderabad Rules) │ │ │ │
│ │ │── Index 909 Chunks ──►│ │ │
│ │ │ Two-Stage Prediction│ │ │
│ │ │ P=87.1%, 67h Delay │ │ │
│ │ │ │── Route Supervisor ─►│ │
│ │ │ │ (GPS Check OK) │ │
│ │ │ │── Send 12h Notice ────────────────────────►│
│ │ │ │ (Clinic Warned) │ │
│ │ │ │── Contract Lawyer ──►│ │
│ │ │ │ (72h Waiver OK) │ │
│ │ │ │── QA Mitigation ────►│ │
│ │ │ │ ($1k Air Freight) │ │
│ │ │ │ │── SAP Write-Backs ─►│
│ │ │ │ │ (LIFSK, VDATU) │
│ │ │ │ │── Teams Card ──────►│
│ │ │ │ │ (2h Approval SLA) │
│ │ │ │◄─ Complete Consensus ┘ │
│ │ │ │── Save Audit Report (daily_agent_report) │
- Open-Meteo detects a
$41.5^\circ\text{C}$ heatwave in Hyderabad; Google News RSS detects a truck strike along NH-44. -
WeatherPolicyGeneratorcreatesHyderabad_Weather_Protocol.docxwith[RULE-W-HYD-02](Force Majeure 72h waiver) and[RULE-W-HYD-03](HPLC assay mandate).
-
MLDatabaseExtensioncomputes 19 features: Haversine distance$= 937\text{ km}$ , weight$= 1,200\text{ kg}$ , customer tier$= \text{Platinum}$ . -
PredictiveEngine(Stage 1 Classifier) outputs$P(\text{delay}) = 87.1%$ . - Stage 2 Conditional Huber Regressor predicts
$67.0\text{ hours}$ delay. - Explainable AI (
feature_importances.json) attributes$85.2%$ risk to in-transit delay and$1.3%$ to weekend dock closure. - Hybrid RAG retrieves Platinum $500/day SLA terms and Force Majeure Section 4.2.
- Route Supervisor: Verifies GPS telematics is active (no $200 blind-tracking breach).
-
Clinic Dispatcher: Automatically records and sends a proactive early warning to the clinic
$>12\text{ hours}$ before arrival. -
Contract Adjudicator: Verifies that because heatwave
$>40^\circ\text{C}$ is documented, telematics is active, and proactive notice was given$\ge 12\text{h}$ , Force Majeure Clause 4.2 applies, completely waiving the $1,000 late delivery SLA penalty. However, because arrival ETA ($18:30$ ) violates the 17:00 dock close time, a $150 redelivery fee is charged back to the carrier. -
Quality Mitigation Planner: Detects that the shipment contains a critical prescription diet (
has_specialty_diet = 1) delayed$>48\text{h}$ . Authorizes an $1,000 emergency Air Freight replacement pallet and places the road consignment on SAP QA Quarantine Hold for laboratory vitamin potency testing. -
Action Execution:
-
SAPActionExecutorsetsSAP_VBAK.LIFSK = '01'(Delivery Block), updatesSAP_VBAK.VDATUto the revised ETA, and posts a $150.00 carrier debit memo tocarrier_debit_memos. -
MSTeamsDispatcherdetects that mitigation expense ($1,000) exceeds the $500 autonomous threshold, formatting and transmitting an interactive Adaptive Card to the Regional Logistics Director with a mandatory 2-hour response SLA.
-
-
Audit Logging: The complete consensus decision is saved in
daily_agent_report_YYYY-MM-DD.json.
| Document | Core Responsibility | Modules Covered | Primary Data Outputs |
|---|---|---|---|
| Part 1: Ingestion & Document Synthesis | External streaming sensor feeds, RSS web scrapers, and regulatory policy generators | config.pydatabase_manager.pyweather_service.pynews_service.pyweather_policy_generator.pystrike_intelligence_generator.py |
weather_readingsstrike_news23 .docx Policy Protocols ([RULE-W-*], [RULE-S-*]) |
| Part 2: Feature Store & Predictive RAG | 10-table SAP relational feature engineering, Two-Stage Hurdle ML models, and hybrid vector retrieval | ml_db_extension.pypredictive_engine.pyrag_engine.pyollama_service.py |
19-Feature Datasetrf_classifier.pkl (97.1% Acc)gb_regressor.pkl (5.6h MAE)FAISS & BM25 Indexes (909 Chunks) |
| Part 3: Agent Reasoning & Execution | Autonomous multi-agent specialists, SAP ERP write-backs, MS Teams Adaptive Cards, and daily cycle orchestration | agent_specialists.pyaction_execution_engine.pyagentic_orchestrator.py |
sap_action_audit_logcarrier_debit_memosclinic_early_warningsAdaptive Cards (v1.4) daily_agent_report.json |