跳到主要内容

评估测试

Deepseek 3.2 中英对照 Model Evaluation Evaluation Testing

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

一种评估回复的方法是使用AI模型本身进行评估。选择最佳AI模型进行评估,这可能与生成回复所用的模型不同。

Spring 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: 上下文数据,例如来自检索增强生成的数据,会附加到原始输入中。

  • responseContent: AI模型的响应内容,类型为 String

相关性评估器

RelevancyEvaluatorEvaluator 接口的一个实现,旨在评估 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:
备注

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

在集成测试中的使用

以下是集成测试中使用 RelevancyEvaluator 的示例,用于验证使用 RetrievalAugmentationAdvisor 的 RAG 流程的结果:

@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

FactCheckingEvaluator是Evaluator接口的另一个实现,旨在根据提供的上下文评估AI生成响应的真实性。该评估器通过验证给定陈述(声称)是否在逻辑上得到所提供上下文(文档)的支持,有助于检测并减少AI输出中的幻觉。

'claim'和'document'会被提交给AI模型进行评估。目前已有专门用于此目的的更小型、更高效的AI模型,例如Bespoke的Minicheck。与GPT-4等旗舰模型相比,这类模型有助于降低执行此类核查的成本。Minicheck也可以通过Ollama平台使用。

使用

FactCheckingEvaluator 构造函数接收一个 ChatClient.Builder 作为参数:

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

评估者使用以下提示模板进行事实核查:

Document: {document}
Claim: {claim}

其中 {document} 是上下文信息,而 {claim} 是需要评估的AI模型回答。

示例

以下是一个使用基于Ollama的聊天模型(特别是Bespoke-Minicheck模型)运行FactCheckingEvaluator的示例:

@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");

}