Mercado Tech Brasil

Learn how RAG grounds AI answers in sources: see examples

Published on · Artificial Intelligence · by William Garcia

Old open book on display
Photo: mdburnette · WordPress Photo Directory (CC0, public domain)

Mercado Tech Brasil shows how RAG, short for retrieval-augmented generation, solves one of the most frustrating problems of generative AI: answers that sound right but have nothing behind them. If you want an assistant that answers with your company's rules, not with whatever seems reasonable, this is the concept you need to know.

What RAG is

A language model only knows what it saw during training. It doesn't know your manuals, contracts and internal policies, or what changed last week. RAG (Retrieval-Augmented Generation) adds a step before the answer: the system searches a document base for relevant passages and hands them to the model along with the question. The model then writes the answer based on that evidence and can point to where each piece of information came from.

How RAG works, step by step

Behind the conversation, the flow usually has five stages:

  • Preparation: documents (PDFs, wikis, procedures, support knowledge base) are split into smaller chunks;
  • Indexing: each chunk becomes a numerical representation, called an embedding, stored in a search index;
  • Retrieval: when a question arrives, the system finds the chunks closest to it in meaning;
  • Generation: the model receives the question plus the retrieved chunks and writes the answer;
  • Citation: the answer shows the sources used, so the person can check them.

A simple RAG example

Imagine an employee asks what the deadline is to request an equipment replacement. Without RAG, the model may invent a plausible rule. With RAG, the system finds the exact paragraph in the internal procedure, answers with the current rule and points to the source document. If the policy changes, you just update the document; there's no need to retrain the model.

Why RAG became the standard in companies

RAG has become the most common approach to so-called "chat with your documents" for a few reasons:

  • it reduces hallucinations, because the answer is anchored in real text;
  • it keeps content up to date without retraining the model;
  • it lets you audit the answer through the cited sources;
  • it respects permissions, as long as the search only returns documents the person can already access.

One important caveat: RAG reduces errors, but doesn't eliminate them. If the search brings back the wrong passage, or if the documents are outdated and contradictory, the answer inherits the problem.

Secure RAG: what I see in practice

In the automation projects I work on, RAG quality depends less on the model chosen and more on having your house in order:

  • documents with an owner, a review date and a clear version;
  • access control applied in the search itself, so no one receives what they shouldn't see;
  • logging of questions and the sources used, to investigate poor answers;
  • attention to external content, which may carry hidden instructions trying to steer the assistant's behavior.

Start small: pick a well-maintained set of documents, such as the manual for a single process, and measure whether the answers cite the right source before expanding the scope.

Read also: Discover how MLOps keeps AI reliable in production: check it out

Translated from the original in Portuguese. Read the original.