File size: 9,288 Bytes
a8a3c90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
QueryForge Baseline Inference Script
─────────────────────────────────────
Runs a Claude model as an agent against all 3 built-in tasks and reports
a reproducible baseline score.

Usage:
    # All tasks, default model (claude-haiku-4-5):
    python baseline.py

    # Specific model:
    python baseline.py --model claude-opus-4-6

    # Single task:
    python baseline.py --task task_easy_syntax

    # More verbose output:
    python baseline.py --verbose

Requirements:
    ANTHROPIC_API_KEY must be set in the environment.
"""

import argparse
import os
import re
import sys

import anthropic

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from models import SQLAction
from server.queryforge_environment import QueryforgeEnvironment
from tasks import REGISTRY

# ── Constants ─────────────────────────────────────────────────────────────────

DEFAULT_MODEL = "claude-haiku-4-5"

SYSTEM_PROMPT = """\
You are an expert SQL engineer. You will be given a SQL debugging or \
optimisation challenge. Your job is to submit a corrected or improved SQL query.

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 (no semicolons except at the very end).
- If you receive feedback on a previous attempt, use it to improve your query.
"""

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

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


def _extract_sql(text: str) -> str:
    """Pull the first SQL code block out of Claude's response."""
    match = _SQL_BLOCK.search(text)
    if match:
        return match.group(1).strip()
    # Fallback: return the whole response stripped β€” better than crashing
    return text.strip()


# ── Formatting helpers ────────────────────────────────────────────────────────

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

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


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

def run_task(
    task_id: str,
    model: str,
    client: anthropic.Anthropic,
    verbose: bool = False,
) -> dict:
    """
    Run one episode of a single task.

    Returns a dict with keys:
        task_id, task_title, task_level,
        best_score, attempts, done
    """
    env = QueryforgeEnvironment()
    obs = env.reset(task_id=task_id)

    if obs.done:
        # reset() returned an error (unknown task_id)
        print(f"  ERROR: {obs.feedback}")
        return {"task_id": task_id, "best_score": 0.0, "attempts": 0, "done": False}

    print(f"\n  Task : {obs.task_title}  [{obs.task_level}]  (max {env._current_task.max_steps} steps)")
    if verbose:
        print(f"  ID   : {obs.task_id}")

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

    step = 0
    while not obs.done:
        step += 1

        # ── Call Claude ───────────────────────────────────────────────────────
        with client.messages.stream(
            model=model,
            max_tokens=512,
            system=SYSTEM_PROMPT,
            messages=messages,
        ) as stream:
            response_text = ""
            for text in stream.text_stream:
                response_text += text

        sql = _extract_sql(response_text)

        if verbose:
            print(f"\n  ── Step {step}")
            short_sql = sql[:120] + ("…" if len(sql) > 120 else "")
            print(f"     SQL: {short_sql}")

        # ── Submit to environment ─────────────────────────────────────────────
        obs = env.step(SQLAction(sql=sql))

        score_bar = _score_bar(obs.reward or 0.0)
        status = "βœ“ DONE" if obs.done else f"step {step}/{env._current_task.max_steps}"
        print(f"  [{status}]  Score: {score_bar}")

        if verbose and obs.feedback:
            fb = obs.feedback[:200] + ("…" if len(obs.feedback) > 200 else "")
            print(f"     Feedback: {fb}")

        if obs.done:
            break

        # ── Append exchange to conversation for next attempt ──────────────────
        messages.append({"role": "assistant", "content": response_text})
        messages.append({
            "role": "user",
            "content": (
                f"Your query scored {obs.reward:.3f}. Here is the feedback:\n\n"
                f"{obs.feedback}\n\n"
                f"Hint: {obs.hint}\n\n"
                "Please try again with an improved SQL query."
            ),
        })

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


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

def main():
    parser = argparse.ArgumentParser(description="QueryForge Baseline Inference")
    parser.add_argument(
        "--model", default=DEFAULT_MODEL,
        help=f"Anthropic model ID to use (default: {DEFAULT_MODEL})"
    )
    parser.add_argument(
        "--task", default=None,
        help="Run a single task by ID instead of all built-in tasks"
    )
    parser.add_argument(
        "--verbose", action="store_true",
        help="Print SQL queries and full feedback for each step"
    )
    args = parser.parse_args()

    # ── Validate API key ──────────────────────────────────────────────────────
    api_key = os.environ.get("ANTHROPIC_API_KEY")
    if not api_key:
        print("ERROR: ANTHROPIC_API_KEY is not set.")
        sys.exit(1)

    client = anthropic.Anthropic(api_key=api_key)

    # ── Determine tasks to run ────────────────────────────────────────────────
    if args.task:
        task_ids = [args.task]
    else:
        task_ids = ["task_easy_syntax", "task_medium_join", "task_hard_cte"]

    # ── Header ────────────────────────────────────────────────────────────────
    _hr()
    print("  QueryForge β€” Baseline Inference")
    print(f"  Model  : {args.model}")
    print(f"  Tasks  : {', '.join(task_ids)}")
    _hr()

    # ── Run each task ─────────────────────────────────────────────────────────
    results = []
    for task_id in task_ids:
        print(f"\n{'─' * 70}")
        result = run_task(task_id, args.model, client, verbose=args.verbose)
        results.append(result)

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

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

    avg = total_score / 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()