liveBook · Manning

RelevancyEvaluator

As explained in Spring AI in Action

RelevancyEvaluator is a Spring AI evaluator that uses a language model to judge whether an answer addresses a submitted question.

RelevancyEvaluator is a Spring AI evaluator that asks a language model whether an answer addresses the submitted question. It distinguishes an on-topic answer from a factually correct but unrelated response. section 2.1

Use the evaluator in two complementary ways:

  • Test a service response by creating an EvaluationRequest from the original question and returned answer, then checking EvaluationResponse.isPass(). section 2.1
  • Guard runtime responses by throwing an exception when evaluation fails and retrying the request with Spring Retry. section 2.3

Board-game question answering

A Spring Boot integration test injects BoardGameService and ChatClient.Builder, evaluates the service's answer, and asserts that the result passes. A runtime service can retry failed answers, but should keep maxAttempts low because retries increase model calls, token costs, and possible rate limiting. section 2.1 section 2.3

Listing 2.1 Testing that the response from an LLM is relevant to the question
package com.example.boardgamebuddy;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.evaluation.RelevancyEvaluator;
import org.springframework.ai.evaluation.EvaluationRequest;
import org.springframework.ai.evaluation.EvaluationResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class SpringAiBoardGameServiceTests {

  @Autowired
  private BoardGameService boardGameService;   #1

  @Autowired
  private ChatClient.Builder chatClientBuilder;  #2

  private RelevancyEvaluator relevancyEvaluator;

  @BeforeEach
  public void setup() {
    this.relevancyEvaluator = new RelevancyEvaluator(chatClientBuilder);
  }

  @Test
  public void evaluateRelevancy() {
    String userText = "Why is the sky blue?";
    Question question = new Question(userText);
    Answer answer = boardGameService.askQuestion(question);  #3

    EvaluationRequest evaluationRequest = new EvaluationRequest(
        userText, answer.answer());

    EvaluationResponse response = relevancyEvaluator
        .evaluate(evaluationRequest);  #4

    Assertions.assertThat(response.isPass())    #5
        .withFailMessage("""
          ========================================
          The answer "%s"
          is not considered relevant to the question
          "%s".
          ========================================
          """, answer.answer(), userText)
        .isTrue();
  }

}

Shows the Java integration-test setup, dependency injection, evaluation request, and pass/fail assertion. section 2.1

How Spring AI in Action covers it

How deep it goes
Explains request construction, model-backed judgment, pass/fail handling, and retry consequences without detailing evaluator internals.
Examples
Uses sky-color relevance contrast, BoardGameService tests, and runtime retries as concrete examples.
What it assumes
Assumes a Spring Boot application, an available ChatClient.Builder, and a service that produces question-answer responses.
What you can do after
Apply it via Spring AI to test relevance and reject or retry answers that fail evaluation.
How it connects
Connects evaluation requests and responses with Spring Boot integration testing, ChatClient, Spring Retry, and exception handling.

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.