This project demonstrates an AI-powered chatbot that allows users to query a SQL database using natural language questions. Leveraging LangChain, Azure OpenAI, and Streamlit, the chatbot generates SQL queries, executes them on the database, and provides responses in natural language, with optional visualizations.
- Converts natural language questions into SQL queries.
- Executes queries on a MySQL database (
Chinookdataset). - Provides human-readable responses in natural language.
- Optional dynamic visualizations (e.g., line charts, bar graphs, pie charts) based on query results.
- Built with Azure OpenAI for advanced NLP capabilities.
- User-friendly interface powered by Streamlit.
- Setup Instructions
- Dependencies
- Environment Variables
- Database Setup
- Running the App
- Example Queries
- Directory Structure
git clone https://github.com/D-Harshith/Natural-Language-SQL-Chatbot-with-LangChain.gitInstall the required Python libraries listed in requirements.txt:
pip install -r requirements.txtCreate a .env file in the root directory and populate it with your Azure OpenAI credentials:
AZURE_OPENAI_ENDPOINT=<your_azure_endpoint>
AZURE_OPENAI_KEY=<your_azure_key>
AZURE_OPENAI_DEPLOYMENT_NAME=<your_deployment_name>
OPENAI_API_TYPE=azure
OPENAI_API_BASE=<your_api_base>
OPENAI_API_VERSION=<your_api_version>
OPENAI_API_KEY=<your_api_key>
Note: Replace placeholders with your actual credentials.
The project uses the Chinook MySQL database. Follow these steps to set it up:
db_url = "mysql+mysqlconnector://<username>:<password>@<host>:<port>/<database>"The following Python libraries are required:
streamlitlangchainopenaimysql-connector-pythonpython-dotenvpandas(for visualization support)plotly(for visualization support)
Install all dependencies using:
pip install -r requirements.txtEnsure your .env file contains the following variables:
AZURE_OPENAI_ENDPOINT=<your_azure_endpoint>
AZURE_OPENAI_KEY=<your_azure_key>
AZURE_OPENAI_DEPLOYMENT_NAME=<your_deployment_name>
OPENAI_API_TYPE=azure
OPENAI_API_BASE=<your_api_base>
OPENAI_API_VERSION=<your_api_version>
OPENAI_API_KEY=<your_api_key>
The project includes two main scripts:
-
SQL_chatbot.py- Description: This file accepts questions in natural language and provides answers in natural language.
- Run Command:
streamlit run SQL_chatbot.py
-
SQLwithVisualization.py- Description: This file accepts questions in natural language, provides responses in natural language, and generates appropriate visualizations (such as line charts, bar graphs, or pie charts) based on the question.
- Run Command:
streamlit run SQLwithVisualization.py
Once the app starts, open your browser and navigate to http://localhost:8501 to interact with the chatbot interface.
Here are some example questions you can ask the chatbot:
-
"How many artists are there?"
- SQL Query:
SELECT COUNT(*) AS NumberOfArtists FROM artist; - Response:
"There are a total of 275 artists in the database." - Visualization (in
SQLwithVisualization.py): Single-bar chart.
- SQL Query:
-
"How many employees are there?"
- SQL Query:
SELECT COUNT(*) AS NumberOfEmployees FROM employee; - Response:
"There are a total of 8 employees in the database." - Visualization (in
SQLwithVisualization.py): Single-bar chart.
- SQL Query:
-
"What percentage of total sales revenue comes from each genre?"
- Response:
"The sales revenue is distributed across various music genres, with Rock leading at approximately 35.50% of total sales..." - Visualization (in
SQLwithVisualization.py): Pie chart showing genre percentages.
- Response:
sql-chatbot/
├── .env # Environment variables (DO NOT UPLOAD THIS TO GITHUB)
├── requirements.txt # List of dependencies
├── README.md # Documentation for your project
├── SQL_chatbot.py # Script for natural language queries and responses
├── SQLwithVisualization.py # Script for queries, responses, and visualizations
├── database/ # Folder for database-related files
│ ├── Chinook.sql # SQL dump of the Chinook database schema/data
│ └── README.md # Instructions for setting up the database
└── .gitignore # Files to exclude from GitHub