Skip to content

About

AI-powered Text-to-SQL system using Google Gemini and SQLite that converts natural-language questions into validated SQL queries and executes them against structured data.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Text-to-SQL AI

A Python-based GenAI project that converts natural-language questions into SQL queries using Google Gemini and executes them against a SQLite database.

What it does

Instead of writing SQL manually, users can ask questions such as:

Which customer has placed the most orders?

The application:

  1. Reads the database schema
  2. Sends the schema and question to Gemini
  3. Generates a SQL query
  4. Validates the generated query
  5. Executes it against SQLite
  6. Displays the result

Example:

Question:
Which customer has placed the most orders?

Generated SQL:
SELECT T1.name
FROM customers AS T1
INNER JOIN orders AS T2
ON T1.id = T2.customer_id
GROUP BY T1.id
ORDER BY COUNT(T2.id) DESC
LIMIT 1;

Result:
Suresh

Tech Stack

  • Python
  • Google Gemini 2.5 Flash
  • Google Cloud Vertex AI
  • SQLite
  • google-genai

Project Structure

text-to-sql-ai/
│
├── app/
│   ├── main.py
│   ├── gemini_client.py
│   ├── sql_validator.py
│   ├── database.py
│   └── __init__.py
│
├── database/
│   ├── create_database.py
│   ├── schema.py
│   └── test_database.py
│
├── data/
│   └── sales.db
│
├── requirements.txt
├── .gitignore
└── README.md

Database

The sample SQLite database contains:

  • customers — customer information
  • products — product information
  • orders — order information

The application reads the schema dynamically rather than hard-coding it into the main program.

SQL Validation

Since the SQL is generated by an LLM, the application validates the query before execution.

Currently, only read-oriented SELECT queries are allowed, while operations such as INSERT, UPDATE, DELETE, DROP, and ALTER are rejected.

Running the Project

Clone the repository and create a virtual environment:

python -m venv venv

Activate it on Windows:

venv\Scripts\activate

Install dependencies:

pip install -r requirements.txt

Authenticate with Google Cloud:

gcloud auth application-default login

Run the application from the project root:

python -m app.main

Current Status

The basic Text-to-SQL pipeline is working:

Question
   ↓
Gemini
   ↓
SQL
   ↓
Validation
   ↓
SQLite
   ↓
Result

Planned Improvements

  • Improve SQL validation and database safety
  • Add SQL error correction/retry
  • Test the system with a larger set of questions
  • Add Text-to-SQL evaluation metrics
  • Add a simple web interface
  • Deploy the application

About

AI-powered Text-to-SQL system using Google Gemini and SQLite that converts natural-language questions into validated SQL queries and executes them against structured data.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages