PDF RAG Chatbot
Inspired by tonykipkemboi's code on RAG with PDFs
This project implements a Retrieval-Augmented Generation (RAG) system that allows you to chat with PDF documents. The system uses advanced language models to understand and answer questions about the content of uploaded PDFs.
The PDF RAG pipeline consists of the following components:
-
Document Loading: PDFs are loaded and parsed using
UnstructuredPDFLoaderto extract text content. -
Text Chunking: The extracted text is split into manageable chunks (1000 characters with 200 character overlap) using
RecursiveCharacterTextSplitter. This ensures context is preserved across chunks while maintaining optimal token lengths. -
Vector Embeddings: Text chunks are converted into vector embeddings using the
sentence-transformers/all-mpnet-base-v2model via HuggingFace embeddings. This allows semantic similarity searches. -
Vector Database: Embeddings are stored in a Chroma vector database for fast retrieval of relevant document passages.
-
Multi-Query Retrieval: When you ask a question, the system uses
MultiQueryRetrieverto generate multiple alternative formulations of your query. This helps overcome limitations of distance-based similarity search by retrieving documents from different semantic angles. -
LLM Response Generation: The OpenAI
gpt-4o-minilanguage model processes the retrieved context and your question to generate accurate, context-aware answers. -
RAG Chain: All components are connected in a LangChain pipeline that orchestrates the retrieval, prompting, and response generation in a single, coherent workflow.
The notebook provides a simple chat_with_pdf(question) function that takes a natural language question and returns an answer based exclusively on the PDF content. The system is designed to provide accurate, grounded responses without hallucination.
- Python 3.10+
- Dependencies listed in
requirements.txt - OpenAI API key (see setup below)
-
Get your OpenAI API key:
- Go to https://platform.openai.com/api/keys
- Sign in with your OpenAI account (create one at https://openai.com if needed)
- Click "Create new secret key"
- Copy the generated key
-
Add API key to
.envfile:- Open the
.envfile in the root directory of this project - Replace
your-api-key-herewith your actual OpenAI API key:OPENAI_API_KEY=sk-...your-key-here... - Save the file
- Open the
-
Important:
- The
.envfile is already listed in.gitignore, so your API key will never be committed to git - Keep your API key private and never share it
- Ensure you have billing set up on your OpenAI account to use the API
- The
To build the Docker image, run the following command in the terminal from the root directory of the project:
docker build -t rag-pdf .docker run --rm -it -p 8888:8888 -p 8501:8501 -v $(pwd):/workspace rag-pdf bash -c "streamlit run app/streamlit_app.py --server.address=0.0.0.0"Access at: http://localhost:8501
docker run --rm -it -p 8888:8888 -p 8501:8501 -v $(pwd):/workspace rag-pdf bash -c "jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser --allow-root"Access at: http://localhost:8888 (copy token from logs)
docker run --rm -it -p 8888:8888 -p 8501:8501 -v $(pwd):/workspace rag-pdfThen run inside container:
- Streamlit:
streamlit run app/streamlit_app.py --server.address=0.0.0.0 - Jupyter:
jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser --allow-root
Port Mappings:
8888- Jupyter Notebook8501- Streamlit App
The notebook provides an interactive environment for exploring RAG capabilities with detailed output.
-
Run Jupyter:
jupyter notebook
-
Open and run:
- Navigate to
notebooks/rag_notebook.ipynb - Place your PDF in
/workspace/pdfs/or update the path in the notebook - Run the cells sequentially to process the PDF and chat with it
- Navigate to
The Streamlit app provides a user-friendly web interface for uploading PDFs and chatting with them.
-
Run the Streamlit app:
streamlit run app/streamlit_app.py
-
Use the app:
- Upload a PDF using the sidebar
- Ask questions about the document in the chat
- View retrieved context chunks for transparency
- Adjust model and temperature settings in the sidebar
Features:
- 📤 Dynamic PDF upload and processing
- 💬 Multi-turn chat interface
- 📚 View retrieved context chunks
- ⚙️ Adjustable model and temperature settings
- 💾 Chat history within session