A powerful conversational chatbot that enables users to query SQL databases using natural language, built with Streamlit, LangChain, and Groq's advanced LLM models. This application translates everyday language into SQL queries, making database interaction intuitive and accessible.
- Natural Language Processing: Convert plain English queries into SQL statements using Groq's Llama3-8b-8192 model
- Multi-Database Support: Seamlessly connect to SQLite or MySQL databases
- Interactive Chat Interface: Streamlit-powered UI with persistent chat history
- Real-time Query Execution: Live feedback and results display
- Pre-populated Database: Includes a sample SQLite database with student records for immediate testing
- Secure API Key Management: Password-protected input for Groq API credentials
- Extensible Architecture: Easily adaptable to other database types and LLM providers
LLM_Text_To_SQL/
├── app.py # Main Streamlit application
├── sqlite.py # Database setup script for SQLite
├── student.db # Pre-populated SQLite database
├── requirements.txt # Python dependencies
├── README.md # Project documentation
├── LICENSE # MIT License
├── .gitignore # Git ignore rules
└── TODO.md # Development tasks (generated)
- Python 3.8 or higher
- Groq API key (obtain from console.groq.com)
-
Clone the Repository
git clone https://github.com/Monish-Nallagondalla/LLM_Text_To_SQL.git cd LLM_Text_To_SQL -
Create Virtual Environment (Recommended)
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install Dependencies
pip install -r requirements.txt
-
Set Up Environment Variables Create a
.envfile in the project root:GROQ_API_KEY=your_actual_groq_api_key_here
-
Prepare the Database The project includes a pre-populated SQLite database. To recreate it:
python sqlite.py
streamlit run app.py-
Select Database Type
- Choose between SQLite (local) or MySQL (remote) in the sidebar
-
Configure Database Connection
- SQLite: Automatically uses the included
student.db - MySQL: Enter host, username, password, and database name
- SQLite: Automatically uses the included
-
Enter API Key
- Input your Groq API key in the secure text field
-
Start Chatting
- Type natural language queries like:
- "Show me all students in Data Science class"
- "Who has the highest marks?"
- "List students with marks above 80 in section A"
- Type natural language queries like:
-
View Results
- The chatbot will display the generated SQL query and results
- Chat history is maintained for context
- "How many students are in the DEVOPS class?"
- "What is the average marks for Data Science students?"
- "Show me students with marks between 50 and 90"
The included SQLite database (student.db) contains a STUDENT table:
| Column | Type | Description |
|---|---|---|
| NAME | VARCHAR(25) | Student name |
| CLASS | VARCHAR(25) | Class/Subject |
| SECTION | VARCHAR(25) | Section/Grade |
| MARKS | INT | Academic marks |
Sample data includes students from Data Science and DEVOPS classes.
GROQ_API_KEY: Your Groq API key (required)
- SQLite: No additional configuration needed
- MySQL: Provide connection details in the app sidebar
Key packages (see requirements.txt for full list):
streamlit: Web app frameworklangchain: LLM orchestrationlangchain-groq: Groq integrationsqlalchemy: Database ORMpython-dotenv: Environment variable management
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- API Key Errors: Ensure your Groq API key is valid and correctly entered
- Database Connection: Verify MySQL credentials and network access
- Dependencies: Run
pip install -r requirements.txtto ensure all packages are installed - Python Version: Confirm you're using Python 3.8+
Run with verbose logging:
streamlit run app.py --logger.level=debugThis project is licensed under the MIT License - see the LICENSE file for details.
- LangChain for the powerful SQL agent framework
- Groq for providing fast and efficient LLM inference
- Streamlit for the intuitive web app interface
If you encounter any issues or have questions:
- Check the Troubleshooting section
- Open an issue on GitHub
- Review the LangChain and Groq documentation
Happy querying! 🦜