A powerful SQL AI agent built with Phi Data that helps you interact with your databases through natural language conversations. This project combines the power of LLMs with database operations to provide an intuitive interface for database queries.
- Natural language to SQL query conversion
- Interactive chat interface using Chainlit
- Secure database connection handling
- Context-aware conversation with history retention
- Error handling and graceful error messages
- Support for multiple SQL databases (MySQL, PostgreSQL, SQLite, etc.)
- Easy database configuration through connection URL
- Phi Data: Core AI agent functionality and tools
- Groq: LLM integration using the llama-3.3-70b-versatile model
- Chainlit: Web-based chat interface
- SQLAlchemy: SQL toolkit and ORM
- Database Connectors:
- PyMySQL for MySQL
- psycopg2 for PostgreSQL
- Other SQLAlchemy-supported databases
- Python-dotenv: Environment variable management
- Python 3.8 or higher
- A SQL database (MySQL, PostgreSQL, SQLite, etc.)
- Groq API key
- Clone the repository:
git clone <your-repository-url>
cd <repository-name>
- Install the required packages:
pip install -r requirements.txt
- Create a
.env
file in the root directory with your credentials:
GROQ_API_KEY=your_groq_api_key
DB_URL=your_database_url
The application supports various SQL databases. Here are examples of connection URLs for different databases:
DB_URL=mysql+pymysql://username:password@host:port/database_name
DB_URL=postgresql://username:password@host:port/database_name
DB_URL=sqlite:///path/to/database.db
Note: Make sure to install the appropriate database connector package for your chosen database.
Start the application using Chainlit:
chainlit run app.py
Once the application is running, you can:
- Open your browser and navigate to the provided Chainlit URL
- Start chatting with the AI agent using natural language
- Ask questions about your database and get detailed responses
- View the SQL queries used to retrieve the information
Example queries:
- "Show me all tables in the database"
- "What are the top 10 records from users table?"
- "How many orders were placed last month?"
The application includes robust error handling for:
- Missing API keys
- Database connection issues
- Invalid queries
- Session management errors
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
For any questions or concerns, please open an issue in the repository.
Made with ❤️ using Phi Data and Chainlit