File size: 10,767 Bytes
3867c62
 
 
 
 
 
 
 
 
d151777
3867c62
d151777
 
 
 
 
3867c62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3502e46
cf6fec1
3867c62
d151777
 
 
 
3867c62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d151777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3867c62
 
 
 
 
 
 
 
 
 
 
 
d151777
3867c62
 
 
 
 
 
 
 
 
 
 
 
 
3502e46
 
 
 
 
 
 
 
 
d151777
 
3867c62
d151777
3502e46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d151777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3502e46
d151777
 
3502e46
d151777
 
 
 
 
 
 
 
3502e46
d151777
 
3502e46
d151777
3502e46
d151777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3502e46
 
 
 
 
 
d151777
 
3502e46
3867c62
 
d151777
3502e46
 
 
 
 
3867c62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3502e46
 
 
 
 
 
 
 
 
3867c62
d151777
3867c62
 
 
 
 
 
 
 
 
d151777
 
 
 
 
3867c62
 
 
 
 
 
 
 
 
 
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
"""
QueryForge Inference Script
===================================
MANDATORY env vars:
  API_BASE_URL   The API endpoint for the LLM (e.g. https://router.huggingface.co/v1)
  MODEL_NAME     The model identifier to use for inference
  HF_TOKEN       Your Hugging Face / API key

Optional env vars:
  ENV_URL          QueryForge environment server URL (default: live HF Space)
  ANTHROPIC_API_KEY  Enables AI judge for scores up to 1.0 (default: deterministic mode)

STDOUT FORMAT (required by evaluator):
  [START] task=<task_id> env=queryforge model=<model_name>
  [STEP]  step=<n> action=<sql_oneline> reward=<0.00> done=<true|false> error=<msg|null>
  [END]   success=<true|false> steps=<n> score=<0.000> rewards=<r1,r2,...>
"""

import os
import re
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 client import QueryforgeEnv
from models import SQLAction

# ── Configuration ─────────────────────────────────────────────────────────────

API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
API_KEY      = os.getenv("HF_TOKEN") or os.getenv("API_KEY")
MODEL_NAME   = os.getenv("MODEL_NAME", "meta-llama/Llama-3.1-8B-Instruct")
ENV_URL      = os.getenv("ENV_URL", "https://prithvigg-queryforge.hf.space")

MAX_STEPS              = 5
TEMPERATURE            = 0.2
MAX_TOKENS             = 512
SUCCESS_SCORE_THRESHOLD = 0.9

TASK_IDS = [
    "task_easy_syntax",
    "task_medium_join",
    "task_hard_cte",
    "task_expert_rank",
    "task_expert_recursive",
    "task_expert_window",
]

# ── Prompts ───────────────────────────────────────────────────────────────────

SYSTEM_PROMPT = textwrap.dedent("""
    You are an expert SQL engineer tasked with debugging and optimising SQL queries.
    You will be given a SQL challenge that includes a schema, a broken or slow query,
    and a description of what the correct output should be.

    Rules:
    - Respond with ONLY a single SQL query inside a ```sql ... ``` code block.
    - Do not explain your reasoning outside the code block.
    - Do not include multiple statements separated by semicolons.
    - If you receive grading feedback on a previous attempt, use it to improve.
""").strip()

# ── Structured log helpers (evaluator-required format) ────────────────────────

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


def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
    # SQL may contain newlines β€” collapse to single line (spec: no newlines within a line)
    action_oneline = " ".join(action.split())
    error_val = error if error else "null"
    print(
        f"[STEP] step={step} action={action_oneline} reward={reward:.2f}"
        f" done={str(done).lower()} 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}"
        f" score={score:.3f} rewards={rewards_str}",
        flush=True,
    )

# ── SQL extraction ─────────────────────────────────────────────────────────────

_SQL_BLOCK = re.compile(r"```(?:sql)?\s*(.*?)```", re.DOTALL | re.IGNORECASE)


def extract_sql(text: str) -> str:
    match = _SQL_BLOCK.search(text)
    if match:
        return match.group(1).strip()
    return text.strip()


# ── Formatting helpers (human-readable output) ────────────────────────────────

def score_bar(score: float, width: int = 25) -> str:
    filled = int(score * width)
    return "[" + "β–ˆ" * filled + "β–‘" * (width - filled) + f"] {score:.3f}"


def hr(char="═", width=70):
    print(char * width)


# ── Per-task agent loop ────────────────────────────────────────────────────────

def run_task(task_id: str, llm: OpenAI, env_client) -> dict:
    # Initialise before anything that can throw β€” guarantees [END] is always emitted.
    step       = 0
    rewards: List[float] = []
    success    = False
    best_score = 0.0
    task_title = task_id
    task_level = "unknown"
    attempts   = 0
    done       = False

    log_start(task=task_id, model=MODEL_NAME)

    try:
        result = env_client.reset(task_id=task_id)
        obs    = result.observation

        if result.done:
            print(f"  ERROR loading task: {obs.feedback}")
            log_end(success=False, steps=0, score=0.0, rewards=[])
            return {"task_id": task_id, "best_score": 0.0, "attempts": 0, "done": False}

        task_title = obs.task_title
        task_level = obs.task_level
        print(f"\n  Task  : {task_title}  [{task_level}]")

        messages = [
            {"role": "system", "content": SYSTEM_PROMPT},
            {
                "role": "user",
                "content": (
                    f"Here is your SQL challenge:\n\n{obs.task_description}\n\n"
                    "Provide your fixed SQL query."
                ),
            },
        ]

        while not result.done:
            step += 1

            try:
                completion = llm.chat.completions.create(
                    model=MODEL_NAME,
                    messages=messages,
                    temperature=TEMPERATURE,
                    max_tokens=MAX_TOKENS,
                    stream=False,
                )
                response_text = completion.choices[0].message.content or ""
            except Exception as exc:
                print(f"  LLM call failed at step {step}: {exc}")
                break

            sql = extract_sql(response_text)

            print(f"\n  β”Œβ”€ Step {step} Β· SQL submitted {'─' * (50 - len(str(step)))}")
            for line in sql.splitlines():
                print(f"  β”‚  {line}")
            print(f"  β””{'─' * 56}")

            result = env_client.step(SQLAction(sql=sql))
            obs    = result.observation

            reward = result.reward or 0.0
            rewards.append(reward)
            done = result.done

            if not obs.syntax_valid:
                step_error: Optional[str] = "syntax_error"
                print(f"  βœ— Syntax error β€” query could not be parsed")
            elif not obs.execution_success:
                step_error = (obs.execution_error or "execution_error")[:120]
                print(f"  βœ— Execution failed β€” {step_error[:80]}")
            else:
                step_error = None
                print(f"  βœ“ Executed Β· rows returned: {obs.rows_returned}")

            done_marker = "  βœ“ DONE" if done else ""
            print(f"  Score : {score_bar(reward)}{done_marker}")

            log_step(step=step, action=sql, reward=reward, done=done, error=step_error)

            if done:
                break

            print(f"\n  ↻ Retrying β€” score {reward:.3f} below threshold")
            if obs.feedback:
                for part in obs.feedback.split("  "):
                    part = part.strip()
                    if part:
                        print(f"  {part}")
            if obs.hint:
                print(f"  Hint     : {obs.hint[:120]}")

            messages.append({"role": "assistant", "content": response_text})
            messages.append({
                "role": "user",
                "content": (
                    f"Your query scored {reward:.3f}.\n\n"
                    f"Feedback: {obs.feedback}\n\n"
                    f"Hint: {obs.hint}\n\n"
                    "Please submit an improved SQL query."
                ),
            })

        best_score = obs.best_score
        attempts   = obs.attempt
        success    = best_score >= SUCCESS_SCORE_THRESHOLD

    except Exception as exc:
        print(f"  FATAL error in task {task_id}: {exc}", flush=True)

    finally:
        log_end(success=success, steps=step, score=best_score, rewards=rewards)

    return {
        "task_id":    task_id,
        "task_title": task_title,
        "task_level": task_level,
        "best_score": best_score,
        "attempts":   attempts,
        "done":       done,
    }


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

def main() -> None:
    if not API_KEY:
        print("ERROR: HF_TOKEN (or API_KEY) is not set.")
        sys.exit(1)

    llm = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)

    hr()
    print("  QueryForge β€” Inference")
    print(f"  Model  : {MODEL_NAME}")
    print(f"  Env    : {ENV_URL}")
    print(f"  Tasks  : {', '.join(TASK_IDS)}")
    hr()

    results = []
    try:
        env_ctx = QueryforgeEnv(base_url=ENV_URL).sync()
        with env_ctx as env_client:
            for task_id in TASK_IDS:
                print(f"\n{'─' * 70}")
                results.append(run_task(task_id, llm, env_client))
    except Exception as exc:
        print(f"FATAL: could not connect to environment at {ENV_URL}: {exc}", flush=True)
        sys.exit(1)

    # ── Results summary ───────────────────────────────────────────────────────
    print(f"\n{'═' * 70}")
    print("  RESULTS")
    print(f"  Model: {MODEL_NAME}")
    print(f"{'═' * 70}")
    print(f"  {'Task':<28} {'Level':<8} {'Steps':>5}  {'Best Score'}")
    print(f"  {'─' * 28} {'─' * 8} {'─' * 5}  {'─' * 30}")

    total = 0.0
    for r in results:
        title  = r.get("task_title", r["task_id"])[:27]
        level  = r.get("task_level", "?")
        steps  = r.get("attempts", "?")
        score  = r["best_score"]
        total += score
        print(f"  {title:<28} {level:<8} {steps:>5}  {score_bar(score)}")

    avg = total / len(results) if results else 0.0
    print(f"{'─' * 70}")
    print(f"  {'AVERAGE':<28} {'':8} {'':5}  {score_bar(avg)}")
    print(f"{'═' * 70}\n")


if __name__ == "__main__":
    main()