CoolFace
Apppublic

gLiTcH9724/CrewAI-SQL-Agent

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
App README

CrewAI SQL Agent

An 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.

Features

  • —Natural Language to SQL: Ask questions in plain English, get SQL queries automatically
  • —CSV Upload & Analysis: Upload CSV files directly through the web interface
  • —Intelligent Query Generation: AI agents write optimized SQL queries
  • —Business Insights: Get data analysis and strategic recommendations
  • —Query History: Track all queries with deduplication and export options
  • —Interactive Results: View query results in tables with export to CSV
  • —Multi-Agent System: Specialized agents for SQL, analysis, and business strategy

Prerequisites

  • —Python 3.8 or higher
  • —Groq API key (for LLaMA 3 access)

Installation

  1. 1.Clone the repository
bash
git clone https://github.com/Mohit2497/CrewAI-SQL-Agent.git
cd CrewAI-SQL-Agent
  1. 1.Create a virtual environment
bash
python -m venv venv

# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On Mac/Linux:
source venv/bin/activate
  1. 1.Install dependencies
bash
pip install -r requirements.txt
  1. 1.Set up environment variables

Create a .env file in the project root:

env
GROQ_API_KEY=your_groq_api_key_here
DATABASE_PATH=company_data.db

To get a Groq API key:

Usage

  1. 1.Start the application
bash
streamlit run main.py
  1. 1.Open your browser Navigate to http://localhost:8501
  1. 1.Upload your data
  2. 2.Go to the "CSV Upload" tab
  3. 3.Upload one or more CSV files
  4. 4.Preview the data and load it into the database
  1. 1.Ask questions
  2. 2.Switch to the "Analysis" tab
  3. 3.Type your question in natural language
  4. 4.Examples:
  5. 5."What is the average salary by department?"
  6. 6."Show me the top 10 customers by revenue."
  7. 7."What are the monthly sales trends?"
  1. 1.View results
  2. 2.See the generated SQL query
  3. 3.View results in a table format
  4. 4.Download results as CSV
  5. 5.Read AI-generated analysis and recommendations

Project Structure

CrewAI-SQL-Agent/
│
├── streamlit_app_clean.py    # Main Streamlit application
├── requirements.txt          # Python dependencies
├── .env                     # Environment variables (create this)
├── .env.example             # Environment variables template
├── README.md                # This file
├── .gitignore              # Git ignore configuration
└── setup_database.py        # (Optional) Pre-populate database with sample data

Configuration

Environment Variables

  • —GROQ_API_KEY: Your Groq API key for LLaMA 3 access
  • —DATABASE_PATH: Path to SQLite database (default: company_data.db)

Model Selection

The system uses LLaMA 3 70B by default.

How It Works

  1. 1.Multi-Agent Architecture
  2. 2.SQL Specialist: Converts natural language to SQL queries
  3. 3.Data Analyst: Analyzes query results and finds patterns
  4. 4.Business Consultant: Provides strategic recommendations
  1. 1.Workflow
  2. 2.Upload CSV → Ask question → SQL generation → Query execution → Analysis → Business insights
  1. 1.Query Processing
  2. 2.Questions are processed by specialized AI agents
  3. 3.SQL queries are validated before execution
  4. 4.Results are analyzed for patterns and insights
  5. 5.Business recommendations are generated

Troubleshooting

Common Issues

  1. 1."LLM initialization failed"
  2. 2.Check your GROQ_API_KEY in the .env file
  3. 3.Ensure you have an active internet connection
  1. 1.CSV upload errors
  2. 2.Ensure CSV files are properly formatted
  3. 3.Check for special characters in column names
  4. 4.Try different encoding if you see errors
  1. 1.Query errors
  2. 2.Verify table names match uploaded CSV filenames
  3. 3.Check column names in your questions
  4. 4.Ensure data types are compatible for operations

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes:

  1. 1.Fork the repository
  2. 2.Create your feature branch (git checkout -b feature/AmazingFeature)
  3. 3.Commit your changes (git commit -m 'Add some AmazingFeature')
  4. 4.Push to the branch (git push origin feature/AmazingFeature)
  5. 5.Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Contact

For questions or support, please open an issue in the GitHub repository.


Note: This project is for educational and demonstration purposes. Always verify AI-generated SQL queries before running them on production databases.