Spaces:
Running
Running
metadata
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
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
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
python scripts/ingest.py
5. Launch the app
streamlit run app/main.py
Open http://localhost:7860 in your browser.
Running Tests
pytest tests/ -v
pytest tests/ -v --cov=app --cov=components --cov-report=term-missing
Deployment
HuggingFace Spaces (Free)
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