liveBook · Manning

QuestionAnswerAdvisor

As explained in Spring AI in Action

QuestionAnswerAdvisor is a Spring AI advisor that searches a vector store for documents related to a question and adds them as context to a prompt.

QuestionAnswerAdvisor turns retrieval-augmented generation into a ChatClient advisor. Before the model call, it searches a vector store for documents similar to the question and adds them to the prompt as context. section 4.5 section 4.7

Use it incrementally:

  • Attach QuestionAnswerAdvisor.builder(vectorStore).build() to a request through advisors(). section 4.5
  • Register it with defaultAdvisors() when every ChatClient request should use the same vector store. section 4.5
  • Restrict retrieval with FILTER_EXPRESSION when questions must match document metadata such as gameTitle. section 4.5
@Override
public Answer askQuestion(Question question) {

  String gameNameMatch = String.format("gameTitle == 
  '%s'", question.gameTitle());

  return chatClient.prompt()
      .system(systemSpec -> systemSpec
          .text(promptTemplate)
          .param("gameTitle", question.gameTitle()))
      .user(question.question())
      .advisors(
          QuestionAnswerAdvisor.builder(vectorStore).build())
      .call()
      .entity(Answer.class);
}

A complete askQuestion() call combines a system prompt, user question, QuestionAnswerAdvisor, and typed Answer mapping. section 4.5

How Spring AI in Action covers it

How deep it goes
Explains vector-store search, prompt injection, default configuration, and metadata filtering while leaving advisor internals under the covers.
Examples
Uses tabletop-game rules, an askQuestion() method, a configured ChatClient bean, and a gameTitle metadata filter.
What it assumes
Assumes a ChatClient and vector store, while showing how to connect them through advisor configuration.
What you can do after
Apply it via Spring AI to add relevant vector-store documents to prompts without implementing retrieval manually.
How it connects
Connects retrieval-augmented generation with ChatClient, vector stores, prompt templates, and RetrievalAugmentationAdvisor.

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.