模型评估
约 1571 字大约 5 分钟
2026-09-15
📌 原文地址:https://docs.spring.io/java-ai/reference/api/testing.html
📌 原文版本:Java AI 2.0.1(Stable)
测试 AI 应用时,需要评估生成的内容,以确保 AI 模型没有产生幻觉(hallucinated)响应。
评估响应的一种方法是使用 AI 模型本身进行评估。应选择最适合用于评估的 AI 模型——它可能与生成响应的模型不是同一个(比如用 GPT-4 当裁判、而用便宜模型当选手)。
白话解读
LLM 评估的两条路:一是"裁判也是 LLM"——用一个强模型给另一个模型的输出打分;二是用专门的小模型(如 Bespoke Minicheck)做事实核查。Java AI 都帮你封装好了,写一个 @Test 就能在集成测试里直接验证"模型的回答是否相关 / 是否符合事实"。
Java AI 用于评估响应的接口是 Evaluator,定义如下:
@FunctionalInterface
public interface Evaluator {
EvaluationResponse evaluate(EvaluationRequest evaluationRequest);
}评估的输入是 EvaluationRequest,定义如下:
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 | List<Content> | 上下文数据,例如来自检索增强生成(RAG)并附加到原始输入上的内容 |
responseContent | String | AI 模型的响应内容(字符串) |
一、RelevancyEvaluator(相关性评估器)
RelevancyEvaluator 是 Evaluator 接口的一个实现,用于评估 AI 生成响应与所提供的上下文之间的相关性。该评估器有助于评估 RAG 流程的质量,判断 AI 模型的响应相对于检索到的上下文是否与用户输入相关。
评估基于以下三者:用户输入、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:你可以通过 .promptTemplate() 构建器方法提供自己的 PromptTemplate 对象来自定义提示词模板。详见「自定义模板」章节。
1.1 在集成测试中的用法
下面是在集成测试中使用 RelevancyEvaluator 的示例,用于验证使用 RetrievalAugmentationAdvisor 的 RAG 流程的结果:
@Test
void evaluateRelevancy() {
String question = "Where does the adventure of Anacletus and Birba take place?";
// 1) 构造 RAG advisor
RetrievalAugmentationAdvisor ragAdvisor = RetrievalAugmentationAdvisor.builder()
.documentRetriever(VectorStoreDocumentRetriever.builder()
.vectorStore(pgVectorStore)
.build())
.build();
// 2) 真实调一次 ChatClient
ChatResponse chatResponse = ChatClient.builder(chatModel).build()
.prompt(question)
.advisors(ragAdvisor)
.call()
.chatResponse();
// 3) 组装评估请求:用户问题 + 检索上下文 + 模型响应
EvaluationRequest evaluationRequest = new EvaluationRequest(
// 原始用户问题
question,
// 从 RAG 流程中检索到的上下文
chatResponse.getMetadata().get(RetrievalAugmentationAdvisor.DOCUMENT_CONTEXT),
// AI 模型的响应
chatResponse.getResult().getOutput().getText()
);
// 4) 用同一 chatModel 当裁判
RelevancyEvaluator evaluator = new RelevancyEvaluator(ChatClient.builder(chatModel));
EvaluationResponse evaluationResponse = evaluator.evaluate(evaluationRequest);
// 5) 断言:响应应与上下文相关
assertThat(evaluationResponse.isPass()).isTrue();
}你可以在 Java AI 项目中找到多个集成测试,它们使用 RelevancyEvaluator 来测试 QuestionAnswerAdvisor 的功能以及 RetrievalAugmentationAdvisor 的功能。
1.2 自定义模板
RelevancyEvaluator 使用默认模板来提示 AI 模型进行评估。你可以通过 .promptTemplate() 构建器方法提供自己的 PromptTemplate 对象来自定义这一行为。
自定义的 PromptTemplate 可以使用任意 TemplateRenderer 实现(默认使用基于 StringTemplate 引擎的 StPromptTemplate)。重要要求:模板必须包含以下三个占位符:
query占位符:接收用户问题。response占位符:接收 AI 模型的响应。context占位符:接收上下文信息。
二、FactCheckingEvaluator(事实核查评估器)
FactCheckingEvaluator 是 Evaluator 接口的另一个实现,用于评估 AI 生成响应相对于所提供上下文的事实准确性。该评估器通过验证给定的陈述(claim)是否被提供的上下文(document)在逻辑上支持,来帮助检测和减少 AI 输出中的幻觉。
评估时将「claim(声明)」和「document(文档)」提交给 AI 模型进行评估。有一些更小、更高效的专用 AI 模型可用于此目的,例如 Bespoke 的 Minicheck——与 GPT-4 等旗舰模型相比,它有助于降低执行这类检查的成本。Minicheck 也可以通过 Ollama 使用。
2.1 用法
FactCheckingEvaluator 的构造函数接收一个 ChatClient.Builder 作为参数:
public FactCheckingEvaluator(ChatClient.Builder chatClientBuilder) {
this.chatClientBuilder = chatClientBuilder;
}该评估器使用以下提示词模板进行事实核查:
Document: {document}
Claim: {claim}其中 {document} 是上下文信息,{claim} 是待评估的 AI 模型响应。
2.2 示例
下面是一个使用基于 Ollama 的 ChatModel(具体为 Bespoke-Minicheck 模型)的 FactCheckingEvaluator 示例:
@Test
void testFactChecking() {
// 1) 设置 Ollama API(默认连本地 11434 端口)
OllamaApi ollamaApi = new OllamaApi("http://localhost:11434");
// 2) 用 Bespoke-Minicheck 当裁判——小模型、温度 0、只输出 2 token(YES/NO)
ChatModel chatModel = new OllamaChatModel(ollamaApi,
OllamaChatOptions.builder()
.model(BESPOKE_MINICHECK)
.numPredict(2)
.temperature(0.0d)
.build());
// 3) 创建 FactCheckingEvaluator
var factCheckingEvaluator = new FactCheckingEvaluator(ChatClient.builder(chatModel));
// 4) 示例上下文与声明
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.";
// 5) 组装评估请求:把 context 作为 userText(注意:dataList 为空)
EvaluationRequest evaluationRequest = new EvaluationRequest(context, Collections.emptyList(), claim);
// 6) 执行评估
EvaluationResponse evaluationResponse = factCheckingEvaluator.evaluate(evaluationRequest);
// 7) 断言:声明应不被上下文支持(即评估不通过)
assertFalse(evaluationResponse.isPass(), "The claim should not be supported by the context");
}三、要点总结
Evaluator接口:只有一个方法evaluate(EvaluationRequest) -> EvaluationResponse,返回isPass()布尔结果。RelevancyEvaluator用于评估"回答是否与上下文相关"——常用于 RAG 流程。FactCheckingEvaluator用于评估"回答是否符合事实"——可搭配 Bespoke Minicheck 等小模型降低成本。- 自定义模板:两者都支持
.promptTemplate()自定义,但必须保留{query}/{response}/{context}或{document}/{claim}占位符。 - 集成测试模式:用 ChatClient 跑一次真实请求 → 把 metadata 里的上下文、模型输出塞进
EvaluationRequest→ 评估 → 断言。 - 裁判模型选择:建议选用更强的模型做裁判,但 Bespoke Minicheck 这种专用小模型在事实核查场景更划算。