-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_agent.py
More file actions
239 lines (174 loc) · 5.48 KB
/
Copy pathsql_agent.py
File metadata and controls
239 lines (174 loc) · 5.48 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
import re
FORBIDDEN_SQL = re.compile(
r"\b(DELETE|UPDATE|DROP|ALTER|INSERT|TRUNCATE|CREATE|REPLACE|ATTACH|DETACH|PRAGMA|VACUUM)\b",
re.IGNORECASE,
)
def _chat_with_ollama(prompt):
try:
import ollama
except ModuleNotFoundError as exc:
raise RuntimeError(
"The ollama Python package is not installed. Run: pip install ollama"
) from exc
return ollama.chat(
model="qwen2.5:7b",
messages=[
{
"role": "user",
"content": prompt
}
]
)
def _clean_sql(sql):
sql = sql.strip()
sql = sql.replace("```sql", "")
sql = sql.replace("```", "")
sql = sql.strip()
matches = re.findall(
r"SELECT\b.*?(?:;|$)",
sql,
re.IGNORECASE | re.DOTALL
)
if not matches:
raise ValueError("No valid SELECT query found.")
sql = matches[0].strip()
sql = sql.rstrip(";").strip() + ";"
return sql
def validate_select_sql(sql):
if FORBIDDEN_SQL.search(sql):
raise ValueError("Unsafe SQL blocked.")
cleaned = _clean_sql(sql)
if FORBIDDEN_SQL.search(cleaned):
raise ValueError("Unsafe SQL blocked.")
if cleaned.count(";") > 1:
raise ValueError("Only one SQL statement allowed.")
if not cleaned.upper().startswith("SELECT"):
raise ValueError("Only SELECT queries are allowed.")
return cleaned
def select_relevant_tables(question, table_names, conversation_context=""):
"""Identify which database tables are needed to answer the user's question.
If there are few tables (<= 15), returns all table names directly without filtering.
"""
if len(table_names) <= 15:
return table_names
prompt = f"""
Identify which database tables are required to answer the user's question.
Respond ONLY with a comma-separated list of the relevant table names from the list below. Do not include any explanations, markdown, or extra words.
Table Names:
{", ".join(table_names)}
User Question:
{question}
Relevant Tables:
"""
try:
response = _chat_with_ollama(prompt)
content = response["message"]["content"].strip()
# Clean potential markdown or extra symbols
content = content.replace("`", "").replace("[", "").replace("]", "").replace("'", "").replace('"', "")
selected = [t.strip() for t in content.split(",") if t.strip() in table_names]
if not selected:
return table_names
return selected
except Exception:
return table_names
def generate_sql(question, schema_text, relationship_text="", conversation_context=""):
prompt = f"""
You are an expert MySQL query generator.
Database Relationships:
{relationship_text}
Database Schema:
{schema_text}
Conversation Context:
{conversation_context or "No previous context."}
Rules:
1. Return ONLY a SELECT statement.
2. Do not include any explanations, markdown, or comments.
3. Use only tables and columns present in the schema.
4. Use the provided conversation context for follow-up queries.
5. Generate valid MySQL syntax only. Do NOT use SQLite-specific syntax like COLLATE NOCASE.
6. Return exactly one SELECT query.
7. Do not generate UPDATE, DELETE, INSERT, DROP, ALTER, or other non-SELECT statements.
Examples:
Question:
Who works in sales?
SQL:
SELECT name FROM employees WHERE department='sales';
Question:
Who has the highest salary?
SQL:
SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 1;
Question:
{question}
"""
print("\n===================")
print("QUESTION:", question)
print("SCHEMA LENGTH:", len(schema_text))
print("PROMPT LENGTH:", len(prompt))
print("===================\n")
response = _chat_with_ollama(prompt)
return validate_select_sql(
response["message"]["content"]
)
def is_database_question(
question,
schema_text,
conversation_context=""
):
prompt = f"""
Database Schema:
{schema_text}
Conversation Context:
{conversation_context or "No previous context."}
Can the following question be answered using this database?
Question:
{question}
Respond ONLY:
YES
or
NO
"""
response = _chat_with_ollama(prompt)
answer = response["message"]["content"].strip().upper()
return "YES" in answer
def generate_corrected_sql(
question,
schema_text,
relationship_text="",
failed_sql="",
error_message="",
conversation_context=""
):
prompt = f"""
You are an expert MySQL query generator.
A previously generated SQL query failed with an execution error. Please correct the SQL query to resolve the error.
Database Relationships:
{relationship_text}
Database Schema:
{schema_text}
Conversation Context:
{conversation_context or "No previous context."}
User Question:
{question}
Failed SQL:
{failed_sql}
Error Message:
{error_message}
Rules:
1. Correct the SQL query to fix the error.
2. Return ONLY a SELECT statement.
3. Do not include any explanations, markdown, or comments.
4. Use only tables and columns present in the schema.
5. Generate valid MySQL syntax only. Do NOT use SQLite-specific syntax.
6. Return exactly one SELECT query.
7. Do not generate UPDATE, DELETE, INSERT, DROP, ALTER, or other non-SELECT statements.
Corrected SQL:
"""
print("\n===================")
print("CORRECTING SQL FOR QUESTION:", question)
print("FAILED SQL:", failed_sql)
print("ERROR:", error_message)
print("===================\n")
response = _chat_with_ollama(prompt)
return validate_select_sql(
response["message"]["content"]
)