This project is an intelligent, conversational AI agent that empowers you to chat with your SQL database using natural language. Forget writing complex SQL queriesโjust ask questions and get answers. The agent features conversational memory, allowing it to understand follow-up questions and provide context-aware responses.
This screenshot demonstrates the agent's core conversational memory feature, allowing it to understand and answer a follow-up question.
- Natural Language to SQL: Translates plain English questions into precise SQL queries.
- True Conversational Memory: Remembers the context of the conversation to answer follow-up questions accurately.
- Safety First Guardrails: Built with a read-only system prompt to prevent any destructive database operations (
DELETE,UPDATE,DROP, etc.). - Schema Aware: Intelligently inspects the database schema to write correct and relevant queries.
- User-Friendly Interface: A clean and intuitive chat interface powered by Streamlit.
- Backend: Python
- AI Framework: LangChain
- LLM: Google Gemini Pro
- UI Framework: Streamlit
- Database: SQLite
Follow these steps to get the project running on your local machine.
- Python 3.10 or higher installed.
- A Google Gemini API Key. You can get one from Google AI Studio.
First, clone the repository and navigate into the project directory.
git clone https://github.com/skyline-GTRr32/SQL-Database-Agent.git
cd SQL-Database-AgentCreate and activate a virtual environment, then install the required packages.
# Create the virtual environment
python3 -m venv venv
# Activate it
# On macOS/Linux:
source venv/bin/activate
# On Windows:
.\venv\Scripts\activate
# Install the dependencies
pip install -r requirements.txtCreate a .env file in the root directory to store your API key securely.
# .env
GOOGLE_API_KEY="your_actual_google_api_key_here"
Security Note: The
.envfile is listed in.gitignoreto ensure your secret key is never committed to the repository.
The project includes a schema.sql file to create and populate the SQLite database. Run this command from your terminal:
# This command creates 'sales.db' and populates it with sample data.
sqlite3 sales.db < schema.sqlYou're all set! Launch the Streamlit application.
streamlit run main.pyThe application will open automatically in your web browser.
Here is a real example demonstrating the agent's conversational memory:
You:
List all the products in the 'Apparel' category
๐ค Agent:
Here are the products in the 'Appearel' category: 'Zephyr Jacket' and 'Orbit Backpack'.
You:
Now, which of those is more expensive?
๐ค Agent:
The 'Zephyr Jacket' is more expensive at 8999 cents, compared to the 'Orbit Backpack' at 7499 cents.
This project is licensed under the MIT License. See the LICENSE file for more details.
