rag-chatbot / README.md
Mobiworks's picture
Sync from GitHub via hub-sync
3779ff3 verified
|
Raw
History Blame Contribute Delete
7.94 kB
---
title: RAG Chatbot
colorFrom: blue
colorTo: purple
sdk: docker
pinned: false
---
# πŸ€– RAG Chatbot
A fully open-source, free-to-deploy **Retrieval-Augmented Generation (RAG)** chatbot.
Upload your documents, ask questions, and get grounded answers with source citations β€” no paid APIs required.
**Live demo:** https://huggingface.co/spaces/Mobiworks/rag-chatbot
---
## Architecture
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ RAG Pipeline β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” load β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” split β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ PDF/TXT/ │──────────►│ document_ │─────────►│ text_ β”‚
β”‚ DOCX/HTMLβ”‚ β”‚ loader.py β”‚ β”‚ splitter.py β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ chunks
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ embedder.py β”‚
β”‚ all-MiniLM β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ vectors
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ vector_store │◄─── load / save
β”‚ (FAISS) β”‚ data/vector_db/
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β–²
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” query vector β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ top-K chunks
β”‚ User Query │────────────────►│ retriever β”‚β”€β”€β”€β”€β”€β”€β”˜
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚ prompt_template β”‚
β”‚ β”‚ context + question β†’ str β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚ formatted prompt
β”‚ β–Ό
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚ llm_handler.py β”‚
β”‚ β”‚ Phi-2 Q4 / Mistral-7B β”‚
β”‚ β”‚ (llama-cpp-python GGUF) β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚
β–Ό β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ app/main.py (Streamlit UI) β”‚
β”‚ chat history β€’ source citations β€’ doc upload β”‚
β”‚ clear chat button β€’ top-k slider β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```
---
## Folder Structure
```
rag-chatbot/
β”œβ”€β”€ app/
β”‚ β”œβ”€β”€ __init__.py
β”‚ β”œβ”€β”€ main.py # Streamlit UI entry point
β”‚ β”œβ”€β”€ chatbot.py # Orchestrates retriever + LLM chain
β”‚ └── config.py # All config constants
β”œβ”€β”€ components/
β”‚ β”œβ”€β”€ __init__.py
β”‚ β”œβ”€β”€ document_loader.py # Load & parse PDF / TXT / DOCX / HTML
β”‚ β”œβ”€β”€ text_splitter.py # Chunking with overlap
β”‚ β”œβ”€β”€ embedder.py # HuggingFace embedding wrapper
β”‚ β”œβ”€β”€ vector_store.py # FAISS create / save / load
β”‚ β”œβ”€β”€ retriever.py # Similarity search, top-K logic
β”‚ β”œβ”€β”€ llm_handler.py # LLM loading & inference
β”‚ └── prompt_template.py # RAG prompt construction
β”œβ”€β”€ data/
β”‚ β”œβ”€β”€ raw/ # Place your source documents here
β”‚ └── vector_db/ # Persisted FAISS index (auto-created)
β”œβ”€β”€ scripts/
β”‚ β”œβ”€β”€ ingest.py # One-time ingestion script
β”‚ └── evaluate.py # Basic eval: retrieval accuracy + latency
β”œβ”€β”€ tests/
β”‚ β”œβ”€β”€ test_loader.py
β”‚ β”œβ”€β”€ test_retriever.py
β”‚ └── test_chatbot.py
β”œβ”€β”€ .streamlit/
β”‚ └── config.toml # Streamlit server config
β”œβ”€β”€ .env.example
β”œβ”€β”€ .gitignore
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ requirements.txt
└── README.md
```
---
## Features
- **Upload documents** β€” PDF, TXT, DOCX supported
- **Auto-ingestion** β€” Documents in `data/raw/` are ingested automatically on startup
- **Source citations** β€” Every answer shows which document chunks were used with similarity scores
- **Clear chat** β€” Reset the conversation with one click
- **Top-K slider** β€” Control how many chunks are retrieved per query (1–10)
- **Persistent vector store** β€” FAISS index saved to disk, no re-embedding on restart
---
## Quick Start (Local)
### 1. Clone & install
```bash
git clone https://github.com/mmubasharmug-18/rag-chatbot.git
cd rag-chatbot
python -m venv venv && source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
```
### 2. Set environment variables
```bash
cp .env.example .env
# Edit .env β€” HF_TOKEN is only needed for gated models
```
### 3. Add your documents
Place any PDF, TXT, or DOCX files in `data/raw/`.
### 4. Ingest documents
```bash
python scripts/ingest.py
```
### 5. Launch the app
```bash
streamlit run app/main.py
```
Open http://localhost:7860 in your browser.
---
## Running Tests
```bash
pytest tests/ -v
pytest tests/ -v --cov=app --cov=components --cov-report=term-missing
```
---
## Deployment
### HuggingFace Spaces (Free)
```bash
git remote add space https://huggingface.co/spaces/Mobiworks/rag-chatbot
git push space main --force
```
Add secrets under **Settings β†’ Variables and secrets** if needed.
---
## Stack
| Component | Tool |
|-----------------|-----------------------------------------|
| RAG Framework | LangChain |
| Embedding Model | `all-MiniLM-L6-v2` (sentence-transformers) |
| Vector Store | FAISS (local, persisted to disk) |
| LLM | Phi-2 Q4_K_M (GGUF) |
| LLM Runtime | llama-cpp-python |
| Document Loaders| PyMuPDF, docx2txt, unstructured |
| UI | Streamlit |
| Deployment | HuggingFace Spaces / Docker |
| Cost | **$0 β€” 100% free & open-source** |
---
## License
MIT