Spaces:
Sleeping
Sleeping
File size: 9,858 Bytes
5fe9036 | 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 | """
Inference Script β SRE Incident Response Environment
=====================================================
MANDATORY:
- Before submitting, ensure the following variables are defined in your environment:
API_BASE_URL The API endpoint for the LLM.
MODEL_NAME The model identifier to use for inference.
HF_TOKEN Your Hugging Face / API key.
LOCAL_IMAGE_NAME The name of the local Docker image (if using from_docker_image)
- The inference script must be named `inference.py` and placed in the root directory
- Participants must use OpenAI Client for all LLM calls using above variables
STDOUT FORMAT:
[START] task=<task_name> env=<benchmark> model=<model_name>
[STEP] step=<n> action=<action_str> reward=<0.00> done=<true|false> error=<msg|null>
[END] success=<true|false> steps=<n> score=<score> rewards=<r1,r2,...,rn>
"""
import asyncio
import json
import os
import sys
import textwrap
from typing import List
from openai import OpenAI
# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from models import Action, ActionType, RootCauseCategory
from env.environment import IncidentResponseEnv
IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
API_KEY = os.getenv("HF_TOKEN") or os.getenv("API_KEY")
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
BENCHMARK = "sre_incident_response"
MAX_STEPS = 20
TEMPERATURE = 0.7
SUCCESS_SCORE_THRESHOLD = 0.7
# ββ Logging helpers (strict format) βββββββββββββββββββββββββββββββββββ
def log_start(task: str, env: str, model: str) -> None:
print(f"[START] task={task} env={env} model={model}", flush=True)
def log_step(step: int, action: str, reward: float, done: bool, error) -> None:
error_str = str(error) if error is not None else "null"
done_str = "true" if done else "false"
print(
f"[STEP] step={step} action={action} reward={reward:.2f} done={done_str} error={error_str}",
flush=True,
)
def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
success_str = "true" if success else "false"
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
print(
f"[END] success={success_str} steps={steps} score={score:.2f} rewards={rewards_str}",
flush=True,
)
# ββ System prompt βββββββββββββββββββββββββββββββββββββββββββββββββββββ
SYSTEM_PROMPT = textwrap.dedent("""\
You are an expert SRE (Site Reliability Engineer) responding to a production incident.
You are given the current state of a microservice architecture and must:
1. Investigate by reading logs, checking metrics, tracing requests, and examining dependencies
2. Identify the root cause(s)
3. Apply the correct fix(es)
4. Submit a diagnosis
Available actions (respond with a single JSON object):
Investigation actions (require "service" field):
- read_logs: Read recent logs from a service
- check_metrics: Get time-series metrics (CPU, memory, latency, error rate)
- ping_service: Check if service is reachable
- check_dependencies: See upstream/downstream dependencies and their health
- inspect_deploy: See deploy history (versions, timestamps)
- query_traces: See distributed trace spans
- check_runbook: Get operational runbook for the service
- diff_config: Compare current vs previous config
Remediation actions (require "service" field):
- restart_service: Restart all pods for a service
- rollback_deploy: Rollback to a specific version (requires "target_version")
- scale_up: Increase replica count (requires "replicas")
- drain_traffic: Stop routing traffic to a service
Terminal action:
- submit_diagnosis: Submit your diagnosis (requires "root_cause_service", "root_cause_category", "fix_description")
Root cause categories: oom_crash, db_deadlock, bad_deploy, memory_leak, network_partition, disk_full, config_error, cert_expiry, dns_failure, rate_limit
IMPORTANT: Respond with ONLY a JSON object like:
{"action_type": "read_logs", "service": "auth-service"}
{"action_type": "rollback_deploy", "service": "payment-service", "target_version": "v3.8.1"}
{"action_type": "submit_diagnosis", "root_cause_service": "db-postgres", "root_cause_category": "db_deadlock", "fix_description": "Restarted db-postgres to clear deadlock"}
""")
# ββ Helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def format_observation(obs_dict: dict) -> str:
"""Format observation into a readable prompt for the LLM."""
parts = []
if obs_dict.get("incident_summary"):
parts.append(f"INCIDENT SUMMARY: {obs_dict['incident_summary']}")
parts.append(f"\nSTEP: {obs_dict.get('step_number', 0)}")
services = obs_dict.get("services", {})
if services:
parts.append("\nSERVICE STATUS DASHBOARD:")
for name, state in services.items():
status = state.get("status", "UNKNOWN")
version = state.get("version", "")
parts.append(f" {name}: {status} (version: {version})")
alerts = obs_dict.get("active_alerts", [])
if alerts:
parts.append("\nACTIVE ALERTS:")
for alert in alerts:
parts.append(f" {alert}")
action_result = obs_dict.get("action_result")
if action_result:
parts.append(f"\nRESULT OF LAST ACTION:\n{action_result}")
return "\n".join(parts)
def get_model_message(client: OpenAI, obs_text: str, history: List[str]) -> str:
"""Call the LLM and return the raw response text."""
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
]
# Include recent history for context
for h in history[-6:]:
messages.append({"role": "user", "content": h})
messages.append({"role": "user", "content": obs_text})
try:
response = client.chat.completions.create(
model=MODEL_NAME,
messages=messages,
temperature=TEMPERATURE,
max_tokens=512,
)
return response.choices[0].message.content
except Exception as exc:
print(f"[DEBUG] Model request failed: {exc}", flush=True)
return '{"action_type": "read_logs", "service": "auth-service"}'
def parse_action(response_text: str) -> Action:
"""Parse LLM response into an Action object."""
text = response_text.strip()
if "```json" in text:
text = text.split("```json")[1].split("```")[0].strip()
elif "```" in text:
text = text.split("```")[1].split("```")[0].strip()
start = text.find("{")
end = text.rfind("}")
if start != -1 and end != -1:
text = text[start : end + 1]
data = json.loads(text)
return Action(**data)
# ββ Main ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async def run_task(task_id: str) -> float:
"""Run inference on a single task. Returns score in [0, 1]."""
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
env = IncidentResponseEnv()
history: List[str] = []
rewards: List[float] = []
steps_taken = 0
score = 0.0
success = False
log_start(task=task_id, env=BENCHMARK, model=MODEL_NAME)
try:
obs, session_id = env.reset(task_id=task_id)
obs_dict = obs.model_dump()
for step in range(1, MAX_STEPS + 1):
if obs_dict.get("done", False):
break
obs_text = format_observation(obs_dict)
message = get_model_message(client, obs_text, history)
try:
action = parse_action(message)
error = None
except Exception as e:
error = str(e)
log_step(step=step, action="parse_error", reward=0.0, done=False, error=error)
rewards.append(0.0)
steps_taken = step
history.append(f"Step {step}: parse_error -> reward 0.00")
continue
obs, reward, done, info = env.step(session_id, action)
obs_dict = obs.model_dump()
reward = reward or 0.0
rewards.append(reward)
steps_taken = step
action_str = action.action_type.value
if action.service:
action_str += f"({action.service})"
log_step(step=step, action=action_str, reward=reward, done=done, error=error)
history.append(f"Step {step}: {action_str} -> reward {reward:+.2f}")
if done:
if "grader_result" in info:
score = info["grader_result"]["score"]
break
# Clamp score to [0, 1]
score = min(max(score, 0.0), 1.0)
success = score >= SUCCESS_SCORE_THRESHOLD
finally:
log_end(success=success, steps=steps_taken, score=score, rewards=rewards)
return score
async def main() -> None:
task_ids = os.getenv("SRE_TASKS", "easy,medium,hard").split(",")
scores = {}
for task_id in task_ids:
task_id = task_id.strip()
score = await run_task(task_id)
scores[task_id] = score
print(f"\n{'='*60}", flush=True)
print("FINAL SCORES:", flush=True)
for task_id, score in scores.items():
print(f" {task_id}: {score:.2f}", flush=True)
avg = sum(scores.values()) / len(scores) if scores else 0
print(f" AVERAGE: {avg:.2f}", flush=True)
print(f"{'='*60}", flush=True)
if __name__ == "__main__":
asyncio.run(main())
|