This application allows users to input natural language questions about a database and generates SQL queries to retrieve answers. It uses Streamlit for the user interface, LangChain for SQL query generation, and Grok (via ChatGroq) for natural language processing. The app connects to a SQLite database (Chinook.db) and displays both the generated SQL query and its results.
- Converts natural language questions into SQL queries.
- Executes queries on the Chinook SQLite database.
- Displays the generated SQL query and its results in a user-friendly Streamlit interface.
- Includes sample questions for easy testing.
- Handles errors gracefully with user feedback.
- Python 3.8+
- SQLite database (
Chinook.db) in the project directory - Required Python packages (install via
pip):pip install streamlit langchain langchain-community langchain-groq sqlalchemy python-dotenv
-
Clone the repository or copy the code to your local machine.
-
Ensure the
Chinook.dbSQLite database is in the project directory. -
Create a
.envfile with your Grok API key:GROQ_API_KEY=your_api_key_here
-
Place the
screenshot.pngimage in the project directory or animages/subdirectory. -
Install dependencies:
pip install -r requirements.txt
-
Run the Streamlit app:
streamlit run app.py
- Launch the app using the command above.
- Open the provided URL in your browser (typically
http://localhost:8501). - Enter a question in the text input field (e.g., "Show me the total sales by year").
- Click the "Execute" button to generate and run the SQL query.
- View the generated SQL query and its results in the interface.
- Show me the total sales by year base.
- What is the top selling product in 2012 and 2013?
- Who is the top customer in 2013?
- What was the top selling product in 2011?
- List the track composed by 'Steven Tyler'.
- Create a sales summary by country.
- Imports: Libraries for Streamlit, LangChain, SQL handling, and logging.
- Database Connection: Connects to
Chinook.dbusingSQLDatabase. - LLM Setup: Uses
ChatGroqwith thellama3-70b-8192model for query generation. - Prompt Template: Custom prompt for generating SQL queries.
- Query Execution: Converts questions to SQL and executes them, handling errors.
- Streamlit Interface: Displays input field, sample questions, and results.
- Logs errors using Python's
loggingmodule. - Displays user-friendly error messages in the Streamlit interface if a query fails.
- Ensure the
.envfile contains a valid Grok API key.
