Update README.md
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143# Text-to-SQL RAG AppThis 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```---## Installation1. **Clone the Repository:**```bashgit clone https://github.com/Bindhu-T-Devidas/Natural-Language-to-SQL.gitcd Natural-Language-to-SQL```2. **Install Dependencies:**```bashpip 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.---## Usage1. **Run the Streamlit App:**```bashstreamlit 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 GenerationThe application uses the `Graphviz` library to generate ER diagrams from the uploaded database schemas.---## π Supported Database Types1. **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:**```sqlSELECT * FROM tracks WHERE genre = 'Rock';```* **Results:**| TrackID | Name | Genre || ------- | ------------- | ----- || 1 | Thunderstruck | Rock || 2 | Back in Black | Rock |---123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149---title: NL-to-SQL RAG Appemoji: πcolorFrom: bluecolorTo: indigosdk: streamlitsdk_version: 1.26.0app_file: app.pypinned: false---# Text-to-SQL RAG AppThis 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```---## Installation1. **Clone the Repository:**```bashgit clone https://github.com/Bindhu-T-Devidas/Natural-Language-to-SQL.gitcd Natural-Language-to-SQL```2. **Install Dependencies:**```bashpip 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.---## Usage1. **Run the Streamlit App:**```bashstreamlit 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 GenerationThe application uses the `Graphviz` library to generate ER diagrams from the uploaded database schemas.---## π Supported Database Types1. **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:**```sqlSELECT * FROM tracks WHERE genre = 'Rock';```* **Results:**| TrackID | Name | Genre || ------- | ------------- | ----- || 1 | Thunderstruck | Rock || 2 | Back in Black | Rock |---
README.md+149-143