Skip to content
Raisalon

Generative AI · Planned

Text-to-SQL analytics assistant with guardrails

An assistant that answers business questions in plain English by writing SQL against a real schema, with read-only access, query validation, and an execution-accuracy benchmark.

Planned project. This page describes the goal, the approach, and how the system will be evaluated. Code, a live demo, and results will be published here once it is built.

At a glance

Solo project: schema context, SQL generation, validation, and API

Problem
Let non-technical teams ask questions of a production database without writing SQL, and without risking data or wrong numbers.
Planned approach
Schema descriptions and example queries in the prompt, LLM-generated SQL parsed and checked with SQLGlot, and execution on a read-only replica with row and time limits.
Design decisions
  • A read-only database role and a parser check that rejects anything but a single SELECT, so a bad generation can never modify data.
  • Every answer shows the SQL that produced it, so analysts can verify the numbers instead of trusting them blindly.
  • Questions the schema cannot answer get a clear refusal instead of a plausible but invented query.
Evaluation plan
Execution accuracy on a labelled question set (do the results match a hand-written query?), the refusal rate on unanswerable questions, and latency per answer.

Goal

Answer questions such as “Which ten customers had the biggest drop in orders last quarter?” by generating SQL against a real schema, running it safely, and showing both the answer and the query. The hard part is not producing SQL. It is producing correct SQL, refusing safely when the schema cannot answer, and never risking the data.

Planned approach

  1. Schema context. Give the model table and column descriptions, relationships, and a few example question–query pairs, instead of the raw schema alone.
  2. Generation. Ask the model for a single SQL query in a structured response.
  3. Validation. Parse the query with SQLGlot, reject anything that is not a single SELECT, and check that every table and column exists.
  4. Execution. Run it with a read-only role on a replica, with a statement timeout and a row limit.
  5. Serving. A FastAPI endpoint that returns the answer, the SQL, and a short explanation.

Evaluation plan

  • Execution accuracy: whether the generated query returns the same result as a hand-written reference query, which is stricter and more meaningful than comparing SQL text.
  • Refusals: the share of unanswerable questions correctly refused.
  • Safety tests: prompts that try to make the assistant run writes or read restricted tables, which must all be rejected. The controls follow defending LLM apps against prompt injection.

Definition of done

A public repository, a demo on a sample database, a results table for execution accuracy and refusals, and a list of the question types that still fail.

All projects