"""Natural language to SQL engine.""" from app.core.ai import call_ai_json _SYSTEM = """You are an expert SQL engineer and database architect. Convert natural language questions into precise, optimized SQL queries. Always produce correct, runnable SQL. Explain your query so a junior dev can learn. Return ONLY valid JSON — no markdown fences, no preamble. CRITICAL: All JSON string values must be properly escaped. Use \\n for newlines inside strings.""" _PROMPT_TMPL = """Convert this natural language question into SQL. QUESTION: {question} DATABASE SCHEMA: {schema} DIALECT: {dialect} Return JSON with EXACTLY these keys: {{ "sql": "", "explanation": "", "warnings": [""], "alternatives": [ {{"label": "", "sql": "", "trade_off": ""}} ], "sample_result_shape": "" }} If the question is ambiguous, make the most reasonable assumption and note it in warnings. If no schema is provided, generate SQL for a generic table structure that matches the question.""" def whisper(question: str, schema: str, dialect: str = "PostgreSQL") -> dict: prompt = _PROMPT_TMPL.format( question=question[:2000], schema=schema[:4000] if schema else "(no schema provided — infer reasonable table structure)", dialect=dialect ) try: result = call_ai_json([{"role": "user", "content": prompt}], system=_SYSTEM) return result if isinstance(result, dict) else {} except Exception: return {}