Spaces:
Runtime error
Runtime error
Update databot.py
Browse files- databot.py +309 -417
databot.py
CHANGED
|
@@ -1,418 +1,310 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import re
|
| 3 |
-
import time
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
self.
|
| 28 |
-
self.
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
self.
|
| 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 |
-
for
|
| 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 |
-
if
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
if not self.
|
| 134 |
-
return True, ""
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
for
|
| 141 |
-
if re.search(rf'\b{
|
| 142 |
-
return False, f"
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
def
|
| 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 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
}
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
rows = [dict(zip(columns, row)) for row in result.fetchall()]
|
| 311 |
-
return columns, rows
|
| 312 |
-
|
| 313 |
-
def _summarize_results(self, question, sql, columns, rows):
|
| 314 |
-
"""Ask GPT to summarize the results in a well-structured, insightful way."""
|
| 315 |
-
# Limit rows to avoid token overflow
|
| 316 |
-
display_rows = rows[:50]
|
| 317 |
-
total_count = len(rows)
|
| 318 |
-
truncated = total_count > 50
|
| 319 |
-
|
| 320 |
-
result_text = f"Columns: {columns}\nRows ({total_count} total"
|
| 321 |
-
if truncated:
|
| 322 |
-
result_text += f", showing first 50"
|
| 323 |
-
result_text += "):\n"
|
| 324 |
-
for row in display_rows:
|
| 325 |
-
result_text += str(row) + "\n"
|
| 326 |
-
|
| 327 |
-
response = self.client.chat.completions.create(
|
| 328 |
-
model=self.model,
|
| 329 |
-
temperature=0.3,
|
| 330 |
-
max_tokens=2000,
|
| 331 |
-
messages=[{
|
| 332 |
-
"role": "system",
|
| 333 |
-
"content": (
|
| 334 |
-
"You are DataBot, an intelligent ERP database assistant for the dev_poly system. "
|
| 335 |
-
"Your job is to answer the user's question based on the SQL query results provided.\n\n"
|
| 336 |
-
"RESPONSE GUIDELINES:\n"
|
| 337 |
-
"1. **Answer the question directly first** β start with a clear, direct answer to what "
|
| 338 |
-
"the user asked. Don't start with 'Based on the query results...' or similar filler.\n"
|
| 339 |
-
"2. **Be specific with numbers** β always include exact counts, totals, amounts, "
|
| 340 |
-
"and percentages where relevant. Round monetary values to 2 decimal places.\n"
|
| 341 |
-
"3. **Use structured formatting** when presenting multiple items:\n"
|
| 342 |
-
" - Use numbered lists or bullet points for lists of items\n"
|
| 343 |
-
" - Use simple text tables for comparisons (align columns with spaces)\n"
|
| 344 |
-
" - Bold important values or key findings using **bold**\n"
|
| 345 |
-
"4. **Add brief insights** β after presenting the data, add 1-2 sentences of "
|
| 346 |
-
"business-relevant observations if applicable (e.g., trends, outliers, notable patterns).\n"
|
| 347 |
-
"5. **Handle empty results gracefully** β if there are 0 rows, say so clearly and "
|
| 348 |
-
"suggest possible reasons (e.g., 'No matching records found. This could mean the "
|
| 349 |
-
"date range has no activity, or the filter criteria may be too narrow.').\n"
|
| 350 |
-
"6. **Keep it conversational but professional** β write as a knowledgeable business "
|
| 351 |
-
"analyst would speak, not as a robotic data dump.\n"
|
| 352 |
-
"7. **If results are truncated** (showing partial data), mention that more records exist "
|
| 353 |
-
"and the summary covers the displayed portion.\n"
|
| 354 |
-
"8. **Match the user's language** β always reply in the same language the user "
|
| 355 |
-
"used in their question. If they asked in French, respond entirely in French. "
|
| 356 |
-
"If in English, respond in English. Only these two languages are supported.\n\n"
|
| 357 |
-
"SECURITY RULES:\n"
|
| 358 |
-
"- NEVER include personal data: phone numbers, email addresses, passwords, salaries, "
|
| 359 |
-
"bank account numbers, identity document numbers, or home addresses.\n"
|
| 360 |
-
"- If the results contain such data, omit it and note that it is restricted.\n"
|
| 361 |
-
"- Focus on business-relevant information: counts, totals, trends, entity names, and statuses."
|
| 362 |
-
)
|
| 363 |
-
}, {
|
| 364 |
-
"role": "user",
|
| 365 |
-
"content": (
|
| 366 |
-
f"Question: {question}\n"
|
| 367 |
-
f"SQL executed: {sql}\n"
|
| 368 |
-
f"Results:\n{result_text}"
|
| 369 |
-
)
|
| 370 |
-
}]
|
| 371 |
-
)
|
| 372 |
-
return (response.choices[0].message.content or "").strip()
|
| 373 |
-
|
| 374 |
-
# ββ Main Entry Point βββββββββββββββββββββββββββββββββββββββββββββ
|
| 375 |
-
|
| 376 |
-
def ask(self, question):
|
| 377 |
-
"""Processes a natural language question and returns an answer."""
|
| 378 |
-
try:
|
| 379 |
-
# Step 1: Pick relevant tables (fast LLM call, only from allowed tables)
|
| 380 |
-
relevant_tables = self._pick_relevant_tables(question)
|
| 381 |
-
print(f" β Tables: {', '.join(relevant_tables)}")
|
| 382 |
-
|
| 383 |
-
# Step 2: Build schema context from cache (instant, no DB query)
|
| 384 |
-
schema_context = self._build_schema_context(relevant_tables)
|
| 385 |
-
|
| 386 |
-
# Step 3: Generate SQL (fast LLM call with security prompt)
|
| 387 |
-
sql = self._generate_sql(question, schema_context)
|
| 388 |
-
|
| 389 |
-
# Step 3a: Check for non-database questions
|
| 390 |
-
if sql == "NOT_A_QUERY":
|
| 391 |
-
return ("Hello! I'm DataBot, your ERP database assistant. "
|
| 392 |
-
"Ask me questions about your business data and I'll "
|
| 393 |
-
"query the database to find the answer for you.")
|
| 394 |
-
|
| 395 |
-
# Step 3b: Check if the LLM blocked the query
|
| 396 |
-
if sql == "SECURITY_BLOCK":
|
| 397 |
-
return ("I'm sorry, but I cannot provide that information. "
|
| 398 |
-
"Your request involves sensitive or personal data "
|
| 399 |
-
"(such as salaries, passwords, phone numbers, or identity details) "
|
| 400 |
-
"which I am not authorized to access.")
|
| 401 |
-
|
| 402 |
-
print(f" β SQL: {sql}")
|
| 403 |
-
|
| 404 |
-
# Step 3c: Validate SQL doesn't reference restricted tables/columns
|
| 405 |
-
is_safe, reason = self._validate_sql_security(sql)
|
| 406 |
-
if not is_safe:
|
| 407 |
-
print(f" β SECURITY BLOCK: {reason}")
|
| 408 |
-
return ("I'm sorry, but I cannot execute that query. "
|
| 409 |
-
f"Security check: {reason}")
|
| 410 |
-
|
| 411 |
-
# Step 4: Execute SQL (one fast query)
|
| 412 |
-
columns, rows = self._execute_sql(sql)
|
| 413 |
-
|
| 414 |
-
# Step 5: Summarize results (fast LLM call)
|
| 415 |
-
return self._summarize_results(question, sql, columns, rows)
|
| 416 |
-
|
| 417 |
-
except Exception as e:
|
| 418 |
return f"DataBot Error: {str(e)}"
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import time
|
| 4 |
+
import json
|
| 5 |
+
from dotenv import load_dotenv # pyre-ignore[21]
|
| 6 |
+
from sqlalchemy import create_engine, text # pyre-ignore[21]
|
| 7 |
+
from openai import OpenAI as OpenAIClient
|
| 8 |
+
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
# Config file paths
|
| 12 |
+
BASE_DIR = os.path.dirname(__file__)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _load_json(path, name):
|
| 16 |
+
try:
|
| 17 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 18 |
+
return json.load(f)
|
| 19 |
+
except (FileNotFoundError, json.JSONDecodeError) as e:
|
| 20 |
+
print(f" β {name}: {e}")
|
| 21 |
+
return {}
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class DataBot:
|
| 25 |
+
def __init__(self):
|
| 26 |
+
print("Loading configurations...")
|
| 27 |
+
self.db_cfg = _load_json(os.path.join(BASE_DIR, "db_config.json"), "db_config")
|
| 28 |
+
self.ai_cfg = _load_json(os.path.join(BASE_DIR, "ai_config.json"), "ai_config")
|
| 29 |
+
self.prompts = _load_json(os.path.join(BASE_DIR, "prompts_config.json"), "prompts_config")
|
| 30 |
+
self.access_cfg = _load_json(os.path.join(BASE_DIR, "data_access_config.json"), "data_access_config")
|
| 31 |
+
|
| 32 |
+
# Query limits
|
| 33 |
+
ql = self.db_cfg.get("query_limits", {})
|
| 34 |
+
self.MAX_ROWS = ql.get("max_rows", 100)
|
| 35 |
+
self.MAX_QUERY_TIME = ql.get("max_query_time_seconds", 30)
|
| 36 |
+
self.MAX_JOIN_TABLES = ql.get("max_join_tables", 3)
|
| 37 |
+
|
| 38 |
+
# Pre-cache restricted columns as a lowercase set (used on every column check)
|
| 39 |
+
self._restricted_cols = {c.lower() for c in self.access_cfg.get("restricted_columns", [])}
|
| 40 |
+
|
| 41 |
+
# AI model
|
| 42 |
+
self.model = self.ai_cfg.get("model", os.getenv("LLM_MODEL", "gpt-4o"))
|
| 43 |
+
self.client = OpenAIClient(api_key=os.getenv("OPENAI_API_KEY"))
|
| 44 |
+
|
| 45 |
+
# Database engine
|
| 46 |
+
conn_cfg = self.db_cfg.get("connection", {})
|
| 47 |
+
timeouts = self.db_cfg.get("timeouts", {})
|
| 48 |
+
pool = self.db_cfg.get("pool", {})
|
| 49 |
+
self.db_user = os.getenv("DB_USER")
|
| 50 |
+
self.db_pass = os.getenv("DB_PASSWORD")
|
| 51 |
+
self.db_host = os.getenv("DB_HOST", conn_cfg.get("host", "51.89.104.26"))
|
| 52 |
+
self.db_name = os.getenv("DB_NAME", conn_cfg.get("database", "dev_poly"))
|
| 53 |
+
self.port = conn_cfg.get("port", "3306")
|
| 54 |
+
|
| 55 |
+
self.engine = create_engine(
|
| 56 |
+
f"mysql+pymysql://{self.db_user}:{self.db_pass}@{self.db_host}:{self.port}/{self.db_name}?charset={conn_cfg.get('charset', 'utf8')}",
|
| 57 |
+
connect_args={
|
| 58 |
+
"connect_timeout": timeouts.get("connect_timeout", 30),
|
| 59 |
+
"read_timeout": timeouts.get("read_timeout", 60),
|
| 60 |
+
"write_timeout": timeouts.get("write_timeout", 60),
|
| 61 |
+
},
|
| 62 |
+
pool_pre_ping=pool.get("pool_pre_ping", True),
|
| 63 |
+
pool_recycle=pool.get("pool_recycle", 300),
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Load and filter schema
|
| 67 |
+
print("Loading database schema...")
|
| 68 |
+
schema_cfg = self.db_cfg.get("schema_loading", {})
|
| 69 |
+
raw = self._load_schema(schema_cfg.get("max_retries", 3), schema_cfg.get("retry_delay_seconds", 5))
|
| 70 |
+
self.schema_info = self._filter_schema(raw)
|
| 71 |
+
print(f"Loaded {len(self.schema_info)} accessible tables (from {len(raw)} total).")
|
| 72 |
+
|
| 73 |
+
# ββ Schema ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 74 |
+
|
| 75 |
+
def _load_schema(self, retries=3, delay=5):
|
| 76 |
+
for attempt in range(1, retries + 1):
|
| 77 |
+
try:
|
| 78 |
+
schema = {}
|
| 79 |
+
with self.engine.connect() as conn:
|
| 80 |
+
rows = conn.execute(text(
|
| 81 |
+
"SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE "
|
| 82 |
+
"FROM INFORMATION_SCHEMA.COLUMNS "
|
| 83 |
+
"WHERE TABLE_SCHEMA = :db ORDER BY TABLE_NAME, ORDINAL_POSITION"
|
| 84 |
+
), {"db": self.db_name})
|
| 85 |
+
for r in rows:
|
| 86 |
+
schema.setdefault(r[0], []).append(f"{r[1]} ({r[2]})")
|
| 87 |
+
return schema
|
| 88 |
+
except Exception as e:
|
| 89 |
+
if attempt < retries:
|
| 90 |
+
print(f" β Attempt {attempt}/{retries} failed, retrying in {delay}s...")
|
| 91 |
+
time.sleep(delay)
|
| 92 |
+
else:
|
| 93 |
+
print(f"ERROR: Cannot connect to {self.db_host}:{self.port}/{self.db_name}")
|
| 94 |
+
raise SystemExit(1) from e
|
| 95 |
+
return {}
|
| 96 |
+
|
| 97 |
+
def _filter_schema(self, raw):
|
| 98 |
+
if not self.access_cfg:
|
| 99 |
+
return raw
|
| 100 |
+
filtered = {}
|
| 101 |
+
blocked = 0
|
| 102 |
+
for table, cols in raw.items():
|
| 103 |
+
if not self._table_allowed(table):
|
| 104 |
+
blocked += 1
|
| 105 |
+
continue
|
| 106 |
+
safe = [c for c in cols if self._column_allowed(c.split(" (")[0].strip())]
|
| 107 |
+
if safe:
|
| 108 |
+
filtered[table] = safe
|
| 109 |
+
if blocked:
|
| 110 |
+
print(f" β Blocked {blocked} restricted tables.")
|
| 111 |
+
return filtered
|
| 112 |
+
|
| 113 |
+
def _table_allowed(self, name):
|
| 114 |
+
if not self.access_cfg:
|
| 115 |
+
return True
|
| 116 |
+
t = name.lower()
|
| 117 |
+
for p in self.access_cfg.get("restricted_table_prefixes", []):
|
| 118 |
+
if t.startswith(p.lower()):
|
| 119 |
+
return False
|
| 120 |
+
for p in self.access_cfg.get("allowed_table_prefixes", []):
|
| 121 |
+
if t.startswith(p.lower()):
|
| 122 |
+
return True
|
| 123 |
+
return False
|
| 124 |
+
|
| 125 |
+
def _column_allowed(self, name):
|
| 126 |
+
if not self.access_cfg:
|
| 127 |
+
return True
|
| 128 |
+
return name.lower() not in self._restricted_cols
|
| 129 |
+
|
| 130 |
+
# ββ Security & Limits βββββββββββββββββββββββββββββββββββββββββββββ
|
| 131 |
+
|
| 132 |
+
def _validate_security(self, sql):
|
| 133 |
+
if not self.access_cfg:
|
| 134 |
+
return True, ""
|
| 135 |
+
sql_up = sql.upper()
|
| 136 |
+
for op in ("INSERT", "UPDATE", "DELETE", "DROP", "ALTER", "TRUNCATE", "CREATE"):
|
| 137 |
+
if re.search(rf'\b{op}\b', sql_up):
|
| 138 |
+
return False, f"Write operation '{op}' is not allowed."
|
| 139 |
+
sql_lo = sql.lower()
|
| 140 |
+
for prefix in self.access_cfg.get("restricted_table_prefixes", []):
|
| 141 |
+
if re.search(rf'\b{re.escape(prefix.lower())}\w*\b', sql_lo):
|
| 142 |
+
return False, f"Restricted data ('{prefix}*' tables). Access denied."
|
| 143 |
+
for col in self.access_cfg.get("restricted_columns", []):
|
| 144 |
+
if re.search(rf'\b{re.escape(col.lower())}\b', sql_lo):
|
| 145 |
+
return False, f"Restricted column '{col}'. Access denied."
|
| 146 |
+
return True, ""
|
| 147 |
+
|
| 148 |
+
def _validate_complexity(self, sql):
|
| 149 |
+
sql_up = sql.upper()
|
| 150 |
+
if "CROSS JOIN" in sql_up:
|
| 151 |
+
return False, "CROSS JOIN is not allowed."
|
| 152 |
+
if len(re.findall(r'\bJOIN\b', sql_up)) > self.MAX_JOIN_TABLES:
|
| 153 |
+
return False, f"Too many JOINs (max {self.MAX_JOIN_TABLES}). Simplify your question."
|
| 154 |
+
if re.search(r'SELECT\s+\*', sql_up) and not re.search(r'SELECT\s+COUNT\s*\(\s*\*\s*\)', sql_up):
|
| 155 |
+
return False, "SELECT * is not allowed. Specific columns must be selected."
|
| 156 |
+
has_where = bool(re.search(r'\bWHERE\b', sql_up))
|
| 157 |
+
has_agg = bool(re.search(r'SELECT\s+(COUNT|SUM|AVG|MIN|MAX)\s*\(', sql_up))
|
| 158 |
+
has_group = bool(re.search(r'\bGROUP\s+BY\b', sql_up))
|
| 159 |
+
if not has_where and not has_agg and not has_group:
|
| 160 |
+
return False, "No WHERE clause or aggregation. Add filters to your question."
|
| 161 |
+
return True, ""
|
| 162 |
+
|
| 163 |
+
def _enforce_limit(self, sql):
|
| 164 |
+
sql_up = sql.upper().strip()
|
| 165 |
+
# Skip pure aggregates without GROUP BY
|
| 166 |
+
if re.search(r'^SELECT\s+(COUNT|SUM|AVG|MIN|MAX)\s*\(', sql_up) and not re.search(r'\bGROUP\s+BY\b', sql_up):
|
| 167 |
+
return sql
|
| 168 |
+
m = re.search(r'\bLIMIT\s+(\d+)', sql_up)
|
| 169 |
+
if m:
|
| 170 |
+
if int(m.group(1)) > self.MAX_ROWS:
|
| 171 |
+
sql = re.sub(r'\bLIMIT\s+\d+', f'LIMIT {self.MAX_ROWS}', sql, flags=re.IGNORECASE)
|
| 172 |
+
return sql
|
| 173 |
+
return f"{sql.rstrip()} LIMIT {self.MAX_ROWS}"
|
| 174 |
+
|
| 175 |
+
# ββ Prompt Helper βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 176 |
+
|
| 177 |
+
def _prompt(self, key, **kw):
|
| 178 |
+
t = self.prompts.get(key, "")
|
| 179 |
+
if not t:
|
| 180 |
+
print(f" β WARNING: prompt '{key}' not found in prompts_config.json")
|
| 181 |
+
return ""
|
| 182 |
+
try:
|
| 183 |
+
return t.format(**kw)
|
| 184 |
+
except KeyError as e:
|
| 185 |
+
print(f" β WARNING: missing placeholder {e} in prompt '{key}'")
|
| 186 |
+
return t
|
| 187 |
+
|
| 188 |
+
# ββ LLM Pipeline βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 189 |
+
|
| 190 |
+
def _pick_tables(self, question):
|
| 191 |
+
cfg = self.ai_cfg.get("table_picker", {})
|
| 192 |
+
max_t = cfg.get("max_tables", 5)
|
| 193 |
+
names = list(self.schema_info.keys())
|
| 194 |
+
resp = self.client.chat.completions.create(
|
| 195 |
+
model=self.model,
|
| 196 |
+
temperature=cfg.get("temperature", 0),
|
| 197 |
+
max_tokens=cfg.get("max_tokens", 200),
|
| 198 |
+
messages=[
|
| 199 |
+
{"role": "system", "content": self._prompt("table_picker_system")},
|
| 200 |
+
{"role": "user", "content": self._prompt("table_picker_user",
|
| 201 |
+
db_name=self.db_name, table_list=", ".join(names),
|
| 202 |
+
question=question, max_tables=max_t)},
|
| 203 |
+
]
|
| 204 |
+
)
|
| 205 |
+
picked = [t.strip().strip("'\"` ") for t in (resp.choices[0].message.content or "").split(",")]
|
| 206 |
+
valid = [t for t in picked if t in self.schema_info]
|
| 207 |
+
return valid or names[:max_t]
|
| 208 |
+
|
| 209 |
+
def _generate_sql(self, question, schema_ctx):
|
| 210 |
+
cfg = self.ai_cfg.get("sql_generator", {})
|
| 211 |
+
resp = self.client.chat.completions.create(
|
| 212 |
+
model=self.model,
|
| 213 |
+
temperature=cfg.get("temperature", 0),
|
| 214 |
+
max_tokens=cfg.get("max_tokens", 500),
|
| 215 |
+
messages=[
|
| 216 |
+
{"role": "system", "content": self._prompt("sql_generator_system",
|
| 217 |
+
db_name=self.db_name, max_rows=self.MAX_ROWS, max_join_tables=self.MAX_JOIN_TABLES)},
|
| 218 |
+
{"role": "user", "content": self._prompt("sql_generator_user",
|
| 219 |
+
schema_context=schema_ctx, question=question)},
|
| 220 |
+
]
|
| 221 |
+
)
|
| 222 |
+
sql = (resp.choices[0].message.content or "").strip()
|
| 223 |
+
if "SECURITY_BLOCK" in sql.upper():
|
| 224 |
+
return "SECURITY_BLOCK"
|
| 225 |
+
if "NOT_A_QUERY" in sql.upper():
|
| 226 |
+
return "NOT_A_QUERY"
|
| 227 |
+
sql = sql.replace("```sql", "").replace("```", "").strip()
|
| 228 |
+
if ";" in sql:
|
| 229 |
+
sql = sql.split(";")[0].strip()
|
| 230 |
+
return sql
|
| 231 |
+
|
| 232 |
+
def _execute(self, sql):
|
| 233 |
+
with self.engine.connect() as conn:
|
| 234 |
+
# Try setting query timeout (MariaDB vs MySQL have different syntax)
|
| 235 |
+
try:
|
| 236 |
+
conn.execute(text(f"SET SESSION max_statement_time = {self.MAX_QUERY_TIME}"))
|
| 237 |
+
except Exception:
|
| 238 |
+
try:
|
| 239 |
+
conn.execute(text(f"SET SESSION MAX_EXECUTION_TIME = {self.MAX_QUERY_TIME * 1000}"))
|
| 240 |
+
except Exception:
|
| 241 |
+
pass # Neither supported β LIMIT and row cap still protect us
|
| 242 |
+
result = conn.execute(text(sql))
|
| 243 |
+
cols = list(result.keys())
|
| 244 |
+
batch = result.fetchmany(self.MAX_ROWS + 1)
|
| 245 |
+
rows = [dict(zip(cols, r)) for r in batch[:self.MAX_ROWS]]
|
| 246 |
+
if len(batch) > self.MAX_ROWS:
|
| 247 |
+
print(f" β Capped at {self.MAX_ROWS} rows")
|
| 248 |
+
return cols, rows
|
| 249 |
+
|
| 250 |
+
def _summarize(self, question, sql, cols, rows):
|
| 251 |
+
cfg = self.ai_cfg.get("summarizer", {})
|
| 252 |
+
max_disp = cfg.get("max_display_rows", 50)
|
| 253 |
+
shown = rows[:max_disp]
|
| 254 |
+
result_text = f"Columns: {cols}\nRows ({len(rows)} total"
|
| 255 |
+
if len(rows) > max_disp:
|
| 256 |
+
result_text += f", showing first {max_disp}"
|
| 257 |
+
result_text += "):\n" + "\n".join(str(r) for r in shown)
|
| 258 |
+
|
| 259 |
+
resp = self.client.chat.completions.create(
|
| 260 |
+
model=self.model,
|
| 261 |
+
temperature=cfg.get("temperature", 0.3),
|
| 262 |
+
max_tokens=cfg.get("max_tokens", 2000),
|
| 263 |
+
messages=[
|
| 264 |
+
{"role": "system", "content": self._prompt("summarizer_system", db_name=self.db_name)},
|
| 265 |
+
{"role": "user", "content": self._prompt("summarizer_user",
|
| 266 |
+
question=question, sql=sql, result_text=result_text)},
|
| 267 |
+
]
|
| 268 |
+
)
|
| 269 |
+
return (resp.choices[0].message.content or "").strip()
|
| 270 |
+
|
| 271 |
+
# ββ Main Entry ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 272 |
+
|
| 273 |
+
def ask(self, question):
|
| 274 |
+
try:
|
| 275 |
+
tables = self._pick_tables(question)
|
| 276 |
+
print(f" β Tables: {', '.join(tables)}")
|
| 277 |
+
|
| 278 |
+
schema_ctx = "\n".join(
|
| 279 |
+
f"Table '{t}': {', '.join(self.schema_info[t])}"
|
| 280 |
+
for t in tables if t in self.schema_info
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
sql = self._generate_sql(question, schema_ctx)
|
| 284 |
+
|
| 285 |
+
responses = self.prompts.get("responses", {})
|
| 286 |
+
if sql == "NOT_A_QUERY":
|
| 287 |
+
return responses.get("not_a_query", "I'm DataBot. Ask me about your business data.")
|
| 288 |
+
if sql == "SECURITY_BLOCK":
|
| 289 |
+
return responses.get("security_block", "Access denied: sensitive data requested.")
|
| 290 |
+
|
| 291 |
+
print(f" β SQL: {sql}")
|
| 292 |
+
|
| 293 |
+
ok, reason = self._validate_security(sql)
|
| 294 |
+
if not ok:
|
| 295 |
+
print(f" β BLOCKED: {reason}")
|
| 296 |
+
return responses.get("security_check_fail", "Query blocked: {reason}").format(reason=reason)
|
| 297 |
+
|
| 298 |
+
ok, reason = self._validate_complexity(sql)
|
| 299 |
+
if not ok:
|
| 300 |
+
print(f" β BLOCKED: {reason}")
|
| 301 |
+
return responses.get("complexity_fail", "Query too complex: {reason}").format(reason=reason)
|
| 302 |
+
|
| 303 |
+
sql = self._enforce_limit(sql)
|
| 304 |
+
print(f" β Final: {sql}")
|
| 305 |
+
|
| 306 |
+
cols, rows = self._execute(sql)
|
| 307 |
+
return self._summarize(question, sql, cols, rows)
|
| 308 |
+
|
| 309 |
+
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
return f"DataBot Error: {str(e)}"
|