A Retrieval-Augmented Generation (RAG) application that enables conversational Q&A with PDF documents using LangChain and OpenAI.
- PDF Document Loading: Load and process multiple PDF files
- Vector Database: Store document embeddings in FAISS for efficient similarity search
- Conversational Memory: Maintains chat history for context-aware responses
- RAG Pipeline: Combines document retrieval with LLM-powered answers
- Python 3.8+
- OpenAI API key
-
Clone the repository
cd SimpleChatWithPDF -
Install dependencies
pip install langchain langchain-core langchain-community langchain-openai langchain-text-splitters faiss-cpu python-dotenv pypdf
-
Set up environment variables
Create a
.envfile in the project root:OPENAI_API_KEY=your_openai_api_key_here
Place your PDF files in the ./data/ folder. By default, the project includes:
azure-cloud-cheatsheet.pdfazure-security-best-practices-cheatsheet.pdf
To use your own PDFs, edit saveLoadedPDFToVectorDB.py and update the loaders list:
loaders = [
PyPDFLoader("./data/your-document.pdf"),
PyPDFLoader("./data/another-document.pdf")
]Run the script to process PDFs and create the FAISS vector database:
python saveLoadedPDFToVectorDB.pyThis will:
- Load all specified PDF files
- Split documents into chunks (1000 chars with 150 overlap)
- Generate embeddings using OpenAI's
text-embedding-3-smallmodel - Save the vector database to
./data/chat_database/
Run the chat script to ask questions:
python chat.pyTo ask different questions, edit chat.py and modify:
question = "Your question here"
second_question = "Your follow-up question"SimpleChatWithPDF/
├── chat.py # Main chat application with RAG
├── saveLoadedPDFToVectorDB.py # Script to build vector database
├── data/
│ ├── azure-cloud-cheatsheet.pdf
│ ├── azure-security-best-practices-cheatsheet.pdf
│ └── chat_database/ # FAISS vector store (generated)
│ ├── index.faiss
│ └── index.pkl
├── .env # Environment variables (create this)
└── README.md
-
Document Ingestion (
saveLoadedPDFToVectorDB.py)- PDFs are loaded using
PyPDFLoader - Text is split into chunks using
CharacterTextSplitter - Chunks are embedded using OpenAI embeddings
- Embeddings are stored in a FAISS vector database
- PDFs are loaded using
-
Conversational Retrieval (
chat.py)- User questions are reformulated using chat history for context
- Relevant document chunks are retrieved via similarity search
- Retrieved context + question are sent to GPT-3.5-turbo
- Response is generated and chat history is updated
| Parameter | Location | Default | Description |
|---|---|---|---|
chunk_size |
saveLoadedPDFToVectorDB.py |
1000 | Size of text chunks |
chunk_overlap |
saveLoadedPDFToVectorDB.py |
150 | Overlap between chunks |
k |
chat.py |
6 | Number of documents to retrieve |
model |
chat.py |
gpt-3.5-turbo-0125 | LLM model for responses |
Use direct imports to bypass the problematic package-level import:
from langchain_community.document_loaders.pdf import PyPDFLoaderDelete and regenerate the vector database:
Remove-Item -Recurse -Force .\data\chat_database
python .\saveLoadedPDFToVectorDB.pyEnsure all LangChain packages are compatible:
pip install -U langchain langchain-core langchain-community langchain-openai langchain-text-splittersMIT