A small end‑to‑end Retrieval‑Augmented Generation (RAG) system:
upload a PDF, index it into MongoDB + FAISS, and chat with an LLM over its content through a web UI.
- PDF upload (drag & drop or file picker)
- Text extraction + chunking
- Semantic search with FAISS
- Answer generation with GPT‑2
- MongoDB document storage
- FastAPI backend + simple HTML/JS frontend
- Python, FastAPI, Uvicorn
- MongoDB
- FAISS (CPU)
- SentenceTransformers (
all-MiniLM-L6-v2) - Transformers (GPT‑2)
- PyPDF
- Vanilla HTML/CSS/JS
'''bash rag_mongo_faiss/ ├── app/ │ ├── main.py # FastAPI app (upload + ask endpoints, serve UI) │ ├── db/ │ │ └── mongo_client.py # MongoDB client │ ├── retrieval/ │ │ └── retriever.py # FAISS index + search │ ├── generation/ │ │ └── llm.py # GPT‑2 generation │ ├── utils/ │ │ └── pdf_parser.py # PDF parsing + chunking │ └── static/ │ └── index.html # Web UI (upload + chat) ├── requirements.txt ├── .gitignore └── README.md '''
git clone https://github.com/maryemchk/rag_mongo_faiss.git cd rag_mongo_faiss
python -m venv venv
Windows: venv\Scripts\activate
Linux/macOS: source venv/bin/activate
pip install -r requirements.txt
Use a local MongoDB instance, e.g.:
mongod
(or a running MongoDB service).
The app uses:
- DB:
rag_demo - Collection:
documents
(see app/db/mongo_client.py)
python -m app.main
You should see Uvicorn running on http://0.0.0.0:8000.

-
Open your browser at:
http://localhost:8000
-
In the left panel:
- Upload a PDF (drag & drop or click)
- The backend:
- extracts text with PyPDF
- chunks it
- stores chunks in MongoDB
- rebuilds the FAISS index
-
In the chat box (right side):
- Ask a question about the uploaded PDF
- The backend:
- embeds your question
- retrieves similar chunks with FAISS
- builds a context string
- calls GPT‑2 to generate an answer
-
You can also inspect the API:
http://localhost:8000/docs
- Encodes all document chunks with SentenceTransformers
- Builds an in‑memory FAISS index
- Supports rebuilding the index after new uploads
search(query, k)returns top‑k similar chunks from MongoDB
- Loads GPT‑2 via Hugging Face Transformers
- Builds a simple prompt:
Context:
Question: Answer:
- Generates a short answer from the context
- Extracts raw text per page with
pypdf.PdfReader - Splits text into overlapping chunks (size ~512 chars, small overlap)
- Returns a list of text chunks ready to embed and store
GET /health– simple health checkPOST /upload– upload a PDF (multipart/form-data, field namefile)POST /ask– ask a question ({"question": "...", "top_k": 3})GET /docs– FastAPI Swagger UI/– serves the HTML UI
- GPT‑2 is small and can hallucinate; it’s used here as a lightweight local demo.
- FAISS index is rebuilt in memory when new PDFs are uploaded (okay for small demos).
- No auth, no multi‑user separation (demo only).
- Use a stronger instruction‑tuned model instead of GPT‑2
- Persist FAISS index to disk or use a vector DB
- Add document listing/deletion in the UI
- Better chunking (sentence/paragraph based)
- Hybrid retrieval (BM25 + FAISS) and reranking
This project is mainly for learning and portfolio: it shows end‑to‑end understanding of a basic RAG stack (ingestion → storage → retrieval → generation → UI).