โ† Back to Projects

AI SQL Assistant

Ask your database questions in plain English โ€” get safe, accurate SQL queries generated and executed instantly.

LangChain Ollama Streamlit MySQL Python sqlparse
SQL Assistant Overview

๐Ÿ“ธ Screenshots

๐Ÿ’ก The Problem

Non-technical users โ€” business analysts, managers, product teams โ€” need to query databases daily but don't know SQL. Existing solutions are either cloud-based (data privacy concerns) or lack safety guardrails, making it risky to let an AI execute arbitrary queries against production databases.

๐ŸŽฏ The Solution

A fully local AI assistant that runs on your machine โ€” no data leaves your network. Users type questions in plain English, the LLM generates SQL, and a tiered safety system classifies, validates, and optionally blocks dangerous queries before they ever touch the database.

โšก Key Features

๐Ÿ—ฃ๏ธ

Natural Language to SQL

Type questions like "Show all employees in engineering" and get precise SQL generated automatically.

๐Ÿ›ก๏ธ

Tiered Safety Framework

Destructive queries (DROP, TRUNCATE, DELETE without WHERE) are automatically blocked. Write operations require confirmation.

โœ…

Confirmation Workflow

UPDATE/INSERT operations show the affected row count and ask for explicit approval before execution.

๐Ÿ“

Audit Trail

Every query attempt โ€” allowed, blocked, or confirmed โ€” is logged with timestamps for full accountability.

๐Ÿ”’

Read-Only Mode

One toggle to restrict all operations to SELECT queries only. Perfect for safe data exploration.

๐Ÿ”„

Auto-Retry & Schema-Aware

Failed queries are automatically corrected. The LLM understands your table schemas, columns, and relationships.

๐Ÿ—๏ธ Architecture

๐Ÿ‘ค User Input Natural Language
โ†’
๐Ÿง  LangChain Agent Mistral 7B via Ollama
โ†’
๐Ÿ›ก๏ธ Safety Gate Classify โ†’ Block/Confirm/Allow
โ†’
๐Ÿ—„๏ธ MySQL Database Execute & Return Results

Core Pipeline

  • agent.py โ€” LLM agent that converts natural language โ†’ SQL via LangChain
  • database.py โ€” MySQL connection management with SQLAlchemy
  • schema.py โ€” Auto-introspects table schemas so the LLM knows your data structure
  • query_explainer.py โ€” Translates generated SQL back to plain English

Safety Layer

  • classifier.py โ€” Categorizes queries as READ / WRITE / DESTRUCTIVE
  • guard.py โ€” Safety gate that blocks, confirms, or allows based on classification
  • sanitizer.py โ€” Input sanitization to prevent injection
  • audit.py โ€” Logs every query attempt with timestamps and outcomes

๐Ÿ›ก๏ธ Safety System in Action

๐Ÿ“ธ

Safe Query Execution

โœ… Safe Query โ€” Allowed

SELECT queries execute immediately with results displayed in a clean table format with CSV export option.

๐Ÿ“ธ

Destructive Query Blocked

๐Ÿ”ด Destructive Query โ€” Blocked

DROP TABLE, TRUNCATE, and unscoped DELETE queries are automatically blocked with a warning message.

๐Ÿ“ธ

Write Confirmation

โš ๏ธ Write Query โ€” Confirm

UPDATE and INSERT operations show the affected row count and require explicit user approval before executing.

๐Ÿ› ๏ธ Tech Stack

LLM

Ollama (Mistral 7B)

Agent Framework

LangChain

Database

MySQL + SQLAlchemy

UI

Streamlit

Safety

sqlparse + regex

Language

Python 3.11+

๐Ÿงฉ Challenges & Learnings

Reliable SQL Generation from a 7B Model

Smaller LLMs can hallucinate column names or generate invalid syntax. Solved this by feeding the full database schema into the system prompt, so the model always knows the exact table and column names available. Added auto-retry logic that catches SQL errors and asks the LLM to self-correct.

Balancing Safety with Usability

Too many confirmation dialogs frustrate users; too few create risk. The tiered approach (allow/confirm/block) based on SQL classification strikes the right balance โ€” reads are instant, writes need one click, and destructive operations are impossible.

Keeping Everything Local

Unlike cloud-based solutions (OpenAI API), running Ollama locally means zero data exposure but requires handling model loading times, GPU memory management, and graceful fallbacks when the model server is unavailable.