Text-to-SQL is a project that translates natural language questions into SQL queries. It aims to provide a user-friendly interface for interacting with databases, enabling users with limited SQL knowledge to retrieve information efficiently.
- Natural Language to SQL Conversion: Converts user-provided natural language questions into executable SQL queries.
- Database Interaction: Executes generated SQL queries against a configured database and returns the results.
- User Interface: Provides a web-based interface for users to input questions and view results.
- LLM Integration: Leverages Google's Gemini model to understand natural language and generate SQL.
- Customizable Prompts: Allows customization of prompts to guide the LLM for better SQL generation.
- Error Handling: Includes mechanisms for correcting invalid SQL queries.
- Modular Architecture: Clean separation of concerns for easy maintenance and extensibility.
- Comprehensive Testing: Includes unit tests and integration tests.
- Development Tools: Jupyter notebooks for experimentation and analysis.
- Python 3.8 or higher
- PostgreSQL 12 or higher
- Google API key for Gemini model access
- Git (for cloning the repository)
- Linux/macOS Systems
-
Clone the repository:
git clone https://github.com/p-sumann/nl2sql cd nl2sql -
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate -
Install dependencies:
pip install -r requirements.txt
-
Create environment file:
Create a
.envfile in the project root with the following variables: sample keys used in this project are provided except for Google.touch .env
GOOGLE_API_KEY=<your_google_api_key> DB_PASS=<your pass> #postgres DATABASE=<your db> #text2sql USER=<your user> #postgres HOST=<your host> #localhost DATABASE_CLIENT=<your db client> #postgresql PORT=<your port> #5432 -
LLM Configuration:
- Model settings can be adjusted in
src/core/llm.py - Default model:
gemini-2.0-flash - Customize generation parameters in the
GenerativeModelWrapperclass
- Model settings can be adjusted in
-
Database Configuration:
- Connection settings are managed through environment variables
- Default database name:
text2sql - Default user:
postgres
For, this demo purpose, the default user and password is used, for production use, use safe values and use environment variables in bash script.
-
Install and Initialize the database:
# Run the database initialization script cd src/db chmod +x init.sh ./init.sh
-
Create database tables:
cd ../.. python src/db/create_table.py -
Load sample data:
python src/db/load_data.py
-
Start the FastAPI server:
python src/server/app.py
-
Access the web interface:
- Open your browser and navigate to
http://localhost:8000 - Enter natural language questions in the chat input
- View generated SQL queries and results
- Open your browser and navigate to
.env: Environment variables configuration.gitignore: Git ignore fileDockerfile: Docker container configurationrequirements.txt: Python dependencies
Contains CSV files with flight-related information:
airlines.csv: Airline company dataairports.csv: Airport informationflights.csv: Flight recordseval.csv: Evaluation dataset
Jupyter notebooks for development and testing:
airline_dataset.ipynb: Dataset analysiseval.ipynb: Evaluation scriptsinitial_exp.ipynb: Initial experimentsllm_retrying.ipynb: LLM retry logic tests
Main application code:
config/: Configuration settingsdb/: Database operations and initializationserver/: Web server implementationcore/llm.py: LLM integrationtemplates/: Server-side templates
static/: CSS stylestests/: Unit and integration testsutils/: Utility functionscommon_utils.py: Common helper functionsdatabase_utils.py: Database utilitiesprompts.py: LLM prompt templates
Frontend templates:
chat_response.html: Chat interfaceindex.html: Main pagestatic/style.css: CSS styling
.
├── .env
├── .gitignore
├── README.md
├── requirements.txt
├── data
│ ├── airlines.csv
│ ├── airports.csv
│ ├── eval.csv
│ └── flights.csv
├── notebooks
│ ├── airline_dataset.ipynb
│ ├── eval.ipynb
│ ├── initial_exp.ipynb
│ └── llm_retrying.ipynb
├── src
│ ├── __init__.py
│ ├── config #config module
│ │ ├── __init__.py
│ │ └── config.py
│ ├── db #database module
│ │ ├── __init__.py
│ │ └── database.py
│ │ └── create_table.py
│ │ └── database.py
│ │ └── load_data.py
│ │ └── init.sh
│ ├── server #core fastapi app
│ │ ├── __init__.py
│ │ ├── app.py
│ │ ├── core
│ │ │ └── llm.py
│ │ └── templates
│ ├── tests #tests modules
│ │ ├── __init__.py
│ │ └── conftest.py
│ │ └── test_database.py
│ │ └── test_query.py
│ └── utils #utility module
│ ├── __init__.py
│ ├── common_utils.py
│ ├── database_utils.py
│ └── prompts.py
└── templates #html templates
│ └── chat_response.html
│ └── index.html
├── static #UI styling
│ └── style.css
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.