gLiTcH9724/CrewAI-SQL-Agent
0
1---2title: CrewAI SQL Agent3emoji: ๐4colorFrom: blue5colorTo: green6sdk: streamlit7sdk_version: 1.47.18app_file: app.py9pinned: false10license: mit11---12 13# CrewAI SQL Agent14 15An AI-powered SQL agent system that enables natural language database queries using CrewAI, LangChain, and Streamlit. Upload CSV files and ask questions in plain English to get SQL queries, data analysis, and business insights.16 17## Features18 19- **Natural Language to SQL**: Ask questions in plain English, get SQL queries automatically20- **CSV Upload & Analysis**: Upload CSV files directly through the web interface21- **Intelligent Query Generation**: AI agents write optimized SQL queries22- **Business Insights**: Get data analysis and strategic recommendations23- **Query History**: Track all queries with deduplication and export options24- **Interactive Results**: View query results in tables with export to CSV25- **Multi-Agent System**: Specialized agents for SQL, analysis, and business strategy26 27## Prerequisites28 29- Python 3.8 or higher30- Groq API key (for LLaMA 3 access)31 32## Installation33 341. **Clone the repository**35```bash36git clone https://github.com/Mohit2497/CrewAI-SQL-Agent.git37cd CrewAI-SQL-Agent38```39 402. **Create a virtual environment**41```bash42python -m venv venv43 44# Activate virtual environment45# On Windows:46venv\Scripts\activate47# On Mac/Linux:48source venv/bin/activate49```50 513. **Install dependencies**52```bash53pip install -r requirements.txt54```55 564. **Set up environment variables**57 58Create a `.env` file in the project root:59```env60GROQ_API_KEY=your_groq_api_key_here61DATABASE_PATH=company_data.db62```63 64To get a Groq API key:65- Visit [console.groq.com](https://console.groq.com)66- Sign up for a free account67- Generate an API key68 69## Usage70 711. **Start the application**72```bash73streamlit run main.py74```75 762. **Open your browser**77Navigate to `http://localhost:8501`78 793. **Upload your data**80- Go to the "CSV Upload" tab81- Upload one or more CSV files82- Preview the data and load it into the database83 844. **Ask questions**85- Switch to the "Analysis" tab86- Type your question in natural language87- Examples:88 - "What is the average salary by department?"89 - "Show me the top 10 customers by revenue."90 - "What are the monthly sales trends?"91 925. **View results**93- See the generated SQL query94- View results in a table format95- Download results as CSV96- Read AI-generated analysis and recommendations97 98## Project Structure99 100```101CrewAI-SQL-Agent/102โ103โโโ streamlit_app_clean.py # Main Streamlit application104โโโ requirements.txt # Python dependencies105โโโ .env # Environment variables (create this)106โโโ .env.example # Environment variables template107โโโ README.md # This file108โโโ .gitignore # Git ignore configuration109โโโ setup_database.py # (Optional) Pre-populate database with sample data110```111 112## Configuration113 114### Environment Variables115 116- `GROQ_API_KEY`: Your Groq API key for LLaMA 3 access117- `DATABASE_PATH`: Path to SQLite database (default: `company_data.db`)118 119### Model Selection120 121The system uses LLaMA 3 70B by default.122 123## How It Works124 1251. **Multi-Agent Architecture**126 - **SQL Specialist**: Converts natural language to SQL queries127 - **Data Analyst**: Analyzes query results and finds patterns128 - **Business Consultant**: Provides strategic recommendations129 1302. **Workflow**131 - Upload CSV โ Ask question โ SQL generation โ Query execution โ Analysis โ Business insights132 1333. **Query Processing**134 - Questions are processed by specialized AI agents135 - SQL queries are validated before execution136 - Results are analyzed for patterns and insights137 - Business recommendations are generated138 139## Troubleshooting140 141### Common Issues142 1431. **"LLM initialization failed"**144 - Check your `GROQ_API_KEY` in the `.env` file145 - Ensure you have an active internet connection146 1472. **CSV upload errors**148 - Ensure CSV files are properly formatted149 - Check for special characters in column names150 - Try different encoding if you see errors151 1523. **Query errors**153 - Verify table names match uploaded CSV filenames154 - Check column names in your questions155 - Ensure data types are compatible for operations156 157## Contributing158 159Contributions are welcome! Please feel free to submit a Pull Request. For major changes:160 1611. Fork the repository1622. Create your feature branch (`git checkout -b feature/AmazingFeature`)1633. Commit your changes (`git commit -m 'Add some AmazingFeature'`)1644. Push to the branch (`git push origin feature/AmazingFeature`)1655. Open a Pull Request166 167## License168 169This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.170 171## Acknowledgments172 173- [CrewAI](https://github.com/joaomdmoura/crewAI) for the multi-agent framework174- [LangChain](https://github.com/langchain-ai/langchain) for LLM tooling175- [Streamlit](https://streamlit.io/) for the web interface176- [Groq](https://groq.com/) for LLaMA 3 API access177 178## Contact179 180For questions or support, please open an issue in the GitHub repository.181 182---183 184**Note**: This project is for educational and demonstration purposes. Always verify AI-generated SQL queries before running them on production databases.