收藏本站,收获最前沿的人工智能与编程资讯!!

Spring AI 评估测试

技术文档 21℃ 0


测试人工智能应用需要对生成的内容进行评估,以确保人工智能模型没有产生幻觉式的响应。

评估响应的一种方法是使用人工智能模型本身进行评估。选择最适合评估的人工智能模型,该模型可能与生成响应所用的模型不同。

Spring AI 中用于评估响应的接口是 Evaluator,其定义如下:

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

评估的输入是 EvaluationRequest,其定义如下:

public class EvaluationRequest {

	private final String userText;

	private final ListdataList;

	private final String responseContent;

	public EvaluationRequest(String userText, ListdataList, String responseContent) {
		this.userText = userText;
		this.dataList = dataList;
		this.responseContent = responseContent;
	}

  ...
}
  • userText:用户输入的原始文本字符串

  • dataList:附加到原始输入的上下文数据,例如来自检索增强生成的数据

  • responseContent:人工智能模型生成的响应内容字符串

相关性评估器

RelevancyEvaluator 是 Evaluator 接口的实现类,用于评估人工智能生成的响应与所提供上下文的相关性。该评估器通过判断人工智能模型的响应在检索上下文的前提下是否与用户输入相关,来评估检索增强生成流程的质量。

评估基于用户输入、人工智能模型的响应和上下文信息。它使用提示模板询问人工智能模型,响应是否与用户输入和上下文相关。

以下是 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 验证检索增强生成流程的结果:

@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(
        // 用户的原始问题
        question,
        // 从检索增强生成流程中检索到的上下文
        chatResponse.getMetadata().get(RetrievalAugmentationAdvisor.DOCUMENT_CONTEXT),
        // 人工智能模型的响应
        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 使用默认模板提示人工智能模型进行评估。你可以通过 .promptTemplate() 建造者方法提供自定义的 PromptTemplate 对象来定制该行为。

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

  • query 占位符:用于接收用户问题

  • response 占位符:用于接收人工智能模型的响应

  • context 占位符:用于接收上下文信息

事实核查评估器

FactCheckingEvaluator 是 Evaluator 接口的另一个实现类,用于评估人工智能生成的响应相对于所提供上下文的事实准确性。该评估器通过验证给定陈述(主张)是否有所提供的上下文(文档)的逻辑支持,帮助检测并减少人工智能输出中的幻觉现象。

评估时会将「主张」和「文档」提供给人工智能模型。目前已有更小、更高效的专用模型用于此任务,例如 Bespoke 的 Minicheck,与 GPT-4 等旗舰模型相比,它能降低执行此类核查的成本。Minicheck 也可通过 Ollama 使用。

用法

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

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

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

Document: {document}
Claim: {claim}

其中 {document} 是上下文信息,{claim} 是待评估的人工智能模型响应。

示例

以下是如何将 FactCheckingEvaluator 与基于 Ollama 的 ChatModel(特指 Bespoke-Minicheck 模型)结合使用的示例:

@Test
void testFactChecking() {
  // 设置 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())


  // 创建事实核查评估器
  var factCheckingEvaluator = new FactCheckingEvaluator(ChatClient.builder(chatModel));

  // 示例上下文和主张
  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.";

  // 创建评估请求
  EvaluationRequest evaluationRequest = new EvaluationRequest(context, Collections.emptyList(), claim);

  // 执行评估
  EvaluationResponse evaluationResponse = factCheckingEvaluator.evaluate(evaluationRequest);

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

}

相关推荐