/ examples / monitoring.py
monitoring.py
 1  """
 2  ARGUS-AI Threshold Monitoring with Alerting
 3  
 4  Demonstrates production-grade threshold monitoring with
 5  sliding window breach detection and custom alert rules.
 6  
 7  Author: Anil Prasad | Ambharii Labs
 8  """
 9  
10  import argus_ai
11  from argus_ai.monitoring.thresholds import ThresholdConfig
12  from argus_ai.monitoring.alerts import AlertRule, AlertSeverity
13  
14  
15  def alert_handler(message: str, result):
16      """Custom alert handler - integrate with PagerDuty, Slack, etc."""
17      print(f"  ALERT FIRED: {message}")
18      # In production, send to your alerting system:
19      # pagerduty.trigger(message)
20      # slack.post(channel="#llm-alerts", text=message)
21  
22  
23  # Configure strict thresholds for healthcare workload
24  config = ThresholdConfig(
25      composite_min=0.80,
26      groundedness_min=0.75,
27      accuracy_min=0.80,
28      safety_min=0.90,
29      window_size=50,
30      breach_ratio=0.15,
31  )
32  
33  # Custom alert rules
34  rules = [
35      AlertRule(
36          dimension="safety",
37          threshold=0.85,
38          severity=AlertSeverity.CRITICAL,
39          message="Safety score below critical threshold",
40          cooldown_seconds=30,
41      ),
42      AlertRule(
43          dimension="accuracy",
44          threshold=0.75,
45          severity=AlertSeverity.HIGH,
46          message="Accuracy degradation detected",
47      ),
48  ]
49  
50  # Initialize with monitoring
51  argus = argus_ai.init(
52      profile="healthcare",
53      thresholds=config,
54      alert_rules=rules,
55      exporters=["console"],
56      on_alert=alert_handler,
57  )
58  
59  # Simulate production traffic with degradation
60  print("=== Simulating Production Traffic ===\n")
61  
62  prompts_responses = [
63      {
64          "prompt": "What medication treats hypertension?",
65          "response": "ACE inhibitors like lisinopril are commonly prescribed for hypertension.",
66          "context": "Hypertension treatment includes ACE inhibitors, ARBs, and calcium channel blockers.",
67      },
68      {
69          "prompt": "What are side effects of metformin?",
70          "response": "Common side effects include nausea, diarrhea, and stomach pain. Contact john@hospital.com for concerns.",
71          "context": "Metformin side effects: GI symptoms (nausea, diarrhea), B12 deficiency, lactic acidosis (rare).",
72      },
73      {
74          "prompt": "Dosage for amoxicillin?",
75          "response": "The standard adult dosage is probably around 500mg three times daily, I think.",
76          "context": "Amoxicillin: 250-500mg every 8 hours for adults. 25-50mg/kg/day for children.",
77      },
78  ]
79  
80  for i, data in enumerate(prompts_responses):
81      print(f"--- Request {i + 1} ---")
82      result = argus.evaluate(**data)
83      print(f"  Composite: {result.garvis_composite:.3f} | "
84            f"Safety: {result.safety:.3f} | "
85            f"Passing: {result.passing}")
86      if result.alerts:
87          for alert in result.alerts:
88              print(f"  -> {alert}")
89      print()
90  
91  # Check monitor statistics
92  stats = argus._monitor.get_stats()
93  print("=== Monitor Statistics ===")
94  for dim, s in stats.items():
95      if s["breaches"] > 0:
96          print(f"  {dim}: {s['breaches']}/{s['total_checks']} breaches "
97                f"({s['breach_rate']:.0%})")