AI explained

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.

By the Askocto team · · 4 min read

A RAG chatbot answers questions by first retrieving relevant passages from your own documents and then asking a language model to write an answer using only those passages. RAG stands for retrieval-augmented generation. It is the usual way to make a chatbot answer from your content instead of from general knowledge. This article walks through the pipeline stage by stage, shows a small code sketch and lists the failures to test for.

In short: Clean documents, split them sensibly, index them, retrieve the best passages, instruct the model to answer only from them and show the source. Most quality problems come from the first three stages.

The pipeline at a glance

  1. Ingest: read documents and web pages into plain text.
  2. Split: cut the text into passages, often called chunks.
  3. Embed: turn each passage into a vector, a list of numbers that captures its meaning.
  4. Store: save the vectors and the passage text in an index.
  5. Retrieve: turn the question into a vector and find the closest passages.
  6. Rerank: reorder the candidates by how well they match the question.
  7. Generate: give the model the question and the best passages and ask for an answer with sources.
  8. Evaluate: test the whole chain on real questions.

The next sections cover each stage in the order where it most often goes wrong.

Ingest, clean and split

Ingest and clean

Everything downstream depends on clean text. Tables, scanned pages and navigation menus can turn into noise. Strip repeated headers and footers, keep headings, and preserve the title of each page, because the title helps retrieval later. The article on document parsing explains why formats behave differently.

Split sensibly

A passage should hold one idea and enough context to make sense alone. Too small, and a passage loses its meaning. Too large, and it mixes topics, so retrieval returns text that is only partly relevant.

Practical rules:

  • Split at headings and paragraphs, not at a fixed number of characters in mid-sentence.
  • Keep a short overlap between neighbors, so an answer that crosses a boundary is not lost.
  • Attach metadata to each passage: page title, section heading, address and last updated date.

Embed and store

An embedding model converts text to vectors so that similar meanings land near each other. You store the vector with the passage and its metadata in a vector index. For background, read vector embeddings explained. Use the same embedding model for passages and questions, and note which one you used, since changing it means re-embedding everything.

Retrieve and rerank

At question time, embed the question and fetch the nearest passages, perhaps the top ten to twenty. Then rerank: a second model scores each candidate against the question and you keep the best few. This step often fixes cases where the right passage was retrieved but ranked low. Reranking describes how it works.

Generate with a strict instruction

The prompt tells the model what to do with the passages. A sketch:

You are a support assistant for a bicycle shop.
Answer only from the passages below.
If the passages do not contain the answer, say you do not know
and offer to connect the customer with the team.
Name the page each fact came from.

Passages:
{{passages}}

Question: {{question}}

The same flow in code, with placeholder functions standing in for whichever services you use:

async function answer(question) {
  const qVector = await embed(question)
  const candidates = await index.search(qVector, { limit: 15 })
  const best = await rerank(question, candidates, { keep: 4 })
  if (best.length === 0) return handover(question)
  const reply = await generate({ question, passages: best })
  return { text: reply.text, sources: best.map((p) => p.url) }
}

Note the early exit. When retrieval finds nothing useful, the bot should hand over instead of letting the model guess.

Failures to test for

Failure What you see Likely stage
Right fact, wrong answer The passage was retrieved but the model ignored it Generate: tighten the instruction
Missing answer The document has it, but the bot says it does not know Split or retrieve: passages too small or too large
Mixed answer Two products blended together Split: passages cover several topics
Stale answer Old policy repeated Ingest: outdated documents still indexed
Confident invention A fact appears in no passage Generate: loosen limits, no handover rule
Slow replies Long wait before the first words Retrieve or rerank: too many candidates

Keep a test set of real questions with the expected source for each, and run it after every change. The article on RAG from scratch goes deeper into the mechanics.

RAG or fine-tuning

RAG brings in current facts at question time, so updating an answer means updating a document. Fine-tuning changes how a model behaves, which suits style and format more than facts. Many support setups use RAG for knowledge and instructions for behavior. See RAG vs fine-tuning for the comparison.

Build or buy

Building the pipeline teaches you a great deal and gives you full control, but you also own ingestion, indexing, refresh schedules, access control, evaluation, a chat interface and a way for humans to take over. A hosted platform provides these. With Askocto, you add sources under Build > Sources, such as a website crawl, files or Q&A pairs, and retrieval, retraining and the chat run for you. The data sources documentation lists the source types. If you want the result without the plumbing, Askocto has a free plan to try it on.

Frequently asked questions

What does RAG stand for?

Retrieval-augmented generation. The system retrieves passages from your content and the model writes its answer using them.

Do I need a vector database for a RAG chatbot?

You need an index that can find passages by meaning. A dedicated vector database is one option, and some general databases offer vector search too.

How do I stop a RAG chatbot from inventing answers?

Instruct it to answer only from the passages, hand over when retrieval finds nothing useful, show sources and test with trap questions.

How often should the index be refreshed?

Whenever the source content changes. Many teams refresh on a schedule and trigger a manual refresh after a policy change.

Train an agent on your own help center

Askocto answers the repeat questions across chat, email and voice and hands the hard ones to your team.

Keep reading

All articles
AI explainedreranking
Sep 9, 20245 min read

Reranking in RAG: what it does and when to add it

Retrieval 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 article
AI explainedvector embedding
Sep 1, 20245 min read

Vector embedding explained: how text becomes searchable

An 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