How to Build a RAG Chatbot With Your Own Documents: Retrieval, Chunking and Evaluation

Build a RAG chatbot with your own documents: chunk text, embed it, store it in a vector database, retrieve the best passages, and answer with citations.

By Galactic News Editorial · Published 2026-11-23T07:28:18+00:00 · Updated 2026-11-23T07:28:18+00:00

How to build a RAG chatbot with your own documents

How to Build a RAG Chatbot With Your Own Documents: Retrieval, Chunking and Evaluation

A language model cannot answer questions about your documents. It was not trained on them. Retrieval-augmented generation, or RAG, fixes that. The system retrieves passages from your documents at question time, then feeds them to the model as context. The model answers from your data, not from memory. This tutorial builds a working RAG chatbot step by step: documents in, chunks out, retrieval, generation, and evaluation.

How to Build a RAG Chatbot With Your Own Documents: The Pipeline

Every RAG system has two paths. The indexing path runs when documents change. The query path runs on every question. Keep them separate in your mind. That makes the system easier to debug.

The indexing path has four stages. Load documents. Split them into chunks. Convert each chunk into a vector with an embedding model. Store the vectors in a vector database. The query path has three stages. Embed the question with the same model. Search the vector database for the nearest chunks. Pass those chunks and the question to the language model, which writes the answer. Return the answer with source references so users can verify it.

LangChain and LlamaIndex are the two frameworks most developers reach for. LangChain is a general-purpose framework for LLM applications. It handles agents, chains, tools, memory, and RAG. LlamaIndex is a data framework built specifically for connecting language models to data. It focuses on ingesting, indexing, and querying. For a first RAG chatbot, either works. LangChain is the broader choice. LlamaIndex is the more focused one, with built-in loaders for over 160 data sources and smarter chunking out of the box.

Step 1: pick your stack

Choose the simplest stack that works. A good default for a first chatbot uses Python, LangChain, Chroma, and OpenAI models. Chroma runs locally as a folder on disk. No Docker. No cloud account. No sign-up. OpenAI's text-embedding-3-small is a cheap, reliable embedding model. For the chat model, a small fast model such as GPT-4o-mini keeps costs low while you learn.

If you want everything local, swap the pieces. Use Ollama to run a local chat model and a local embedding model. Use Chroma or FAISS as the local vector store. A freeCodeCamp tutorial builds exactly this: a private local RAG question-answering agent with LangChain v1, Ollama, Qwen, and Chroma, running on the developer's own machine. The pattern is the same. Only the providers change.

ComponentBeginner choiceLocal alternativeProduction option
FrameworkLangChainLangChainLangChain or LlamaIndex
Embedding modelOpenAI text-embedding-3-smallnomic-embed-text via OllamaOpenAI or BGE models
Vector storeChromaChroma or FAISSPostgres with pgvector, Qdrant, Pinecone
Chat modelGPT-4o-miniQwen via OllamaGPT-4o or Claude
Document loadingPyPDFLoader, TextLoaderSame loadersUnstructured, Docling, Apache Tika

Step 2: prepare your documents

Good RAG starts with clean source material. Collect a small, representative set first: 20 to 50 files, not your whole archive. Convert PDFs to text with PyMuPDF, Unstructured, or your own parser. Strip headers, footers, and repeated navigation text. Remove duplicate pages. Keep metadata with every document: source file, page or heading, date, and access group. Metadata is what lets you filter results later, for example by showing a user only the documents they are allowed to see.

Watch out for tables and multi-column layouts. A naive PDF parser can turn a table into word soup. If your documents are heavy on tables, test the parser on three real files before you index the rest. Docling and Unstructured handle tables better than basic text extraction. Pick the parser that keeps your tables readable.

Step 3: chunk intelligently

Chunking is the most underrated part of RAG. Bad chunks lose context. Good chunks make retrieval work. The rules of thumb are consistent across guides. Target 300 to 500 tokens per chunk. Keep related paragraphs together. Add overlap of 50 to 100 tokens between neighbours, so an answer is not cut mid-thought. Preserve headings as metadata. Do not split tables or code blocks in the middle.

LangChain's RecursiveCharacterTextSplitter is the standard starting point. It splits on paragraph breaks first, then sentences, then words, so it respects the structure of the text. A common setup is a chunk size of 500 characters or tokens with 100 of overlap. LlamaIndex adds sentence-aware splitting and hierarchical chunking out of the box, plus metadata extraction. Chunk by structure where possible: headings and paragraphs beat fixed character counts.

Test your chunking by reading ten random chunks. Each one should make sense on its own. If a chunk starts mid-sentence, increase the overlap. If a chunk mixes two topics, make the chunks smaller. This ten-minute check saves hours of debugging retrieval later.

Step 4: embed and store

Embeddings turn text into vectors. The same model must embed both the chunks and the questions. That is a hard rule. A question embedded with one model cannot be compared with chunks embedded by another.

With LangChain, the code is short. Create the embedding object, split the documents, then build the vector store from the chunks. Chroma persists to a folder on disk, so the index survives a restart. FAISS works the same way for a local index. For a web app, Postgres with pgvector is a pragmatic default, because your documents, users, and permissions already live in one database. Pinecone and Qdrant are the managed options when you want a hosted store.

Step 5: retrieve the right chunks

At question time, embed the question and search for the nearest chunks. Retrieve the top 5 to 10, then narrow them down. Two upgrades help here. Hybrid search combines vector similarity with keyword matching, which catches exact terms such as product names and error codes that pure vector search can miss. Reranking runs the candidates through a cross-encoder reranker and keeps the best few. Cohere's reranker is a common choice, at a small latency cost.

Apply metadata filters before or during retrieval. Filter by the user's permissions, by document date, or by document type. A filter at retrieval time is far cheaper than asking the model to ignore chunks it should never have seen.

Step 6: generate the answer

Build the prompt from three parts: system instructions, the retrieved chunks with source labels, then the question. Tell the model to answer only from the sources. Tell it to say it does not know when the sources do not cover the question. That instruction is the difference between a useful chatbot and a confident liar.

Return the answer with clickable source references. Users check the sources. That checking is what builds trust in the system. A chatbot that cites its sources gets used. One that does not gets ignored, because nobody can verify it.

Note one change in LangChain v1. The classic RetrievalQA chain is deprecated. The current pattern uses the agent and middleware architecture. Tutorials written for older LangChain versions still show RetrievalQA. Follow a current guide, such as the freeCodeCamp LangChain v1 tutorial, when you write the generation code.

Step 7: evaluate on real questions

Evaluation is the step most first-time builders skip. Write 30 to 100 real questions with expected sources. Re-run them after every change to chunking, embeddings, or prompts. That is the whole method. It catches regressions that no amount of staring at code will find.

Score two things. Retrieval: did the right chunks come back for each question? Generation: did the answer use those chunks, and did it stay inside them? A common failure is good retrieval with bad generation. The right chunks arrived. The model ignored them and answered from memory. The fix is in the prompt, not in the index. Another common failure is the reverse: good generation on bad retrieval. The answer is well written and wrong. The fix is in chunking or in the embedding model.

Keep a small set of golden questions in the repository. Run them in CI. When a change drops the score, you know before your users do. That loop is what turns a demo into a product.

Bottom line

Build the pipeline in order: clean documents, smart chunks of 300 to 500 tokens with overlap, one embedding model for questions and chunks, a vector store that fits your deployment, retrieval of the top 5 to 10 chunks with hybrid search and reranking, and generation that answers only from the retrieved context with citations. Start with LangChain and Chroma, or LlamaIndex if retrieval is the whole product. Then evaluate on 30 to 100 real questions, and re-run them after every change. A RAG chatbot is not finished when it answers one question well. It is finished when it answers a hundred questions well, and you can prove it.