Chat-to-SQL: Ask your database in plain language (technical guide)
1 February 2025 · 12 min
What is Chat-to-SQL?
Chat-to-SQL is an architecture that combines Retrieval-Augmented Generation (RAG) with language models to translate questions in plain language into executable SQL queries.
It’s not magic: it’s applied engineering with a proven stack.
System architecture
User → Question in plain language
↓
Catalog embedding (pgvector)
↓
Relevant context + Schema
↓
LLM generates SQL
↓
Validation + Execution
↓
Response in natural language
Technical stack
- PostgreSQL 16 with
pgvectorextension for embeddings - FastAPI as inference API
- LangChain for RAG pipeline orchestration
- Fine-tuning of the model with the client-specific catalog
Accuracy and limitations
In our deployments, the system achieves:
- 94% accuracy on inventory queries
- 87% accuracy on complex financial queries
- < 2 seconds average latency
Queries that fail are usually ambiguous or refer to data that doesn’t exist in the schema. The system learns with each correction.
Typical implementation: 10 days to pilot
- Days 1-5: ERP connection, schema extraction, embedding generation
- Days 6-10: Validation with real data, adjustments, and scoped pilot deployment