Transform natural language questions into SQL queries with the power of AI! ๐
SQL Natural Language Interface is a revolutionary tool that bridges the gap between human language and database queries. Simply ask questions in plain English, and watch as our AI-powered system generates precise SQL queries and delivers beautiful, actionable results.
- Democratize Data Access: No SQL knowledge required - anyone can query databases
- Boost Productivity: Get insights in seconds, not hours
- Reduce Errors: AI-generated queries minimize human mistakes
- Universal Compatibility: Works with PostgreSQL and MySQL databases
- Dual AI Support: Choose between OpenAI GPT-4 or Google Gemini
- Context-Aware: Understands your database schema for accurate queries
- Natural Language: Ask questions like "Show me customers who bought products last month"
- Read-Only Queries: Only SELECT statements allowed - your data stays safe
- SQL Injection Protection: Advanced validation prevents malicious queries
- Input Sanitization: Multiple layers of security checks
- Interactive Web Interface: Beautiful, responsive React-based UI
- Real-Time Results: Instant query execution and results display
- Export Capabilities: Download results as CSV files
- Smart Summaries: AI-generated natural language summaries of your data
- RESTful API: Easy integration with existing applications
- Comprehensive Logging: Full audit trail of all operations
- Error Handling: Graceful error management with detailed feedback
- Database Agnostic: Seamless switching between PostgreSQL and MySQL
- Python 3.8 or higher
- PostgreSQL or MySQL database
- OpenAI API key or Google Gemini API key
Clone the repository
git clone https://github.com/yourusername/sql-nlp-interface.git cd sql-nlp-interfaceInstall dependencies
pip install -r requirements.txt
Configure environment variables Create a
.envfile in the project root:# API KeysOPENAI_API_KEY=your_openai_api_key_hereGEMINI_API_KEY=your_gemini_api_key_here# Database ConfigurationDATABASE_URL=postgresql://username:password@localhost:5432/database_nameDATABASE_TYPE=postgresql# or mysqlMYSQLDB_URL=mysql+pymysql://username:password@localhost:3306/database_name# AI Model ProviderMODEL_PROVIDER=openai# or gemini
Launch the application
uvicorn main:app --reload --host 0.0.0.0 --port 8000
Open your browser Navigate to
http://localhost:8000and start querying!
Natural Language: "Show me all customers from New York" Generated SQL: SELECT * FROM customers WHERE city = 'New York'; Natural Language: "What are the top 5 products by sales in the last quarter?" Generated SQL: SELECT product_name, SUM(quantity * price) as total_sales FROM orders o JOIN order_items oi ON o.id = oi.order_id JOIN products p ON oi.product_id = p.id WHERE o.created_at >= DATE_SUB(NOW(), INTERVAL 3 MONTH) GROUP BY product_name ORDER BY total_sales DESC LIMIT 5; importrequestsresponse=requests.post('http://localhost:8000/generate-query', json={'user_query': 'Show me monthly revenue trends', 'db_schema': 'your_schema_here' }) data=response.json() print(f"SQL Query: {data['sql_query']}") print(f"Results: {data['results']}")โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ Frontend โ โ FastAPI โ โ Database โ โ (React) โโโโโบโ Backend โโโโโบโ (PostgreSQL/ โ โ โ โ โ โ MySQL) โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ โผ โโโโโโโโโโโโโโโโโโโ โ AI Models โ โ (OpenAI/ โ โ Gemini) โ โโโโโโโโโโโโโโโโโโโ | Method | Endpoint | Description |
|---|---|---|
GET | / | Serve the web interface |
GET | /health | Health check endpoint |
GET | /fetch-schema | Retrieve database schema |
POST | /generate-query | Generate and execute SQL from natural language |
Generate Query
POST /generate-query{"user_query": "Show me all users who registered this month", "db_schema": "Table: users\nColumns: id (integer), name (varchar), email (varchar), created_at (timestamp)" }Response
{"sql_query": "SELECT * FROM users WHERE created_at >= DATE_TRUNC('month', CURRENT_DATE);", "results": [...], "csv_base64": "...", "csv_filename": "result_abc123.csv", "summary": "Found 42 users who registered this month." }| Variable | Description | Required | Default |
|---|---|---|---|
OPENAI_API_KEY | OpenAI API key | Yes* | - |
GEMINI_API_KEY | Google Gemini API key | Yes* | - |
DATABASE_URL | PostgreSQL connection string | Yes | - |
DATABASE_TYPE | Database type (postgresql/mysql) | No | postgresql |
MYSQLDB_URL | MySQL connection string | No** | - |
MODEL_PROVIDER | AI model provider (openai/gemini) | No | openai |
*At least one API key is required **Required if DATABASE_TYPE is mysql
- PostgreSQL: Full support with advanced features
- MySQL: Complete compatibility with MySQL-specific syntax
We welcome contributions from the community! Here's how you can help:
- Use the issue tracker
- Include detailed steps to reproduce
- Provide system information and error logs
- Open a feature request
- Describe the use case and expected behavior
- Include mockups or examples if possible
- 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
- Follow PEP 8 style guidelines
- Add tests for new features
- Update documentation as needed
- Ensure all tests pass before submitting
FROM python:3.9-slim WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . EXPOSE 8000 CMD ["uvicorn", "db_NLP:app", "--host", "0.0.0.0", "--port", "8000"]- Use environment-specific configuration
- Implement proper logging and monitoring
- Set up database connection pooling
- Configure CORS for your domain
- Use HTTPS in production
- Query Generation: < 2 seconds average response time
- Database Execution: Depends on query complexity and data size
- Concurrent Users: Supports 100+ concurrent requests
- Memory Usage: ~200MB base memory footprint
- Input Validation: All user inputs are sanitized
- SQL Injection Protection: Multiple layers of protection
- Read-Only Access: Only SELECT queries are allowed
- API Key Security: Secure handling of API credentials
- CORS Configuration: Configurable cross-origin policies
Connection Errors
# Check database connectivity python -c "from db_NLP import engine; print(engine.connect())"API Key Issues
# Verify environment variables python -c "import os; print(os.getenv('OPENAI_API_KEY'))"Port Already in Use
# Use different port uvicorn db_NLP:app --port 8001- OpenAI for providing powerful language models
- Google for Gemini AI capabilities
- FastAPI for the excellent web framework
- SQLAlchemy for robust database interactions
- React for the beautiful frontend interface
This project is licensed under the MIT License - see the LICENSE file for details.
If this project helped you, please consider:
- โญ Starring the repository
- ๐ด Forking for your own use
- ๐ข Sharing with your network
- GitHub: @yourusername
- Email: [email protected]
- Twitter: @yourusername
- LinkedIn: Your Name