Skip to content

prathams0ni/QueryMind_LLM-Powered_Natural_Language_to_SQL_Engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

25 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  QueryMind: LLM-Powered Natural Language to SQL Engine

๐Ÿš€ Project Overview

QueryMind is a production-style Natural Language to SQL engine that transforms human language into secure, executable SQL queries using a locally hosted Large Language Model (Mistral via Ollama). Unlike basic NL-to-SQL demos, QueryMind is designed as a complete system with real-world architectural considerations including:

  • Dynamic multi-database discovery
  • Schema-grounded prompt engineering
  • Secure query execution layer
  • Self-healing SQL correction loop
  • Modular backend design
  • Real-time visualization

The application directly connects to the MySQL server and automatically detects all available databases. Any database present in MySQL Workbench will appear in the UI without hardcoding.


image
image
image
image

Users can:

โ€ข Select any database
โ€ข Explore available tables
โ€ข View complete schema (columns + data types)
โ€ข Ask questions in plain English
โ€ข Generate SQL queries
โ€ข Execute them safely
โ€ข Automatically correct query errors
โ€ข Visualize results instantly

This system simulates how AI-powered analytics tools would function in a real enterprise environment.


๐Ÿ—๏ธ System Architecture Philosophy

QueryMind is not just a UI wrapper around an LLM. It implements:

1๏ธโƒฃ Schema Grounding

The selected database schema is injected into the LLM prompt before SQL generation. This:

  • Reduces hallucinations
  • Improves JOIN accuracy
  • Enables multi-table reasoning
  • Increases execution success rate

2๏ธโƒฃ Secure Execution Guard

Before executing generated SQL:

  • Only SELECT statements are allowed
  • Destructive operations (DELETE, UPDATE, DROP) are blocked
  • Query is cleaned and validated

This ensures database safety.

3๏ธโƒฃ Self-Healing SQL Engine

If a query fails:

  1. The database error message is captured
  2. Error + original query + schema are sent back to the LLM
  3. A corrected query is generated
  4. The system retries execution automatically

This creates a resilient AI-assisted query engine.


๐Ÿ—๏ธ Pipeline Architecture

QueryMind follows a structured, multi-layer AI execution pipeline designed for safety, resilience, and schema grounding.

Stage Layer Component Responsibility Key Output
1 User Interface Layer Streamlit UI (app.py) Captures user natural language input and selected database User query + selected DB
2 Metadata Layer db.py Dynamically fetches databases, tables, and schema from MySQL Structured schema metadata
3 Context Engineering Layer prompt_builder.py Injects schema + user question into structured LLM prompt Schema-grounded prompt
4 AI Reasoning Layer llm.py (Ollama + Mistral) Converts natural language into SQL query Generated SQL
5 Security Validation Layer executor.py (Pre-check) Validates SQL (SELECT-only guard, sanitization) Safe executable SQL
6 Execution Layer MySQL Connector Executes validated SQL against selected database Query result / error
7 Resilience Layer Auto-Correction Loop If error occurs โ†’ feeds error + schema back to LLM Corrected SQL
8 Data Processing Layer Pandas Formats results into structured dataframe Clean dataset
9 Visualization Layer Streamlit Charts Auto-detects numeric columns & generates visualization Interactive chart
10 Presentation Layer UI Rendering Displays SQL, explanation (optional), result table, chart Final user output

๐Ÿ” Error Recovery Sub-Pipeline

When execution fails, QueryMind activates a secondary correction loop:

Step Action Description
1 Capture Error MySQL execution error message is extracted
2 Context Packaging Error + Original SQL + Schema bundled
3 Regeneration Mistral generates corrected SQL
4 Re-validation SELECT-only guard re-applied
5 Re-execution Corrected query executed
6 Final Response Success result or final error displayed

This creates a self-healing AI SQL engine.


๐Ÿง  Layered Architectural Model

QueryMind follows a multi-layer architecture:

Architecture Layer Purpose
Presentation Layer User interaction & UI
Metadata Layer Schema discovery & grounding
AI Reasoning Layer Natural language โ†’ SQL conversion
Security Layer Query validation & safety
Execution Layer Database interaction
Resilience Layer Error correction loop
Analytics Layer Result processing & visualization

๐Ÿ” Security & Governance Controls

Control Implementation
Query Restriction Only SELECT statements allowed
SQL Sanitization Markdown & formatting cleaned before execution
Schema Grounding Reduces hallucinated table references
Error Feedback Loop Controlled retry mechanism
Local LLM Execution No external API exposure

๐Ÿ“Š End-to-End Data Flow Summary

Input Transformation Output
Natural Language Schema-Grounded Prompt SQL Query
SQL Query Validation & Execution Data Result
Data Result Pandas Formatting Structured Table
Structured Table Auto Visualization Chart Output

๐Ÿš€ Architectural Strengths

โœ” Modular separation of concerns
โœ” AI reasoning isolated from execution layer
โœ” Built-in safety constraints
โœ” Automatic retry resilience
โœ” Dynamic database discovery
โœ” Local LLM inference (privacy-safe)


๐Ÿ”ฅ Engineering Insight

QueryMind is designed as a layered AI system rather than a simple LLM wrapper.

It combines:

  • Prompt Engineering
  • Secure Systems Design
  • Database Metadata Abstraction
  • Error-Driven Regeneration
  • Real-Time Data Visualization

into a cohesive pipeline.


๐Ÿง  AI Model Details

Model Used: Mistral
Runtime: Ollama
Deployment: Local

Why Local LLM?

  • No API cost
  • Full data privacy
  • Fast inference
  • Offline capability

๐Ÿ—‚ Modular Architecture

The project follows a clean modular design:

โ€ข app.py โ€“ UI & orchestration
โ€ข db.py โ€“ Database metadata retrieval
โ€ข executor.py โ€“ Secure query execution layer
โ€ข prompt_builder.py โ€“ Schema-aware prompt generation
โ€ข llm.py โ€“ Ollama (Mistral) integration
โ€ข config.py โ€“ Database configuration

This separation improves maintainability and scalability.


๐Ÿ“Š Real-World Applications

QueryMind can be extended into:

โ€ข AI-powered BI dashboards
โ€ข Conversational analytics tools
โ€ข Enterprise SQL copilots
โ€ข Internal data exploration assistants
โ€ข Educational SQL tutors


๐Ÿ’ผ Who Benefits From This?

๐ŸŽ“ Students

Learn SQL interactively using natural language.

๐Ÿ“Š Data Analysts

Accelerate query writing and aggregation.

๐Ÿ‘จโ€๐Ÿ’ป Developers

Prototype complex joins quickly.

๐Ÿข Organizations

Enable non-technical teams to query databases conversationally.


๐Ÿงฉ Key Engineering Strengths

โœ” Multi-database support
โœ” Schema-aware LLM grounding
โœ” Self-healing SQL retry mechanism
โœ” Secure execution layer
โœ” Modular architecture
โœ” Local AI inference
โœ” Automatic visualization
โœ” Production-style workflow


๐Ÿš€ What Makes This Project Stand Out?

Most NL-to-SQL demos:

  • Hardcode schema
  • Lack safety guard
  • Have no retry mechanism
  • Do not handle real DB environments

QueryMind addresses these limitations and simulates a real-world AI SQL assistant system.


๐Ÿ”ฎ Future Scope

  • PostgreSQL support
  • Query optimization feedback
  • Conversational memory
  • Role-based access control
  • Cloud deployment
  • LLM fine-tuning on SQL datasets

๐Ÿ“‚ Project Structure

QueryMind/
โ”‚
โ”œโ”€โ”€ app.py              # Streamlit UI (Main Application)
โ”œโ”€โ”€ db.py               # Database connection & metadata retrieval
โ”œโ”€โ”€ executor.py         # Secure SQL execution layer
โ”œโ”€โ”€ llm.py              # Ollama (Mistral) integration
โ”œโ”€โ”€ prompt_builder.py   # Schema-grounded prompt logic
โ”œโ”€โ”€ config.py           # Database configuration
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ SQL Generator App.bat
โ””โ”€โ”€ README.md

๐Ÿ”ฅ Core Features

1๏ธโƒฃ Multi-Database Detection

QueryMind dynamically fetches all databases from MySQL server.

Any database present in MySQL Workbench automatically appears in the UI.


2๏ธโƒฃ Table & Schema Explorer

Left-side panel shows:

  • Database selector
  • Table list
  • Expandable schema
  • Column names + data types

Improves query grounding and reduces hallucination.


3๏ธโƒฃ Natural Language to SQL

Example:

Input:

Show total sales by country

Generated SQL:

SELECT c.country,
       SUM(od.quantityOrdered * od.priceEach) AS total_sales
FROM customers c
JOIN orders o ON c.customerNumber = o.customerNumber
JOIN orderdetails od ON o.orderNumber = od.orderNumber
GROUP BY c.country;

4๏ธโƒฃ Self-Healing SQL Engine

If execution fails:

  • Database error captured
  • Error sent back to Mistral
  • Corrected query generated
  • Query automatically retried

5๏ธโƒฃ Secure Execution Layer

  • Only SELECT queries allowed
  • Blocks DELETE / UPDATE / DROP
  • Prevents destructive operations

6๏ธโƒฃ Query Explanation Mode

Optional toggle to understand:

  • JOIN logic
  • Aggregations
  • Query structure

7๏ธโƒฃ Automatic Visualization

  • Detects numeric columns
  • Auto-generates charts
  • Works for aggregation queries

๐Ÿš€ How To Run

1๏ธโƒฃ Start Ollama

ollama run mistral

2๏ธโƒฃ Install Dependencies

pip install -r requirements.txt

3๏ธโƒฃ Run Application

streamlit run app.py

Or double-click:

SQL Generator App.bat

๐Ÿ“ฌ Contact Details

For queries, contributions, or collaboration opportunities, feel free to reach out:

๐Ÿ“ง Email: prathamsoni1128@gmail.com

๐Ÿ”— LinkedIn: https://www.linkedin.com/in/pratham-soni-600787268/

๐Ÿ’ป GitHub: https://github.com/prathams0ni

QueryMind is more than a Natural Language to SQL tool โ€”
it represents a practical implementation of schema-aware AI reasoning, secure database interaction, and self-healing system design.

Turning databases into intelligent, conversational systems โ€” one query at a time.

โญ If you found this project interesting, consider giving it a star and connecting!

About

QueryMind is a production-grade Natural Language to SQL system powered by Large Language Models (LLMs), designed to translate human language queries into optimized SQL statements with intelligent error correction and self-healing execution capabilities.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors