Skip to content

joshuvavinith/AI_ChatBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

36 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– AI ChatBot

CI Python License

A modern, extensible Python chatbot that generates context-aware, dynamic responses. It ships with two interchangeable backends and three ready-to-use interfaces β€” no external services are required to get started.

Mode When active What it uses
LLM OPENAI_API_KEY is set OpenAI Chat Completions (GPT-3.5 / GPT-4o)
Pattern Matching No API key present Offline CSV dialog dataset

Three interfaces are included out of the box:

Interface Entry point Default URL
Streamlit web app streamlit run web_demo.py http://localhost:8501
FastAPI REST API uvicorn api:app --reload http://localhost:8000/docs
Tkinter desktop GUI python ai_chatbot.py (native window)

πŸ“š Table of Contents


✨ Key Features

  • πŸ€– LLM backend β€” connects to OpenAI's API for intelligent, context-aware responses
  • πŸ“‹ Offline fallback β€” pattern matching on a dialog dataset; works without internet/API key
  • 🌐 Streamlit web UI β€” chat from any browser with streaming token output (LLM mode)
  • πŸ”Œ FastAPI REST API β€” /chat and /train endpoints; per-session conversation memory
  • πŸ–₯️ Tkinter desktop GUI β€” original GUI updated to show backend mode
  • 🧠 Conversation memory β€” recent exchanges are passed to the LLM for follow-up questions
  • 🐳 Docker support β€” single image supports both web and API modes via MODE build arg
  • βœ… Tests β€” pytest suite covering core logic and API endpoints
  • πŸ”„ CI/CD β€” GitHub Actions workflow: lint β†’ test β†’ Docker build

πŸš€ Quick Start

git clone https://github.com/joshuvavinith/AI_ChatBot.git
cd AI_ChatBot
pip install -r requirements.txt

# (optional) enable LLM mode
echo "OPENAI_API_KEY=sk-..." > .env

# Start the web UI
streamlit run web_demo.py

Prerequisites

  • Python 3.10 or later (tested on 3.10, 3.11, and 3.12)
  • pip (included with Python)
  • (Optional) An OpenAI API key to enable LLM mode
  • (Optional) Docker for containerised deployment

πŸ”§ Installation

1. Clone & set up environment

git clone https://github.com/joshuvavinith/AI_ChatBot.git
cd AI_ChatBot
python -m venv venv
source venv/bin/activate   # Windows: venv\Scripts\activate
pip install -r requirements.txt

2. (Optional) Configure API key

Create a .env file in the project root:

OPENAI_API_KEY=sk-your-key-here

Or export it as an environment variable:

export OPENAI_API_KEY=sk-your-key-here

Without an API key the bot automatically falls back to offline pattern matching.


βš™οΈ Configuration

Variable Default Description
OPENAI_API_KEY (unset) Enables LLM mode when present

Kaggle Dataset (optional)

The pattern-matching bot can use a richer Kaggle dialog dataset. To enable it, place kaggle.json in ~/.kaggle/ (or set KAGGLE_USERNAME / KAGGLE_KEY). If unavailable, the bot falls back to dialog.csv.


πŸ’¬ Running the Chatbot

πŸ–₯️ Desktop GUI (Tkinter)

python ai_chatbot.py

🌐 Web UI (Streamlit)

streamlit run web_demo.py

Open your browser at http://localhost:8501.

Features:

  • Full conversation history
  • Streaming token output in LLM mode (looks like ChatGPT)
  • "Clear conversation" button in the sidebar

πŸ”Œ REST API (FastAPI)

uvicorn api:app --reload

Interactive docs available at http://localhost:8000/docs.


🐳 Docker

Build

# Web UI (default)
docker build -t ai-chatbot:web .

# API mode
docker build --build-arg MODE=api -t ai-chatbot:api .

Run

# Web UI β€” visit http://localhost:8501
docker run -p 8501:8501 -e OPENAI_API_KEY=sk-... ai-chatbot:web

# REST API β€” visit http://localhost:8000/docs
docker run -p 8000:8000 -e OPENAI_API_KEY=sk-... ai-chatbot:api

πŸ“‘ API Reference

POST /chat

Send a message and get a reply. Omit session_id to start a new session.

// Request
{ "message": "Hello!", "session_id": "optional-uuid" }

// Response
{
  "reply": "Hi there! How can I help you?",
  "session_id": "550e8400-e29b-41d4-a716-446655440000",
  "mode": "pattern"
}

DELETE /sessions/{session_id}

Reset (delete) a conversation session.

POST /train

Reload pattern-matching data from a CSV file on the server.

// Request
{ "dialog_file": "/path/to/dialog.csv" }

// Response
{ "status": "retrained", "patterns_loaded": 42 }

GET /health

{ "status": "ok" }

πŸ§ͺ Testing

pytest test_chatbot.py -v

The test suite covers:

  • SimpleBot β€” training, exact/partial matching, defaults, missing file
  • ChatBot β€” offline mode, history management, streaming, history cap, retraining
  • FastAPI β€” all endpoints (health, chat, delete session, train)

πŸ”„ CI/CD

GitHub Actions runs on every push and pull request to main:

  1. Lint β€” flake8 checks for syntax errors and undefined names
  2. Test β€” pytest full suite with coverage, across Python 3.10, 3.11, and 3.12
  3. Docker build β€” both web and api images are built to verify the Dockerfile

Coverage reports are uploaded as build artifacts for each Python version.


πŸ“‚ Project Structure

AI_ChatBot/
β”œβ”€β”€ ai_chatbot.py      # Core module: SimpleBot, LLMBot, ChatBot facade, Tkinter GUI
β”œβ”€β”€ api.py             # FastAPI REST backend
β”œβ”€β”€ web_demo.py        # Streamlit web interface
β”œβ”€β”€ dialog.csv         # Default offline dialog dataset
β”œβ”€β”€ test_chatbot.py    # Pytest test suite (SimpleBot, ChatBot, FastAPI)
β”œβ”€β”€ requirements.txt   # Python dependencies
β”œβ”€β”€ Dockerfile         # Multi-mode Docker image (web / api)
β”œβ”€β”€ LICENSE            # MIT License
└── .github/
    └── workflows/
        └── ci.yml     # CI pipeline: lint β†’ test β†’ Docker build

πŸ“ Architecture

+------------------+     +------------------+     +------------------+
|  Streamlit Web   |     |  FastAPI REST     |     |  Tkinter Desktop |
|  (web_demo.py)   |     |  (api.py)         |     |  (ai_chatbot.py) |
+--------+---------+     +--------+---------+     +--------+---------+
         |                        |                         |
         +------------------------+-------------------------+
                                  |
                         +--------v---------+
                         |    ChatBot       |  ← ai_chatbot.py
                         |  (facade)        |
                         +--+----------+----+
                            |          |
               +------------+          +------------+
               |                                    |
      +--------v---------+              +-----------v------+
      |   LLMBot         |              |   SimpleBot      |
      |  (OpenAI API)    |              |  (CSV patterns)  |
      +------------------+              +------------------+

🀝 Contributing

  1. Fork this repository
  2. Create a feature branch: git checkout -b feature/my-feature
  3. Make your changes and run pytest test_chatbot.py -v
  4. Commit and push: git push origin feature/my-feature
  5. Open a pull request

Please follow PEP 8 and include tests for any new logic.

Adding dialog patterns

To extend the offline pattern-matching bot, add rows to dialog.csv. Each question/answer pair uses two rows sharing the same dialog_id:

dialog_id,line_id,text
8,1,What is Python?
8,2,Python is a popular programming language!
  • line_id 1 = the user question (matched case-insensitively)
  • line_id 2 = the bot response

πŸ“„ License

MIT License


πŸ”— Connect

About

A Python-based chatbot leveraging machine learning and natural language processing to generate context-aware, dynamic responses. Easily customizable and capable of continuous learning from user interactions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors