-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
273 lines (219 loc) · 9.73 KB
/
inference.py
File metadata and controls
273 lines (219 loc) · 9.73 KB
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
"""
inference.py — SQL Query Debugger OpenEnv
Follows the mandatory [START]/[STEP]/[END] stdout format.
Uses OpenAI client with API_BASE_URL, MODEL_NAME, HF_TOKEN.
"""
import os
import json
import textwrap
from typing import List, Optional
from openai import OpenAI
from env.environment import SQLDebuggerEnvironment
from env.models import Action, ActionType, DifficultyLevel
# ─────────────────────────────────────────────
# ENVIRONMENT VARIABLES
# ─────────────────────────────────────────────
API_KEY = os.getenv("HF_TOKEN") or os.getenv("API_KEY") or os.getenv("OPENAI_API_KEY") or "dummy-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-query-debugger"
MAX_STEPS = 10
SUCCESS_SCORE_THRESHOLD = 0.5
# ─────────────────────────────────────────────
# LOGGING FUNCTIONS — exact format required
# ─────────────────────────────────────────────
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()
print(f"[STEP] step={step} action={action} 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:.3f} rewards={rewards_str}", flush=True)
# ─────────────────────────────────────────────
# SYSTEM PROMPT
# ─────────────────────────────────────────────
SYSTEM_PROMPT = textwrap.dedent("""
You are an expert SQL debugger. You will be given a buggy SQL query and must fix it.
You must respond with a JSON object only — no explanation outside the JSON.
For syntax/logic errors, respond with:
{
"action_type": "submit_answer",
"fixed_query": "<your fixed SQL query here>",
"explanation": "<brief explanation of what was wrong>",
"error_type": "<syntax|logic|performance>",
"error_location": "<where in the query the error is>",
"confidence": 0.9
}
For performance issues, respond with:
{
"action_type": "optimize_query",
"optimized_query": "<your optimized SQL query here>",
"optimization_type": "<what optimization was applied>",
"explanation": "<why this optimization works>",
"root_cause": "<what caused the performance issue>",
"expected_improvement": "<expected performance gain>",
"confidence": 0.85
}
Always provide valid JSON. Never include markdown code blocks.
""").strip()
def build_user_prompt(obs) -> str:
ctx = obs.current_context
return textwrap.dedent(f"""
Task: {obs.task_description}
Difficulty: {obs.difficulty}
Buggy Query:
{ctx.get('buggy_query', 'N/A')}
Error Message:
{ctx.get('error_message', 'N/A')}
Database Schema:
{json.dumps(ctx.get('database_schema', {}), indent=2)}
Error Type Hint: {ctx.get('error_type_hint', 'unknown')}
Category: {ctx.get('category', 'unknown')}
Steps Remaining: {ctx.get('steps_remaining', 20)}
Analyze the buggy query and provide your fix as a JSON object.
""").strip()
# ─────────────────────────────────────────────
# LLM CALL
# ─────────────────────────────────────────────
def get_llm_action(client: OpenAI, obs, step: int) -> Action:
"""Call the LLM and parse its response into an Action."""
user_prompt = build_user_prompt(obs)
try:
completion = client.chat.completions.create(
model=MODEL_NAME,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_prompt},
],
temperature=0.3,
max_tokens=512,
stream=False,
)
text = (completion.choices[0].message.content or "").strip()
# Parse JSON response
# Remove markdown code blocks if present
if "```" in text:
text = text.split("```")[1]
if text.startswith("json"):
text = text[4:]
text = text.strip()
data = json.loads(text)
action_type = data.get("action_type", "submit_answer")
if action_type == "optimize_query":
return Action(
action_type=ActionType.OPTIMIZE_QUERY,
payload={
"optimized_query": data.get("optimized_query", "SELECT 1"),
"optimization_type": data.get("optimization_type", "Performance fix"),
"explanation": data.get("explanation", ""),
"root_cause": data.get("root_cause", ""),
"expected_improvement": data.get("expected_improvement", ""),
"confidence": float(data.get("confidence", 0.7)),
}
)
else:
return Action(
action_type=ActionType.SUBMIT_ANSWER,
payload={
"fixed_query": data.get("fixed_query", "SELECT 1"),
"explanation": data.get("explanation", ""),
"error_type": data.get("error_type", "syntax"),
"error_location": data.get("error_location", "unknown"),
"confidence": float(data.get("confidence", 0.7)),
}
)
except Exception as exc:
print(f"[DEBUG] LLM call failed: {exc}", flush=True)
# Fallback to identify_error action
return Action(
action_type=ActionType.IDENTIFY_ERROR,
payload={
"error_location": "unknown",
"error_type": "syntax",
"explanation": "LLM call failed, using fallback"
}
)
# ─────────────────────────────────────────────
# MAIN INFERENCE LOOP
# ─────────────────────────────────────────────
def run_episode(client: OpenAI, difficulty: str, task_id: str) -> dict:
"""Run one full episode and return results."""
env = SQLDebuggerEnvironment()
obs = env.reset(difficulty=difficulty, task_id=task_id)
rewards = []
steps = 0
success = False
score = 0.0
log_start(task=task_id, env=BENCHMARK, model=MODEL_NAME)
try:
for step in range(1, MAX_STEPS + 1):
if env.state().done:
break
# Get action from LLM
action = get_llm_action(client, obs, step)
action_str = f"{action.action_type.value}"
error_str = None
try:
resp = env.step(action)
reward = resp.reward.score
done = resp.done
obs = resp.observation
except Exception as e:
reward = -0.1
done = False
error_str = str(e)[:100]
rewards.append(reward)
steps = step
log_step(
step = step,
action = action_str,
reward = reward,
done = done,
error = error_str
)
if done:
break
# Calculate score
total_reward = sum(rewards)
score = min(max(total_reward / MAX_STEPS, 0.0), 1.0)
success = score >= SUCCESS_SCORE_THRESHOLD
except Exception as e:
print(f"[DEBUG] Episode error: {e}", flush=True)
error_str = str(e)[:100]
finally:
log_end(
success = success,
steps = steps,
score = score,
rewards = rewards
)
return {
"task_id": task_id,
"difficulty": difficulty,
"score": score,
"steps": steps,
"success": success,
}
def main():
"""Main entry point — runs inference on all 3 difficulty levels."""
print(f"[DEBUG] API_BASE_URL={API_BASE_URL}", flush=True)
print(f"[DEBUG] MODEL_NAME={MODEL_NAME}", flush=True)
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
tasks = [
("easy", "easy_001"),
("medium", "medium_001"),
("hard", "hard_001"),
]
results = []
for difficulty, task_id in tasks:
result = run_episode(client, difficulty, task_id)
results.append(result)
# Final summary
avg_score = sum(r["score"] for r in results) / len(results)
print(f"\n[DEBUG] Average Score: {avg_score:.3f}", flush=True)
for r in results:
print(f"[DEBUG] {r['difficulty']:8} | {r['task_id']:12} | score={r['score']:.3f} | steps={r['steps']}", flush=True)
if __name__ == "__main__":
main()