invincible-jha
commited on
Commit
•
b437302
1
Parent(s):
3af4f1c
Add agent and monitoring implementations
Browse files
src/agents/crisis_agent.py
ADDED
File without changes
|
src/agents/therapeutic_agent.py
ADDED
File without changes
|
src/monitoring/interaction_monitor.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
from typing import Dict, Any
|
3 |
+
from prometheus_client import Counter, Histogram, start_http_server
|
4 |
+
import time
|
5 |
+
from datetime import datetime
|
6 |
+
|
7 |
+
class InteractionMonitor:
|
8 |
+
"""Monitor and track user interactions with the platform"""
|
9 |
+
|
10 |
+
def __init__(self, enable_metrics_server: bool = True, metrics_port: int = 8000):
|
11 |
+
"""Initialize the monitoring system
|
12 |
+
|
13 |
+
Args:
|
14 |
+
enable_metrics_server: Whether to start the Prometheus metrics server
|
15 |
+
metrics_port: Port for the metrics server
|
16 |
+
"""
|
17 |
+
self.logger = logging.getLogger(__name__)
|
18 |
+
|
19 |
+
# Initialize Prometheus metrics
|
20 |
+
self.interaction_counter = Counter(
|
21 |
+
'mental_wellness_interactions_total',
|
22 |
+
'Total number of interactions',
|
23 |
+
['agent_type', 'interaction_type']
|
24 |
+
)
|
25 |
+
|
26 |
+
self.response_time = Histogram(
|
27 |
+
'mental_wellness_response_time_seconds',
|
28 |
+
'Time taken to generate response',
|
29 |
+
['agent_type']
|
30 |
+
)
|
31 |
+
|
32 |
+
self.error_counter = Counter(
|
33 |
+
'mental_wellness_errors_total',
|
34 |
+
'Total number of errors',
|
35 |
+
['agent_type', 'error_type']
|
36 |
+
)
|
37 |
+
|
38 |
+
# Start metrics server if enabled
|
39 |
+
if enable_metrics_server:
|
40 |
+
try:
|
41 |
+
start_http_server(metrics_port)
|
42 |
+
self.logger.info(f"Metrics server started on port {metrics_port}")
|
43 |
+
except Exception as e:
|
44 |
+
self.logger.error(f"Failed to start metrics server: {str(e)}")
|
45 |
+
|
46 |
+
def track_interaction(self, data: Dict[str, Any]):
|
47 |
+
"""Track a user interaction
|
48 |
+
|
49 |
+
Args:
|
50 |
+
data: Dictionary containing interaction data
|
51 |
+
- agent_type: Type of agent (e.g., therapeutic, crisis)
|
52 |
+
- interaction_type: Type of interaction (e.g., chat, voice)
|
53 |
+
- response_time: Time taken to generate response
|
54 |
+
- error: Optional error information
|
55 |
+
"""
|
56 |
+
try:
|
57 |
+
# Log the interaction
|
58 |
+
self.logger.info(
|
59 |
+
f"Interaction tracked - Agent: {data.get('agent_type')}, "
|
60 |
+
f"Type: {data.get('interaction_type')}"
|
61 |
+
)
|
62 |
+
|
63 |
+
# Update Prometheus metrics
|
64 |
+
self.interaction_counter.labels(
|
65 |
+
agent_type=data.get('agent_type', 'unknown'),
|
66 |
+
interaction_type=data.get('interaction_type', 'unknown')
|
67 |
+
).inc()
|
68 |
+
|
69 |
+
# Track response time if available
|
70 |
+
if 'response_time' in data:
|
71 |
+
self.response_time.labels(
|
72 |
+
agent_type=data.get('agent_type', 'unknown')
|
73 |
+
).observe(data['response_time'])
|
74 |
+
|
75 |
+
# Track errors if any
|
76 |
+
if 'error' in data:
|
77 |
+
self.error_counter.labels(
|
78 |
+
agent_type=data.get('agent_type', 'unknown'),
|
79 |
+
error_type=type(data['error']).__name__
|
80 |
+
).inc()
|
81 |
+
|
82 |
+
except Exception as e:
|
83 |
+
self.logger.error(f"Error tracking interaction: {str(e)}")
|
84 |
+
|
85 |
+
def get_metrics(self) -> Dict[str, Any]:
|
86 |
+
"""Get current metrics
|
87 |
+
|
88 |
+
Returns:
|
89 |
+
Dictionary containing current metrics
|
90 |
+
"""
|
91 |
+
return {
|
92 |
+
"total_interactions": self.interaction_counter._value.sum(),
|
93 |
+
"total_errors": self.error_counter._value.sum(),
|
94 |
+
"average_response_time": self.response_time._sum.sum() / max(self.response_time._count.sum(), 1)
|
95 |
+
}
|