Skip to content
Spring AI 文档版本 2.1.0-SNAPSHOT 源快照 629771b5 更新于 2026-10-16

评估测试

测试 AI 应用需要评估所生成的内容,以确保 AI 模型没有产生幻觉响应。

评估响应的一种方法是让 AI 模型本身来评估。为评估挑选最佳的 AI 模型,它可能不是用于生成响应的那个模型。

Spring AI 用于评估响应的接口是 Evaluator,定义为:

java
@FunctionalInterface
public interface Evaluator {
    EvaluationResponse evaluate(EvaluationRequest evaluationRequest);
}

评估的输入是定义为以下的 EvaluationRequest

java
public class EvaluationRequest {

	private final String userText;

	private final List<Content> dataList;

	private final String responseContent;

	public EvaluationRequest(String userText, List<Content> dataList, String responseContent) {
		this.userText = userText;
		this.dataList = dataList;
		this.responseContent = responseContent;
	}

  ...
}
  • userText:作为 String 的用户原始输入
  • dataList:上下文数据,例如来自检索增强生成的、追加到原始输入的内容。
  • responseContent:作为 String 的 AI 模型响应内容。

相关性评估器

RelevancyEvaluator 是 Evaluator 接口的一个实现,旨在评估 AI 生成响应相对于所提供上下文的相关性。这个评估器通过判定 AI 模型的响应就检索到的上下文而言是否与用户输入相关,来帮助评估 RAG 流程的质量。

评估基于用户输入、AI 模型的响应和上下文信息。它用一个提示词模板询问 AI 模型:响应是否与用户输入和上下文相关。

这是 RelevancyEvaluator 使用的默认提示词模板:

Your task is to evaluate if the response for the query
is in line with the context information provided.

You have two options to answer. Either YES or NO.

Answer YES, if the response for the query
is in line with context information otherwise NO.

Query:
{query}

Response:
{response}

Context:
{context}

Answer:

NOTE

你可以通过 .promptTemplate() 构建器方法提供你自己的 PromptTemplate 对象来自定义提示词模板。细节参见自定义模板。

在集成测试中使用

下面是在一个集成测试中使用 RelevancyEvaluator 的例子,它用 RetrievalAugmentationAdvisor 验证一个 RAG 流程的结果:

java
@Test
void evaluateRelevancy() {
    String question = "Where does the adventure of Anacletus and Birba take place?";

    RetrievalAugmentationAdvisor ragAdvisor = RetrievalAugmentationAdvisor.builder()
        .documentRetriever(VectorStoreDocumentRetriever.builder()
            .vectorStore(pgVectorStore)
            .build())
        .build();

    ChatResponse chatResponse = ChatClient.builder(chatModel).build()
        .prompt(question)
        .advisors(ragAdvisor)
        .call()
        .chatResponse();

    EvaluationRequest evaluationRequest = new EvaluationRequest(
        // The original user question
        question,
        // The retrieved context from the RAG flow
        chatResponse.getMetadata().get(RetrievalAugmentationAdvisor.DOCUMENT_CONTEXT),
        // The AI model's response
        chatResponse.getResult().getOutput().getText()
    );

    RelevancyEvaluator evaluator = new RelevancyEvaluator(ChatClient.builder(chatModel));

    EvaluationResponse evaluationResponse = evaluator.evaluate(evaluationRequest);

    assertThat(evaluationResponse.isPass()).isTrue();
}

你可以在 Spring AI 项目中找到若干使用 RelevancyEvaluator 测试 QuestionAnswerAdvisor(见测试)和 RetrievalAugmentationAdvisor(见测试)功能的集成测试。

自定义模板

RelevancyEvaluator 用一个默认模板来提示 AI 模型做评估。你可以通过 .promptTemplate() 构建器方法提供你自己的 PromptTemplate 对象来自定义这一行为。

自定义的 PromptTemplate 可使用任何 TemplateRenderer 实现(默认使用基于 StringTemplate 引擎的 StPromptTemplate)。重要的要求是模板必须包含以下占位符:

  • 一个 query 占位符以接收用户问题。
  • 一个 response 占位符以接收 AI 模型的响应。
  • 一个 context 占位符以接收上下文信息。

事实核查评估器

FactCheckingEvaluator 是 Evaluator 接口的另一个实现,旨在评估 AI 生成响应相对于所提供上下文的事实准确性。这个评估器通过核验给定陈述(主张)是否在逻辑上被所提供的上下文(文档)支持,来帮助检测并减少 AI 输出中的幻觉。

“主张”和“文档”被呈交给 AI 模型做评估。已有为此目的专做的更小、更高效 AI 模型,例如 Bespoke 的 Minicheck,它相比 GPT-4 这样的旗舰模型能降低执行这类核查的成本。Minicheck 也可通过 Ollama 使用。

用法

FactCheckingEvaluator 构造器以一个 ChatClient.Builder 作为参数:

java
public FactCheckingEvaluator(ChatClient.Builder chatClientBuilder) {
  this.chatClientBuilder = chatClientBuilder;
}

该评估器使用以下提示词模板做事实核查:

Document: {document}
Claim: {claim}

Where +{document}+ is the context information, and +{claim}+ is the AI model's response to be evaluated.

Example

Here's an example of how to use the FactCheckingEvaluator with an Ollama-based ChatModel, specifically the Bespoke-Minicheck model:

java
@Test
void testFactChecking() {
  // Set up the Ollama API
  OllamaApi ollamaApi = new OllamaApi("http://localhost:11434");

  ChatModel chatModel = new OllamaChatModel(ollamaApi,
				OllamaChatOptions.builder().model(BESPOKE_MINICHECK).numPredict(2).temperature(0.0d).build())


  // Create the FactCheckingEvaluator
  var factCheckingEvaluator = new FactCheckingEvaluator(ChatClient.builder(chatModel));

  // Example context and claim
  String context = "The Earth is the third planet from the Sun and the only astronomical object known to harbor life.";
  String claim = "The Earth is the fourth planet from the Sun.";

  // Create an EvaluationRequest
  EvaluationRequest evaluationRequest = new EvaluationRequest(context, Collections.emptyList(), claim);

  // Perform the evaluation
  EvaluationResponse evaluationResponse = factCheckingEvaluator.evaluate(evaluationRequest);

  assertFalse(evaluationResponse.isPass(), "The claim should not be supported by the context");

}

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。