AI Compass

RAG (retrieval-augmented generation)

Letting an AI answer from your own documents by finding relevant passages first.

What is it?

Retrieval-augmented generation (RAG) combines search with text generation. When a question arrives, the system first searches a collection of documents, such as manuals, policies or notes, for the most relevant passages. Those passages are added to the prompt, and the model writes its answer based on them.

RAG lets a model use information it was never trained on, including private or very recent material, without retraining. It also makes it possible to show sources, so people can check where an answer came from.

Which models use it?

  • Many company chatbots and help centers answer questions from their own documentation using RAG.
  • ChatGPT, Claude and Gemini can answer from files you upload, which uses similar retrieval ideas.
  • AI search tools such as Perplexity retrieve web pages and cite them in answers.
  • Developer frameworks such as LlamaIndex and LangChain provide RAG building blocks.

How to use it

  1. Collect the documents the assistant should answer from and remove outdated versions.
  2. Split them into small passages (chunks) and index them for search.
  3. For each question, retrieve the most relevant passages.
  4. Put those passages in the prompt and instruct the model to answer only from them and cite them.
  5. Tell the model to say "I don't know" when the passages do not contain the answer.
  6. Test with real questions and check both the retrieved passages and the answers.

Benefits

  • Answers can use private and up-to-date information.
  • Citations make answers easier to verify.
  • Fewer made-up facts compared with relying on the model's memory.
  • Updating documents updates answers without retraining a model.

Limits and risks

  • If retrieval finds the wrong passages, the answer will be wrong too.
  • The model can still misread or over-generalize the sources.
  • Documents must be kept current and access-controlled.
  • Retrieved content may contain prompt injection if the sources are not trusted.

Prompt template

Copy it, replace the parts in [brackets] and paste it into any AI chat.

Answer the question below using ONLY the sources provided. Quote or cite the source number for each claim, like [1]. If the sources do not contain the answer, say "The sources do not say" and suggest what document might help.

Sources:
[1] [paste passage]
[2] [paste passage]

Question: [your question]

More in Building with AI

Learn AI in the AI Compass app

Every guide and dictionary card is also in the AI Compass app, with saved items, search and “Try it” buttons that open chat, image or video with the example filled in. Reading is free; creating uses credits, and the price is shown before you start.