File size: 10,392 Bytes
3c665d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2014920
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c665d2
 
 
 
2014920
 
 
 
 
 
3c665d2
2014920
3c665d2
 
 
 
 
2014920
 
 
 
 
 
3c665d2
2014920
 
3c665d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2014920
3c665d2
 
 
 
263261a
 
 
2014920
 
263261a
 
3c665d2
 
2014920
 
 
 
3c665d2
 
 
 
263261a
2014920
 
3c665d2
 
 
2014920
 
 
 
3c665d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba69b5f
2014920
 
3c665d2
2014920
 
 
 
 
 
3c665d2
 
 
2014920
3c665d2
 
 
 
 
 
 
 
2014920
 
 
 
 
 
 
 
 
 
 
3c665d2
 
2014920
 
 
 
 
 
3c665d2
 
 
 
 
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
"""
SQL Agent OpenEnv β€” Baseline Inference Script
==============================================

Runs a baseline LLM agent against all 3 tasks of the SQL Agent OpenEnv environment.

Environment variables (required):
  API_BASE_URL   β€” OpenAI-compatible base URL  (default: https://router.huggingface.co/v1)
  MODEL_NAME     β€” Model identifier            (default: Qwen/Qwen2.5-72B-Instruct)
  HF_TOKEN       β€” Hugging Face / API key

STDOUT format (strictly enforced):
  [START] task=<task_id> env=sql-agent-openenv model=<model>
  [STEP]  step=<n> action=<action> reward=<0.00> done=<true|false> error=<msg|null>
  [END]   success=<true|false> steps=<n> score=<0.000> rewards=<r1,r2,...>
"""

from __future__ import annotations

import asyncio
import os
import sys
import textwrap
from typing import List, Optional

# ── Path setup (inference.py lives at repo root; backend is a subdirectory) ──
_BACKEND = os.path.join(os.path.dirname(os.path.abspath(__file__)), "backend")
if _BACKEND not in sys.path:
    sys.path.insert(0, _BACKEND)

from openai import OpenAI  # noqa: E402

from env.sql_env import SQLAgentEnv, Action, Observation  # noqa: E402

# ── Config ────────────────────────────────────────────────────────────────────

API_KEY = os.getenv("HF_TOKEN") or os.getenv("API_KEY") or os.getenv("OPENAI_API_KEY", "")
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
BENCHMARK = "sql-agent-openenv"

TASKS = ["simple_queries", "join_queries", "complex_queries"]
MAX_STEPS = 5
TEMPERATURE = 0.2
MAX_TOKENS = 50

REPAIR_ACTIONS = [
    "rewrite_full",
    "fix_column",
    "fix_table",
    "add_groupby",
    "rewrite_cte",
    "fix_syntax",
    "change_dialect",
    "relax_filter",
]

SYSTEM_PROMPT = textwrap.dedent("""
    You are an expert SQL agent interacting with a SQL repair environment.

    At each step you receive a natural language question, a database schema,
    and optionally the last SQL attempt + error message.

    Your job: pick ONE repair action from the list below that is most likely
    to fix the SQL error on the next attempt.

    Available actions:
      generate       β€” write fresh SQL from scratch (use on first attempt)
      rewrite_full   β€” completely rewrite the query from scratch
      fix_column     β€” fix wrong column name references
      fix_table      β€” fix wrong table name references
      add_groupby    β€” add or fix GROUP BY / aggregation clauses
      rewrite_cte    β€” restructure subqueries or CTEs
      fix_syntax     β€” fix syntax errors (brackets, commas, keywords)
      change_dialect β€” convert to SQLite-compatible functions
      relax_filter   β€” broaden or remove overly strict WHERE conditions

    Reply with ONLY the action name. No explanation. No punctuation.
    Example: fix_column
""").strip()


# ── Logging ───────────────────────────────────────────────────────────────────

# Hard bounds: every score/reward we ever emit is clamped to this closed range.
# 0.05 margin guarantees that :.2f and :.3f formatting never produces
# "0.00", "0.000", "1.00", or "1.000" (all of which parse as exactly 0.0 / 1.0).
_MIN_SCORE = 0.05
_MAX_SCORE = 0.95


def _safe_score(x) -> float:
    """Coerce anything (None, NaN, str, bool, int, float) to a float strictly in (0, 1)."""
    try:
        if x is None:
            return _MIN_SCORE
        if isinstance(x, bool):
            return _MAX_SCORE if x else _MIN_SCORE
        v = float(x)
        if v != v:  # NaN
            return _MIN_SCORE
        if v == float("inf"):
            return _MAX_SCORE
        if v == float("-inf"):
            return _MIN_SCORE
    except (TypeError, ValueError):
        return _MIN_SCORE
    return max(_MIN_SCORE, min(_MAX_SCORE, v))


def log_start(task: str, model: str) -> None:
    print(f"[START] task={task} env={BENCHMARK} model={model}", flush=True)


def log_step(step: int, action: str, reward, done: bool, error: Optional[str]) -> None:
    r = _safe_score(reward)
    error_val = (error or "null")
    if hasattr(error_val, "replace"):
        error_val = error_val.replace("\n", " ").strip() or "null"
    done_val = str(bool(done)).lower()
    print(
        f"[STEP] step={int(step)} action={action or 'noop'} reward={r:.2f} "
        f"done={done_val} error={error_val}",
        flush=True,
    )


def log_end(success: bool, steps: int, score, rewards: List) -> None:
    s = _safe_score(score)
    safe_rewards = [_safe_score(r) for r in (rewards or [])]
    if not safe_rewards:
        safe_rewards = [_MIN_SCORE]
    rewards_str = ",".join(f"{r:.2f}" for r in safe_rewards)
    print(
        f"[END] success={str(bool(success)).lower()} steps={int(steps)} "
        f"score={s:.3f} rewards={rewards_str}",
        flush=True,
    )


# ── LLM helper ────────────────────────────────────────────────────────────────

def pick_action(
    client: OpenAI,
    obs: Observation,
    step: int,
) -> str:
    """Ask the LLM to pick a repair action given the current observation."""
    if step == 1 or obs.current_sql is None:
        return "generate"

    user_msg = textwrap.dedent(f"""
        Question: {obs.question}

        Current SQL (failed):
        {obs.current_sql}

        Error: {obs.error_message or "unknown"}
        Error class: {obs.error_class or "unknown"}
        Attempt number: {obs.attempt_number} of {obs.max_attempts}

        Which repair action should I use next?
    """).strip()

    try:
        completion = client.chat.completions.create(
            model=MODEL_NAME,
            messages=[
                {"role": "system", "content": SYSTEM_PROMPT},
                {"role": "user", "content": user_msg},
            ],
            temperature=TEMPERATURE,
            max_tokens=MAX_TOKENS,
        )
        raw = (completion.choices[0].message.content or "").strip().lower()
        # Normalise to valid action name
        for action in REPAIR_ACTIONS:
            if action in raw:
                return action
        return "rewrite_full"
    except Exception as exc:
        print(f"[DEBUG] LLM call failed: {exc}", flush=True)
        return "rewrite_full"


# ── Single-episode runner ─────────────────────────────────────────────────────

async def run_episode(
    env: SQLAgentEnv,
    client: OpenAI,
    task_id: str,
) -> None:
    """Run one full episode for a task, emitting structured stdout logs."""
    log_start(task=task_id, model=MODEL_NAME)

    rewards: List[float] = []
    steps_taken = 0
    score = _MIN_SCORE
    success = False
    last_error: Optional[str] = None

    try:
        try:
            obs = env.reset(task_id)
        except Exception as exc:
            log_step(step=1, action="reset", reward=_MIN_SCORE, done=True, error=str(exc))
            rewards.append(_MIN_SCORE)
            steps_taken = 1
            return

        for step in range(1, MAX_STEPS + 1):
            try:
                action_name = pick_action(client, obs, step)
            except Exception:
                action_name = "generate"
            action = Action(repair_action=action_name)

            try:
                obs, reward_info = await env.step(action)
            except Exception as exc:
                log_step(step=step, action=action_name, reward=_MIN_SCORE, done=True, error=str(exc))
                rewards.append(_MIN_SCORE)
                steps_taken = step
                break

            reward = _safe_score(getattr(reward_info, "value", None))
            done = bool(getattr(reward_info, "done", False))
            last_error = getattr(obs, "error_message", None)
            success = bool(getattr(reward_info, "success", False))

            rewards.append(reward)
            steps_taken = step

            log_step(
                step=step,
                action=action_name,
                reward=reward,
                done=done,
                error=last_error,
            )

            if done:
                break

        denom = max(len(rewards), 1)
        avg = sum(rewards) / denom if rewards else _MIN_SCORE
        score = _safe_score(avg)

    except Exception as exc:
        # Catch-all so we always emit a valid [END] line
        log_step(step=steps_taken or 1, action="error", reward=_MIN_SCORE, done=True, error=str(exc))
        if not rewards:
            rewards.append(_MIN_SCORE)
        score = _MIN_SCORE
    finally:
        log_end(
            success=success,
            steps=max(int(steps_taken), 1),
            score=score,
            rewards=rewards,
        )


# ── Main ──────────────────────────────────────────────────────────────────────

async def main() -> None:
    try:
        client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
        env = SQLAgentEnv()
    except Exception as exc:
        # Environment couldn't init β€” still emit a valid [START]/[STEP]/[END] per task
        for task_id in TASKS:
            log_start(task=task_id, model=MODEL_NAME)
            log_step(step=1, action="init_error", reward=_MIN_SCORE, done=True, error=str(exc))
            log_end(success=False, steps=1, score=_MIN_SCORE, rewards=[_MIN_SCORE])
            print("", flush=True)
        return

    for task_id in TASKS:
        try:
            await run_episode(env, client, task_id)
        except Exception as exc:
            # run_episode already has its own catch-all, but guard against anything leaking
            log_end(success=False, steps=1, score=_MIN_SCORE, rewards=[_MIN_SCORE])
            print(f"[DEBUG] run_episode({task_id}) crashed: {exc}", flush=True)
        print("", flush=True)


if __name__ == "__main__":
    asyncio.run(main())