sravaniamere commited on
Commit
2184846
·
1 Parent(s): 1a1713a

Make inference fallback to offline SQL correction

Browse files
Files changed (1) hide show
  1. inference.py +43 -6
inference.py CHANGED
@@ -16,9 +16,13 @@ import os
16
  import sys
17
  import textwrap
18
  from typing import List, Optional
 
19
 
20
  import httpx
21
- from openai import OpenAI
 
 
 
22
 
23
  API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
24
  MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
@@ -72,7 +76,35 @@ SYSTEM_PROMPT = textwrap.dedent(
72
  ).strip()
73
 
74
 
75
- def get_model_action(client: OpenAI, obs: dict, history: List[str]) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  history_block = "\n".join(history[-4:]) if history else "None"
77
  user_prompt = textwrap.dedent(
78
  f"""
@@ -103,14 +135,19 @@ def get_model_action(client: OpenAI, obs: dict, history: List[str]) -> str:
103
  stream=False,
104
  )
105
  text = (completion.choices[0].message.content or "").strip()
106
- return text if text else "SELECT 1"
107
  except Exception as exc:
108
  print(f"[DEBUG] LLM call failed: {exc}", flush=True)
109
- return "SELECT 1"
110
 
111
 
112
  async def run_task(task_name: str) -> None:
113
- client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
 
 
 
 
 
114
 
115
  rewards: List[float] = []
116
  history: List[str] = []
@@ -137,7 +174,7 @@ async def run_task(task_name: str) -> None:
137
  action_str = get_model_action(client, obs, history)
138
  except Exception as exc:
139
  print(f"[DEBUG] Model failed: {exc}", flush=True)
140
- action_str = "SELECT 1"
141
 
142
  try:
143
  step_resp = await http.post("/step", json={"corrected_query": action_str})
 
16
  import sys
17
  import textwrap
18
  from typing import List, Optional
19
+ import re
20
 
21
  import httpx
22
+ try:
23
+ from openai import OpenAI
24
+ except Exception:
25
+ OpenAI = None
26
 
27
  API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
28
  MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
 
76
  ).strip()
77
 
78
 
79
+ SQL_REPLACEMENTS = {
80
+ "FORM": "FROM",
81
+ "WEHRE": "WHERE",
82
+ "WHER": "WHERE",
83
+ "GRUP": "GROUP",
84
+ "HAVNG": "HAVING",
85
+ "ORDR": "ORDER",
86
+ "INNE": "INNER",
87
+ "LFT": "LEFT",
88
+ "BETWEN": "BETWEEN",
89
+ "DSC": "DESC",
90
+ "SELCT": "SELECT",
91
+ "LIMT": "LIMIT",
92
+ "DPT_ID": "DEPT_ID",
93
+ }
94
+
95
+
96
+ def heuristic_correct_sql(query: str) -> str:
97
+ corrected = query
98
+ for broken, fixed in SQL_REPLACEMENTS.items():
99
+ corrected = re.sub(rf"\b{re.escape(broken)}\b", fixed, corrected, flags=re.IGNORECASE)
100
+ return corrected.strip()
101
+
102
+
103
+ def get_model_action(client: Optional["OpenAI"], obs: dict, history: List[str]) -> str:
104
+ heuristic = heuristic_correct_sql(obs["broken_query"])
105
+ if client is None:
106
+ return heuristic
107
+
108
  history_block = "\n".join(history[-4:]) if history else "None"
109
  user_prompt = textwrap.dedent(
110
  f"""
 
135
  stream=False,
136
  )
137
  text = (completion.choices[0].message.content or "").strip()
138
+ return text if text else heuristic
139
  except Exception as exc:
140
  print(f"[DEBUG] LLM call failed: {exc}", flush=True)
141
+ return heuristic
142
 
143
 
144
  async def run_task(task_name: str) -> None:
145
+ client = None
146
+ if OpenAI is not None and API_KEY not in {"", "dummy"}:
147
+ try:
148
+ client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
149
+ except Exception as exc:
150
+ print(f"[DEBUG] OpenAI client init failed: {exc}", flush=True)
151
 
152
  rewards: List[float] = []
153
  history: List[str] = []
 
174
  action_str = get_model_action(client, obs, history)
175
  except Exception as exc:
176
  print(f"[DEBUG] Model failed: {exc}", flush=True)
177
+ action_str = heuristic_correct_sql(obs["broken_query"])
178
 
179
  try:
180
  step_resp = await http.post("/step", json={"corrected_query": action_str})