Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

RAG MongoDB + FAISS + PDF Chat

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.

Features

  • 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

Tech Stack

  • Python, FastAPI, Uvicorn
  • MongoDB
  • FAISS (CPU)
  • SentenceTransformers (all-MiniLM-L6-v2)
  • Transformers (GPT‑2)
  • PyPDF
  • Vanilla HTML/CSS/JS

Project Structure

'''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 '''

Setup

1. Clone and create venv

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

2. Install dependencies

pip install -r requirements.txt

3. Start MongoDB

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)

4. Run the app

python -m app.main

You should see Uvicorn running on http://0.0.0.0:8000. image

Usage

  1. Open your browser at:

    • http://localhost:8000
  2. 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
  3. 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
  4. You can also inspect the API:

    • http://localhost:8000/docs

Key Components

Retrieval (app/retrieval/retriever.py)

  • 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

Generation (app/generation/llm.py)

  • Loads GPT‑2 via Hugging Face Transformers
  • Builds a simple prompt:

Context:

Question: Answer:

  • Generates a short answer from the context

PDF Handling (app/utils/pdf_parser.py)

  • 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

Endpoints

  • GET /health – simple health check
  • POST /upload – upload a PDF (multipart/form-data, field name file)
  • POST /ask – ask a question ({"question": "...", "top_k": 3})
  • GET /docs – FastAPI Swagger UI
  • / – serves the HTML UI

Notes / Limitations

  • 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).

Possible Improvements

  • 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).

About

This project is a small end‑to‑end RAG app: you upload PDFs, it indexes their content with MongoDB + FAISS, and you can ask questions through a web UI. The backend (FastAPI) retrieves relevant chunks using embeddings and generates answers with a GPT‑2 model.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages