petter2025 commited on
Commit
0b3ce9a
·
verified ·
1 Parent(s): f4c1041

Update claude_adapter.py

Browse files
Files changed (1) hide show
  1. claude_adapter.py +217 -0
claude_adapter.py CHANGED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Claude Opus 4.5 Adapter for ARF
3
+ Drop-in replacement for Hugging Face inference
4
+ """
5
+
6
+ import os
7
+ import logging
8
+ from typing import Optional
9
+ from dataclasses import dataclass
10
+
11
+ logger = logging.getLogger(__name__)
12
+
13
+ # Try to import anthropic, with graceful fallback
14
+ try:
15
+ import anthropic
16
+ ANTHROPIC_AVAILABLE = True
17
+ except ImportError:
18
+ ANTHROPIC_AVAILABLE = False
19
+ logger.warning("anthropic package not installed - using mock mode only")
20
+
21
+
22
+ @dataclass
23
+ class ClaudeConfig:
24
+ """Claude API configuration"""
25
+ api_key: str
26
+ model: str = "claude-opus-4"
27
+ max_tokens: int = 512
28
+ temperature: float = 0.3
29
+
30
+
31
+ class ClaudeAdapter:
32
+ """
33
+ Drop-in replacement for HF inference in ARF agents
34
+
35
+ Features:
36
+ - Automatic fallback to mock mode if no API key
37
+ - Intelligent pre-written responses for demo
38
+ - Same interface as HF inference
39
+ - Built-in error handling
40
+ """
41
+
42
+ def __init__(self, config: Optional[ClaudeConfig] = None):
43
+ self.config = config or ClaudeConfig(
44
+ api_key=os.environ.get("ANTHROPIC_API_KEY", "")
45
+ )
46
+
47
+ if not ANTHROPIC_AVAILABLE:
48
+ logger.warning("Anthropic package not available - mock mode only")
49
+ self.mock_mode = True
50
+ elif not self.config.api_key:
51
+ logger.warning("No ANTHROPIC_API_KEY found - using mock mode")
52
+ self.mock_mode = True
53
+ else:
54
+ try:
55
+ self.client = anthropic.Anthropic(api_key=self.config.api_key)
56
+ self.mock_mode = False
57
+ logger.info(f"✅ Claude adapter initialized with model: {self.config.model}")
58
+ except Exception as e:
59
+ logger.error(f"Failed to initialize Claude client: {e}")
60
+ self.mock_mode = True
61
+
62
+ def generate_completion(
63
+ self,
64
+ prompt: str,
65
+ system_prompt: Optional[str] = None
66
+ ) -> str:
67
+ """
68
+ Generate completion using Claude or fallback to mock
69
+
70
+ Args:
71
+ prompt: User prompt
72
+ system_prompt: Optional system context
73
+
74
+ Returns:
75
+ Generated text response
76
+ """
77
+ if self.mock_mode:
78
+ logger.debug("Using mock mode (no API key or package not available)")
79
+ return self._mock_response(prompt)
80
+
81
+ try:
82
+ messages = [{"role": "user", "content": prompt}]
83
+
84
+ kwargs = {
85
+ "model": self.config.model,
86
+ "max_tokens": self.config.max_tokens,
87
+ "temperature": self.config.temperature,
88
+ "messages": messages
89
+ }
90
+
91
+ if system_prompt:
92
+ kwargs["system"] = system_prompt
93
+
94
+ response = self.client.messages.create(**kwargs)
95
+
96
+ # Extract text from response
97
+ if response.content and len(response.content) > 0:
98
+ return response.content[0].text
99
+
100
+ logger.warning("Empty response from Claude - using mock")
101
+ return self._mock_response(prompt)
102
+
103
+ except Exception as e:
104
+ logger.error(f"Claude API error: {e} - falling back to mock")
105
+ return self._mock_response(prompt)
106
+
107
+ def _mock_response(self, prompt: str) -> str:
108
+ """
109
+ Intelligent fallback mock response for demo
110
+ Pre-crafted to show system capabilities
111
+ """
112
+ prompt_lower = prompt.lower()
113
+
114
+ # Detective Agent Response
115
+ if "detective" in prompt_lower or "anomaly" in prompt_lower:
116
+ return """🔍 ANOMALY DETECTED: Payment gateway timeout pattern identified.
117
+
118
+ PATTERN ANALYSIS:
119
+ • Current error rate: 87% (baseline: <5%)
120
+ • Latency spike: 8500ms P99 (baseline: ~100ms)
121
+ • Pattern match: 94% similarity to incident 2024-11-15 (database connection pool exhaustion)
122
+
123
+ CONFIDENCE: HIGH (0.87)
124
+
125
+ CLASSIFICATION: Infrastructure failure - upstream dependency timeout
126
+
127
+ AFFECTED METRICS:
128
+ Primary: Error rate (+1740% vs baseline)
129
+ Secondary: Latency (+8400% vs baseline)
130
+ Tertiary: Throughput degradation
131
+
132
+ RECOMMENDATION: Immediate investigation of upstream payment provider status + connection pool health check required."""
133
+
134
+ # Diagnostician Agent Response
135
+ elif "diagnostician" in prompt_lower or "root cause" in prompt_lower:
136
+ return """🔬 ROOT CAUSE ANALYSIS:
137
+
138
+ PRIMARY CAUSE:
139
+ Upstream payment provider latency spike (avg response: 8.5s, normal: <500ms)
140
+
141
+ SECONDARY FACTORS:
142
+ • Connection pool exhaustion (95% utilized)
143
+ • Retry storm amplifying load (exponential backoff not engaged)
144
+ • Circuit breaker threshold not reached (87% < 90% threshold)
145
+
146
+ EVIDENCE CHAIN:
147
+ 1. Error rate spike correlates with provider status page incident (timestamp alignment)
148
+ 2. Connection pool saturation occurred 45 seconds before error spike
149
+ 3. Upstream API latency increased 17x baseline
150
+ 4. Historical pattern match: 94% similarity to Nov 15 incident
151
+
152
+ RECOMMENDED ACTION: REROUTE
153
+ • Target: gateway-2 (backup payment processor)
154
+ • Expected recovery: 45±5 seconds
155
+ • Success probability: 92% (based on historical data)
156
+
157
+ RATIONALE: Rerouting bypasses degraded provider, allows time for upstream recovery."""
158
+
159
+ # Predictive Agent Response
160
+ elif "predictive" in prompt_lower or "forecast" in prompt_lower:
161
+ return """📈 PREDICTIVE FORECAST ANALYSIS:
162
+
163
+ CURRENT TRAJECTORY:
164
+ • Error rate: Increasing at 12%/minute (exponential trend)
165
+ • Latency: Accelerating degradation (quadratic curve)
166
+ • Resource utilization: CPU 75%, Memory 82% (stable)
167
+
168
+ TIME-TO-FAILURE ESTIMATES:
169
+ • Critical threshold (>95% error rate): ~8 minutes
170
+ • Complete service failure: ~12 minutes
171
+ • Current impact: 1,240 active users affected
172
+
173
+ RISK ASSESSMENT:
174
+ Risk Score: 0.85 (HIGH)
175
+ Confidence: 0.79
176
+ Trend: DETERIORATING
177
+
178
+ BUSINESS IMPACT FORECAST:
179
+ • Current revenue loss: \$12,000/minute
180
+ • Projected 15-min loss (no action): \$180,000
181
+ • Customer churn risk: MEDIUM (historical correlation: 0.67)
182
+ • SLA violation: IMMINENT (99.9% target, current: 13% availability)
183
+
184
+ RECOMMENDATIONS:
185
+ Primary: Execute REROUTE action immediately (Diagnostician recommendation)
186
+ Secondary: Scale connection pool +50% capacity
187
+ Tertiary: Enable aggressive circuit breaking (lower threshold to 75%)
188
+
189
+ PREVENTIVE MEASURES:
190
+ Monitor upstream provider health proactively, implement predictive circuit breaking."""
191
+
192
+ # Generic/Synthesis Response
193
+ else:
194
+ return """✅ MULTI-AGENT ANALYSIS COMPLETE
195
+
196
+ SYSTEM STATUS: Incident detected and analyzed
197
+ CONFIDENCE: HIGH (0.85)
198
+
199
+ SYNTHESIS:
200
+ All agents have completed analysis. The system has identified a critical upstream dependency failure requiring immediate intervention. Recovery action has been selected based on historical success patterns and current system state.
201
+
202
+ Recommended action: REROUTE to backup systems
203
+ Expected outcome: Service restoration within 45 seconds
204
+
205
+ Continuing autonomous monitoring..."""
206
+
207
+
208
+ # Singleton instance
209
+ _claude_adapter: Optional[ClaudeAdapter] = None
210
+
211
+
212
+ def get_claude_adapter() -> ClaudeAdapter:
213
+ """Get or create Claude adapter singleton"""
214
+ global _claude_adapter
215
+ if _claude_adapter is None:
216
+ _claude_adapter = ClaudeAdapter()
217
+ return _claude_adapter