Plain-English questions over a warehouse

A personal project: ask a question in plain words and get a SQL query, a table and a chart back, with the query always on screen so the answer can be checked.

Type
Personal project
Stack
Python, an LLM API, dbt docs, DuckDB, FastAPI
Focus
Data science and LLMs

How it works

  1. Find the right tables

    Only the tables and columns that match the question are pulled from the dbt docs into the prompt.

  2. Write the query

    The model writes SQL against that schema, guided by a handful of worked examples.

  3. Check before running

    The query is parsed, limited to SELECT and capped in rows, then run on a read-only connection.

  4. Show the work

    The answer, the table, a chart and the SQL itself come back together.

answer.py
def answer(question: str) -> Answer:
    tables = retrieve_schema(question, top_k=6)
    sql = write_sql(question, tables)
    check_read_only(sql)        # rejects anything but SELECT
    rows = duckdb.sql(sql).df()
    return Answer(sql=sql, rows=rows,
                  chart=pick_chart(rows))

Decisions I made. Each one is a trade-off, written down.

Answer(sql=..., rows=..., chart=...)

The SQL is always shown

Every answer carries the query that produced it, so a wrong number can be traced in seconds.

GRANT SELECT ON ALL TABLES

Read-only by construction

A database user with SELECT rights only, plus a parser check, so a bad prompt cannot change data.

eval/questions.yaml

An evaluation set first

A fixed set of questions with known answers runs on every prompt change before it ships.

What I would add next

  • Follow-up questions that keep context
  • Cost and latency logged per question
  • Feedback buttons that grow the evaluation set

Next project: Flags risky card payments as they stream in

Kafka events scored by a Spark streaming job and a gradient-boosted model, with risky payments sent to review.