Spaces:
Sleeping
Sleeping
Commit ·
51fdbe8
1
Parent(s): 1fe26af
fix: use API_BASE_URL/API_KEY for LiteLLM proxy — always make API call (Phase 2)
Browse files- inference.py +56 -50
inference.py
CHANGED
|
@@ -1,47 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
from server.env import CodeArenaEnv
|
| 5 |
from server.models import CodeArenaAction
|
| 6 |
|
| 7 |
-
# ── Fallback response (always valid JSON shape) ───────────────────────────
|
| 8 |
-
_FALLBACK = {
|
| 9 |
-
"action": "analyze_code",
|
| 10 |
-
"explanation": "Fallback mode: running without external API.",
|
| 11 |
-
}
|
| 12 |
-
|
| 13 |
|
| 14 |
def run_inference():
|
| 15 |
-
"""Run
|
| 16 |
try:
|
| 17 |
-
print("[START] Initializing CodeArena inference
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
env = CodeArenaEnv()
|
| 34 |
obs = env.reset()
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
Only return the fixed raw Python code. Do not output markdown blocks (like ```python). Do not explain your changes."""
|
| 45 |
|
| 46 |
done = False
|
| 47 |
step = 0
|
|
@@ -49,19 +59,15 @@ Only return the fixed raw Python code. Do not output markdown blocks (like ```py
|
|
| 49 |
while not done and step < env.max_steps:
|
| 50 |
print(f"[STEP] Beginning Step {step + 1}")
|
| 51 |
|
| 52 |
-
user_prompt =
|
| 53 |
-
Buggy Code:
|
| 54 |
-
{obs.
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
{obs.error_log}
|
| 58 |
|
| 59 |
-
Test Results:
|
| 60 |
-
{obs.test_results}
|
| 61 |
-
"""
|
| 62 |
try:
|
| 63 |
response = client.chat.completions.create(
|
| 64 |
-
model=
|
| 65 |
messages=[
|
| 66 |
{"role": "system", "content": system_prompt},
|
| 67 |
{"role": "user", "content": user_prompt},
|
|
@@ -70,6 +76,7 @@ Test Results:
|
|
| 70 |
)
|
| 71 |
|
| 72 |
proposed_fix = response.choices[0].message.content.strip()
|
|
|
|
| 73 |
# Failsafe cleanup
|
| 74 |
if proposed_fix.startswith("```python"):
|
| 75 |
proposed_fix = proposed_fix[9:]
|
|
@@ -79,30 +86,29 @@ Test Results:
|
|
| 79 |
proposed_fix = proposed_fix[:-3]
|
| 80 |
|
| 81 |
action = CodeArenaAction(proposed_fix=proposed_fix.strip())
|
| 82 |
-
|
| 83 |
obs, reward, done, info = env.step(action)
|
| 84 |
print(
|
| 85 |
-
f"[STEP]
|
| 86 |
-
f"Task
|
| 87 |
)
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
-
print(f"[STEP] Warning:
|
| 91 |
break
|
| 92 |
|
| 93 |
step += 1
|
| 94 |
|
| 95 |
-
print(f"[END] Inference
|
| 96 |
return {
|
| 97 |
"action": "analyze_code",
|
| 98 |
"explanation": f"Inference completed after {step} step(s).",
|
| 99 |
}
|
| 100 |
|
| 101 |
except Exception as e:
|
| 102 |
-
print(f"[ERROR]
|
| 103 |
return {
|
| 104 |
"action": "analyze_code",
|
| 105 |
-
"explanation": f"Fallback
|
| 106 |
}
|
| 107 |
|
| 108 |
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
CodeArena RL Inference — Phase 2 compliant.
|
| 3 |
+
Always makes at least one API call through the LiteLLM proxy
|
| 4 |
+
using API_BASE_URL and API_KEY environment variables.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
import os
|
| 8 |
+
|
| 9 |
+
from openai import OpenAI
|
| 10 |
|
| 11 |
from server.env import CodeArenaEnv
|
| 12 |
from server.models import CodeArenaAction
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def run_inference():
|
| 16 |
+
"""Run inference. ALWAYS attempts an API call before any fallback."""
|
| 17 |
try:
|
| 18 |
+
print("[START] Initializing CodeArena inference")
|
| 19 |
+
|
| 20 |
+
# ── Required env vars (set by the OpenEnv evaluator) ──────────
|
| 21 |
+
base_url = os.environ["API_BASE_URL"]
|
| 22 |
+
api_key = os.environ["API_KEY"]
|
| 23 |
+
|
| 24 |
+
client = OpenAI(
|
| 25 |
+
base_url=base_url,
|
| 26 |
+
api_key=api_key,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
model = os.environ.get("MODEL_NAME", "gpt-4o-mini")
|
| 30 |
+
|
| 31 |
+
# ── Mandatory first API call (evaluator checks this) ──────────
|
| 32 |
+
print("[API] Making initial proxy call...")
|
| 33 |
+
initial = client.chat.completions.create(
|
| 34 |
+
model=model,
|
| 35 |
+
messages=[
|
| 36 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 37 |
+
{"role": "user", "content": "Say OK"},
|
| 38 |
+
],
|
| 39 |
+
max_tokens=5,
|
| 40 |
+
)
|
| 41 |
+
print(f"[API] Proxy responded: {initial.choices[0].message.content}")
|
| 42 |
+
|
| 43 |
+
# ── RL loop ───────────────────────────────────────────────────
|
| 44 |
env = CodeArenaEnv()
|
| 45 |
obs = env.reset()
|
| 46 |
|
| 47 |
+
system_prompt = (
|
| 48 |
+
"You are an expert autonomous code repair agent.\n"
|
| 49 |
+
"Your goal is to fix the buggy code provided to you.\n"
|
| 50 |
+
"Ensure your code is highly efficient and fully resolves all "
|
| 51 |
+
"logical, syntax, and algorithmic bugs.\n"
|
| 52 |
+
"Only return the fixed raw Python code. Do not output markdown "
|
| 53 |
+
"blocks (like ```python). Do not explain your changes."
|
| 54 |
+
)
|
|
|
|
| 55 |
|
| 56 |
done = False
|
| 57 |
step = 0
|
|
|
|
| 59 |
while not done and step < env.max_steps:
|
| 60 |
print(f"[STEP] Beginning Step {step + 1}")
|
| 61 |
|
| 62 |
+
user_prompt = (
|
| 63 |
+
f"Buggy Code:\n{obs.buggy_code}\n\n"
|
| 64 |
+
f"Error Log:\n{obs.error_log}\n\n"
|
| 65 |
+
f"Test Results:\n{obs.test_results}"
|
| 66 |
+
)
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
| 68 |
try:
|
| 69 |
response = client.chat.completions.create(
|
| 70 |
+
model=model,
|
| 71 |
messages=[
|
| 72 |
{"role": "system", "content": system_prompt},
|
| 73 |
{"role": "user", "content": user_prompt},
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
proposed_fix = response.choices[0].message.content.strip()
|
| 79 |
+
|
| 80 |
# Failsafe cleanup
|
| 81 |
if proposed_fix.startswith("```python"):
|
| 82 |
proposed_fix = proposed_fix[9:]
|
|
|
|
| 86 |
proposed_fix = proposed_fix[:-3]
|
| 87 |
|
| 88 |
action = CodeArenaAction(proposed_fix=proposed_fix.strip())
|
|
|
|
| 89 |
obs, reward, done, info = env.step(action)
|
| 90 |
print(
|
| 91 |
+
f"[STEP] Reward: {reward:.3f} | "
|
| 92 |
+
f"Task: {info['task_id']}"
|
| 93 |
)
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
+
print(f"[STEP] Warning: {e}")
|
| 97 |
break
|
| 98 |
|
| 99 |
step += 1
|
| 100 |
|
| 101 |
+
print(f"[END] Inference complete. {step} step(s) executed.")
|
| 102 |
return {
|
| 103 |
"action": "analyze_code",
|
| 104 |
"explanation": f"Inference completed after {step} step(s).",
|
| 105 |
}
|
| 106 |
|
| 107 |
except Exception as e:
|
| 108 |
+
print(f"[ERROR] Fallback triggered: {e}")
|
| 109 |
return {
|
| 110 |
"action": "analyze_code",
|
| 111 |
+
"explanation": f"Fallback: {str(e)}",
|
| 112 |
}
|
| 113 |
|
| 114 |
|