Back to Projects
aiFeatured Project

Query Builder LLM

Natural language to SQL/MongoDB queries with RAG

Tech Stack

PythonLangChainOllamaPostgreSQLMongoDBpsycopg2pymongopython-decouple

2

Databases

87%

Accuracy

1

Stars

Problem Statement

Non-technical stakeholders spend hours trying to extract database insights, requiring constant support from data teams. Traditional BI tools require training and don't support natural language queries across SQL and NoSQL databases.

Overview

An intelligent database query interface that converts natural language to SQL and MongoDB queries using LLMs, executes them against production databases, and provides natural language interpretation of results. Built with LangChain and local Ollama models for privacy and cost efficiency.

My Role & Contributions

Backend Developer & ML Engineer - Designed NL-to-query pipeline with RAG, implemented dynamic schema introspection for SQL and NoSQL, built LangChain integration with Ollama, created query validation and safety checks.

Tech Stack

PythonLangChainOllamaPostgreSQLMongoDBpsycopg2pymongopython-decouple

Challenges & Solutions

1

Challenge

Handling complex multi-table SQL JOINs and nested MongoDB aggregation pipelines from ambiguous natural language with 87% accuracy

Solution

Built two-stage LLM approach: (1) Intent classifier determines DB type + operation, (2) Specialized agents with few-shot examples (5 per DB) generate queries

2

Challenge

Implementing dynamic RAG system that adapts to schema changes (new tables/fields) without retraining or manual prompt updates

Solution

Architected schema introspection with caching: PostgreSQL information_schema + MongoDB collection stats → embedded in RAG context with FAISS retrieval

3

Challenge

Preventing SQL injection, destructive operations (DROP/DELETE), and unauthorized data access while maintaining query flexibility

Solution

Implemented query AST parsing with sqlparse/pymongo validation, whitelist-only operations, read-only DB connections, row limits (1000), and audit logs to Postgres