-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.py
More file actions
54 lines (35 loc) · 1.35 KB
/
executor.py
File metadata and controls
54 lines (35 loc) · 1.35 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
from llm import generate_sql
from prompt_builder import build_prompt, build_error_correction_prompt
from db import execute_query
MAX_RETRIES = 5
def validate_sql(sql):
sql = sql.strip().lower()
return sql.startswith("select")
def clean_sql_output(sql):
if "```" in sql:
sql = sql.replace("```sql", "").replace("```", "").strip()
if "select" in sql.lower():
sql = sql[sql.lower().index("select"):]
return sql.strip()
def execute_with_retry(selected_db, schema, user_question):
attempt = 0
# 🔹 First SQL generation
prompt = build_prompt(schema, user_question, explain=False)
sql_query = generate_sql(prompt)
sql_query = clean_sql_output(sql_query)
while attempt < MAX_RETRIES:
if not validate_sql(sql_query):
return None, sql_query, "Only SELECT queries are allowed."
columns, result = execute_query(selected_db, sql_query)
if columns is not None:
return (columns, result), sql_query, None
# 🔹 Error occurred → Retry
attempt += 1
correction_prompt = build_error_correction_prompt(
schema,
sql_query,
result
)
sql_query = generate_sql(correction_prompt)
sql_query = clean_sql_output(sql_query)
return None, sql_query, f"Failed after {MAX_RETRIES} attempts."