Natural-Language Analytics Assistant with Guarded Text-to-SQL

Build a natural-language analytics assistant that grounds a local open-weight model in governed definitions, checks every generated SQL statement before it runs, lets the database enforce each reader's access, and measures answers by comparing query results.

Generative AI Engineer. Advanced.

What you will build and explain

  • Explain the ways a language model writing SQL goes wrong, and which layer of a design handles each.
  • Ground a model in a semantic layer of governed definitions and reviewed examples, within a prompt budget it can cache.
  • Serve an open-weight model locally with llama.cpp and constrain its output to a JSON schema, knowing what that guarantees and what it does not.
  • Check generated SQL with a syntax tree, allow-lists, a row limit and a plan-cost ceiling, and explain why text matching is not a checker.
  • Enforce data access in PostgreSQL with roles, row-level security, invoker-rights views and column privileges, so model output never decides it.
  • Design an assistant's endings: one repair with the reason, clarification, declines, permission refusals and honest failures.
  • Keep a generated summary honest by checking every number in it against the query result.
  • Evaluate text-to-SQL by comparing results with gold queries across dev, held-out and adversarial splits, with hard gates, and operate the assistant behind that gate.

Before you begin

  • Write Python functions, classes and pytest tests, and SQL SELECT statements with joins, GROUP BY and date filters.
  • Use a terminal, Git and Docker Compose, and read HTTP requests and JSON responses.
  • A computer with 4 or more CPU cores (8 recommended), 16 GB of memory and 15 GB of free disk, running Linux, macOS on Apple silicon or Windows with WSL 2.
  • Language-model serving, quantized models, constrained decoding, SQL syntax trees, PostgreSQL roles and row-level security, and evaluation design are taught in the project.

Who this is for

For developers who write Python and SQL with joins and use Docker Compose, and want to let people ask a data warehouse questions in plain English with a local model, without letting the model decide what anyone may see.

Curriculum

Learning topics (12)
  1. Check what you need before the first command

    This project builds an assistant that answers questions about a retailer's sales in plain English, by writing SQL with a model that runs on your own computer. You do not need any background in language models, model serving or database security: the lessons teach those. You do need a few skills and a computer that can hold the model. This page says exactly which, and how to check each one before you start.

  2. Follow a question through the assistant

    "Which North stores had the largest drop in net sales last month?" sounds like one query. Answering it safely needs four decisions the question does not state: what net sales means, which dates last month covers, which stores this person may see, and whether the SQL a model writes is safe to run. This page shows where each of those decisions is made, follows one question from the browser to the answer, and then covers how the system is changed, operated and recovered.

  3. See what goes wrong when a model writes SQL

    "Text-to-SQL" means turning a question in ordinary language into a SQL query and running it. Modern code models are good at it: give one a table definition and "total sales by region last month" and you will usually get a sensible query. The word "usually" is the subject of this lesson. Before building anything, it helps to list the specific ways an assistant like this goes wrong, because each part of the design exists to catch one of them, and the evaluation is built to meas

  4. Give the model governed definitions

    Ask three people at Harbourline Outfitters for "sales last month" and you can get three numbers. The store manager means what went through the tills. Finance means revenue after discounts and refunds, on completed orders only. Someone else is thinking of the fiscal month, which at Harbourline does not line up with the calendar month. None of them is wrong; they are using different definitions.

  5. Serve an open-weight model and constrain its output

    The assistant needs a model that reads a question and the semantic layer and writes one PostgreSQL query. This project runs that model on your own computer, on the CPU, with no API key and no data leaving the machine. This lesson explains what is actually running when the llm container starts, how a request becomes tokens and tokens become JSON, and what the JSON schema in each request does and does not promise.

  6. Assemble the prompt and retrieve verified examples

    The semantic layer tells the model what the data means. That is often not enough for a 7B model to write the SQL Harbourline expects: which join to use for "fiscal quarter", how to compare two months side by side, how to name the output columns. People learn those conventions from examples, and so do models. This lesson builds the full prompt the assistant sends for one question: the instructions, the definitions, a handful of reviewed examples found for this question, and th

  7. Check the SQL before it runs

    The model has returned a well-formed decision with "action": "query" and a SQL string. That string came from a language model that read untrusted text. It may be exactly right, subtly wrong, or something nobody asked for: a second statement after a semicolon, a DELETE, a call to pg_sleep(30), a query against the customer table. This lesson builds the check that every statement passes before the database sees it, and explains why it parses the SQL instead of searching the text

  8. Let the database decide who sees what

    The North manager asks, "What were Coast net sales last month?" The checker passes the model's SQL: it reads sales_lines, an allowed view, with allowed functions. Should the assistant answer?

  9. Repair once, ask, or decline

    An assistant that always produces a number is dangerous: some questions are ambiguous, some cannot be answered from this data, and some first attempts are simply wrong. This lesson follows a question through the decision loop in nlq/pipeline.py and explains each way it can end: an answer, a clarifying question, a decline, a permission refusal, a bounded repair, or a failure that says so. The point is that every ending is distinct, so the reader, the logs and the evaluation ca

  10. Narrate only what the result supports

    A table of numbers answers the question, but readers like a sentence: "North had the highest net sales in July, 182,410.55." The same model that wrote the SQL can write that sentence from the result. It can also write "North's sales doubled", when the result says nothing about the previous month, or round 182,410.55 to "about 190,000". The sentence would be the first thing the reader reads and the least checked thing on the page.

  11. Measure the assistant with gold queries

    You change one line of the semantic layer, or swap the model for a newer one, or add ten curated examples. Is the assistant better or worse? Trying a few questions by hand tells you how it did on those questions, today, as you remembered to check them. This lesson builds the measurement the project relies on instead: versioned sets of questions with known answers, run through the real assistant, compared by result, with gates that a change must pass.

  12. Operate the assistant: identity, logs, review and change

    The earlier lessons follow one question through the assistant. Running it for real readers adds the questions an operator asks: who is calling, what is safe to write in a log, how answers are reviewed and how good ones improve later answers, how many readers one machine can serve, and how to change the model or the definitions without finding out in production that answers got worse. This lesson covers each, as this project implements it, and says where a production deploymen

Practical build steps (10)
  1. Prepare and check your computer

    Starting point: nothing yet. End point: you know your computer has the cores, memory, disk, tools and free port the lab needs.

  2. Build the image and run the unit tests

    Starting point: a checked computer and the source folder. End point: the assistant image exists and its unit tests and linters pass, with networking turned off.

  3. Create the secrets, data and model

    Starting point: the image. End point: state/ holds the passwords and reader tokens, data/ the synthetic warehouse, and models/ the verified model file.

  4. Start the stack

    Starting point: state, data, model and image. End point: PostgreSQL, the model server and the assistant are running and healthy on one Compose network, with only the assistant published on your loopback interface.

  5. Ask questions as each reader

    Starting point: a healthy stack. End point: you have seen the same question answered with different rows for different readers, and questions that end in a clarification, a decline or a refusal.

  6. Prove the database boundary

    Starting point: a healthy stack. End point: the stack tests have shown, against the real database, that access is enforced whatever SQL arrives.

  7. Evaluate the assistant

    Starting point: a healthy stack. End point: reports that say how often the assistant is right, by category, for this model and these definitions, and whether it passes its gates.

  8. Review and curate an answer

    Starting point: answered requests in the database. End point: one answer you have checked is in the example library, where later similar questions retrieve it.

  9. Make and ship a change

    Starting point: a working stack and a report for each split. End point: you have added a metric, measured it on the dev and held-out splits, and either kept it or gone back.

  10. Clean up

    Starting point: the lab, finished. End point: nothing of this project remains on your computer except the source folder.

Access

Learning Access: INR 199 plus 18% GST. Source Pack: INR 399 plus 18% GST. 90 days of online access. No auto-renewal.

Cloud costs and boundaries

Runs entirely on your computer with Docker Compose: about 6 GB of downloads (a 4.7 GB model file and three container images) and a synthetic warehouse generated locally. No cloud account, API key, payment method or hosted lab is used or included. The model runs on your CPU; the timings quoted in the lessons were measured on an 8-vCPU Graviton cloud virtual machine, and reproducing them on similar hardware is optional and at your own cost.

Licence and completion

Vectametric Project Source Licence (2026-09-27): lawfully obtained source may be copied, modified and deployed for personal learning and interview preparation, including personal portfolio demonstrations with attribution. These permissions continue after online access expires. Do not resell or redistribute the Source Pack or its original source files, unchanged or modified. Commercial or client production use requires separate written permission. The model file, container images and Python packages downloaded when building or running, such as Qwen2.5-Coder, llama.cpp, PostgreSQL, FastAPI, psycopg and sqlglot, retain their own licences. See LICENSE.txt and the Project Source Licence section of the site Terms for the full rules.

Questions about projects

Do I need a cloud account?

You can read the project material without deploying it. Running a cloud build requires the accounts, permissions and tools listed in that project's prerequisites. Deployment runs in your environment, not in a hosted Vectametric lab.

Can I read the code with Learning Access?

Yes. Learning Access includes the online source-file browser and explanations. The Source Pack adds the downloadable reference repository where available. The project's licence governs use of the source.

Are cloud charges included?

No. Cloud and CI usage are separate. Read the project's cost notice, set a budget and stop deadline, and follow its destruction and leftover-resource checks.

Does the project include troubleshooting and interview preparation?

The project curriculum lists the available scenarios and interview topics. These focus on understanding the architecture and explaining decisions; they are not a promise of particular interview questions or a job.

How long can I use the project?

The project page and checkout show the online access period for your selected plan. Access does not auto-renew. Downloaded source remains subject to its licence; future releases are not automatically included.

Review the Terms and No-Refund Policy before confirming access. All sales are final; Vectametric does not offer refunds.

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy