Skip to content

Latest commit

Β 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🦜 LangChain SQL Chatbot

A conversational SQL database chatbot built with Streamlit, LangChain, Groq, and Qwen 3.6 27B.

The application allows users to interact with SQL databases using natural language instead of writing SQL queries manually. For example, users can simply ask:

β€œShow me all students with marks above 80.”

The LangChain SQL Agent interprets the question, determines the required database operation, generates and executes the appropriate SQL query, and returns the result in a human-readable format.

πŸš€ Features

  • πŸ’¬ Ask questions about SQL databases using natural language
  • πŸ€– Powered by Qwen 3.6 27B through Groq
  • 🦜 Built using the LangChain SQL Agent
  • πŸ—„οΈ SQLite database support
  • 🐬 MySQL database support
  • πŸ”§ SQLDatabaseToolkit for database operations
  • πŸ“Š Automatic SQL query generation and execution
  • πŸ–₯️ Interactive Streamlit interface
  • πŸ’Ύ Chat history using Streamlit session state
  • 🧹 Clear conversation history
  • πŸ” Secure runtime Groq API key input through the sidebar

πŸ› οΈ Tech Stack

Technology Purpose
Python Core programming language
Streamlit Web interface
LangChain Agent and LLM orchestration
Groq LLM inference platform
Qwen 3.6 27B Language model
SQLite 3 Local database
MySQL Optional database
SQLAlchemy Database connection layer

🧠 How It Works

User
  ↓
Streamlit Chat Interface
  ↓
LangChain SQL Agent
  ↓
Qwen 3.6 27B via Groq
  ↓
SQLDatabaseToolkit
  ↓
SQLDatabase
  ↓
SQLite / MySQL
  ↓
SQL Query Result
  ↓
Natural Language Response

The user does not need to write SQL manually. The agent determines what information is required, inspects the database schema, selects the appropriate SQL tools, executes the query, and converts the result into a natural-language response.

πŸ“Έ Screenshots

🏠 Main Application

LangChain SQL Chatbot Main Page

The main interface allows users to select their database and provide their Groq API key before starting a conversation with the database.

πŸ—„οΈ SQLite Database Response

SQLite Database Response

The SQL Agent understands natural-language questions, inspects the SQLite database, generates the required SQL query, executes it, and returns the result.

Example:

Show me all the records from the table.

πŸ’» Local Database

Local Database Chat

The application can also be configured to work with a local database, allowing users to interact with stored data through natural-language questions.

πŸ“‚ Project Structure

Chatbot-sql/
β”‚
β”œβ”€β”€ app.py
β”œβ”€β”€ student.db
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ README.md
β”‚
└── assets/
    β”œβ”€β”€ main-page.png
    β”œβ”€β”€ sqlite-response.png
    └── local-database.png

The student.db file contains the local SQLite database used by the application.

βš™οΈ Installation

1. Clone the Repository

git clone https://github.com/amnsingh05/Chatbot-sql.git
cd Chatbot-sql

2. Create a Virtual Environment

python -m venv chatbot-sql

3. Activate the Virtual Environment

Windows:

chatbot-sql\Scripts\activate

4. Install Dependencies

pip install -r requirements.txt

If requirements.txt is not available, install the required packages manually:

pip install streamlit langchain langchain-community langchain-classic langchain-groq sqlalchemy

πŸ”‘ Groq API Key

This project uses Groq for LLM inference.

Enter your Groq API key through the Groq API Key field in the Streamlit sidebar.

The API key is provided at runtime and does not need to be hard-coded into the source code.

Never commit your actual API key to GitHub.

Avoid code such as:

ChatGroq(
    groq_api_key="YOUR_ACTUAL_API_KEY"
)

For production deployments, use environment variables or Streamlit Secrets to manage credentials securely.

πŸ€– Model

The project currently uses:

qwen/qwen3.6-27b

The model is accessed through Groq and provides the language and tool-calling capabilities required by the SQL agent.

▢️ Run the Application

From the project directory, run:

streamlit run app.py

Alternatively:

python -m streamlit run app.py

Streamlit will start the application and provide a local URL, typically:

http://localhost:8501

Open the URL in your browser to access the chatbot.

πŸ—„οΈ SQLite Support

The project includes support for a local SQLite database.

The default database file is:

student.db

The application locates the database relative to the project directory:

dbfilepath = (Path(__file__).parent / "student.db").absolute()

Once connected, users can ask natural-language questions about the database.

Example Questions

Show all students.

Which student has the highest marks?

Show students who scored more than 80 marks.

How many students are in the database?

What is the average marks of all students?

🐬 MySQL Support

The application also provides an option to connect to a MySQL database.

When MySQL is selected, the application requests:

  • MySQL Host
  • MySQL Username
  • MySQL Password
  • MySQL Database Name

The connection is established using SQLAlchemy, allowing the LangChain SQL components to interact with the MySQL database.

πŸ”§ Main LangChain Components

SQLDatabase

Provides LangChain with an interface for connecting to and interacting with the SQL database.

SQLDatabaseToolkit

Provides database-related tools that the SQL agent can use to inspect tables, retrieve schema information, and execute SQL queries.

create_sql_agent

Creates the SQL agent responsible for understanding natural-language requests and interacting with the database through the available SQL tools.

ChatGroq

Connects LangChain to the Groq API and the selected Qwen model.

Together, these components allow the application to transform:

Natural Language
       ↓
SQL Agent
       ↓
SQL Query
       ↓
Database
       ↓
Result
       ↓
Natural Language Answer

πŸ” Security

Never upload your Groq API key to GitHub.

Do not hard-code sensitive credentials into your source code.

For example, avoid storing your actual API key directly inside:

ChatGroq(
    groq_api_key="YOUR_API_KEY"
)

For local development, runtime input or environment variables can be used.

For public deployments, use Streamlit Secrets or environment variables to protect sensitive credentials.

🚧 Future Improvements

Possible future enhancements include:

  • πŸ“€ Upload custom SQLite databases
  • πŸ‘₯ Support separate databases for different users
  • 🌐 Deploy the application online
  • πŸ” Improve API-key management with Streamlit Secrets
  • πŸ“ Support additional database formats
  • πŸ“Š Add database schema visualization
  • 🧠 Improve SQL-agent error handling
  • πŸ“ Provide better explanations of generated queries and results
  • πŸ”’ Add stronger database access controls
  • πŸ“ˆ Add data visualization for query results

🌐 Planned Online Architecture

A future online version could allow each user to upload and interact with their own SQLite database.

User
  ↓
Online Streamlit Application
  ↓
Upload SQLite Database
  ↓
Temporary User Database
  ↓
LangChain SQL Agent
  ↓
Qwen 3.6 27B via Groq
  ↓
SQL Query
  ↓
SQL Results
  ↓
Natural Language Response

This architecture would prevent users from sharing the developer's local student.db and allow each user to work with their own database.

πŸ‘¨β€πŸ’» Author

Aman Singh

B.Tech CSE | Generative AI Developer

Connect With Me

  • πŸ’Ό LinkedIn: linkedin.com/in/amnsingh0
  • πŸ™ GitHub: github.com/amnsingh05

⭐ Support

If you find this project useful, consider giving the repository a ⭐ on GitHub.

πŸ“„ License

This project is intended for educational and personal use.

If you plan to distribute or modify the project publicly, consider adding an appropriate open-source license such as the MIT License.

About

Conversational SQL chatbot built with LangChain, Streamlit, Groq, and Qwen 3.6 27B. Query SQLite & MySQL databases using natural language.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages