Your Own RAG Bot with Documents
Briefly
In this guide, we will build a simple bot that reads your PDF documents and answers questions about them. Stack: Python, LangChain, ChromaDB, and a language model.
What We Are Building
A bot that:
- uploads PDFs;
- splits the document into chunks;
- converts chunks into embeddings;
- stores them in a vector database;
- finds similar pieces based on the question;
- passes them to the LLM and gets an answer.
Architecture
flowchart LR
A[PDF] --> B[Chunking]
B --> C[Embedding]
C --> D[Vector DB]
E[Question] --> F[Search]
D --> F
F --> G[LLM]
G --> H[Answer]
Step 1. Installation
pip install langchain langchain-community langchain-openai chromadb pypdf
If you want local embeddings through Ollama, add:
pip install langchain-ollama
Step 2. Upload PDF
from langchain_community.document_loaders import PyPDFLoader
loader = PyPDFLoader("docs/manual.pdf")
documents = loader.load()
Step 3. Split into Chunks
from langchain_text_splitters import RecursiveCharacterTextSplitter
splitter = RecursiveCharacterTextSplitter(
chunk_size=800,
chunk_overlap=100,
)
chunks = splitter.split_documents(documents)
Step 4. Create Embeddings and Vector Database
Option with OpenAI:
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
vectorstore = Chroma.from_documents(
documents=chunks,
embedding=OpenAIEmbeddings(),
)
Option with local Ollama:
from langchain_ollama import OllamaEmbeddings
from langchain_community.vectorstores import Chroma
vectorstore = Chroma.from_documents(
documents=chunks,
embedding=OllamaEmbeddings(model="nomic-embed-text"),
)
Step 5. Connect LLM and Ask a Question
from langchain_openai import ChatOpenAI
from langchain.chains import RetrievalQA
llm = ChatOpenAI(
model="deepseek-chat",
base_url="https://api.deepseek.com",
api_key="YOUR_KEY",
)
qa = RetrievalQA.from_chain_type(
llm=llm,
retriever=vectorstore.as_retriever(),
)
answer = qa.invoke("What is the warranty on the motor?")
print(answer)
Useful Tips
- Start with
chunk_size=800andchunk_overlap=100. - Store metadata: file name, section, date.
- Do not send too many chunks to the LLM—3–5 are usually sufficient.
- For production, add filters and re-ranking.
- Test quality on several questions from the documents.