File size: 8,043 Bytes
72805b8 ac49ad8 72805b8 | 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 | """
Inference Script - SQL Arena OpenEnv Environment
Baseline agent that uses an LLM to solve SQL challenges.
"""
import os
import sys
import textwrap
from typing import List, Optional
from openai import OpenAI
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from src.sql_arena.environment import SQLArenaEnvironment
from src.sql_arena.models import SQLArenaAction
# =====================================================
# Configuration
# =====================================================
API_KEY = os.getenv("HF_TOKEN") or os.getenv("API_KEY")
API_BASE_URL = os.getenv("API_BASE_URL") or "https://router.huggingface.co/v1"
MODEL_NAME = os.getenv("MODEL_NAME") or "Qwen/Qwen2.5-72B-Instruct"
BENCHMARK = "sql_arena"
TEMPERATURE = 0.3
MAX_TOKENS = 500
TASKS = [
{"difficulty": "basic_select", "task_id": "easy_001", "name": "basic_select", "max_steps": 5},
{"difficulty": "join_aggregate", "task_id": "medium_001", "name": "join_aggregate", "max_steps": 7},
{"difficulty": "complex_analysis", "task_id": "hard_001", "name": "complex_analysis", "max_steps": 10},
]
# =====================================================
# Logging (MANDATORY 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: Optional[str]) -> None:
error_val = error if error else "null"
done_val = str(done).lower()
action_short = action.replace('\n', ' ').strip()[:100]
print(
f"[STEP] step={step} action={action_short} reward={reward:.2f} done={done_val} error={error_val}",
flush=True,
)
def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
print(
f"[END] success={str(success).lower()} steps={steps} score={score:.2f} rewards={rewards_str}",
flush=True,
)
# =====================================================
# LLM Agent
# =====================================================
SYSTEM_PROMPT = textwrap.dedent("""
You are an expert SQL query writer. You are interacting with a SQL challenge environment.
Each turn you receive: database schema, a question, previous query results, and feedback.
Your goal: Write a SQL query that correctly answers the question.
Rules:
- Output ONLY the SQL query, nothing else
- No explanations, no markdown, no code fences
- Use standard SQLite syntax
- Be precise with column names and table names
- If your previous query had errors, fix them based on the feedback
""").strip()
def build_user_prompt(observation: dict, step: int, history: List[str]) -> str:
parts = []
parts.append(f"=== SQL Challenge (Step {step}) ===")
parts.append(f"\nDifficulty: {observation.get('difficulty', 'unknown')}")
parts.append(f"\n--- Database Schema ---\n{observation.get('schema_description', '')}")
parts.append(f"\n--- Question ---\n{observation.get('question', '')}")
if observation.get('expected_columns'):
parts.append(f"\n--- Expected Columns ---\n{observation['expected_columns']}")
if observation.get('query_result'):
parts.append(f"\n--- Previous Query Result ---\n{observation['query_result']}")
if observation.get('error_message'):
parts.append(f"\n--- Error ---\n{observation['error_message']}")
if observation.get('feedback'):
parts.append(f"\n--- Feedback ---\n{observation['feedback']}")
parts.append(f"\nAttempts remaining: {observation.get('attempts_remaining', 0)}")
if history:
parts.append("\n--- Previous Attempts ---")
for h in history[-3:]:
parts.append(h)
parts.append("\nWrite your SQL query now:")
return "\n".join(parts)
def get_sql_from_llm(client: OpenAI, observation: dict, step: int, history: List[str]) -> str:
user_prompt = build_user_prompt(observation, step, history)
try:
completion = client.chat.completions.create(
model=MODEL_NAME,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_prompt},
],
temperature=TEMPERATURE,
max_tokens=MAX_TOKENS,
stream=False,
)
raw = (completion.choices[0].message.content or "").strip()
sql = raw
if sql.startswith("```sql"):
sql = sql[6:]
if sql.startswith("```"):
sql = sql[3:]
if sql.endswith("```"):
sql = sql[:-3]
sql = sql.strip()
return sql if sql else "SELECT 1"
except Exception as exc:
print(f"[DEBUG] LLM request failed: {exc}", flush=True)
return "SELECT 1"
# =====================================================
# Main Inference Loop
# =====================================================
def run_task(client: OpenAI, env: SQLArenaEnvironment, task_config: dict) -> float:
difficulty = task_config["difficulty"]
task_id = task_config["task_id"]
task_name = task_config["name"]
max_steps = task_config["max_steps"]
history: List[str] = []
rewards: List[float] = []
steps_taken = 0
best_score = 0.0
log_start(task=task_name, env=BENCHMARK, model=MODEL_NAME)
try:
result = env.reset(difficulty=difficulty, task_id=task_id)
obs_dict = result.observation.model_dump()
for step in range(1, max_steps + 1):
if result.done:
break
sql_query = get_sql_from_llm(client, obs_dict, step, history)
action = SQLArenaAction(sql_query=sql_query)
result = env.step(action)
obs_dict = result.observation.model_dump()
reward = result.reward
done = result.done
error = obs_dict.get("error_message")
rewards.append(reward)
steps_taken = step
best_score = max(best_score, result.info.get("score", 0.0))
log_step(step=step, action=sql_query, reward=reward, done=done, error=error)
history.append(
f"Step {step}: {sql_query[:80]}... -> reward={reward:.2f}"
)
if done:
break
final_score = min(max(best_score, 0.0), 1.0)
# Clamp to strictly between 0 and 1
if final_score <= 0.0:
final_score = 0.01
if final_score >= 1.0:
final_score = 0.99
success = final_score >= 0.5
except Exception as e:
print(f"[DEBUG] Task {task_name} error: {e}", flush=True)
final_score = 0.0
success = False
finally:
log_end(success=success, steps=steps_taken, score=final_score, rewards=rewards)
return final_score
def main() -> None:
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
env = SQLArenaEnvironment()
all_scores = []
for task_config in TASKS:
print(f"\n{'='*60}", flush=True)
print(f"Running task: {task_config['name']} ({task_config['difficulty']})", flush=True)
print(f"{'='*60}", flush=True)
score = run_task(client, env, task_config)
all_scores.append(score)
print(f"\nTask {task_config['name']} final score: {score:.2f}\n", flush=True)
avg_score = sum(all_scores) / len(all_scores) if all_scores else 0.0
print(f"\n{'='*60}", flush=True)
print("SUMMARY", flush=True)
print(f"{'='*60}", flush=True)
for tc, sc in zip(TASKS, all_scores):
print(f" {tc['name']:20s}: {sc:.2f}", flush=True)
print(f" {'Average':20s}: {avg_score:.2f}", flush=True)
env.close()
if __name__ == "__main__":
main() |