liveBook · Manning

Retrieval-augmented generation

As explained in Data Pipelines with Apache Airflow, Second Edition

A RAG workflow is a retrieval-augmented generation workflow that retrieves relevant information and supplies it to a generative model for producing an output.

A recipe assistant needs to answer questions from a growing collection of family recipes. Retrieval-augmented generation (RAG) lets a language model use that external collection without retraining whenever a recipe is added: it retrieves relevant recipes, then supplies them with the question to the model for an answer. section 14.3

The workflow has two connected parts:

  • Prepare the collection. Split each recipe into an ingredients chunk and an instructions chunk, retaining the recipe name with both. Embed the content and store its vectors in Weaviate. Airflow can automate ingestion and updates; existing versions are identified before replacement. section 14.5 section 14.10
  • Retrieve context. Embed the user’s question with the same model used for the recipes, then search the vector database for relevant documents. Cosine similarity compares vector direction, emphasizing meaning rather than identical wording. section 14.10.1
  • Generate an answer. Send the question and retrieved recipes to a language model so its response can draw on the collection. section 14.3

A recipe question

For “How can I make lasagna?”, the assistant searches the stored recipes before answering with ingredients and steps. If the requested recipe is absent, the chat assistant says it does not know that recipe. It can also suggest stored recipes from a list of ingredients or share recipes in another language. section 14.10.1 section 14.10.3

Figure 14.3 Simplified representation of a RAG workflow: (1) A user asks a question about a recipe. (2) Vector search retrieves (3) the most relevant recipes. (4) The question and retrieved recipes are combined and sent to an LLM, which (5) generates an answer based on the relevant context.

The diagram follows a question through vector search, retrieval, and generation with the retrieved recipes as context. section 14.3

Chunk size is an operational choice. One enormous chunk makes a source recipe hard to pinpoint; sentence-sized chunks can return many redundant matches from the same recipe. Keeping ingredients and instructions as separate chunks provides a practical middle ground while preserving a link to the original recipe. section 14.5

How Data Pipelines with Apache Airflow, Second Edition covers it

How deep it goes
Works through ingestion, two-part chunking, embedding, similarity search, and answer generation; leaves embedding-model training details aside.
Examples
Uses family recipes, a lasagna question, a two-dimensional flavor analogy, and a recipe chat assistant.
What it assumes
Introduces vectors and embedding models through a flavor-space analogy; implements the workflow with Python and Airflow tasks.
What you can do after
Build it from scratch with Airflow-managed recipe ingestion, Weaviate retrieval, and a language-model response to a user question.
How it connects
Connects embeddings, vector databases, cosine similarity, text chunking, Airflow ingestion, and language models.

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 Data Pipelines with Apache Airflow, Second Edition. The section links point to the parts of the book it draws on.