A Streamlit-based application that allows users to query their PostgreSQL database using natural language questions. The app leverages the Groq LLM API to convert natural language queries into SQL and provide human-readable answers.
- 🤖 Natural language to SQL conversion
- 📊 Interactive database schema visualization
- 📈 Real-time query results with data visualization
- 💾 CSV export functionality
- 🔍 Sample data preview for all tables
- 📝 Detailed database statistics
- 🔗 Table relationship visualization
- Python 3.8+
- PostgreSQL database
- Groq API key
- Streamlit account (for secrets management)
- Clone the repository:
git clone <repository-url>
cd sql-query-assistant
- Install required packages:
pip install -r requirements.txt
- Set up your environment variables in
.streamlit/secrets.toml
:
DATABASE_URL = "your-postgresql-connection-string"
DATABASE_NAME = "your-database-name"
GROQ_API_KEY = "your-groq-api-key"
.
├── LICENSE # Apache License 2.0
├── README.md # Project documentation
├── requirements.txt # Project dependencies
├── src/
│ ├── __init__.py
│ ├── database.py # Database connection and operations
│ └── llm_chain.py # LLM integration and query processing
└── streamlit_app.py # Main Streamlit application
The application uses PostgreSQL as its database. Configure your database connection in .streamlit/secrets.toml
:
DATABASE_URL = "postgresql://username:password@host:port/database?sslmode=require"
DATABASE_NAME = "Your Database Name"
The application uses Groq's LLM API for natural language processing. Add your API key to .streamlit/secrets.toml
:
GROQ_API_KEY = "your-groq-api-key"
The following packages are required and can be installed via requirements.txt:
- streamlit
- psycopg2-binary
- google-generativeai
- pandas
- groq
- Start the Streamlit application:
streamlit run streamlit_app.py
- Open your browser and navigate to the provided URL (typically http://localhost:8501)
-
View Database Schema:
- The sidebar displays all tables, their columns, and relationships
- Sample data is available for each table
- Database statistics show total tables, rows, and database size
-
Query Your Data:
- Enter your question in natural language
- Click "Get Answer" to process your query
- View the generated SQL, results table, and natural language explanation
- Download results as CSV if needed
-
Example Questions:
- "Show total revenue by category"
- "Which products have the highest sales quantity?"
- "List top 5 customers by order value"
- SQL injection prevention through proper query sanitization
- Secure credential management using Streamlit secrets
- Parameterized queries for safe database operations
-
database.py:
- Handles all database operations
- Manages connections and queries
- Provides schema information
-
llm_chain.py:
- Processes natural language questions
- Generates SQL queries using Groq LLM
- Creates natural language responses
-
streamlit_app.py:
- Main application interface
- Handles user interaction
- Displays results and visualizations
- User inputs a natural language question
- System generates database schema context
- Groq LLM converts question to SQL
- Query is validated and executed
- Results are formatted and displayed
- Natural language explanation is generated
- Database connection errors are gracefully handled
- Invalid queries are caught and reported
- LLM API errors are managed with appropriate user feedback
Please feel free to submit issues and pull requests for any improvements.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
- Built with Streamlit
- Powered by Groq LLM API
- Uses PostgreSQL for database management