Spaces:
Sleeping
Sleeping
Commit Β·
cc1335d
1
Parent(s): e48bc80
fix: /v1 base_url, verbose logging, raise on exception
Browse files- inference.py +68 -20
inference.py
CHANGED
|
@@ -3,7 +3,8 @@ inference.py β Bug Triage Env
|
|
| 3 |
OpenEnv Hackathon submission inference script.
|
| 4 |
|
| 5 |
Required env vars:
|
| 6 |
-
|
|
|
|
| 7 |
ENV_BASE_URL Bug Triage env URL (optional)
|
| 8 |
MODEL_NAME Model identifier (optional)
|
| 9 |
"""
|
|
@@ -19,15 +20,27 @@ from client import BugTriageClient
|
|
| 19 |
from model import TriageAction
|
| 20 |
|
| 21 |
# ββ config βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 22 |
-
API_BASE_URL = os.getenv("API_BASE_URL")
|
| 23 |
-
MODEL_NAME = os.getenv("MODEL_NAME", "meta-llama/Llama-3.3-70B-Instruct")
|
| 24 |
-
API_KEY = os.getenv("API_KEY") or os.getenv("HF_TOKEN") or os.getenv("OPENAI_API_KEY")
|
| 25 |
-
ENV_BASE_URL = os.getenv("ENV_BASE_URL", "https://siteshcodes-bug-triage-env.hf.space")
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if not API_KEY:
|
| 30 |
-
raise RuntimeError("API_KEY not set")
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
TASK_IDS = ["easy", "medium", "hard"]
|
| 33 |
BENCHMARK = "bug-triage-env"
|
|
@@ -35,6 +48,13 @@ TEMPERATURE = 0.0
|
|
| 35 |
MAX_TOKENS = 400
|
| 36 |
SUCCESS_SCORE_THRESHOLD = 0.4
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
SYSTEM_PROMPT = textwrap.dedent("""
|
| 39 |
You are a senior software engineering manager.
|
| 40 |
You will receive a bug report and must triage it. Respond ONLY with
|
|
@@ -70,8 +90,14 @@ def log_task_start(task_id: str) -> None:
|
|
| 70 |
print(f"\n--- Starting Task: {task_id} ---", flush=True)
|
| 71 |
|
| 72 |
|
| 73 |
-
def log_step(
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
print(
|
| 76 |
f"[STEP] step={step} task={task_id} action={action} "
|
| 77 |
f"reward={reward:.2f} done={str(done).lower()} error={error or 'null'}",
|
|
@@ -80,7 +106,7 @@ def log_step(step: int, task_id: str, action: str, reward: float,
|
|
| 80 |
|
| 81 |
|
| 82 |
def log_task_end(task_id: str, reward: float) -> None:
|
| 83 |
-
print(f"
|
| 84 |
|
| 85 |
|
| 86 |
def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
|
|
@@ -92,20 +118,22 @@ def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> No
|
|
| 92 |
)
|
| 93 |
|
| 94 |
|
| 95 |
-
# ββ
|
| 96 |
|
| 97 |
def format_bug(obs) -> str:
|
| 98 |
bug = obs.bug_report
|
| 99 |
-
comments = "\n".join(f" - {c}" for c in bug.comments)
|
| 100 |
return (
|
| 101 |
f"Title: {bug.title}\n\n"
|
| 102 |
f"Description:\n{bug.body}\n\n"
|
| 103 |
-
f"Existing labels: {', '.join(bug.labels_hint)
|
| 104 |
f"Comments:\n{comments}"
|
| 105 |
)
|
| 106 |
|
| 107 |
|
| 108 |
def call_model(client: OpenAI, bug_text: str) -> TriageAction:
|
|
|
|
|
|
|
| 109 |
completion = client.chat.completions.create(
|
| 110 |
model=MODEL_NAME,
|
| 111 |
messages=[
|
|
@@ -116,13 +144,20 @@ def call_model(client: OpenAI, bug_text: str) -> TriageAction:
|
|
| 116 |
max_tokens=MAX_TOKENS,
|
| 117 |
stream=False,
|
| 118 |
)
|
|
|
|
| 119 |
raw = (completion.choices[0].message.content or "").strip()
|
|
|
|
|
|
|
|
|
|
| 120 |
if raw.startswith("```"):
|
| 121 |
-
|
|
|
|
| 122 |
if raw.startswith("json"):
|
| 123 |
-
raw = raw[4:]
|
|
|
|
| 124 |
data = json.loads(raw)
|
| 125 |
-
|
|
|
|
| 126 |
priority=data.get("priority", "P2"),
|
| 127 |
labels=data.get("labels", ["bug"]),
|
| 128 |
assigned_team=data.get("assigned_team", "backend"),
|
|
@@ -130,6 +165,13 @@ def call_model(client: OpenAI, bug_text: str) -> TriageAction:
|
|
| 130 |
reasoning=data.get("reasoning", ""),
|
| 131 |
)
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
# ββ main ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 135 |
|
|
@@ -157,8 +199,13 @@ def main() -> None:
|
|
| 157 |
f"team={action.assigned_team},"
|
| 158 |
f"milestone={action.milestone}"
|
| 159 |
)
|
| 160 |
-
log_step(
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
log_task_end(task_id, reward)
|
| 163 |
time.sleep(0.5)
|
| 164 |
|
|
@@ -166,9 +213,10 @@ def main() -> None:
|
|
| 166 |
success = score >= SUCCESS_SCORE_THRESHOLD
|
| 167 |
|
| 168 |
except Exception as exc:
|
| 169 |
-
print(f"[ERROR] {exc}", flush=True)
|
| 170 |
score = sum(rewards) / len(TASK_IDS) if rewards else 0.0
|
| 171 |
success = False
|
|
|
|
| 172 |
|
| 173 |
finally:
|
| 174 |
log_end(success, len(rewards), score, rewards)
|
|
|
|
| 3 |
OpenEnv Hackathon submission inference script.
|
| 4 |
|
| 5 |
Required env vars:
|
| 6 |
+
API_BASE_URL LiteLLM proxy base URL (injected by validator)
|
| 7 |
+
API_KEY API key (injected by validator)
|
| 8 |
ENV_BASE_URL Bug Triage env URL (optional)
|
| 9 |
MODEL_NAME Model identifier (optional)
|
| 10 |
"""
|
|
|
|
| 20 |
from model import TriageAction
|
| 21 |
|
| 22 |
# ββ config βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
_raw_base_url = os.getenv("API_BASE_URL", "").strip()
|
| 25 |
+
if not _raw_base_url:
|
| 26 |
+
raise RuntimeError("API_BASE_URL is not set β validator must inject this")
|
| 27 |
+
|
| 28 |
+
# LiteLLM strictly requires /v1 at the end of the base URL
|
| 29 |
+
if not _raw_base_url.rstrip("/").endswith("/v1"):
|
| 30 |
+
API_BASE_URL = _raw_base_url.rstrip("/") + "/v1"
|
| 31 |
+
else:
|
| 32 |
+
API_BASE_URL = _raw_base_url.rstrip("/")
|
| 33 |
+
|
| 34 |
+
API_KEY = (
|
| 35 |
+
os.getenv("API_KEY")
|
| 36 |
+
or os.getenv("HF_TOKEN")
|
| 37 |
+
or os.getenv("OPENAI_API_KEY")
|
| 38 |
+
)
|
| 39 |
if not API_KEY:
|
| 40 |
+
raise RuntimeError("API_KEY is not set β validator must inject this")
|
| 41 |
+
|
| 42 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "meta-llama/Llama-3.3-70B-Instruct")
|
| 43 |
+
ENV_BASE_URL = os.getenv("ENV_BASE_URL", "https://siteshcodes-bug-triage-env.hf.space")
|
| 44 |
|
| 45 |
TASK_IDS = ["easy", "medium", "hard"]
|
| 46 |
BENCHMARK = "bug-triage-env"
|
|
|
|
| 48 |
MAX_TOKENS = 400
|
| 49 |
SUCCESS_SCORE_THRESHOLD = 0.4
|
| 50 |
|
| 51 |
+
print(f"[CONFIG] API_BASE_URL={API_BASE_URL}", flush=True)
|
| 52 |
+
print(f"[CONFIG] MODEL_NAME={MODEL_NAME}", flush=True)
|
| 53 |
+
print(f"[CONFIG] ENV_BASE_URL={ENV_BASE_URL}", flush=True)
|
| 54 |
+
print(f"[CONFIG] API_KEY={'set' if API_KEY else 'MISSING'}", flush=True)
|
| 55 |
+
|
| 56 |
+
# ββ prompt ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 57 |
+
|
| 58 |
SYSTEM_PROMPT = textwrap.dedent("""
|
| 59 |
You are a senior software engineering manager.
|
| 60 |
You will receive a bug report and must triage it. Respond ONLY with
|
|
|
|
| 90 |
print(f"\n--- Starting Task: {task_id} ---", flush=True)
|
| 91 |
|
| 92 |
|
| 93 |
+
def log_step(
|
| 94 |
+
step: int,
|
| 95 |
+
task_id: str,
|
| 96 |
+
action: str,
|
| 97 |
+
reward: float,
|
| 98 |
+
done: bool,
|
| 99 |
+
error: Optional[str] = None,
|
| 100 |
+
) -> None:
|
| 101 |
print(
|
| 102 |
f"[STEP] step={step} task={task_id} action={action} "
|
| 103 |
f"reward={reward:.2f} done={str(done).lower()} error={error or 'null'}",
|
|
|
|
| 106 |
|
| 107 |
|
| 108 |
def log_task_end(task_id: str, reward: float) -> None:
|
| 109 |
+
print(f"[TASK_END] task={task_id} reward={reward:.3f}", flush=True)
|
| 110 |
|
| 111 |
|
| 112 |
def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
|
|
|
|
| 118 |
)
|
| 119 |
|
| 120 |
|
| 121 |
+
# ββ helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 122 |
|
| 123 |
def format_bug(obs) -> str:
|
| 124 |
bug = obs.bug_report
|
| 125 |
+
comments = "\n".join(f" - {c}" for c in bug.comments) if bug.comments else " None"
|
| 126 |
return (
|
| 127 |
f"Title: {bug.title}\n\n"
|
| 128 |
f"Description:\n{bug.body}\n\n"
|
| 129 |
+
f"Existing labels: {', '.join(bug.labels_hint) if bug.labels_hint else 'none'}\n"
|
| 130 |
f"Comments:\n{comments}"
|
| 131 |
)
|
| 132 |
|
| 133 |
|
| 134 |
def call_model(client: OpenAI, bug_text: str) -> TriageAction:
|
| 135 |
+
print("[LLM] Sending request to model...", flush=True)
|
| 136 |
+
|
| 137 |
completion = client.chat.completions.create(
|
| 138 |
model=MODEL_NAME,
|
| 139 |
messages=[
|
|
|
|
| 144 |
max_tokens=MAX_TOKENS,
|
| 145 |
stream=False,
|
| 146 |
)
|
| 147 |
+
|
| 148 |
raw = (completion.choices[0].message.content or "").strip()
|
| 149 |
+
print(f"[LLM] Raw response: {raw[:200]}", flush=True)
|
| 150 |
+
|
| 151 |
+
# Strip markdown fences if model ignores instructions
|
| 152 |
if raw.startswith("```"):
|
| 153 |
+
parts = raw.split("```")
|
| 154 |
+
raw = parts[1] if len(parts) > 1 else raw
|
| 155 |
if raw.startswith("json"):
|
| 156 |
+
raw = raw[4:].strip()
|
| 157 |
+
|
| 158 |
data = json.loads(raw)
|
| 159 |
+
|
| 160 |
+
action = TriageAction(
|
| 161 |
priority=data.get("priority", "P2"),
|
| 162 |
labels=data.get("labels", ["bug"]),
|
| 163 |
assigned_team=data.get("assigned_team", "backend"),
|
|
|
|
| 165 |
reasoning=data.get("reasoning", ""),
|
| 166 |
)
|
| 167 |
|
| 168 |
+
print(
|
| 169 |
+
f"[LLM] Parsed action: priority={action.priority} "
|
| 170 |
+
f"team={action.assigned_team} milestone={action.milestone}",
|
| 171 |
+
flush=True,
|
| 172 |
+
)
|
| 173 |
+
return action
|
| 174 |
+
|
| 175 |
|
| 176 |
# ββ main ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 177 |
|
|
|
|
| 199 |
f"team={action.assigned_team},"
|
| 200 |
f"milestone={action.milestone}"
|
| 201 |
)
|
| 202 |
+
log_step(
|
| 203 |
+
step=step,
|
| 204 |
+
task_id=task_id,
|
| 205 |
+
action=action_str,
|
| 206 |
+
reward=reward,
|
| 207 |
+
done=True,
|
| 208 |
+
)
|
| 209 |
log_task_end(task_id, reward)
|
| 210 |
time.sleep(0.5)
|
| 211 |
|
|
|
|
| 213 |
success = score >= SUCCESS_SCORE_THRESHOLD
|
| 214 |
|
| 215 |
except Exception as exc:
|
| 216 |
+
print(f"[ERROR] Exception during run: {type(exc).__name__}: {exc}", flush=True)
|
| 217 |
score = sum(rewards) / len(TASK_IDS) if rewards else 0.0
|
| 218 |
success = False
|
| 219 |
+
raise exc # CRITICAL: never fail silently β let validator see real error
|
| 220 |
|
| 221 |
finally:
|
| 222 |
log_end(success, len(rewards), score, rewards)
|