Spaces:
Running
Running
File size: 11,719 Bytes
bc20ef9 | 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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | """
inference.py β OpenEnv SQL Debug Environment Baseline Agent
MUST be at root level. MUST use exact [START]/[STEP]/[END] log format.
Uses OpenAI client. Reads from environment variables.
Runtime target: < 20 minutes on 2vCPU / 8GB.
"""
import asyncio
import os
import json
import sys
import time
from typing import List, Dict, Any, Optional
from openai import OpenAI
import httpx
# ββ Configuration from environment variables ββββββββββββββββββββββββββββββββ
API_BASE_URL = os.environ.get("API_BASE_URL", "https://api.openai.com/v1")
MODEL_NAME = os.environ.get("MODEL_NAME", "gpt-4o-mini")
HF_TOKEN = os.environ.get("HF_TOKEN")
# Optional: used only when running environments via from_docker_image() flows.
LOCAL_IMAGE_NAME = os.environ.get("LOCAL_IMAGE_NAME")
try:
if not HF_TOKEN:
print("[DEBUG] WARNING: HF_TOKEN not found in environment. Model calls will fail.", flush=True)
except Exception:
pass
# ββ Environment config βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ENV_BASE_URL = os.environ.get("ENV_BASE_URL", "http://localhost:7860")
BENCHMARK = "sql-debug-env"
TEMPERATURE = 0.0
MAX_TOKENS = 1024
SEED = int(os.environ.get("SEED", "1"))
# ββ Per-task config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
TASK_CONFIGS = {
"easy_syntax_fix": {"max_steps": 10, "success_threshold": 0.8},
"medium_logic_fix": {"max_steps": 20, "success_threshold": 0.7},
"hard_multi_bug": {"max_steps": 30, "success_threshold": 0.5},
}
MIN_STRICT_SCORE = 0.001
MAX_STRICT_SCORE = 0.999
def strict_score(value: float) -> float:
return min(MAX_STRICT_SCORE, max(MIN_STRICT_SCORE, value))
# ββ Logging functions (EXACT FORMAT β DO NOT MODIFY) ββββββββββββββββββββββββ
def log_start(task: str, env: str, model: str):
print(f"[START] task={task} env={env} model={model}", flush=True)
def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]):
error_str = error if error else "null"
# Escape action for single-line logging
action_clean = action.replace("\n", "\\n").replace('"', '\\"')[:200]
print(
f"[STEP] step={step} action=\"{action_clean}\" "
f"reward={reward:.4f} done={str(done).lower()} error={error_str}",
flush=True
)
def log_end(success: bool, steps: int, score: float, rewards: List[float]):
rewards_str = json.dumps([round(r, 4) for r in rewards])
print(
f"[END] success={str(success).lower()} steps={steps} "
f"score={score:.4f} rewards={rewards_str}",
flush=True
)
# ββ System prompt ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
SYSTEM_PROMPT = """You are an expert SQL debugger. You will receive a broken SQL query and must fix it.
You interact with a SQL debugging environment via JSON actions.
Available actions (respond with ONLY valid JSON, no markdown, no explanation):
1. Submit a fixed query:
{"action_type": "submit_query", "query": "SELECT ..."}
2. Inspect schema (free, no penalty):
{"action_type": "inspect_schema"}
3. Inspect last error (free, no penalty):
{"action_type": "inspect_error"}
4. Inspect sample rows from a table (free, no penalty):
{"action_type": "inspect_sample", "table_name": "table_name_here"}
Strategy:
- Start by submitting a fixed query if the bug is obvious
- Use inspect_schema first if you need to verify column names/table structure
- Use inspect_error to understand why your query failed
- Read error messages carefully β they tell you exactly what's wrong
- Fix one bug at a time and resubmit
- You get partial credit for partially correct queries
IMPORTANT: Respond with ONLY the JSON action. No explanation, no markdown blocks, just raw JSON."""
def build_prompt(obs: Dict[str, Any], step: int, reward_history: List[float]) -> str:
"""Build the user prompt for each step."""
lines = [
f"=== SQL Debugging Task (Step {step}) ===",
f"Task: {obs.get('task_description', '')[:500]}",
f"",
f"ORIGINAL BROKEN QUERY:",
f"```sql",
f"{obs.get('original_query', '')}",
f"```",
]
if obs.get('current_query'):
lines += [
f"",
f"YOUR LAST SUBMITTED QUERY:",
f"```sql",
f"{obs.get('current_query', '')}",
f"```",
]
last_result = obs.get('last_query_result')
if last_result:
if last_result.get('success'):
rows = last_result.get('rows', [])
lines += [
f"",
f"LAST QUERY RESULT: {len(rows)} rows returned",
f"Sample (first 3): {json.dumps(rows[:3], default=str)}",
]
else:
lines += [
f"",
f"LAST QUERY ERROR: {last_result.get('error_message', 'Unknown error')}",
]
if obs.get('schema_info'):
schema = obs['schema_info'].get('tables', {})
lines += [f"", f"DATABASE SCHEMA:"]
for table, cols in schema.items():
col_str = ", ".join(f"{c['name']} ({c['type']})" for c in cols)
lines.append(f" {table}: {col_str}")
if obs.get('error_details'):
lines += [f"", f"ERROR DETAILS: {obs['error_details']}"]
if obs.get('sample_rows'):
lines += [f"", f"SAMPLE ROWS: {json.dumps(obs['sample_rows'][:3], default=str)}"]
if obs.get('hint'):
lines += [f"", f"HINT: {obs['hint']}"]
lines += [
f"",
f"Current score: {obs.get('current_score', 0):.3f}",
f"Steps remaining: {obs.get('steps_remaining', 0)}",
f"Expected output: {obs.get('expected_description', '')}",
f"",
f"What is your next action? (respond with ONLY valid JSON)"
]
return "\n".join(lines)
def call_model(client: OpenAI, prompt: str) -> Dict[str, Any]:
"""Call model and parse JSON action response."""
try:
response = client.chat.completions.create(
model=MODEL_NAME,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": prompt}
],
temperature=TEMPERATURE,
seed=SEED,
max_tokens=MAX_TOKENS,
)
text = (response.choices[0].message.content or "").strip()
# Strip markdown if model wraps in backticks
if text.startswith("```"):
text = text.split("```")[1]
if text.startswith("json"):
text = text[4:]
text = text.strip()
return json.loads(text)
except json.JSONDecodeError:
# Fallback: try to extract JSON from response
import re
match = re.search(r'\{.*\}', text, re.DOTALL)
if match:
try:
return json.loads(match.group())
except:
pass
# Default fallback action
return {"action_type": "inspect_schema"}
except Exception as e:
print(f"[DEBUG] Model error: {e}", flush=True)
return {"action_type": "inspect_schema"}
def run_task(
client: OpenAI,
task_id: str,
config: Dict[str, Any]
) -> Dict[str, Any]:
"""Run one task episode synchronously via HTTP."""
max_steps = config["max_steps"]
success_threshold = config["success_threshold"]
log_start(task=task_id, env=BENCHMARK, model=MODEL_NAME)
rewards = []
steps_taken = 0
score = MIN_STRICT_SCORE
success = False
with httpx.Client(base_url=ENV_BASE_URL, timeout=30.0) as http:
# Reset
reset_resp = http.post("/reset", json={"task_id": task_id})
reset_resp.raise_for_status()
result = reset_resp.json()
obs = result["observation"]
done = result["done"]
reward_history = []
for step in range(1, max_steps + 1):
if done:
break
# Get model action
prompt = build_prompt(obs, step, reward_history)
action_dict = call_model(client, prompt)
# Execute step
try:
step_resp = http.post("/step", json={"action": action_dict})
step_resp.raise_for_status()
step_result = step_resp.json()
except Exception as e:
log_step(step=step, action=str(action_dict), reward=MIN_STRICT_SCORE, done=False, error=str(e))
continue
obs = step_result["observation"]
reward = float(step_result.get("reward") or MIN_STRICT_SCORE)
done = step_result["done"]
error = None
info = step_result.get("info") or {}
# Extract error for logging
last_result = obs.get("last_query_result")
if last_result and not last_result.get("success"):
error = last_result.get("error_message", "")
action_str = action_dict.get("query") or action_dict.get("action_type", "unknown")
rewards.append(reward)
reward_history.append(reward)
steps_taken = step
score = float(info.get("grade_score") or obs.get("current_score") or MIN_STRICT_SCORE)
log_step(step=step, action=action_str, reward=reward, done=done, error=error)
if done:
break
# Compute final score
score = strict_score(score)
success = score >= success_threshold
log_end(success=success, steps=steps_taken, score=score, rewards=rewards)
return {
"task_id": task_id,
"score": score,
"success": success,
"steps": steps_taken,
"rewards": rewards
}
def main():
"""Run baseline agent across all 3 tasks."""
client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
print(f"[DEBUG] Starting SQL Debug Env baseline", flush=True)
print(f"[DEBUG] Model: {MODEL_NAME}", flush=True)
print(f"[DEBUG] Env URL: {ENV_BASE_URL}", flush=True)
# Wait for server to be ready
max_wait = 30
for i in range(max_wait):
try:
resp = httpx.get(f"{ENV_BASE_URL}/health", timeout=5)
if resp.status_code == 200:
print(f"[DEBUG] Server ready", flush=True)
break
except:
pass
print(f"[DEBUG] Waiting for server... ({i+1}/{max_wait})", flush=True)
time.sleep(1)
all_results = []
for task_id, config in TASK_CONFIGS.items():
print(f"\n[DEBUG] Running task: {task_id}", flush=True)
try:
result = run_task(client, task_id, config)
all_results.append(result)
except Exception as e:
print(f"[DEBUG] Task {task_id} failed: {e}", flush=True)
log_end(success=False, steps=0, score=MIN_STRICT_SCORE, rewards=[])
# Small delay between tasks
time.sleep(2)
# Summary
print(f"\n[DEBUG] === BASELINE RESULTS ===", flush=True)
total_score = 0.0
for r in all_results:
print(f"[DEBUG] {r['task_id']}: score={r['score']:.3f} success={r['success']}", flush=True)
total_score += r['score']
if all_results:
avg = total_score / len(all_results)
print(f"[DEBUG] Average score: {avg:.3f}", flush=True)
if __name__ == "__main__":
main()
|