Real-World Project: PDF Question-Answer Assistant

Harry · 13 Sep 2026 · 2 views

The Goal

Drop a PDF, ask a question, get an answer grounded in the document. This is the classic RAG starter project and it forms the core of most support and knowledge tools.

Step 1 - Extract Text

pip install pypdf

from pypdf import PdfReader

reader = PdfReader("company-guide.pdf")
text = "
".join(page.extract_text() or "" for page in reader.pages)
print(len(text), "characters extracted")

Step 2 - Chunk and Index

from sentence_transformers import SentenceTransformer
import chromadb

model = SentenceTransformer("all-MiniLM-L6-v2")
chunks = [text[i:i + 500] for i in range(0, len(text), 450)]

client = chromadb.PersistentClient(path="pdf_kb")
kb = client.get_or_create_collection("guide", metadata={"hnsw:space": "cosine"})
kb.add(ids=[f"c{i}" for i in range(len(chunks))], documents=chunks)

Step 3 - Ask

def ask(question, llm):
    hits = kb.query(query_texts=[question], n_results=3)
    context = "

".join(hits["documents"][0])
    prompt = (f"Answer ONLY from the document notes.

{context}

"
              f"Question: {question}")
    return llm(prompt)   # plug in your model call

print(ask("What is the probation period?", call_your_llm))

Step 4 - Polish for Real Use

  • Add page numbers as metadata for citation links.
  • Handle scanned PDFs with an OCR step first.
  • Deduplicate repeated headers and footers.
  • Cache identical questions.

Key Points

  • pypdf pulls text for ordinary PDFs.
  • Simple chunking is enough for a working demo.
  • Retrieved chunks ground every answer.
  • Add OCR and metadata for production documents.
Share this post:

Comments (0)

Please login or register to comment.