This project is an agentic enterprise NLQ copilot that converts natural-language business questions into safe, executable SQL over Spider databases.
It targets portfolio alignment with enterprise GenAI roles focused on:
- LLM-powered NLQ
- agent orchestration
- SQL safety guardrails
- execution-grounded responses
- reliability and latency benchmarking
Given a question and db_id, the copilot:
- Retrieves relevant schema context from Spider metadata.
- Generates SQL with an LLM.
- Validates SQL with guardrails (read-only policy, allow-list, row limits, single statement).
- Executes SQL in SQLite read-only mode with timeout protection.
- Returns results plus a grounded explanation and full audit trail.
The LangGraph state machine runs these nodes:
retrieve_schemagenerate_sqlvalidate_sqlexecute_sqlexplainblocked
It retries generation when validation fails, up to a configured retry budget.
Current guardrails enforce:
- read-only SQL only (
SELECT/WITH) - disallowed keyword blocking (
DROP,DELETE,UPDATE, etc.) - single-statement policy
- table allow-list support
- output row limit enforcement
- execution timeout via SQLite progress handler
Default dataset root:
Dataset/spider_data
The project uses:
test_tables.jsonfor schema metadata (206 DBs)test_database/as the primary SQLite DB root- fallback to
database/if needed
For free cloud deployment, this repo includes a lightweight Spider-compatible demo subset at:
data/spider_data
python -m pip install -e .Optional dev dependencies:
python -m pip install -e ".[dev]"Set your OpenAI key for agent mode:
set OPENAI_API_KEY=your_key_hereOptional environment overrides are in .env.example.
The project now includes a production-style FastAPI service with OpenAPI docs.
Run API locally:
python -m pip install -e ".[dev]"
nlq-copilot-apiOr:
python -m uvicorn copilot.api:app --host 0.0.0.0 --port 8000Key URLs:
GET /service metadataGET /healthreadiness/livenessGET /docsinteractive Swagger UIGET /redocReDoc docsGET /v1/databaseslist Spider DBsGET /v1/databases/{db_id}/schemaschema detailsPOST /v1/queryNLQ -> SQL -> safe executionPOST /v1/safetyrun guardrail checksPOST /v1/benchmarkrun evaluation job
Example query request:
curl -X POST "http://localhost:8000/v1/query" ^
-H "Content-Type: application/json" ^
-d "{\"db_id\":\"concert_singer\",\"question\":\"How many singers are there?\"}"This project deploys free with a split architecture:
- Render (FastAPI backend)
- Netlify (static frontend +
/api/*proxy)
Deployment config files included:
render.yamlnetlify.tomlweb/(Netlify frontend)DEPLOYMENT.md(step-by-step guide)
- Create Web Service from this repo.
- Render will auto-use
render.yaml. - Add secret
OPENAI_API_KEY. - Set dataset env for demo mode:
SPIDER_DATASET_ROOT=data/spider_data
- Verify:
/health/docs
- Import this repo in Netlify.
- Publish directory:
web - In
netlify.toml, set redirect target to your Render URL.
Then open your Netlify site and run queries from the UI.
python -m copilot ask --db-id world_1 --question "List the names of all countries in Europe"Agent mode (LLM-generated SQL):
python -m copilot benchmark --split dev --mode agent --limit 100Oracle mode (gold SQL forced through the same guardrails/execution path):
python -m copilot benchmark --split dev --mode oracle --limit 100Benchmark artifacts are saved under outputs/ as JSON + CSV.
python -m copilot safetyThe benchmark outputs:
- execution accuracy (denotation comparison)
- blocked query rate
- latency (
mean,p50,p95,max) - safety suite pass rate
src/copilot/
agent.py # LangGraph orchestration
benchmark.py # Spider benchmark harness
cli.py # CLI entrypoint
config.py # Runtime config
executor.py # SQLite read-only execution with timeout
guardrails.py # SQL validation and safety policies
llm.py # OpenAI LLM adapter and fallback client
reliability.py # Safety suite + latency stats
schema_retriever.py
spider.py # Spider loaders and schema model
tests/
- For strict reproducibility and model-free checks, use
--mode oracle. - In offline mode, pass
--no-llmand use oracle benchmark/safety checks.