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.
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.
Only the tables and columns that match the question are pulled from the dbt docs into the prompt.
The model writes SQL against that schema, guided by a handful of worked examples.
The query is parsed, limited to SELECT and capped in rows, then run on a read-only connection.
The answer, the table, a chart and the SQL itself come back together.
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))Answer(sql=..., rows=..., chart=...)Every answer carries the query that produced it, so a wrong number can be traced in seconds.
GRANT SELECT ON ALL TABLESA database user with SELECT rights only, plus a parser check, so a bad prompt cannot change data.
eval/questions.yamlA fixed set of questions with known answers runs on every prompt change before it ships.
Kafka events scored by a Spark streaming job and a gradient-boosted model, with risky payments sent to review.