A powerful, AI-driven database query assistant that leverages RAG (Retrieval-Augmented Generation) to provide intelligent SQL query generation and database interaction.
- 🤖 AI-Powered Query Generation: Utilizes Qwen 1.8b LLM for natural language to SQL conversion
- 📊 Schema-Aware Responses: Automatically detects and uses database structure
- 🔗 Smart Table Relationships: Identifies and suggests proper table joins
- 📝 Query Validation: Ensures generated queries match existing schema
- 📈 Result Export: Automatic CSV export with metadata tracking
- 🛡️ Error Handling: Robust error management and recovery
- Clone the repository:
git clone https://github.com/98tarunkumar/text_to_sql.git
cd text_to_sql
- Install dependencies:
pip install -r requirements.txt
- Configure database connection in
dbQuery.py
:
self.db_connection = mysql.connector.connect(
host="your_host",
user="your_user",
password="your_password",
database="your_database"
)
- Python 3.8+
- MySQL 8.0+
- Ollama with Qwen 1.8b model
- Required Python packages:
mysql-connector-python
pandas
ollama
- Start the application:
python dbQuery.py
- Enter your database questions in natural language:
Enter your database question: Show me all orders from the last month
- Get AI-generated responses with:
- Query explanation
- SQL query
- Results in both console and CSV format
-- Natural Language: "Show active users and their orders"
SELECT
u.username,
o.order_id,
o.order_date
FROM users u
JOIN orders o ON u.user_id = o.user_id
WHERE u.status = 'active';
dbchat/
├── dbQuery.py # Main application logic
├── requirements.txt # Project dependencies
└── README.md # Documentation
- DatabaseQueryAssistant: Core class handling database interactions
- RAG Implementation: Schema-aware context generation
- Query Validation: Schema compliance checking
- Result Management: CSV export and metadata tracking
- Database connection management
- Query validation
- Schema compliance checking
- JSON serialization handling
- Graceful error recovery
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Qwen LLM team for the language model
- MySQL team for the database engine
- All contributors and users of this project
For support, please open an issue in the GitHub repository or contact the maintainers.
Made with ❤️ by Tarun