Turn natural language questions into executable SQL against your local SQLite database. This project uses LangChain with local Ollama models and runs on the ultra-fast uv Python package manager.
- Automatic schema extraction from
amazon.db - Natural language to SQL using local LLM (Ollama)
- Deterministic SQL generation (
temperature=0) - Easily switch models (e.g. DeepSeek-R1 vs QwenCoder)
- Simple Python API (
get_data_from_database(prompt)) for integration - Fast environment setup and dependency management via
uv
- Python 3.11+
- SQLite (
amazon.db) - SQLAlchemy for schema introspection
- LangChain Core + Community + Ollama integration
- Ollama local models (recommend:
qwen2.5-coder:7bfor speed) uvfor dependency resolution, syncing, and running
- Extract schema using SQLAlchemy inspector
- Feed schema + user question to an LLM prompt template
- Model returns a SQL query (reasoning models may include
<think>blocks – we strip or avoid them) - Execute SQL safely against
amazon.db - Return results list
main.py
└─ extract_schema() -> text_to_sql() -> get_data_from_database()
- Install Ollama: https://ollama.com/download
- Pull a model (choose one):
- Fast coder model:
ollama pull qwen2.5-coder:7b
- Reasoning model (slower):
ollama pull deepseek-r1:8b
- Fast coder model:
- Ensure
amazon.dbexists in the project root.
No need to manually create a virtual environment—uv handles it.
# Install uv if not present
curl -LsSf https://astral.sh/uv/install.sh | sh
# Install dependencies from pyproject.toml
uv sync
# Run a module or script
uv run python main.pyTo add a new dependency:
uv add package_nameTo upgrade dependencies:
uv lock --upgrade
uv syncPython API example:
from main import get_data_from_database
results = get_data_from_database("Show the first 5 products")
print(results)Direct script run (if you extend main.py with CLI later):
uv run python main.pyEdit main.py:
model = OllamaLLM(model="qwen2.5-coder:7b", temperature=0)If you use DeepSeek-R1 models and see long delays, they generate hidden reasoning. Mitigations:
- Add: "Do not use tags." to system prompt
- Strip with regex:
re.sub(r"<think>.*?</think>", "", raw, flags=re.DOTALL) - Prefer a non-reasoning coder model for performance
Before execution you can add a lightweight validator:
if not sql_query.lower().startswith("select"):
raise ValueError("Only SELECT queries are allowed.")Add more guards (block DROP/DELETE/UPDATE) for safety.
- Use smaller local models (
qwen2.5-coder:7b) for snappy responses - Keep
temperature=0for deterministic output - Cache schema: avoid recomputing
extract_schemaeach call - (Advanced) Stream model output and stop at first semicolon
;if the model over-explains
| Issue | Cause | Fix |
|---|---|---|
| Long response time | Reasoning model generating chains | Switch to coder model |
| SQL errors (no such column) | Model hallucinated | Strengthen prompt, show schema clearly |
| Empty results | Query valid but data missing | Inspect amazon.db contents |
| Ollama connection error | Service not running | Run ollama serve or open app |
Executing arbitrary LLM-generated SQL can be risky. Restrict to read-only queries and sanitize user inputs if you later interpolate values.
- Fork & branch
- Make changes
- Run
uv sync && uv run python -m py_compile main.py - Submit PR
MIT
Built with local AI + fast Python tooling.
