File size: 1,814 Bytes
950dcd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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": "<the complete SQL query, formatted with proper indentation>",
  "explanation": "<step-by-step explanation of what the query does and why>",
  "warnings": ["<any potential issues, e.g. missing indexes, large scans>"],
  "alternatives": [
    {{"label": "<alternative approach name>", "sql": "<alternative SQL>", "trade_off": "<when to use this instead>"}}
  ],
  "sample_result_shape": "<description of what the result set looks like, e.g. 'Returns rows with columns: user_id, name, total_orders'>"
}}

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 {}