Spaces:
Sleeping
Sleeping
File size: 2,357 Bytes
81e3338 | 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 | """
Claude Haiku remediation generator.
Same prompt template as notebook 10. Returns a typed RemediationBlock or None
on failure. The endpoint never blows up on a remediation error — it returns the
classification result and lets the dashboard show a placeholder.
"""
import json
import os
from typing import Optional
from anthropic import Anthropic
from .schemas import RemediationBlock
REMEDIATION_SYSTEM = """You are a senior site reliability engineer producing concise,
actionable remediation guidance for cloud infrastructure incidents.
Respond ONLY with valid JSON matching this exact schema:
{
"summary": "one or two sentence description of what broke",
"root_cause_hypothesis": "the most likely root cause given the incident description",
"immediate_mitigation": ["concrete action 1", "concrete action 2", "concrete action 3"],
"investigation": ["specific log or metric to check 1", "..."],
"prevention": ["long-term action 1", "..."]
}
Be concrete. No hedging language. No general SRE advice. Specific to this incident."""
_client: Optional[Anthropic] = None
def _get_client() -> Anthropic:
global _client
if _client is None:
_client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
return _client
def generate_remediation(
description: str,
severity: str,
model: str = "claude-haiku-4-5-20251001",
) -> Optional[RemediationBlock]:
"""Calls Claude Haiku and parses the JSON remediation. Returns None on failure."""
try:
client = _get_client()
response = client.messages.create(
model=model,
max_tokens=800,
system=REMEDIATION_SYSTEM,
messages=[{
"role": "user",
"content": f"Severity: {severity}\nDescription: {description}",
}],
)
text = response.content[0].text.strip()
# Strip markdown fences
if text.startswith("```"):
text = text.split("```")[1]
if text.startswith("json"):
text = text[4:]
text = text.strip()
data = json.loads(text)
return RemediationBlock(**data)
except Exception:
# Caller decides what to do. Failing the whole request because remediation
# generation hiccuped would make the dashboard worse, not better.
return None
|