A lightweight Retrieval-Augmented Generation (RAG) system built in Python. This project lets you query your personal or professional documents using natural language. It processes diverse document types, embeds content with metadata, and retrieves the most relevant chunks using a FAISS HNSW vector database for accurate, context-aware responses.
- Multi-format Document Ingestion: Supports PDF (including scanned/OCR), DOCX, TXT, HTML, and JSON.
- Robust Text Cleaning: Removes noisy OCR artifacts, headers/footers, and extracts metadata.
- Adaptive Chunking: Smart chunking strategies based on document type (Q&A vs. regular).
- High-quality Embeddings: Uses sentence-transformers with
BAAI/bge-base-en-v1.5for efficient embeddings. - Fast Semantic Search: Powered by FAISS HNSW for scalable and accurate retrieval.
- Context-aware Responses: Combines metadata and intent detection for precise answers.
- Easy Extensibility: Modular pipeline for adding new formats, models, or chunking strategies.
personal-knowledge-assistant/
βββ main.py # Entry point for the pipeline
βββ rag/
β βββ generation.py # Response generator module
β βββ query_processor.py # Query parsing, intent & metadata detection
β βββ retriever.py # Handles semantic retrieval from FAISS
βββ utils/
β βββ document_processor.py # Orchestrates ingestion, cleaning, chunking
β βββ embedding_utils.py # Embedding pipeline (sentence-transformers)
β βββ faiss_utils.py # FAISS index building, saving, and loading
βββ ingestion/
β βββ loader.py # Multi-format document loader (PDF, DOCX, etc.)
β βββ cleaner.py # Text cleaning & metadata extraction
βββ chunking/
β βββ chunking_pipeline.py # Q&A detection and adaptive chunking selection
β βββ sliding_window.py # Sliding window chunker for Q&A docs
β βββ recursive_chunker.py # Recursive chunker for regular docs
βββ vector_db/
β βββ faiss.py # FAISS HNSW storage and search implementation
βββ docs/ # Put your source documents here
βββ requirements.txt # Project dependencies
- Python 3.8+ (recommended: Python 3.10 or newer)
- Git
- pip (Python package manager)
- Tesseract OCR (optional, only needed for OCR on scanned PDFs)
git clone https://github.com/mdadnanshuvo/personal-knowledge-assistant.git
cd personal-knowledge-assistantWhy?
A virtual environment ensures all dependencies are isolated and do not interfere with your system Python or other projects.
python -m venv venv
venv\Scripts\activatepython3 -m venv venv
source venv/bin/activatepip install --upgrade pippip install -r requirements.txtExtra notes:
- If you want OCR support for scanned PDFs, install the following system packages and Python libraries:
- System:
- On Ubuntu/Debian:
sudo apt-get install tesseract-ocr - On Mac:
brew install tesseract
- On Ubuntu/Debian:
- Python:
pip install pytesseract pdf2image pillow
- System:
Create a .env file in the root directory (if your project supports custom environment variables).
Common usage (if needed):
# .env file example
MODEL_NAME=BAAI/bge-base-en-v1.5
FAISS_INDEX_NAME=my_knowledge_base
Note:
If the code does not auto-load.envfiles, you can set environment variables in your shell before running.
set MODEL_NAME=BAAI/bge-base-en-v1.5export MODEL_NAME=BAAI/bge-base-en-v1.5- Put your PDFs, DOCX, TXT, Markdown, or JSON files in the
docs/directory. - Each run will process all files in this directory.
python main.pyWhat happens:
- Documents are loaded, cleaned, chunked, embedded, and indexed.
- You can query your knowledge base using natural language.
- OCR Not Working:
- Ensure Tesseract OCR is installed and accessible in your PATH.
- CUDA/FP16 Issues:
- If you encounter GPU/FP16 errors, set
use_fp16=Falsein your model initialization.
- If you encounter GPU/FP16 errors, set
- Out of Memory:
- For large document sets, ensure your machine has enough RAM; consider chunking/tuning parameters.
When done, deactivate with:
deactivateTo update all Python packages (optional):
pip install --upgrade -r requirements.txtTo remove the virtual environment and dependencies:
deactivate
rm -rf venv- Change Embedding Model:
Editutils/embedding_utils.pyand change the model name. - Tune FAISS Parameters:
Editutils/faiss_utils.pyorvector_db/faiss.py. - Add More Document Types:
Extendingestion/loader.py.
git clone https://github.com/mdadnanshuvo/personal-knowledge-assistant.git
cd personal-knowledge-assistant
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
# (Optional: pip install pytesseract pdf2image pillow)
# (Optional: install tesseract-ocr system package)
# Add your documents to docs/
python main.pyEnjoy querying your personal knowledge base!
Run the assistant:
python main.pySample output:
Processing documents...
Building FAISS index...
Query: What was the deadline of submitting Scrappy assignment?
Best Answer:
[answer generated based on your documents]
- Document Ingestion: Loads and processes all documents in
docs/. - Cleaning & Metadata: Cleans text, removes noise, extracts metadata like titles/sections.
- Chunking: Splits content into chunks, adapting to Q&A or regular formats.
- Embedding: Converts chunks into embedding vectors using
BAAI/bge-base-en-v1.5via sentence-transformers. - Indexing: Stores vectors in a FAISS HNSW index for fast retrieval.
- Querying: Processes user queries, extracts intent, retrieves relevant chunks.
- Generation: Synthesizes a context-aware answer from retrieved chunks.
- Model: BAAI/bge-base-en-v1.5
- Library: sentence-transformers, version
5.1.1 - Code Example:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("BAAI/bge-base-en-v1.5") embeddings = model.encode(["your text here"])
- Academic PDFs
- Meeting notes (.docx, .txt)
- Markdown files
- Exported chat logs
- Scanned documents (with OCR)
- Add new chunking strategies: Extend
chunking/. - Switch embedding models: Modify
embedding_utils.pyto use any HuggingFace model. - Tune FAISS parameters: Adjust in
faiss_utils.pyandvector_db/faiss.py.
- FAISS: Facebook AI Similarity Search
- BAAI/bge-base-en-v1.5 Model Card
- Sentence Transformers Documentation
MIT License. See LICENSE for details.