Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SimpleChatWithPDF

A Retrieval-Augmented Generation (RAG) application that enables conversational Q&A with PDF documents using LangChain and OpenAI.

Features

  • 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

Prerequisites

  • Python 3.8+
  • OpenAI API key

Installation

  1. Clone the repository

    cd SimpleChatWithPDF
  2. Install dependencies

    pip install langchain langchain-core langchain-community langchain-openai langchain-text-splitters faiss-cpu python-dotenv pypdf
  3. Set up environment variables

    Create a .env file in the project root:

    OPENAI_API_KEY=your_openai_api_key_here

Usage

Step 1: Prepare Your PDFs

Place your PDF files in the ./data/ folder. By default, the project includes:

  • azure-cloud-cheatsheet.pdf
  • azure-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")
]

Step 2: Build the Vector Database

Run the script to process PDFs and create the FAISS vector database:

python saveLoadedPDFToVectorDB.py

This will:

  • Load all specified PDF files
  • Split documents into chunks (1000 chars with 150 overlap)
  • Generate embeddings using OpenAI's text-embedding-3-small model
  • Save the vector database to ./data/chat_database/

Step 3: Chat with Your Documents

Run the chat script to ask questions:

python chat.py

To ask different questions, edit chat.py and modify:

question = "Your question here"
second_question = "Your follow-up question"

Project Structure

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

How It Works

  1. 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
  2. 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

Configuration

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

Troubleshooting

Windows: ModuleNotFoundError: No module named 'pwd'

Use direct imports to bypass the problematic package-level import:

from langchain_community.document_loaders.pdf import PyPDFLoader

KeyError: '__fields_set__' when loading database

Delete and regenerate the vector database:

Remove-Item -Recurse -Force .\data\chat_database
python .\saveLoadedPDFToVectorDB.py

Version conflicts

Ensure all LangChain packages are compatible:

pip install -U langchain langchain-core langchain-community langchain-openai langchain-text-splitters

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages