File size: 3,917 Bytes
44d9f1a bd340c6 44d9f1a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | ---
title: NL-to-SQL RAG App
emoji: π
colorFrom: blue
colorTo: indigo
sdk: streamlit
sdk_version: 1.45.1
app_file: app.py
pinned: false
---
# Text-to-SQL RAG App
This application leverages **Retrieval-Augmented Generation (RAG)** to convert natural language queries into SQL statements. It integrates the **Gemini LLM** for SQL generation and supports various databases including **SQLite, PostgreSQL, and MySQL**.
## Features
* **Multi-Database Support:** SQLite, PostgreSQL, and MySQL.
* **Flexible Query Modes:**
* **LangChain RAG**: Uses LangChain for SQL generation.
* **Manual FAISS RAG**: Uses FAISS index for retrieval.
* **Schema Only**: Direct SQL generation from schema.
* **Schema Visualization:** Automatically generates ER diagrams for uploaded databases.
* **Real-Time Execution:** Runs generated SQL queries and displays the results.
---
## π Project Structure
```
Text-to-SQL RAG App/
βββ app.py # Main Streamlit app
βββ requirements.txt # Dependencies list
βββ utils/ # Utility scripts
β βββ er_diagram.py # ER diagram generator
β βββ llm_sql_generator.py # SQL generation using Gemini
β βββ schema_extractor.py # Extracts schema from databases
β βββ vector_store.py # FAISS-based vector store
βββ vectorstore/ # Stores FAISS index and metadata
```
---
## Installation
1. **Clone the Repository:**
```bash
git clone https://github.com/Bindhu-T-Devidas/Natural-Language-to-SQL.git
cd Natural-Language-to-SQL
```
2. **Install Dependencies:**
```bash
pip install -r requirements.txt
```
3. **Set Up Environment Variables:**
Create a `.env` file:
```
GEMINI_API_KEY=your_gemini_api_key
```
Replace `your_gemini_api_key` with your actual API key.
---
## Usage
1. **Run the Streamlit App:**
```bash
streamlit run app.py
```
2. **Upload a Database:**
* Choose between **SQLite**, **PostgreSQL**, or **MySQL**.
* Upload the file or enter database credentials.
3. **Generate SQL Query:**
* Enter your natural language query.
* Choose the query mode:
* π **LangChain RAG**
* π οΈ **Manual FAISS RAG**
* π **No RAG (schema only)**
* Click **"π Generate SQL"**.
4. **View Results:**
* The generated SQL query will be displayed.
* If the query executes successfully, the result table will be shown.
---
## Technologies Used
* **Python**: Core programming language.
* **Streamlit**: Frontend for interactive query generation.
* **LangChain**: RAG pipeline for SQL generation.
* **Gemini LLM**: SQL generation engine.
* **FAISS**: Vector store for semantic retrieval.
* **SQLAlchemy**: Database connection handling.
* **Graphviz**: Visualizes ER diagrams.
---
## ER Diagram Generation
The application uses the `Graphviz` library to generate ER diagrams from the uploaded database schemas.
---
## π Supported Database Types
1. **SQLite (.db, .sqlite, .sql)**
2. **PostgreSQL**
3. **MySQL**
---
## Query Modes
* **LangChain RAG**: Uses LangChain to efficiently retrieve top-k schema elements via FAISS and generate SQL using an LLM (Gemini), providing modularity, prompt flexibility, and easier experimentation.
* **Manual FAISS RAG**: Directly uses FAISS for top-k retrieval and constructs a prompt for Gemini manually, offering control but requiring more code.
* **No RAG (Schema Only)**: Generates SQL using the full schema directly, without any retrieval, which can cause hallucination and irrelevant joins in complex schemas.
---
## π Output Example
* **SQL Query:**
```sql
SELECT * FROM tracks WHERE genre = 'Rock';
```
* **Results:**
| TrackID | Name | Genre |
| ------- | ------------- | ----- |
| 1 | Thunderstruck | Rock |
| 2 | Back in Black | Rock |
--- |