Skip to content

About

Privacy-first Natural Language to SQL (NL2SQL) CLI & FastAPI microservice powered by local LLMs (LM Studio, Ollama) with strict read-only SQL validation.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

local-nl2sql

Python FastAPI SQLAlchemy Docker License: MIT

Ask questions about your SQL database in plain English. local-nl2sql is a privacy-first CLI and FastAPI service that inspects your schema and uses a locally hosted, OpenAI-compatible language model to generate SQL. Your prompts and schema stay on your machine when using LM Studio or Ollama locally.

Features

  • Works with SQLAlchemy-supported SQLite, PostgreSQL, and MySQL databases.
  • Inspects tables and columns automatically to give the model schema context.
  • Connects to LM Studio, Ollama, or another OpenAI-compatible endpoint.
  • Offers both a Rich-powered command-line interface and a REST API.
  • Includes a small SQLite sample database for a no-setup first run.

Read-only safety

Generated SQL is checked before it is returned or executed. The validator strips SQL Markdown fences, permits only statements beginning with SELECT or WITH, rejects destructive and administrative keywords (including DROP, DELETE, UPDATE, INSERT, ALTER, TRUNCATE, ATTACH, and PRAGMA), and rejects multiple statements. Unsafe or malformed output is rejected with an error rather than executed.

This application-level validation is defense in depth, not a replacement for database permissions. For sensitive or production data, configure a dedicated database account with read-only access and restrict network access to the service.

Quick start

Requires Python 3.11+ and a local OpenAI-compatible LLM server. For example, start a model in LM Studio's local server or run Ollama with its OpenAI compatible API enabled.

git clone https://github.com/<your-account>/local-nl2sql.git
cd local-nl2sql
python -m venv .venv

Activate the virtual environment, then install and configure:

# macOS/Linux
source .venv/bin/activate

# Windows PowerShell
# .venv\Scripts\Activate.ps1

pip install -r requirements.txt
python seed_db.py
cp .env.example .env

On Windows PowerShell, use Copy-Item .env.example .env instead of cp. The defaults target LM Studio at http://localhost:1234/v1; for Ollama, set LLM_BASE_URL=http://localhost:11434/v1 and update LLM_MODEL in .env to the name of a model available in your Ollama installation.

CLI

Generate a query:

python nl2sql.py "Which department has the largest budget?"

Generate and run a validated query:

python nl2sql.py --execute "What were total sales by region?"

The generated SQL is displayed before any results. The --execute option is required to run it.

FastAPI service

Start the API:

uvicorn api:app --reload
  • Health check: GET http://localhost:8000/health
  • Inspected schema: GET http://localhost:8000/schema
  • Generate SQL: POST http://localhost:8000/query
  • Interactive API docs: http://localhost:8000/docs

Generate a query without executing it:

curl -X POST http://localhost:8000/query \
  -H "Content-Type: application/json" \
  -d '{"question":"Which department has the largest budget?","execute":false}'

Generate and execute a query:

curl -X POST http://localhost:8000/query \
  -H "Content-Type: application/json" \
  -d '{"question":"What were total sales by region?","execute":true}'

Example response:

{
  "sql": "SELECT region, SUM(amount) AS total_sales FROM sales GROUP BY region",
  "columns": ["region", "total_sales"],
  "rows": [["East", 24500], ["North", 31250], ["South", 41000], ["West", 32000]]
}

Database configuration

Set DATABASE_URL in .env to a SQLAlchemy connection URL. The sample database is SQLite:

DATABASE_URL=sqlite:///./sample.db

PostgreSQL and MySQL examples:

DATABASE_URL=postgresql+psycopg://user:password@localhost:5432/database
DATABASE_URL=mysql+pymysql://user:password@localhost:3306/database

The PostgreSQL and MySQL drivers are installed by requirements.txt. Keep credentials out of source control; .env is ignored by Git.

Docker

Seed the sample database and create .env before starting the container:

python seed_db.py
cp .env.example .env
docker build -t local-nl2sql .
docker run --rm -p 8000:8000 --env-file .env \
  -v "$PWD/sample.db:/app/sample.db" local-nl2sql

On Windows PowerShell, replace the copy command with Copy-Item .env.example .env, and use -v "${PWD}\sample.db:/app/sample.db" for the database volume. When the LLM runs on the host, configure its base URL so it is reachable from the container (for example, http://host.docker.internal:1234/v1 on Docker Desktop).

Development

Run the focused safety and SQLite tests with:

python -m unittest discover -s tests -v

License

MIT. See LICENSE.

About

Privacy-first Natural Language to SQL (NL2SQL) CLI & FastAPI microservice powered by local LLMs (LM Studio, Ollama) with strict read-only SQL validation.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages