liveBook · Manning

Retrieval-augmented generation

As explained in Machine Learning Platform Engineering

Retrieval-augmented generation is a method that supplies retrieved external information to a generative model when producing an output.

Retrieval-augmented generation (RAG) answers a question using relevant external information supplied to a language model at query time. It separates finding information from writing the response. An internal documentation assistant, for example, can answer questions about recently updated company procedures without retraining its LLM or placing the entire documentation collection in every prompt. section 12.2.2 section 12.0

Build the pipeline in two phases:

  • At build time, ingest documents, split them into chunks, create embeddings, and index them in a vector store. An embedding model maps semantically similar wording to nearby vectors; FAISS can search those vectors. section 12.1.3 section 12.2.1
  • At query time, embed the question, retrieve relevant chunks, place their text alongside the question in a prompt, and ask the LLM to generate an answer. Retrieve selectively rather than sending the whole collection. section 12.2.1 section 12.2.2
Listing 12.2 Manual RAG vs. LangChain’s declarative pipeline
def manual_rag_query(query: str):
    try:
                query_embedding = embedding_client.embed(query)

        results = vector_db.similarity_search(query_embedding, k=5)

        context = "\n".join([doc.content for doc in results])

        prompt = f"Context: {context}\nQuestion: {query}\nAnswer:"

        response = llm_client.generate(prompt)

        return validate_response(response)
    except Exception as e:

        return handle_error(e)

from langchain.chains import RetrievalQA
from langchain.retrievers import VectorStoreRetriever

chain = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=vector_store.as_retriever(),
    return_source_documents=True
)

result = chain.invoke({"query": query})

Compare the explicit sequence—embed, search, assemble context, generate, validate—with a LangChain RetrievalQA chain that connects an LLM and retriever. section 12.1.3

Answering a key-rotation question

A question about rotating API keys can retrieve documentation even if it uses different wording, such as updating credentials. Put the retrieved text under a Context label, the user’s request under Question, and invoke the LLM for Answer. The labels distinguish retrieved facts from the request and encourage an answer grounded in the documentation. section 12.1.3 section 12.2.6

A working chain is not yet a dependable service. Track retrieval quality, prompts, and LLM behavior with tracing; version prompts, embeddings, retrieval settings, and model configuration independently. Budget tokens before generation: excessive retrieved context can exceed input limits or consume money without improving the answer. A zero-temperature setting improves consistency but does not make output perfectly reproducible, so evaluate answers semantically rather than relying only on exact matches. section 12.5 section 12.2.6

Treat both questions and retrieved content as safety boundaries. Questions influence which documents enter the prompt, creating opportunities for prompt injection or answers outside the assistant’s intended scope. Sanitize inputs and test adversarial cases, hallucinations, and guardrails before exposing a RAG service to users. section 13.2.2 section 2.1.7

How Machine Learning Platform Engineering covers it

How deep it goes
Works through document ingestion, embeddings, vector search, prompt construction, and generation, then addresses operational evaluation and safety.
Examples
Uses an internal documentation chatbot, API-key rotation questions, a manual retrieval pipeline, and a LangChain RetrievalQA chain.
What it assumes
Builds on ML deployment and monitoring practices while introducing the embedding and retrieval components needed for LLM applications.
What you can do after
Build it from scratch as a document-to-answer pipeline, then evaluate retrieval, responses, costs, and safety before deployment.
How it connects
Connects semantic search, vector embeddings, prompt engineering, LLM observability, and adversarial testing.

Also explained in

Compare how 32 books explain retrieval-augmented generation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning Platform Engineering. The section links point to the parts of the book it draws on.