Build a RAG chatbot: the pipeline from documents to answers
A RAG chatbot answers from your documents by retrieving the right passages first. See each stage of the pipeline, a small code sketch and the failures to test for.
Read articleRAG gives a model the right passages at question time. Build a minimal version in six steps, see where it breaks and decide when to use a platform instead.
By the Askocto team · · 5 min read
Retrieval augmented generation, or RAG, is a pattern where your system finds the passages most relevant to a question and gives them to a language model along with the question, so the answer comes from your content and not from the model's memory. A minimal version is a small amount of code. A good one takes care in chunking, retrieval and testing. This article builds the minimal version in six steps and shows where it usually breaks.
In short: Split documents into chunks, turn each into an embedding, store them, retrieve the closest chunks for each question, put them in the prompt and test with real questions. Most failures trace back to chunking and retrieval, not the model.
Think of two phases. Before any question, you prepare your content: split it, index it and store it. At question time, you search the index, build a prompt from the best matches and ask the model to answer only from them.
PREPARE (once, and again when content changes)
documents -> clean text -> chunks -> embeddings -> index
ANSWER (every question)
question -> embedding -> search index -> top chunks -> prompt -> model -> reply
Gather the pages, PDFs and notes that hold the answers. Convert them to plain text and remove navigation, cookie banners and repeated footers. Keep titles, headings and the source link, because you will want them later. Messy input is the most common cause of poor retrieval, so structuring a knowledge base for AI is worth reading before you start. PDFs with tables or scans need extra care before they become clean text.
A model reads a limited amount at a time, and a search works better on focused pieces than on whole documents. Split text into chunks that each hold one idea.
Practical guidelines:
The right size depends on your content, so try a few and compare results on your test questions.
An embedding is a list of numbers that represents the meaning of a piece of text, so that texts with similar meaning end up close together. You send each chunk to an embedding model and store the result. Check the current documentation of the embedding provider for the available models and their terms. Vector embeddings explains the idea in more depth.
Store each chunk with its embedding and metadata in a vector index or a database that supports similarity search. At question time:
Many systems add a keyword search alongside, since exact terms such as product codes match better by words than by meaning. Combining the two and then re-ordering the results, as described in reranking, often improves quality.
Put the retrieved chunks and the question into one prompt with clear instructions:
You answer customer questions for a small shop.
Use only the passages below. If they do not contain the answer,
say you are not sure and offer to pass the question to the team.
Quote policy numbers and time limits exactly as written.
Passages:
[1] Returns (updated page): Items can be returned within 30 days...
[2] Shipping: Orders leave the warehouse within two working days...
Question: Can I return a gift after six weeks?
Include the source of each passage so the reply can point to it. The "if the passages do not contain the answer" line matters as much as the passages themselves, because it gives the model an honest way out.
Build a test set of thirty to fifty real questions, with the passage that should answer each one and the expected answer. Measure two things separately:
Separating them tells you where to work. If the right passage is missing, fix chunking or search. If it is present but the answer is wrong, fix the prompt or the source wording.
| Symptom | Likely cause | Fix |
|---|---|---|
| Answer is right but outdated | Old chunk still indexed | Re-index on content change, remove old versions |
| Right page, wrong section retrieved | Chunks too large or too small | Re-split on headings, adjust size |
| Product codes not found | Pure meaning search misses exact terms | Add keyword search |
| Two sources contradict | Duplicate or stale content | Resolve at the source |
| Model ignores the passages | Prompt too long or unclear | Shorten the instructions, put the passages near the question |
| Confident answer, no source | No relevant chunk, no honest way out | Add the "say you are not sure" rule and a score threshold |
A minimal pipeline needs ongoing care: re-indexing when content changes, monitoring questions that found nothing, versioning your prompt and rerunning the test set after every change. For a customer-facing system you also need logs, handoff to people and access control, which turns a weekend project into a continuing one. The build RAG chatbot guide covers the build in detail, including what to maintain yourself.
In Askocto, the same flow is built in. You add a website, files or text under Build > Sources, each source moves from Queued to Training to Ready, and Test > Playground shows which sources supported an answer. You can schedule Auto-retrain to keep content fresh. The data sources docs cover the options, and starting a free workspace lets you compare your pipeline with a ready one on your own content. Askocto also records each conversation under Activity, so you can read what was retrieved and said.
RAG means looking up relevant passages from your own content first and then asking a language model to answer using them. It keeps answers tied to your sources.
You need some way to search by meaning, which can be a dedicated vector database or a database with vector search. For a small content set, a simple local index can be enough to start.
For facts that change, usually yes, because you update the source and the next answer changes. See RAG vs fine-tuning for the comparison.
There is no universal size. Start with paragraph-sized chunks split on headings, test on your own questions and adjust.
Askocto answers the repeat questions across chat, email and voice and hands the hard ones to your team.
A RAG chatbot answers from your documents by retrieving the right passages first. See each stage of the pipeline, a small code sketch and the failures to test for.
Read articleRetrieval finds candidates and reranking puts them in the right order. See how the second pass works, when it is worth the extra step and how to measure the gain.
Read articleAn embedding turns a sentence into a list of numbers so search can match meaning, not just words. Learn how it works, what it misses and how to check it on your own content.
Read article