liveBook · Manning

Retrieval-augmented generation

As explained in Data Storytelling with Altair and AI

Retrieval-augmented generation is a technique that supplies a language model with relevant information retrieved from an external knowledge source when generating output.

Retrieval-augmented generation (RAG) extends a language model with domain-specific information at response time. It retrieves relevant material from an external knowledge source, then integrates retrieved facts into generated text. Use it when a response needs current or specialized context—for example, turning a long document into a short data-story annotation—rather than relying on a general-purpose model alone. section 7.4 section 7.4.2

Figure 7.13 A RAG-based system
figure

This architecture separates preparation of the knowledge source from question answering: source text becomes searchable vectors, and retrieved passages supply context for the LLM response. section 7.4.2

A practical RAG workflow is:

  • Load source content, such as an HTML page, and split it into manageable text chunks. section 7.4.2
  • Create embeddings for the chunks and store them in a vector database such as Chroma. section 7.4.2
  • Connect the database to an LLM retriever, then ask a focused question. section 7.4.2
  • Use a prompt that restricts answers to the supplied context and permits an explicit “I don't know” when context is insufficient. section 7.4.2

Generate a product commentary from a web page

Load a smartphone product-description HTML page, split and embed its contents in Chroma, and connect the store to ChatOpenAI through a RetrievalQA chain. A request to describe the New SmartX 2023 in 30 words returns a concise description along with the retrieved source documents. The same pattern can query an FDA fact sheet to produce chart commentary about aquaculture seafood safety. section 7.4.2 section 7.5.2

RAG supports contextual data storytelling: retrieve facts about a product, real-time data, customer reviews, or another topic, then generate an ad hoc commentary or annotation. Tailor the resulting context to the intended audience and place it where it helps readers understand the visualization. section 7.4.2 section 7.6

Practice: annotate a declining period

For a professional-facing salmon chart, extract relevant information from specified pages of a marine-aquaculture report with a PDF loader, then build a RAG system that generates an annotation for the declining period. section 7.5.4

How Data Storytelling with Altair and AI covers it

How deep it goes
Builds a complete HTML-to-vector-database retrieval pipeline, including chunking, embeddings, prompting, and querying.
Examples
Demonstrates smartphone-description answers, FDA-based aquaculture commentary, and a PDF-report annotation exercise.
What it assumes
Assumes Python tool installation while introducing document loading, chunking, vector storage, prompting, and retrieval progressively.
What you can do after
Apply it via LangChain, Chroma, and OpenAI to generate source-grounded commentaries from external documents.
How it connects
Connects information retrieval, text generation, vector databases, embeddings, LangChain, Chroma, and fine-tuning.

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 Storytelling with Altair and AI. The section links point to the parts of the book it draws on.