AI PDF Chatbot With LangChain 2026: Build One in 30 Minutes
No Docker, no heavy frameworks - here's a minimal LangChain PDF chatbot you can build in 30 minutes and extend into a real product.
💡 What You Will Learn
No Docker, no heavy frameworks - here's a minimal LangChain PDF chatbot you can build in 30 minutes and extend into a real product.
📜 Table of Contents
The Minimal RAG Pattern
A PDF chatbot is a RAG pipeline with four steps: load, split, embed, retrieve-and-answer. LangChain (144,172 stars, 2026-08-14) gives you each step as a component; the whole bot is about 60 lines.
The Stack
- Python 3.10+
- langchain + langchain-community + langchain-chroma
- pypdf (PDF parsing)
- A vector store: Chroma (local, free, zero-config)
- An LLM: any OpenAI-compatible endpoint - OpenAI, DeepSeek, or local Ollama
The Core Code
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_chroma import Chroma
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
loader = PyPDFLoader("manual.pdf")
docs = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
chunks = splitter.split_documents(docs)
vectorstore = Chroma.from_documents(chunks, OpenAIEmbeddings(model="text-embedding-3-small"))
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
while True:
q = input("Ask: ")
if q.lower() in ("exit", "quit"):
break
docs = retriever.invoke(q)
context = "
".join(d.page_content for d in docs)
print(llm.invoke(f"Answer using ONLY the context. If unsure, say so.
Context: {context}
Question: {q}"))
Why Each Piece Is There
- Chunk size 1000 / overlap 200: answers need surrounding context; overlap prevents cutting a paragraph in half.
- k=4 retrieval: four chunks is enough for most answers without bloating the prompt.
- temperature=0: factual Q&A should be deterministic.
- The prompt constraint: 'only context, admit uncertainty' stops hallucinations from dressing up as answers.
The 30-Minute Path
- pip install langchain langchain-community langchain-chroma pypdf langchain-openai (5 min)
- Save the script, point PyPDFLoader at your PDF (2 min)
- Run, ask questions (10 min of playing)
- Wrap it in FastAPI (102,000 stars) and add a tiny chat UI (rest of the time)
FAQ
Why LangChain instead of building raw? Components handle the fiddly parts (splitting, retrieval, prompt assembly); raw code is educational, LangChain is productive.
Which embedding model? text-embedding-3-small is cheap and good enough; local users can use Ollama embeddings.
Does this work with scanned PDFs? No - PyPDFLoader needs text; add OCR (pytesseract) for scans.
How accurate is it? Depends on chunk quality and the LLM; the k=4 retrieval with source citations is the accuracy lever.
