Retrieval-augmented generation

5 concepts · 10 practice questions

The RAG pipeline

RAG answers from your documents instead of the model's memory. Offline, documents are parsed, chunked, embedded and indexed. Online, the question is turned into a search, the best chunks are retrieved, placed in the prompt, and the model answers from them. It is the default way to give a model private or recent knowledge, because updating an index is far cheaper and faster than retraining a model.

def answer(question):
    chunks = retrieve(question, k=8)                 # search + rerank
    context = "\n\n".join(f"[{i+1}] {c.text}" for i, c in enumerate(chunks))
    prompt = f"<context>\n{context}\n</context>\n\nQuestion: {question}"
    return llm(SYSTEM_GROUNDED, prompt), chunks

Also covered

Grounding and citations
Query transformation
Where RAG fails
Freshness and access control

Sign in to read the rest and take the 10-question test.

Sign in to continue