Building a PDF Chat Bot: A Fun Journey with RAG, LangChain, and ChromaDB

Ever wanted to chat with your PDF files? I know it sounds wild, but trust me - it's not only possible but also super fun to build! Today, I'm going to show you how I created a chatbot that can read and understand PDFs using something called RAG (Retrieval Augmented Generation). Don't worry if these terms sound fancy - we'll break everything down into bite-sized pieces.

What We're Building

Imagine having a smart friend who can read through hundreds of pages of PDFs and answer any questions you have about them. That's exactly what we're going to build! We'll use:

  • LangChain (think of it as our trusty Swiss Army knife for AI stuff)
  • ChromaDB (our cool document storage system)
  • A language model (the brains of our operation)

Setting Up Our Project

First things first, let's get our tools ready. Open up your terminal and type:

pip install langchain chromadb pypdf python-dotenv

Now, create a new Python file called pdf_chatbot.py. Here's our basic setup:

from langchain.document_loaders import PyPDFLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain
from dotenv import load_dotenv
import os

#Load our environment variables

load_dotenv()

The Magic Ingredients: How RAG Works

Before we dive deeper, let me explain RAG in simple terms. Imagine you're helping a friend with their homework. You'd probably:

  1. Read through their textbook
  2. Remember where the important stuff is
  3. Use that information to answer their questions

That's exactly what RAG does! It:

  1. Reads and breaks down our PDFs
  2. Stores the information in a smart way
  3. Retrieves relevant bits when answering questions

Loading Our PDF

Let's add some code to handle our PDF:

def load_pdf(pdf_path):
    loader = PyPDFLoader(pdf_path)
    pages = loader.load()
    print(f"Loaded {len(pages)} pages from the PDF")
    return pages

# Example usage
pdf_path = "your_document.pdf"
documents = load_pdf(pdf_path)

Setting Up Our Brain (Vector Store)

Now comes the cool part - we'll create a "brain" for our chatbot using ChromaDB:

def create_vector_store(documents):
    embeddings = OpenAIEmbeddings()
    vector_store = Chroma.from_documents(
        documents,
        embeddings,
        persist_directory="./data"
    )
    return vector_store

vector_store = create_vector_store(documents)

The Chat Interface

Here's where it gets exciting - let's make our chatbot interactive:

def create_chat_chain():
    llm = ChatOpenAI(temperature=0.7)
    chain = ConversationalRetrievalChain.from_llm(
        llm=llm,
        retriever=vector_store.as_retriever(search_kwargs={"k": 3}),
    )
    return chain

def chat_with_pdf():
    chain = create_chat_chain()
    chat_history = []
    print("🤖 Hi! I've read your PDF. What would you like to know about it?")
    print("(Type 'quit' to exit)")

    while True:
        question = input("\nYou: ")
        if question.lower() == 'quit':
            break

        response = chain({"question": question, "chat_history": chat_history})
        chat_history.append((question, response['answer']))
        print("\n🤖:", response['answer'])

# Let's start chatting!
chat_with_pdf()

Taking It For a Spin

Now for the fun part! Let's see our creation in action. Save your code and run:

python pdf_chatbot.py

Drop any PDF into your project folder, update the pdf_path, and start chatting! Try questions like:

  • "What's the main topic of this document?"
  • "Can you summarize page 5?"
  • "What are the key points about [specific topic]?"

Cool Things You Can Try

Once you've got the basic version working, here are some fun tweaks you can make:

  1. Add support for multiple PDFs
  2. Create a simple web interface using Streamlit
  3. Add memory to your chatbot so it remembers previous conversations
  4. Experiment with different language models

Wrapping Up

And there you have it! We've built a pretty neat PDF chatbot that uses RAG to give smart, contextual answers. The cool thing about this setup is how flexible it is - you can use it for all sorts of documents, not just PDFs.

Remember, this is just the beginning. Feel free to experiment and modify the code to suit your needs. Maybe add some error handling, try different embedding models, or even extend it to handle other file types.

Happy coding! 🚀