Prompt2Query is a powerful command-line tool that translates natural language into SQL queries using AI, allowing users to interact with databases without writing complex SQL code.
- Natural Language Processing: Convert plain English commands into precise SQL queries
- Interactive CLI: User-friendly command-line interface with color coding
- Schema Analysis: Automatic detection of table relationships and join patterns
- Query Preview: Review generated SQL before execution
- Export Functionality: Save results to CSV files
- Database Insights: Explore table schemas and relationships with simple commands
- Python 3.7 or higher
- PostgreSQL database
- OpenAI API key
-
Clone the repository:
git clone https://github.com/yourusername/prompt2query.git cd prompt2query
-
Install required packages:
pip install -r requirements.txt
-
Create a
.env
file in the project root with your API keys and database credentials:# OpenAI configuration OPENAI_API_KEY=your_openai_api_key_here # PostgreSQL configuration DB_HOST=localhost DB_DATABASE=your_database_name DB_USER=your_database_user DB_PASSWORD=your_database_password DB_PORT=5432
Run the main CLI application:
python main_cli.py
help
- Display help informationtables
- List all available tablesschema
- Show complete database schemaclear
- Clear the screenexit
- Exit the application
>> Show me all users who joined last month
>> What are the top 5 products by revenue?
>> Find customers who have purchased more than 3 products
The application consists of several components:
- main_cli.py: Main entry point with CLI interface
- db.py: Database connection and schema analysis
- openai_client.py: Integration with OpenAI API for natural language processing
- query_executor.py: SQL query execution logic
- utils.py: Utility functions for formatting and exporting results
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Project Link: https://github.com/hipjim/prompt2query