Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

19 Commits

Folders and files

Repository files navigation

SQL Chatbot: AI-Powered Natural Language Querying for Databases

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.

Features

  • Converts natural language questions into SQL queries.
  • Executes queries on a MySQL database (Chinook dataset).
  • 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.

Table of Contents

  1. Setup Instructions
  2. Dependencies
  3. Environment Variables
  4. Database Setup
  5. Running the App
  6. Example Queries
  7. Directory Structure

Setup Instructions

1. Clone the Repository

git clone https://github.com/D-Harshith/Natural-Language-SQL-Chatbot-with-LangChain.git

2. Install Dependencies

Install the required Python libraries listed in requirements.txt:

pip install -r requirements.txt

3. Set Up Environment Variables

Create 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.

4. Database Setup

The project uses the Chinook MySQL database. Follow these steps to set it up:

Use an Existing Database

db_url = "mysql+mysqlconnector://<username>:<password>@<host>:<port>/<database>"

Dependencies

The following Python libraries are required:

  • streamlit
  • langchain
  • openai
  • mysql-connector-python
  • python-dotenv
  • pandas (for visualization support)
  • plotly (for visualization support)

Install all dependencies using:

pip install -r requirements.txt

Environment Variables

Ensure 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>

Running the App

The project includes two main scripts:

  1. SQL_chatbot.py

    • Description: This file accepts questions in natural language and provides answers in natural language.
    • Run Command:
      streamlit run SQL_chatbot.py
  2. 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.


Example Queries

Here are some example questions you can ask the chatbot:

  1. "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.
  2. "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.
  3. "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.

Directory Structure

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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages