-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_chatbot.py
More file actions
1320 lines (1124 loc) · 63.3 KB
/
ai_chatbot.py
File metadata and controls
1320 lines (1124 loc) · 63.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
WORLD-CLASS AI Chatbot for Manhattan Power Grid
Advanced conversational AI with deep system knowledge and V2G expertise
"""
import json
import time
import asyncio
import base64
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Any, Union
import re
import os
import threading
from collections import deque
import numpy as np
from dataclasses import dataclass
from enum import Enum
import hashlib
try:
from openai import OpenAI
except ImportError:
OpenAI = None
try:
import cv2
from PIL import Image, ImageDraw, ImageFont
except ImportError:
cv2 = None
Image = ImageDraw = ImageFont = None
class ConversationMode(Enum):
"""AI conversation modes for different interaction styles"""
ASSISTANT = "assistant" # Helpful assistant mode
EXPERT = "expert" # Technical expert mode
ADVISOR = "advisor" # Strategic advisor mode
ANALYST = "analyst" # Data analyst mode
EMERGENCY = "emergency" # Emergency response mode
class IntentComplexity(Enum):
"""Intent complexity levels for response routing"""
SIMPLE = "simple" # Basic queries, rule-based response
MODERATE = "moderate" # Some context needed, hybrid approach
COMPLEX = "complex" # Requires deep analysis, AI-powered
RESEARCH = "research" # Research-level analysis needed
@dataclass
class ConversationContext:
"""Advanced conversation context tracking"""
user_id: str
session_id: str
mode: ConversationMode
topic_focus: Optional[str]
technical_level: int # 1-10, user's technical expertise level
preferences: Dict[str, Any]
history_summary: str
active_tasks: List[str]
last_system_state: Dict[str, Any]
attention_areas: List[str] # Areas user is most interested in
@dataclass
class AIResponse:
"""Structured AI response with metadata"""
text: str
type: str
intent: str
confidence: float
suggestions: List[str]
actions: List[Dict[str, Any]]
visualizations: List[Dict[str, Any]]
follow_up_questions: List[str]
system_changes: List[str]
timestamp: str
metadata: Dict[str, Any]
class ManhattanAIChatbot:
"""
World-class AI chatbot with deep knowledge of:
- Power grid operations
- V2G (Vehicle-to-Grid) systems
- Machine learning analytics
- Traffic management
- Energy trading
- System optimization
"""
def __init__(self, integrated_system, ml_engine, v2g_manager):
self.integrated_system = integrated_system
self.ml_engine = ml_engine
self.v2g_manager = v2g_manager
# Initialize OpenAI client with advanced configuration
self.openai_client = None
if OpenAI and os.getenv('OPENAI_API_KEY'):
self.openai_client = OpenAI(
api_key=os.getenv('OPENAI_API_KEY'),
timeout=30.0,
max_retries=3
)
# Advanced conversation management
self.conversation_contexts = {} # user_id -> ConversationContext
self.conversation_history = deque(maxlen=1000) # Global conversation history
self.active_sessions = {} # session_id -> user_id mapping
# Advanced AI capabilities - Initialize with error handling
try:
from ai_advanced_modules import (
VisualProcessor, PatternAnalyzer, PredictionEngine, ContextAnalyzer,
SystemMonitor, AlertManager, LearningEngine, UserBehaviorAnalyzer,
PerformanceTracker, ConversationAnalytics, BackgroundProcessor
)
self.visual_processor = VisualProcessor() if cv2 else None
self.pattern_analyzer = PatternAnalyzer()
self.prediction_engine = PredictionEngine(ml_engine)
self.context_analyzer = ContextAnalyzer()
# Real-time system monitoring
self.system_monitor = SystemMonitor(integrated_system, ml_engine, v2g_manager)
self.alert_manager = AlertManager()
# Learning and adaptation
self.learning_engine = LearningEngine()
self.user_behavior_analyzer = UserBehaviorAnalyzer()
# Performance analytics
self.performance_tracker = PerformanceTracker()
self.conversation_analytics = ConversationAnalytics()
# Background processing
self.background_processor = BackgroundProcessor()
self.advanced_features_enabled = True
print(" Advanced AI modules loaded successfully")
except ImportError as e:
print(f" Advanced AI modules not available: {e}")
# Initialize with fallback implementations
self.visual_processor = None
self.pattern_analyzer = None
self.prediction_engine = None
self.context_analyzer = None
self.system_monitor = None
self.alert_manager = None
self.learning_engine = None
self.user_behavior_analyzer = None
self.performance_tracker = None
self.conversation_analytics = None
self.background_processor = None
self.advanced_features_enabled = False
# Enhanced knowledge management
self.knowledge_base = self._build_knowledge_base() # Use base implementation for now
self.response_templates = self._initialize_response_templates() # Use base implementation
self.system_context = self._build_system_context() # Use base implementation
# Start background tasks if advanced features are enabled
if self.advanced_features_enabled:
self._start_background_tasks()
print("World-Class Manhattan AI Chatbot initialized with research-level capabilities")
print(" Advanced Natural Language Processing")
print(" Visual Map Analysis Engine")
print(" Real-time System Monitoring")
print(" Predictive Intelligence")
print(" Multi-modal Interaction")
print(" Continuous Learning")
print(" Research-level Analytics")
def _build_system_context(self):
"""Build comprehensive system context for AI responses"""
return {
"system_name": "Manhattan Power Grid",
"capabilities": [
"Real-time power grid monitoring",
"V2G (Vehicle-to-Grid) energy trading",
"Machine learning predictions",
"Traffic light management",
"EV charging optimization",
"Substation control",
"Anomaly detection",
"Energy market analysis"
],
"current_time": datetime.now().isoformat(),
"system_status": "operational"
}
def _initialize_response_templates(self):
"""Initialize response templates for common scenarios"""
return {
"greeting": [
"""# 👋 Welcome to Manhattan Power Grid
I'm your **intelligent AI assistant** for advanced grid management and simulation.
---
## 🎯 What I Can Do
| Category | Capabilities |
|----------|-------------|
| ⚡ **Grid Control** | Turn substations on/off, restore power |
| 🕐 **Time & Weather** | Set time of day, adjust temperature |
| 🎬 **Scenarios** | Run test scenarios (rush hour, heatwave, etc.) |
| 🗺️ **Navigation** | Show locations, zoom to areas |
| 📊 **Analysis** | System status, grid performance |
---
## 💡 Get Started
**Type `help`** for complete command reference
**Try**: `"set time for 8"` or `"morning rush"`
**Just talk naturally** - I understand you! 🚀""",
"""# 🌟 Manhattan Power Grid Control Center
Your **AI-powered assistant** for grid operations and testing.
---
## 🚀 Quick Capabilities
| Feature | What You Can Do |
|---------|-----------------|
| ⚡ **Power** | Control all substations |
| 🌡️ **Simulate** | Test different conditions |
| 🚗 **Traffic** | Realistic vehicle patterns |
| 🗺️ **Navigate** | Explore the grid map |
| 📈 **Monitor** | Real-time analytics |
---
## 💬 Getting Help
**Type `help`** → See all commands
**Type `status`** → Check grid status
**Type `suggest`** → Get recommendations
Ready to assist! 🎯""",
"""# ⚡ Hello from Manhattan Power Grid!
I'm your **expert AI assistant** ready to help.
---
## ✨ Core Features
| 🎯 Feature | 📝 Description |
|-----------|---------------|
| **Grid Control** | Manage substations and power flow |
| **Scenarios** | Test rush hours, heatwaves, emergencies |
| **V2G Systems** | Vehicle-to-Grid emergency response |
| **Map Tools** | Navigate and explore locations |
| **Analytics** | Monitor and analyze performance |
---
## 🎬 Quick Start
→ **`help`** - Full command list
→ **`morning rush`** - Run a test scenario
→ **`status`** - Check system health
Let's get started! 🚀"""
],
"v2g_help": [
"V2G (Vehicle-to-Grid) allows electric vehicles to discharge energy back to the grid during high demand periods. This creates revenue opportunities for vehicle owners while supporting grid stability.",
"Our V2G system offers premium pricing (50x normal charging rates) for vehicles that provide energy during grid emergencies. Vehicles need 60%+ SOC to participate.",
"V2G trading is currently active with dynamic pricing based on grid demand. I can show you current opportunities and optimal trading times."
],
"ml_insights": [
"Our ML models predict power demand, EV charging patterns, and V2G opportunities with 95%+ accuracy. They continuously learn from real-time data.",
"The system uses advanced algorithms including Gradient Boosting, Neural Networks, and Isolation Forests for comprehensive grid analytics.",
"ML predictions help optimize energy distribution, predict failures, and maximize V2G revenue opportunities."
],
"system_status": [
"The Manhattan Power Grid is currently operational with all systems functioning normally.",
"I can provide real-time status updates on substations, traffic lights, EV stations, and V2G operations.",
"The system includes advanced monitoring, predictive analytics, and automated optimization capabilities."
]
}
def _build_knowledge_base(self):
"""Build comprehensive knowledge base for all system components"""
return {
"v2g_concepts": {
"definition": "Vehicle-to-Grid (V2G) technology allows electric vehicles to discharge stored energy back to the power grid",
"benefits": ["Revenue generation for vehicle owners", "Grid stability support", "Peak demand management", "Renewable energy integration"],
"requirements": ["60% minimum SOC", "V2G-compatible vehicle", "Grid connection", "Market participation"],
"pricing": "Dynamic pricing based on grid demand, typically 50x normal charging rates during emergencies",
"components": ["V2G stations", "Vehicle batteries", "Bidirectional chargers", "Energy management system"],
"process": ["Vehicle recruitment", "Route optimization", "Energy discharge", "Revenue calculation"]
},
"power_grid": {
"substations": {
"description": "Transform and distribute power throughout Manhattan",
"types": ["Primary substations", "Distribution substations", "Emergency substations"],
"components": ["Transformers", "Switchgear", "Protection systems", "Monitoring equipment"],
"voltage_levels": ["13.8kV primary", "480V secondary", "120V/240V residential"],
"capacity": "Ranges from 10MVA to 100MVA depending on location"
},
"distribution_lines": {
"primary": "13.8kV overhead and underground cables",
"secondary": "480V distribution to buildings",
"protection": "Circuit breakers, fuses, and protective relays",
"monitoring": "Real-time load monitoring and fault detection"
},
"load_management": {
"peak_demand": "Managed through load balancing and demand response",
"emergency_procedures": "Automatic load shedding and V2G activation",
"optimization": "ML-based load forecasting and distribution optimization"
}
},
"traffic_management": {
"traffic_lights": {
"description": "Smart traffic management with power monitoring and optimization",
"types": ["Fixed-time", "Actuated", "Adaptive", "Connected"],
"power_consumption": "LED lights with 80% energy savings",
"control_systems": ["SCADA integration", "Remote monitoring", "Emergency override"],
"optimization": "AI-based timing optimization for traffic flow"
},
"vehicle_simulation": {
"platform": "SUMO (Simulation of Urban Mobility)",
"vehicle_types": ["Gas cars", "Electric vehicles", "Buses", "Trucks"],
"routing": "Dynamic routing with real-time traffic conditions",
"behavior": "Realistic driving patterns and charging behavior"
},
"intersection_control": {
"adaptive_signals": "Real-time signal timing based on traffic flow",
"priority_systems": "Emergency vehicle and transit priority",
"coordination": "Network-wide signal coordination for optimal flow"
}
},
"ev_charging": {
"charging_stations": {
"types": ["Level 1 (120V)", "Level 2 (240V)", "DC Fast Charging (480V)"],
"locations": "Strategic placement throughout Manhattan",
"capacity": "Multiple charging ports per station",
"management": "Smart charging with load balancing"
},
"vehicle_types": {
"battery_electric": "Full electric vehicles with large battery packs",
"plug_in_hybrid": "Hybrid vehicles with charging capability",
"v2g_compatible": "Vehicles capable of bidirectional energy flow"
},
"charging_behavior": {
"soc_tracking": "State of charge monitoring and optimization",
"charging_patterns": "Peak hours, off-peak, and emergency charging",
"queue_management": "Intelligent queuing and reservation systems"
}
},
"machine_learning": {
"demand_prediction": {
"description": "Predicts power demand up to 24 hours ahead with 95%+ accuracy",
"algorithms": ["Gradient Boosting", "Neural Networks", "Time Series Analysis"],
"features": ["Historical demand", "Weather data", "Traffic patterns", "EV charging"],
"applications": ["Load planning", "Resource allocation", "Emergency preparedness"]
},
"anomaly_detection": {
"description": "Identifies grid anomalies and potential failures in real-time",
"methods": ["Isolation Forest", "Statistical analysis", "Pattern recognition"],
"detection_types": ["Equipment failures", "Load anomalies", "Traffic disruptions"],
"response": "Automatic alerts and recommended actions"
},
"optimization_models": {
"v2g_optimization": "Optimizes V2G pricing and vehicle routing for maximum revenue",
"traffic_optimization": "Optimizes traffic light timing for minimal congestion",
"energy_optimization": "Optimizes power distribution for maximum efficiency",
"predictive_maintenance": "Predicts equipment failures before they occur"
}
},
"blackout_scenarios": {
"emergency_protocols": {
"substation_failure": "Automatic V2G activation and load redistribution",
"grid_instability": "Emergency load shedding and stabilization procedures",
"elevator_emergencies": "V2G-powered elevator rescue operations",
"communication": "Emergency alert systems and public notifications"
},
"restoration_procedures": {
"priority_restoration": "Critical infrastructure first (hospitals, emergency services)",
"v2g_deployment": "Vehicle-to-grid emergency power activation",
"load_management": "Gradual load restoration to prevent cascading failures",
"monitoring": "Real-time monitoring of restoration progress"
}
},
"system_monitoring": {
"real_time_monitoring": {
"power_grid": "Continuous monitoring of voltage, current, and power flow",
"traffic_systems": "Real-time traffic flow and signal status monitoring",
"ev_charging": "Charging station status and utilization monitoring",
"v2g_operations": "V2G session monitoring and revenue tracking"
},
"alert_systems": {
"critical_alerts": "Immediate notification of system failures",
"warning_alerts": "Early warning of potential issues",
"maintenance_alerts": "Scheduled maintenance and equipment status",
"performance_alerts": "Performance degradation notifications"
}
}
}
def process_message(self, user_message: str, user_id: str = "default") -> Dict[str, Any]:
"""
Process user message and generate intelligent response
"""
try:
# Add to conversation history
self.conversation_history.append({
"timestamp": datetime.now().isoformat(),
"user_id": user_id,
"message": user_message,
"type": "user"
})
# Analyze message intent
intent = self._analyze_intent(user_message)
# Get system data
system_data = self._get_current_system_data()
# Generate response
if self.openai_client and intent.get("use_openai", False):
response = self._generate_openai_response(user_message, intent, system_data)
else:
response = self._generate_rule_based_response(user_message, intent, system_data)
# Add response to history
self.conversation_history.append({
"timestamp": datetime.now().isoformat(),
"user_id": user_id,
"message": response["text"],
"type": "assistant",
"intent": intent,
"data": response.get("data", {})
})
return response
except Exception as e:
error_response = {
"text": f"I apologize, but I encountered an error processing your request: {str(e)}. Please try again or rephrase your question.",
"type": "error",
"timestamp": datetime.now().isoformat()
}
return error_response
def _analyze_intent(self, message: str) -> Dict[str, Any]:
"""Analyze user message intent using pattern matching and keywords"""
message_lower = message.lower()
# Comprehensive intent patterns for all system components
intents = {
"greeting": ["hello", "hi", "hey", "good morning", "good afternoon", "good evening", "greetings"],
"v2g_question": ["v2g", "vehicle to grid", "discharge", "energy trading", "revenue", "earnings", "bidirectional", "grid to vehicle"],
"ml_question": ["ml", "machine learning", "prediction", "analytics", "insights", "model", "ai", "algorithm", "forecast"],
"system_status": ["status", "operational", "working", "online", "offline", "failed", "health", "condition", "state"],
"substation": ["substation", "power", "grid", "load", "capacity", "transformer", "switchgear", "distribution"],
"traffic": ["traffic", "lights", "intersection", "vehicles", "cars", "sumo", "signal", "congestion", "flow"],
"ev_charging": ["ev", "electric vehicle", "charging", "station", "battery", "soc", "plug", "charger"],
"blackout": ["blackout", "outage", "emergency", "failure", "restoration", "elevator", "rescue", "scenario"],
"monitoring": ["monitor", "watch", "track", "observe", "surveillance", "alert", "notification", "warning"],
"anomalies": ["anomaly", "anomalies", "high", "medium", "low", "severity", "critical", "urgent", "alert", "problem", "issue", "error", "fault"],
"optimization": ["optimize", "improve", "efficiency", "save", "reduce", "enhance", "maximize", "minimize"],
"help": ["help", "assist", "support", "guide", "how to", "explain", "what is", "tell me about"],
"data_request": ["show", "display", "get", "fetch", "data", "metrics", "stats", "numbers", "values"],
"power_distribution": ["distribution", "voltage", "current", "power flow", "load balancing", "demand response"],
"traffic_management": ["traffic management", "signal control", "intersection", "adaptive signals", "coordination"],
"energy_market": ["energy market", "pricing", "rates", "cost", "economics", "financial", "profit"],
"maintenance": ["maintenance", "repair", "service", "inspection", "upgrade", "replacement", "scheduled"],
"safety": ["safety", "security", "protection", "emergency", "hazard", "risk", "precaution"],
"performance": ["performance", "efficiency", "productivity", "throughput", "utilization", "effectiveness"],
"configuration": ["configure", "setup", "settings", "parameters", "adjust", "modify", "change"],
"reports": ["report", "summary", "analysis", "dashboard", "overview", "review", "assessment"],
"scenario_v2g": ["v2g scenario", "v2g rescue", "run v2g", "start v2g", "trigger v2g", "execute v2g", "show v2g scenario", "demonstrate v2g"],
"scenario_blackout": ["blackout scenario", "trigger blackout", "run blackout", "start blackout", "citywide blackout", "execute blackout", "simulate blackout"],
"scenario_list": ["available scenarios", "what scenarios", "show scenarios", "list scenarios", "scenario options"],
"scenario_confirm": ["confirm", "yes", "proceed", "do it", "go ahead", "start", "execute"],
"scenario_cancel": ["cancel", "no", "abort", "stop", "nevermind", "skip"]
}
# Check for intent matches - prioritize scenario intents
detected_intents = []
# Check scenario intents first (highest priority)
scenario_intents = ["scenario_v2g", "scenario_blackout", "scenario_list", "scenario_confirm", "scenario_cancel"]
for intent in scenario_intents:
if intent in intents and any(keyword in message_lower for keyword in intents[intent]):
detected_intents.append(intent)
# If no scenario intent found, check other intents
if not detected_intents:
for intent, keywords in intents.items():
if intent not in scenario_intents and any(keyword in message_lower for keyword in keywords):
detected_intents.append(intent)
# Determine primary intent
primary_intent = detected_intents[0] if detected_intents else "general"
# Check if complex query requiring OpenAI
complex_indicators = ["explain", "analyze", "compare", "why", "how", "what if", "recommend"]
use_openai = any(indicator in message_lower for indicator in complex_indicators) or len(detected_intents) > 2
return {
"primary": primary_intent,
"all": detected_intents,
"use_openai": use_openai,
"complexity": "high" if use_openai else "low"
}
def _get_current_system_data(self) -> Dict[str, Any]:
"""Get current system data for context"""
try:
# Get ML dashboard data
ml_data = self.ml_engine.get_ml_dashboard_data() if self.ml_engine else {}
# Get V2G data
v2g_data = self.v2g_manager.get_v2g_dashboard_data() if self.v2g_manager else {}
# Get basic system stats
system_stats = {
"substations_operational": sum(1 for s in self.integrated_system.substations.values() if s.get('operational', False)),
"total_substations": len(self.integrated_system.substations),
"traffic_lights_powered": sum(1 for tl in self.integrated_system.traffic_lights.values() if tl.get('powered', False)),
"total_traffic_lights": len(self.integrated_system.traffic_lights),
"ev_stations_operational": sum(1 for ev in self.integrated_system.ev_stations.values() if ev.get('operational', False)),
"total_ev_stations": len(self.integrated_system.ev_stations),
"current_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
return {
"system_stats": system_stats,
"ml_data": ml_data,
"v2g_data": v2g_data,
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return {"error": str(e), "timestamp": datetime.now().isoformat()}
def _generate_rule_based_response(self, message: str, intent: Dict, system_data: Dict) -> Dict[str, Any]:
"""Generate response using rule-based system"""
primary_intent = intent["primary"]
message_lower = message.lower()
if primary_intent == "greeting":
response_text = self.response_templates["greeting"][0]
elif primary_intent == "v2g_question":
v2g_data = system_data.get("v2g_data", {})
if v2g_data:
active_sessions = v2g_data.get("active_sessions", 0)
total_earnings = v2g_data.get("total_earnings", 0)
current_rate = v2g_data.get("current_rate", 0)
response_text = f"""V2G System Status:
- Active V2G sessions: {active_sessions}
- Total earnings: ${total_earnings:.2f}
- Current rate: ${current_rate:.2f}/kWh
- Premium multiplier: 50x normal charging
V2G allows vehicles to discharge energy back to the grid during high demand, earning premium rates while supporting grid stability. Vehicles need 60%+ SOC to participate."""
else:
response_text = self.response_templates["v2g_help"][0]
elif primary_intent == "ml_question":
ml_data = system_data.get("ml_data", {})
if ml_data and "metrics" in ml_data:
metrics = ml_data["metrics"]
response_text = f"""ML Analytics Status:
- Demand prediction accuracy: {100 - metrics.get('demand_mape', 5):.1f}%
- Patterns found: {metrics.get('patterns_found', 0)}
- Optimization savings: {metrics.get('optimization_savings', 0):.1f}%
- V2G revenue optimization: {metrics.get('v2g_revenue_optimization', 0):.1f}%
Our ML models continuously learn from real-time data to optimize grid operations and V2G trading."""
else:
response_text = self.response_templates["ml_insights"][0]
elif primary_intent == "system_status":
stats = system_data.get("system_stats", {})
response_text = f"""System Status Overview:
- Substations: {stats.get('substations_operational', 0)}/{stats.get('total_substations', 0)} operational
- Traffic lights: {stats.get('traffic_lights_powered', 0)}/{stats.get('total_traffic_lights', 0)} powered
- EV stations: {stats.get('ev_stations_operational', 0)}/{stats.get('total_ev_stations', 0)} operational
- Current time: {stats.get('current_time', 'Unknown')}
All systems are functioning normally with advanced monitoring and optimization active."""
elif primary_intent == "substation":
stats = system_data.get("system_stats", {})
response_text = f"""Power Grid & Substations:
- Operational substations: {stats.get('substations_operational', 0)}/{stats.get('total_substations', 0)}
- Voltage levels: 13.8kV primary, 480V secondary
- Load management: Real-time monitoring and optimization
- Protection systems: Circuit breakers, fuses, and relays
- Capacity: 10MVA to 100MVA depending on location
The power grid uses advanced monitoring and ML-based load forecasting for optimal distribution."""
elif primary_intent == "traffic":
stats = system_data.get("system_stats", {})
response_text = f"""Traffic Management System:
- Powered traffic lights: {stats.get('traffic_lights_powered', 0)}/{stats.get('total_traffic_lights', 0)}
- Platform: SUMO (Simulation of Urban Mobility)
- Vehicle types: Gas cars, Electric vehicles, Buses, Trucks
- Control: Adaptive signals with AI-based optimization
- Features: Emergency vehicle priority, network coordination
The system uses real-time traffic flow analysis for optimal signal timing and congestion reduction."""
elif primary_intent == "ev_charging":
stats = system_data.get("system_stats", {})
response_text = f"""EV Charging Infrastructure:
- Operational stations: {stats.get('ev_stations_operational', 0)}/{stats.get('total_ev_stations', 0)}
- Charging types: Level 1 (120V), Level 2 (240V), DC Fast (480V)
- Management: Smart charging with load balancing
- Features: SOC tracking, queue management, reservation systems
- V2G capability: Bidirectional energy flow for grid support
The charging network is strategically placed throughout Manhattan with intelligent management systems."""
elif primary_intent == "blackout":
response_text = """Blackout & Emergency Response:
- Emergency protocols: Automatic V2G activation and load redistribution
- Restoration procedures: Critical infrastructure first, gradual load restoration
- V2G deployment: Vehicle-to-grid emergency power activation
- Elevator rescue: V2G-powered elevator rescue operations
- Monitoring: Real-time restoration progress tracking
The system includes comprehensive emergency response capabilities with V2G-powered backup systems."""
elif primary_intent == "monitoring":
response_text = """System Monitoring & Alerts:
- Real-time monitoring: Power grid, traffic systems, EV charging, V2G operations
- Alert types: Critical, warning, maintenance, performance alerts
- Coverage: Voltage, current, power flow, traffic flow, charging status
- Response: Automatic notifications and recommended actions
- Integration: SCADA systems with remote monitoring capabilities
Advanced monitoring provides 24/7 system surveillance with proactive alerting."""
elif primary_intent == "power_distribution":
response_text = """Power Distribution System:
- Primary lines: 13.8kV overhead and underground cables
- Secondary lines: 480V distribution to buildings
- Load balancing: ML-based demand forecasting and distribution optimization
- Demand response: Peak demand management and load shedding
- Protection: Circuit breakers, fuses, and protective relays
The distribution system uses advanced load management and optimization algorithms."""
elif primary_intent == "traffic_management":
response_text = """Traffic Management & Control:
- Signal types: Fixed-time, Actuated, Adaptive, Connected
- Optimization: AI-based timing optimization for traffic flow
- Coordination: Network-wide signal coordination for optimal flow
- Priority systems: Emergency vehicle and transit priority
- Integration: SCADA integration with remote monitoring
The traffic management system uses advanced algorithms for optimal traffic flow and congestion reduction."""
elif primary_intent == "energy_market":
v2g_data = system_data.get("v2g_data", {})
current_rate = v2g_data.get("current_rate", 0)
response_text = f"""Energy Market & Pricing:
- Current V2G rate: ${current_rate:.2f}/kWh
- Pricing model: Dynamic pricing based on grid demand
- Premium multiplier: 50x normal charging rates during emergencies
- Market factors: Grid demand, V2G availability, time of day
- Revenue optimization: ML-based pricing and vehicle routing
The energy market uses dynamic pricing to optimize V2G participation and grid stability."""
elif primary_intent == "maintenance":
response_text = """Maintenance & Service:
- Predictive maintenance: ML-based equipment failure prediction
- Scheduled maintenance: Regular inspections and upgrades
- Service types: Equipment repair, replacement, and upgrades
- Monitoring: Real-time equipment health monitoring
- Optimization: Maintenance scheduling for minimal disruption
The system uses predictive analytics to optimize maintenance schedules and prevent failures."""
elif primary_intent == "safety":
response_text = """Safety & Security:
- Protection systems: Circuit breakers, fuses, and protective relays
- Emergency protocols: Automatic load shedding and V2G activation
- Hazard detection: Real-time monitoring of safety parameters
- Risk management: Proactive identification and mitigation
- Compliance: Industry safety standards and regulations
Comprehensive safety systems ensure reliable and secure grid operations."""
elif primary_intent == "performance":
ml_data = system_data.get("ml_data", {})
metrics = ml_data.get("metrics", {})
response_text = f"""System Performance:
- Demand prediction accuracy: {100 - metrics.get('demand_mape', 5):.1f}%
- Optimization savings: {metrics.get('optimization_savings', 0):.1f}%
- V2G revenue optimization: {metrics.get('v2g_revenue_optimization', 0):.1f}%
- Grid efficiency: Real-time monitoring and optimization
- Throughput: Continuous system performance tracking
Advanced analytics provide comprehensive performance monitoring and optimization."""
elif primary_intent == "anomalies":
ml_data = system_data.get("ml_data", {})
anomalies = ml_data.get("anomalies", [])
if anomalies:
response_text = f"""[ANOMALIES] System Anomalies Detected ({len(anomalies)} total):
"""
for i, anomaly in enumerate(anomalies, 1):
severity_emoji = "[HIGH]" if anomaly.get("severity") == "HIGH" else "[MEDIUM]" if anomaly.get("severity") == "MEDIUM" else "[LOW]"
response_text += f"{severity_emoji} **{anomaly.get('type', 'Unknown')}** - {anomaly.get('severity', 'Unknown')} Severity\n"
response_text += f" Description: {anomaly.get('description', 'No description')}\n"
response_text += f" Score: {anomaly.get('score', 0):.3f}\n"
response_text += f" Time: {anomaly.get('timestamp', 'Unknown')}\n\n"
response_text += """**Recommended Actions:**
- Review high-severity anomalies immediately
- Check system logs for additional details
- Consider activating V2G emergency protocols if needed
- Monitor system stability closely"""
else:
response_text = """Success No Anomalies Detected
The system is operating normally with no anomalies or issues detected. All components are functioning within expected parameters."""
elif primary_intent == "data_request":
# Provide specific data based on request
if "v2g" in message_lower:
v2g_data = system_data.get("v2g_data", {})
response_text = f"V2G Data: {json.dumps(v2g_data, indent=2)}"
elif "ml" in message_lower:
ml_data = system_data.get("ml_data", {})
response_text = f"ML Data: {json.dumps(ml_data, indent=2)}"
else:
response_text = f"System Data: {json.dumps(system_data, indent=2)}"
elif primary_intent == "help":
response_text = """I can help you with all aspects of the Manhattan Power Grid system:
[POWER] **Power Grid**: Substations, distribution, load management, voltage levels
POWER **V2G Systems**: Energy trading, revenue optimization, vehicle recruitment
AI **Machine Learning**: Predictions, analytics, anomaly detection, optimization
[TRAFFIC] **Traffic Management**: Signal control, congestion reduction, vehicle simulation
Battery **EV Charging**: Charging stations, battery management, SOC tracking
[EMERGENCY] **Emergency Response**: Blackout scenarios, restoration procedures, V2G activation
Stats **Monitoring**: Real-time monitoring, alerts, performance tracking
Money **Energy Market**: Pricing, rates, economics, financial optimization
[MAINTENANCE] **Maintenance**: Predictive maintenance, service scheduling, equipment health
[SAFETY] **Safety**: Protection systems, risk management, compliance
Just ask me about any of these topics!"""
elif primary_intent == "scenario_list":
response_text = """🎬 **Available Scenarios:**
1️⃣ **V2G EMERGENCY RESCUE** ⭐ Recommended
• Demonstrates Vehicle-to-Grid emergency response
• Substation fails, EVs provide emergency power
• Live camera choreography and progress tracking
• Duration: ~60 seconds
Command: "run v2g scenario" or "trigger v2g"
2️⃣ **CITYWIDE BLACKOUT**
• Manhattan-wide power failure simulation
• 7/8 substations fail dramatically
• Shows traffic light and infrastructure impact
• Duration: Until manual restoration
Command: "trigger blackout" or "blackout scenario"
💡 **Scenario Features:**
✓ Automatic vehicle preparation
✓ Cinematic camera movements
✓ Live narration and updates
✓ Real-time system monitoring
✓ Post-scenario analytics
Just type the command to start any scenario!"""
elif primary_intent == "scenario_v2g":
response_text = """[SCENARIO_PREP:v2g]"""
elif primary_intent == "scenario_blackout":
response_text = """[SCENARIO_PREP:blackout]"""
elif primary_intent == "scenario_confirm":
response_text = """I'll need more context. What scenario would you like to confirm?
Available scenarios:
• "run v2g scenario" - V2G Emergency Rescue
• "trigger blackout" - Citywide Blackout"""
elif primary_intent == "scenario_cancel":
response_text = """✅ Operation cancelled. Let me know if you'd like to try something else!"""
else:
response_text = "I understand you're asking about the Manhattan Power Grid. I have comprehensive knowledge of all system components including power grid operations, V2G systems, ML analytics, traffic management, EV charging, emergency response, and more. Could you be more specific about what you'd like to know?"
return {
"text": response_text,
"type": "response",
"intent": primary_intent,
"timestamp": datetime.now().isoformat(),
"data": system_data
}
def _generate_openai_response(self, message: str, intent: Dict, system_data: Dict) -> Dict[str, Any]:
"""Generate response using OpenAI API"""
try:
# Build context for OpenAI
context = self._build_openai_context(system_data)
# Create system prompt
system_prompt = f"""You are an expert AI assistant for the Manhattan Power Grid system. You have deep knowledge of:
- Power grid operations and substation management
- V2G (Vehicle-to-Grid) energy trading systems
- Machine learning analytics and predictions
- Traffic light management and optimization
- EV charging infrastructure
- Energy market dynamics and pricing
Current system context:
{json.dumps(context, indent=2)}
Provide helpful, accurate, and detailed responses about the power grid system. If asked about specific data, use the provided context. Be conversational but professional."""
# Make API call
response = self.openai_client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": message}
],
max_tokens=500,
temperature=0.7
)
response_text = response.choices[0].message.content
return {
"text": response_text,
"type": "ai_response",
"intent": intent["primary"],
"timestamp": datetime.now().isoformat(),
"data": system_data
}
except Exception as e:
# Fallback to rule-based response
return self._generate_rule_based_response(message, intent, system_data)
def _build_openai_context(self, system_data: Dict) -> Dict[str, Any]:
"""Build context for OpenAI API"""
return {
"system_overview": {
"name": "Manhattan Power Grid",
"status": "operational",
"capabilities": ["V2G trading", "ML analytics", "Grid optimization", "Traffic management"]
},
"current_metrics": system_data.get("system_stats", {}),
"v2g_status": system_data.get("v2g_data", {}),
"ml_insights": system_data.get("ml_data", {}),
"knowledge_base": self.knowledge_base
}
def get_conversation_history(self, user_id: str = "default", limit: int = 10) -> List[Dict]:
"""Get conversation history for a user"""
user_history = [msg for msg in self.conversation_history if msg.get("user_id") == user_id]
return user_history[-limit:] if limit else user_history
def clear_conversation_history(self, user_id: str = "default"):
"""Clear conversation history for a user"""
self.conversation_history = [msg for msg in self.conversation_history if msg.get("user_id") != user_id]
def get_system_insights(self) -> Dict[str, Any]:
"""Get comprehensive system insights for the chatbot"""
try:
system_data = self._get_current_system_data()
# Generate insights
insights = {
"grid_health": self._assess_grid_health(system_data),
"v2g_opportunities": self._assess_v2g_opportunities(system_data),
"ml_performance": self._assess_ml_performance(system_data),
"recommendations": self._generate_recommendations(system_data)
}
return insights
except Exception as e:
return {"error": str(e)}
def _assess_grid_health(self, system_data: Dict) -> Dict[str, Any]:
"""Assess overall grid health"""
stats = system_data.get("system_stats", {})
operational_subs = stats.get("substations_operational", 0)
total_subs = stats.get("total_substations", 1)
substation_health = operational_subs / total_subs
powered_lights = stats.get("traffic_lights_powered", 0)
total_lights = stats.get("total_traffic_lights", 1)
traffic_health = powered_lights / total_lights
overall_health = (substation_health + traffic_health) / 2
return {
"overall_score": round(overall_health * 100, 1),
"substation_health": round(substation_health * 100, 1),
"traffic_health": round(traffic_health * 100, 1),
"status": "excellent" if overall_health > 0.9 else "good" if overall_health > 0.7 else "needs_attention"
}
def _assess_v2g_opportunities(self, system_data: Dict) -> Dict[str, Any]:
"""Assess V2G opportunities"""
v2g_data = system_data.get("v2g_data", {})
active_sessions = v2g_data.get("active_sessions", 0)
total_earnings = v2g_data.get("total_earnings", 0)
current_rate = v2g_data.get("current_rate", 0)
return {
"active_sessions": active_sessions,
"total_earnings": total_earnings,
"current_rate": current_rate,
"opportunity_level": "high" if current_rate > 1.0 else "medium" if current_rate > 0.5 else "low"
}
def _assess_ml_performance(self, system_data: Dict) -> Dict[str, Any]:
"""Assess ML model performance"""
ml_data = system_data.get("ml_data", {})
metrics = ml_data.get("metrics", {})
return {
"demand_accuracy": 100 - metrics.get("demand_mape", 5),
"patterns_found": metrics.get("patterns_found", 0),
"optimization_savings": metrics.get("optimization_savings", 0),
"performance_level": "excellent" if metrics.get("demand_mape", 5) < 3 else "good" if metrics.get("demand_mape", 5) < 7 else "needs_improvement"
}
def _generate_recommendations(self, system_data: Dict) -> List[str]:
"""Generate system recommendations"""
recommendations = []
# Grid health recommendations
grid_health = self._assess_grid_health(system_data)
if grid_health["overall_score"] < 90:
recommendations.append("Consider grid maintenance to improve overall health score")
# V2G recommendations
v2g_opps = self._assess_v2g_opportunities(system_data)
if v2g_opps["opportunity_level"] == "high":
recommendations.append("High V2G opportunity detected - consider activating more vehicles")
# ML recommendations
ml_perf = self._assess_ml_performance(system_data)
if ml_perf["performance_level"] == "needs_improvement":
recommendations.append("ML models may need retraining for better accuracy")
return recommendations
def get_v2g_insights(self) -> str:
"""Get V2G-specific insights and recommendations"""
try:
v2g_data = self.v2g_manager.get_v2g_dashboard_data() if self.v2g_manager else {}
active_sessions = v2g_data.get("active_sessions", 0)
total_earnings = v2g_data.get("total_earnings", 0)
current_rate = v2g_data.get("current_rate", 0)
insights = f"""V2G System Insights:
- Current active sessions: {active_sessions}
- Total revenue generated: ${total_earnings:.2f}
- Current market rate: ${current_rate:.2f}/kWh
- Premium multiplier: 50x normal charging rates
Recommendations:
- Monitor grid demand for optimal V2G activation timing
- Recruit vehicles with 60%+ SOC for maximum participation
- Use ML predictions to optimize pricing and vehicle routing
- Consider emergency scenarios for V2G deployment"""
return insights
except Exception as e:
return f"V2G insights unavailable: {str(e)}"
def get_optimization_opportunities(self) -> str:
"""Get system optimization opportunities"""
try:
ml_data = self.ml_engine.get_ml_dashboard_data() if self.ml_engine else {}
optimization = ml_data.get("optimization", {})