liveBook · Manning

Retrieval-augmented generation

As explained in Spring AI in Action

Retrieval-augmented generation is a technique that supplies retrieved external information to a generative model when producing a response.

Retrieval-augmented generation (RAG) lets an application answer questions about information outside an LLM’s training data, including confidential documents and events after its training cutoff. It retrieves relevant document content and supplies it as prompt context. section 1.3 section 4.0

Instead of stuffing entire rulebooks into a prompt, RAG splits documents into chunks, embeds them, stores them in a vector store, and retrieves chunks similar to the user’s question. The application then adds those chunks to the prompt. section 4.1 section 4.7

A Spring AI implementation typically proceeds as follows:

  • Add a vector-store starter, such as the Qdrant integration, and ensure the store contains loaded document chunks. section 4.4 section 4.1
  • Inject a VectorStore and create a SearchRequest using the user’s question as the query. section 4.4.1
  • Filter results with metadata such as a normalized game title when one vector store contains documents for multiple subjects. section 4.4.1
  • Control retrieved context with Top-K and similarity thresholds, balancing relevance against prompt tokens and request cost. section 4.4.1
  • Insert the retrieved text into a prompt that requires the model to answer from the supplied documents and say “I don’t know” otherwise. section 4.4.2

Game rules assistant

A Board Game Buddy service retrieves rule chunks for the requested game before calling the model. A question about Burger Battle’s Grave Digger card can receive an answer from loaded rules; a question about Carcassonne returns “I don’t know” until Carcassonne’s rules are loaded. section 4.4.2

Figure 4.1 RAG involves finding documents relevant to the question and adding them as context in the prompt.
figure

Use the diagram to trace the flow from document loading and vector storage through similarity retrieval to prompt context. section 4.1

How Spring AI in Action covers it

How deep it goes
Explains chunking, embeddings, vector storage, similarity search, metadata filters, prompt injection, and retrieval controls.
Examples
Demonstrates a board-game assistant retrieving Burger Battle and Carcassonne rule chunks through a Qdrant-backed service.
What it assumes
Assumes an LLM application and documents, while teaching the retrieval pipeline and vector-store search from its basic abstraction.
What you can do after
Build it from scratch by loading documents, querying a vector store, and injecting retrieved context into a Spring AI prompt.
How it connects
Connects document loaders, embeddings, vector stores, similarity search, prompt templates, ChatClient, and QuestionAnswerAdvisor.

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 Spring AI in Action. The section links point to the parts of the book it draws on.