Retrieval-augmented generation (RAG)

AI & LLM fundamentals · Practical · 14 min

RAG is the standard way to make a model answer from company knowledge it was never trained on, with fewer hallucinations.

A model knows nothing about your internal handbook, product docs or last month's tickets, and you cannot paste all of them into every prompt. Retrieval-augmented generation (RAG) solves this: first retrieve the few passages relevant to the question, then let the model generate an answer from them.

Setting it up (once, and again when documents change):

1. Split documents into chunks, passages of a few paragraphs each. Chunks that are too small lose context; too large ones waste tokens and blur the match. 2. Embed each chunk and store the embeddings with the text in a vector database (a database built to find the nearest embeddings quickly) or a search index.

Answering a question:

3. Embed the question and fetch the most similar chunks, often combined with keyword search. 4. Put those chunks into the prompt, labelled, with an instruction like "Answer only from the sources below and cite the source id. If they don't contain the answer, say so." 5. The model answers, grounded in the retrieved text.

Many platforms offer this ready-made: OpenAI and Google both have file search features where you upload files and they handle chunking and search, and many automation platforms offer knowledge-base or vector-store steps.

Most RAG problems are retrieval problems: if the right chunk was not found, the model cannot use it. When answers are wrong, look first at which chunks were retrieved. Common fixes are better chunking, adding keyword search, rewriting the question before searching, and storing labels such as date or department to filter by.

With today's large context windows, a small document set can simply be pasted in whole. RAG is for when the material is too big, changes often, or must be filtered per user.

Example

Answer the question using only the sources. Cite source ids like [2].
If the sources don't contain the answer, say "I couldn't find that in the handbook."

<source id="1">Employees accrue 2 days of paid leave per month...</source>
<source id="2">Unused leave of up to 5 days carries over to the next year...</source>

Question: Can I carry over unused leave?

Remember

  • RAG = retrieve relevant chunks, then answer from them
  • Chunk, embed and store documents; embed the question to search
  • Most failures are retrieval failures: inspect what was retrieved

Check yourself

  1. A RAG assistant answers 'I don't know' to a question clearly covered in the handbook. Where should you look first?
  2. When might you skip RAG and paste the documents directly?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: developers.openai.com/api/docs/guides/retrieval