AI Model Wiki

    Skip to main content
    Back to Glossary

    RAG

    Retrieval-Augmented Generation (RAG) is an architecture that supplements a language model's parametric knowledge - the facts baked into its weights during Pretraining - with non-parametric knowledge retrieved at inference time from an external corpus, so the model can generate responses grounded in up-to-date or proprietary information it was never trained on. At query time, the user's question is converted to an Embedding and matched against a Vector Database of similarly embedded document chunks; the top-ranked chunks are inserted into the Context Window alongside the query, and the LLM generates an answer conditioned on both. RAG directly reduces Hallucination by anchoring generation in retrieved evidence rather than requiring the model to recall facts from memory.

    Think of it like an open-book exam instead of a closed-book one: the student (the language model) still needs intelligence and reasoning ability, but rather than relying entirely on memorised knowledge, they can look up relevant passages before writing their answer - the result is more accurate and verifiable.

    A minimal RAG pipeline with LangChain and a Supabase vector store:

    from langchain_community.vectorstores import SupabaseVectorStore
    from langchain_openai import OpenAIEmbeddings, ChatOpenAI
    from langchain.chains import RetrievalQA
    
    vectorstore = SupabaseVectorStore(client=supabase, embedding=OpenAIEmbeddings(),
                                       table_name="documents", query_name="match_documents")
    qa = RetrievalQA.from_chain_type(
        llm=ChatOpenAI(model="gpt-4o"),
        retriever=vectorstore.as_retriever(search_kwargs={"k": 4})
    )
    print(qa.run("What were Q3 revenues?"))
    # retrieves the 4 most relevant document chunks, then generates a grounded answer
    

    RAG depends on high-quality Embedding models for accurate retrieval and benefits from careful Prompt Engineering to instruct the model to cite retrieved passages. It is often combined with Prefix Caching to amortise the cost of repeatedly prepending the same retrieved documents. Fine-tuning and RAG are complementary strategies - fine-tuning internalises style and task format, while RAG supplies current factual content.

    You will encounter RAG in virtually every enterprise AI deployment that must stay current or handle proprietary data: customer support bots, internal document search, legal research tools, and medical reference assistants all rely on RAG to bridge the gap between a model's training cutoff and real-world knowledge needs.

    Related Courses

    AI Terminology
    Glossary