Skip to content

MUBENDIRAN/Mube-ChatBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Mube's Chatbot - FastAPI & Groq LLM

A full-featured chatbot application built with FastAPI, WebSocket streaming, SQLite persistence, and Groq LLM API integration. Features multi-chat support with intelligent memory management and responsive UI for desktop and mobile.

✨ Features

πŸ€– Core AI Features

  • Groq LLM Integration: Real-time streaming responses with Groq API
  • Sliding Window Memory: Intelligent context management (6 most recent messages)
  • ~85% Token Reduction: Optimized memory window reduces token usage significantly
  • System Prompt: Fresh system context with each request for consistent personality

πŸ’Ύ Data Persistence

  • SQLite Database: Persistent chat storage with automatic initialization
  • Multi-Chat Support: Create, manage, and switch between multiple conversations
  • Chat Lifecycle: Lazy creation (no DB clutter), auto-naming from first message
  • Chat Management: Delete and rename conversations

πŸ‘₯ User Management

  • Device Isolation: Browser-generated UUID stored in localStorage
  • User Filtering: Each device/browser gets unique user_id
  • Multi-Device Support: One person can use multiple devices with separate chat histories
  • No Authentication Required: Suitable for personal/dev use (add auth for production)

🎨 UI/UX Features

  • Multi-Chat Sidebar: Quick access to all conversations
  • Sidebar Toggle: Collapse/expand on all screen sizes for more chat space
  • Responsive Design: Works perfectly on desktop, tablet, and mobile
  • Real-Time Streaming: See bot responses appear character-by-character
  • Message History: Load previous conversations instantly
  • Auto-Chat Creation: Start chatting immediately without "New Chat" button
  • Smooth Animations: Professional 0.3s transitions

πŸ”§ Technical Features

  • WebSocket Streaming: Real-time message streaming without polling
  • Lazy Chat Creation: Chat only inserted when first message arrives
  • Efficient Queries: Filtered queries using EXISTS subqueries
  • Error Handling: Comprehensive error handling on frontend and backend
  • No Breaking Changes: Fully backward compatible

πŸš€ Quick Start

Prerequisites

Installation

  1. Clone the repository
git clone <repo-url> cd chatbot-fastapi
  1. Create virtual environment
python3 -m venv fastapivenv source fastapivenv/bin/activate # On Windows: fastapivenv\Scripts\activate
  1. Install dependencies
pip install -r requirements.txt
  1. Configure Groq API
export GROQ_API_KEY="your-api-key-here"
  1. Run the application
python app.py

The app will be available at http://localhost:8000

πŸ“ Project Structure

chatbot-fastapi/ β”œβ”€β”€ app.py # FastAPI application, WebSocket handler, endpoints β”œβ”€β”€ database.py # SQLite database setup and utility functions β”œβ”€β”€ requirements.txt # Python dependencies β”œβ”€β”€ chat.db # SQLite database (auto-created) β”œβ”€β”€ templates/ β”‚ └── index.html # Frontend HTML β”œβ”€β”€ static/ β”‚ β”œβ”€β”€ script.js # Frontend JavaScript (user ID, WebSocket, chat logic) β”‚ β”œβ”€β”€ style.css # Styling and responsive design β”‚ └── mube-chat-bot-logo.jpeg └── README.md # This file 

πŸ”Œ API Endpoints

HTTP Endpoints

POST /new_chat

  • Creates a new chat session
  • Request body: {"user_id": "uuid"}
  • Response: {"chat_id": "uuid"}
  • Note: Chat is not inserted in DB until first message arrives

GET /chats/{user_id}

  • Retrieves all chats for a specific user
  • Returns only chats with messages (no empty chats)
  • Response: [{"chat_id": "...", "title": "..."}, ...]

GET /chat/{chat_id}

  • Retrieves message history for a specific chat
  • Response: [{"role": "user", "content": "..."}, ...]

DELETE /chat/{chat_id}

  • Deletes a chat and all its messages
  • Response: {"status": "deleted"}

PUT /chat/{chat_id}/rename

  • Updates chat title
  • Request body: {"title": "new title"}
  • Response: Updated chat metadata

WebSocket Endpoint

GET /ws

  • WebSocket connection for real-time chat
  • Message format: {"user_id": "uuid", "chat_id": "uuid", "content": "message"}
  • Streams response tokens in real-time

πŸ’Ύ Database Schema

chats table

CREATE TABLE chats ( chat_id TEXT PRIMARY KEY, user_id TEXT NOT NULL, title TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )

messages table

CREATE TABLE messages ( id INTEGER PRIMARY KEY AUTOINCREMENT, chat_id TEXT, role TEXT, content TEXT, timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY(chat_id) REFERENCES chats(chat_id) )

πŸ§ͺ Testing

Manual Testing

  1. Open http://localhost:8000 in your browser
  2. Chat should work immediately (auto-creates first chat)
  3. Switch between chats using sidebar
  4. Toggle sidebar with ☰ button
  5. Open another browser tab/profile to test user isolation

Test Scenarios

  • Single user, multiple devices βœ…
  • Multiple browser profiles βœ…
  • Chat switching βœ…
  • Message history loading βœ…
  • Sidebar toggle βœ…
  • Real-time streaming βœ…

πŸ“Š Performance

Optimizations

  • Sliding Window Memory: 85% token reduction vs full history
  • Lazy Chat Creation: No database clutter from abandoned chats
  • Efficient Queries: Uses EXISTS subqueries to filter empty chats
  • Hardware-Accelerated CSS: Smooth animations at 60fps

Metrics

  • Response Time: ~1-3 seconds (depends on Groq API)
  • Database: Minimal size with smart filtering
  • Frontend: <50KB total JavaScript + CSS
  • Network: ~36 bytes per user_id overhead

πŸš€ Deployment

Render.com Deployment

  1. Push code to GitHub
  2. Connect repository to Render
  3. Set environment variable: GROQ_API_KEY
  4. Deploy (auto-creates database)

Local Deployment

python app.py # Runs on localhost:8000

Happy Chatting! πŸš€