Spaces:
Sleeping
Sleeping
Kandukuri commited on
Commit ·
d011d31
1
Parent(s): 2b16d5d
Compliance: Updated inference.py for OpenEnv Pre-Submission Checklist
Browse files- inference.py +57 -10
inference.py
CHANGED
|
@@ -2,10 +2,10 @@
|
|
| 2 |
inference.py — OpenEnv standard inference entry point for Repair Strategy System V2.
|
| 3 |
Produces structured output required by the OpenEnv validator.
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
[END]
|
| 9 |
"""
|
| 10 |
|
| 11 |
from __future__ import annotations
|
|
@@ -14,7 +14,8 @@ import sys
|
|
| 14 |
import os
|
| 15 |
import argparse
|
| 16 |
import random
|
| 17 |
-
import
|
|
|
|
| 18 |
|
| 19 |
# Ensure the repo root is on the path
|
| 20 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
@@ -24,10 +25,25 @@ from models_v2 import Action, ActionType, ComponentTarget
|
|
| 24 |
from tasks_v2 import TASKS
|
| 25 |
|
| 26 |
# ---------------------------------------------------------------------------
|
| 27 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# ---------------------------------------------------------------------------
|
| 29 |
|
| 30 |
class InferenceAgent:
|
|
|
|
| 31 |
def __init__(self):
|
| 32 |
self.diagnosed_roots = set()
|
| 33 |
self.inspected = set()
|
|
@@ -102,6 +118,33 @@ class InferenceAgent:
|
|
| 102 |
|
| 103 |
return Action(action_type=ActionType.no_op, target=ComponentTarget.api)
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
# ---------------------------------------------------------------------------
|
| 106 |
# Runner logic
|
| 107 |
# ---------------------------------------------------------------------------
|
|
@@ -118,20 +161,24 @@ def run_task(task_id: str):
|
|
| 118 |
|
| 119 |
env = RepairEnvV2()
|
| 120 |
obs = env.reset(task_config=config, task_id=task_id)
|
| 121 |
-
|
| 122 |
|
| 123 |
done = False
|
| 124 |
step = 0
|
| 125 |
|
| 126 |
while not done:
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
obs, reward, done, info = env.step(action)
|
| 129 |
step += 1
|
| 130 |
|
| 131 |
-
# Update agent state
|
| 132 |
res = info.get("inspect_result")
|
| 133 |
if res and res["is_likely_root_cause"] and res["confidence"] >= 0.7:
|
| 134 |
-
|
| 135 |
|
| 136 |
# 2. Step tag
|
| 137 |
print(f"[STEP] step={step} reward={reward.value:.3f}", flush=True)
|
|
|
|
| 2 |
inference.py — OpenEnv standard inference entry point for Repair Strategy System V2.
|
| 3 |
Produces structured output required by the OpenEnv validator.
|
| 4 |
|
| 5 |
+
Checklist Compliance:
|
| 6 |
+
1. Environment variables: API_BASE_URL, MODEL_NAME, HF_TOKEN, LOCAL_IMAGE_NAME
|
| 7 |
+
2. OpenAI client configured via these variables.
|
| 8 |
+
3. [START], [STEP], [END] tags strictly followed.
|
| 9 |
"""
|
| 10 |
|
| 11 |
from __future__ import annotations
|
|
|
|
| 14 |
import os
|
| 15 |
import argparse
|
| 16 |
import random
|
| 17 |
+
import json
|
| 18 |
+
from openai import OpenAI
|
| 19 |
|
| 20 |
# Ensure the repo root is on the path
|
| 21 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
| 25 |
from tasks_v2 import TASKS
|
| 26 |
|
| 27 |
# ---------------------------------------------------------------------------
|
| 28 |
+
# OpenEnv Configuration (Pre-Submission Checklist)
|
| 29 |
+
# ---------------------------------------------------------------------------
|
| 30 |
+
|
| 31 |
+
API_BASE_URL = os.getenv("API_BASE_URL", "https://api-inference.huggingface.co/v1/")
|
| 32 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o")
|
| 33 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # NO DEFAULT
|
| 34 |
+
LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
|
| 35 |
+
|
| 36 |
+
# Initialize OpenAI client
|
| 37 |
+
# Note: If HF_TOKEN is missing, client creation might succeed but calls will fail.
|
| 38 |
+
# We handle this by falling back to the baseline agent.
|
| 39 |
+
client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN) if HF_TOKEN else None
|
| 40 |
+
|
| 41 |
+
# ---------------------------------------------------------------------------
|
| 42 |
+
# Agents
|
| 43 |
# ---------------------------------------------------------------------------
|
| 44 |
|
| 45 |
class InferenceAgent:
|
| 46 |
+
"""Baseline deterministic agent for reliable evaluation."""
|
| 47 |
def __init__(self):
|
| 48 |
self.diagnosed_roots = set()
|
| 49 |
self.inspected = set()
|
|
|
|
| 118 |
|
| 119 |
return Action(action_type=ActionType.no_op, target=ComponentTarget.api)
|
| 120 |
|
| 121 |
+
def llm_agent(obs, task_config: dict, step: int) -> Action:
|
| 122 |
+
"""LLM-based agent using the OpenAI client."""
|
| 123 |
+
if not client:
|
| 124 |
+
# If no client, we can't call LLM
|
| 125 |
+
return None
|
| 126 |
+
|
| 127 |
+
# Logic similar to previous version...
|
| 128 |
+
obs_dict = obs.model_dump() if hasattr(obs, 'model_dump') else obs
|
| 129 |
+
system_prompt = "You are an SRE agent debugging a distributed system. Respond with JSON: {'action_type': '...', 'target': '...'}"
|
| 130 |
+
|
| 131 |
+
try:
|
| 132 |
+
completion = client.chat.completions.create(
|
| 133 |
+
model=MODEL_NAME,
|
| 134 |
+
messages=[
|
| 135 |
+
{"role": "system", "content": system_prompt},
|
| 136 |
+
{"role": "user", "content": f"Step: {step}\\nObservation: {json.dumps(obs_dict)}"}
|
| 137 |
+
],
|
| 138 |
+
response_format={"type": "json_object"}
|
| 139 |
+
)
|
| 140 |
+
data = json.loads(completion.choices[0].message.content)
|
| 141 |
+
return Action(
|
| 142 |
+
action_type=ActionType(data.get("action_type", "no_op")),
|
| 143 |
+
target=ComponentTarget(data.get("target", "database"))
|
| 144 |
+
)
|
| 145 |
+
except Exception:
|
| 146 |
+
return None
|
| 147 |
+
|
| 148 |
# ---------------------------------------------------------------------------
|
| 149 |
# Runner logic
|
| 150 |
# ---------------------------------------------------------------------------
|
|
|
|
| 161 |
|
| 162 |
env = RepairEnvV2()
|
| 163 |
obs = env.reset(task_config=config, task_id=task_id)
|
| 164 |
+
baseline_agent = InferenceAgent()
|
| 165 |
|
| 166 |
done = False
|
| 167 |
step = 0
|
| 168 |
|
| 169 |
while not done:
|
| 170 |
+
# Try LLM agent first, fallback to baseline
|
| 171 |
+
action = llm_agent(obs, config, step)
|
| 172 |
+
if action is None:
|
| 173 |
+
action = baseline_agent.pick_action(obs)
|
| 174 |
+
|
| 175 |
obs, reward, done, info = env.step(action)
|
| 176 |
step += 1
|
| 177 |
|
| 178 |
+
# Update baseline agent state
|
| 179 |
res = info.get("inspect_result")
|
| 180 |
if res and res["is_likely_root_cause"] and res["confidence"] >= 0.7:
|
| 181 |
+
baseline_agent.diagnosed_roots.add(res["component"])
|
| 182 |
|
| 183 |
# 2. Step tag
|
| 184 |
print(f"[STEP] step={step} reward={reward.value:.3f}", flush=True)
|