Skip to content

dheepakshakthi/168_rag_chatbot

Repository files navigation

# 168 - General Purpose RAG Chatbot

A beautiful, intelligent AI chatbot with Retrieval-Augmented Generation (RAG) capabilities. Upload your documents (PDFs, HTML, code files) and chat with an AI that understands your content, or use it as a general-purpose chatbot without any documents.

🌟 Features

  • Dual Mode Operation

    • 🟒 RAG Mode: Upload documents and get AI responses based on your content
    • πŸ”΅ General Mode: Chat with AI without any documents loaded
  • Multiple File Format Support

    • πŸ“„ PDF Documents
    • 🌐 HTML Files (.html, .htm)
    • πŸ’» Code Files (.py, .java, .js, .cpp, .c, .h, .cs, .rb, .go, .rs, .php, .swift, .kt, .ts)
    • πŸ“ Text & Markdown (.txt, .md)
  • Beautiful Web Interface

    • Modern, responsive design
    • File upload with drag-and-drop support
    • Real-time chat with typing indicators
    • Source citations for RAG responses
  • Powerful Technology Stack

    • πŸ€– Ollama (SmolLM2) for AI responses
    • πŸ” ChromaDB for vector storage
    • πŸ“Š Sentence Transformers for embeddings
    • 🌐 Flask for web interface

πŸš€ Quick Start

Prerequisites

  1. Python 3.8+ installed on your system
  2. Ollama installed and running (Download Ollama)
  3. Pull the SmolLM2 model:
    ollama pull smollm2:135m # or for better quality (CLI only): ollama pull smollm2:1.7b

Installation

  1. Clone or download this repository

  2. Install dependencies:

    pip install -r requirements.txt
  3. Start the application:

    python app.py
  4. Open your browser: Navigate to http://localhost:5000

πŸ“– Usage

Web Interface

  1. Upload Documents (Optional)

    • Click the "Upload Files" button
    • Select one or more files (PDF, HTML, code files, etc.)
    • Wait for processing (creates vector embeddings)
    • RAG mode is now active! 🟒
  2. Chat with the AI

    • Type your question in the input box
    • Press Enter to send
    • Get responses based on your documents (RAG mode) or general AI knowledge
  3. Manage Documents

    • Clear Docs: Remove all uploaded documents and return to general mode
    • Clear Chat: Clear the conversation history

Command Line Interface

Run the chatbot in terminal mode:

python chatbot.py

Commands:

  • Type your questions naturally
  • sources - View sources used in the last RAG response
  • quit or exit - Exit the chatbot

Manual Document Ingestion

To manually process documents from the data/ folder:

  1. Place your files in the data/ directory
  2. Run:
    python ingest_documents.py

πŸ“ Project Structure

168_rag_chatbot/ β”œβ”€β”€ app.py # Flask web application β”œβ”€β”€ chatbot.py # CLI chatbot interface β”œβ”€β”€ ingest_documents.py # Document processing script β”œβ”€β”€ requirements.txt # Python dependencies β”œβ”€β”€ data/ # Upload folder for documents β”œβ”€β”€ chroma_db/ # Vector database storage β”œβ”€β”€ templates/ β”‚ β”œβ”€β”€ index.html # Main chat interface β”‚ └── error.html # Error page └── static/ β”œβ”€β”€ style.css # Styling └── script.js # Frontend logic 

πŸ”§ Configuration

Edit the configuration variables in app.py, chatbot.py, or ingest_documents.py:

# Model Configuration OLLAMA_MODEL = "smollm2:135m" # Ollama model to use EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2" # Embeddings model TOP_K_RESULTS = 4 # Number of context chunks to retrieve # Paths DATA_PATH = "data/" # Document upload folder CHROMA_PATH = "chroma_db" # Vector database location

🎯 Use Cases

  • Learning Assistant: Upload course materials and get answers based on your lessons
  • Code Documentation: Upload code files and ask questions about implementation
  • Research Helper: Process research papers and get insights
  • Knowledge Base: Create a searchable knowledge base from your documents
  • General Chat: Use without documents for general AI assistance

πŸ”’ Privacy

  • All processing happens locally on your machine
  • Documents are stored locally in the data/ folder
  • No data is sent to external services (except Ollama API calls)

πŸ› οΈ Troubleshooting

"Ollama connection error"

  • Make sure Ollama is running: ollama serve
  • Verify the model is downloaded: ollama list

"No module named 'xyz'"

  • Install dependencies: pip install -r requirements.txt

Documents not loading

  • Check that files are in supported formats
  • Ensure files are not corrupted
  • Check console output for specific errors

Vector database errors

  • Clear and recreate: Click "Clear Docs" in the web interface
  • Or manually delete the chroma_db folder

πŸ“ Development

To modify or extend the chatbot:

  1. Change AI Model: Edit OLLAMA_MODEL in the configuration
  2. Add File Types: Extend the loaders in ingest_documents.py
  3. Customize UI: Modify templates/index.html and static/style.css
  4. Adjust RAG Behavior: Change chunk size, overlap, or retrieval count

🀝 Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests

πŸ“„ License

This project is open source and available under the MIT License.

πŸ™ Acknowledgments


Made with ❀️ using Python, Flask, and AI for Coding Questions

A Retrieval-Augmented Generation (RAG) chatbot that answers coding questions based on your PDF learning materials using the SmolLM2:1.7b model from Ollama.

Features

  • πŸ“š PDF Processing: Automatically processes PDF documents from the data/ folder
  • πŸ” Semantic Search: Uses vector embeddings to find relevant content
  • πŸ€– Local LLM: Powered by SmolLM2:1.7b via Ollama (runs locally)
  • πŸ’¬ Interactive Chat: Simple command-line interface for asking questions
  • 🎯 Context-Aware: Answers based on your specific learning materials

Prerequisites

  1. Python 3.8+ installed
  2. Ollama installed with SmolLM2:1.7b model

Setup

1. Install Dependencies

# Activate virtual environment .\master\Scripts\activate # Install required packages pip install -r requirements.txt

2. Prepare Your Data

Place your PDF files in the data/ folder. Currently includes:

  • learning_java.pdf
  • Learning_Python.pdf

3. Ingest Documents

Process the PDFs and create the vector database:

python ingest_documents.py

This will:

  • Load all PDF files from the data/ folder
  • Split them into manageable chunks
  • Create embeddings using sentence-transformers
  • Store them in a ChromaDB vector database

Usage

Start the Chatbot

python chatbot.py

Example Questions

  • "How do I create a list in Python?"
  • "What is inheritance in Java?"
  • "Explain Python decorators"
  • "How do I handle exceptions in Java?"
  • "What are Python list comprehensions?"
  • "Explain Java interfaces"

Commands

  • Type your question and press Enter
  • Type sources to see the source documents for the last answer
  • Type quit or exit to end the conversation

How It Works

  1. Document Ingestion (ingest_documents.py):

    • Loads PDF files
    • Splits into chunks (1000 chars with 200 overlap)
    • Creates embeddings using all-MiniLM-L6-v2
    • Stores in ChromaDB
  2. RAG Pipeline (chatbot.py):

    • Takes user question
    • Finds relevant chunks using semantic search
    • Creates context from top 4 results
    • Sends context + question to SmolLM2:1.7b
    • Returns contextual answer

Project Structure

rag_chatbot/ β”œβ”€β”€ data/ # PDF documents β”‚ β”œβ”€β”€ learning_java.pdf β”‚ └── Learning_Python.pdf β”œβ”€β”€ chroma_db/ # Vector database (created after ingestion) β”œβ”€β”€ master/ # Virtual environment β”œβ”€β”€ ingest_documents.py # Document processing script β”œβ”€β”€ chatbot.py # Main chatbot application β”œβ”€β”€ requirements.txt # Python dependencies └── README.md # This file 

Troubleshooting

"Vector store not found" error

Run python ingest_documents.py first to create the vector database.

Ollama connection error

  • Make sure Ollama is running
  • Verify the model is installed: ollama list
  • Pull the model if needed: ollama pull smollm2:1.7b

Out of memory errors

  • Reduce TOP_K_RESULTS in chatbot.py (default: 4)
  • Reduce chunk_size in ingest_documents.py (default: 1000)

Customization

Change the LLM Model

Edit OLLAMA_MODEL in chatbot.py:

OLLAMA_MODEL = "your-model-name"

Adjust Chunk Size

Edit chunk_size in ingest_documents.py:

chunk_size=1000, # Increase or decrease chunk_overlap=200 # Adjust overlap

Change Number of Retrieved Documents

Edit TOP_K_RESULTS in chatbot.py:

TOP_K_RESULTS = 4 # Increase for more context

Adding More Documents

  1. Add PDF, text, or code files to the data/ folder
  2. Re-run the ingestion: python ingest_documents.py
  3. The chatbot will now include the new materials

License

MIT License - Feel free to use and modify as needed!

About

RAG based chatbot

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published