liveBook · Manning

Chapter 4: Talking with your documents

What you will learn in chapter 4 of Spring AI in Action

The introduction explains why giving an LLM relevant document content when a question is asked can improve answers beyond what its training alone supports.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why giving an LLM relevant document content when a question is asked can improve answers beyond what its training alone supports.

Key terms: retrieval-augmented generation (RAG), LLM, hallucinations

4.1 Understanding RAG

Explains why retrieval-augmented generation (RAG) selects relevant document chunks for a prompt instead of stuffing entire rulebooks into it.

Key terms: RAG, prompt stuffing, context window, document chunks, document loader, vector store, embeddings, prompt as context, embedding models, vector stores

4.2 Setting up a vector store

The section explains how vector search finds similar content and how to run Qdrant as a vector store for the Board Game Buddy application.

Key terms: vector search, cosine similarity, cosine distance, vector store, Spring AI, Qdrant, Docker Compose, compose.yaml, port 6334, port 6333

4.3 Loading documents

The section explains the basic document-loading flow for a vector store and why Board Game Buddy needs a pipeline that can load multiple documents over time.

Key terms: document reader, text splitter, vector store client, TextReader, TokenTextSplitter, Document, document-loading pipeline, Resource, Board Game Buddy, vector store

4.3.1 Initializing the loader project

The section sets up a Spring Boot document-loader project, explaining its dependencies, build configuration, and properties for writing documents to Qdrant.

Key terms: fileSupplier, custom consumer, Spring Cloud Function, Spring AI Tika Document Reader, Spring AI Qdrant, embedding API, embedding models, game rules application, Spring Function Catalog, file supplier dependency

4.3.2 Defining the loader pipeline

This section defines a Spring Cloud Function pipeline that selects files, turns them into document chunks, and writes those chunks to a vector store.

Key terms: spring.cloud.function.definition, composed function, Spring application context, beans, fileSupplier, file.supplier.directory, file.supplier.filename-regex, documentReader, Document, splitter

4.3.3 Creating the pipeline components

This section implements a document-loading pipeline that reads game rules, splits them into chunks, adds game-title metadata, and writes the chunks to a vector store.

Key terms: Function, Consumer, Flux, @Bean, composite function, TikaDocumentReader, PagePdfDocumentReader, ParagraphPdfDocumentReader, table of contents metadata, documentReader

4.3.4 Running the pipeline

This section starts the document-loading pipeline and shows how to confirm that PDF rulebooks were written to Qdrant.

Key terms: spring.cloud.function.definition, ApplicationRunner, FunctionCatalog, lookup(), run(), /tmp/dropoff, Qdrant, /collections, points, exact

4.4 Implementing RAG

Introduces the RAG flow for Board Game Buddy and identifies the vector store dependency needed before implementing it.

Key terms: RAG, vector store, prompt, context, Qdrant, vector store starter dependency, Board Game Buddy

4.4.1 Searching for similar documents

This section changes GameRulesService from loading rules at a fixed location to retrieving relevant rule chunks from a vector store for a specific game.

Key terms: GameRulesService, VectorStore, SearchRequest, gameTitle, normalized game title, FilterExpressionBuilder, Top-K, withTopK(), prompt context, tokens

4.4.2 Updating the service

Updates the board-game question service to use retrieved rules, tightens the prompt to discourage unsupported answers, and shows how to try the result.

Key terms: SpringAiBoardGameService, GameRulesService, getRulesFor(), askQuestion(), {rules}, systemPromptTemplate.st, hallucinations, I don’t know, vector store, /ask endpoint

4.5 Implementing RAG with an advisor

This section shows how QuestionAnswerAdvisor handles RAG retrieval and prompt context, then configures it as a default ChatClient advisor with a per-request game-title filter.

Key terms: QuestionAnswerAdvisor, ChatClient, VectorStore, similarity search, defaultAdvisors(), advisors(), FILTER_EXPRESSION, game title filter, SearchRequest

4.6 Applying modular RAG

This section introduces RetrievalAugmentationAdvisor as a configurable alternative to QuestionAnswerAdvisor and shows how to use it with a vector-store retriever and prompt-time filters.

Key terms: RetrievalAugmentationAdvisor, QuestionAnswerAdvisor, DocumentRetriever, VectorStoreDocumentRetriever, RAG dependency, default advisor, ChatClient, advisors(), FILTER_EXPRESSION

4.6.1 Rewriting the user’s query

This subsection explains why a focused query may improve document retrieval and how to configure Spring AI to rewrite a user’s query before vector-store search.

Key terms: query, vector store, RewriteQueryTransformer, ChatClient.Builder, chatClientBuilder(), RetrievalAugmentationAdvisor, queryTransformers()

4.6.2 Translating user queries

This section explains how translating user queries into the language of stored documents can improve retrieval, and how to configure that translation alongside query rewriting.

Key terms: TranslationQueryTransformer, vector store, target language, RetrievalAugmentationAdvisor, queryTransformers(), RewriteQueryTransformer, ChatClient .Builder, chatClientBuilder()

4.6.3 Expanding user queries

This section explains how MultiQueryExpander broadens vector retrieval with alternative phrasings, how to configure it, and the tradeoff between explicit RAG and advisor-based RAG.

Key terms: MultiQueryExpander, RewriteQueryTransformer, vector search, ChatClient.Builder, RetrievalAugmentationAdvisor, queryExpander(), numberOfQueries(), includeOriginal, GameRulesService, QuestionAnswerAdvisor

4.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 3: Submitting prompts for generation Chapter 5: Enabling conversational memory →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.