評估測試

測試 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 是 Evaluator 介面的另一個實現,旨在評估 AI 生成的響應與所提供上下文的事實準確性。此評估器透過驗證給定陳述(主張)是否得到所提供上下文(文件)的邏輯支援,幫助檢測並減少 AI 輸出中的幻覺。

“主張”和“文件”將呈現給 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 模型響應。

示例

以下是如何使用 FactCheckingEvaluator 與基於 Ollama 的 ChatModel(特別是 Bespoke-Minicheck 模型)的示例

@Test
void testFactChecking() {
  // Set up the Ollama API
  OllamaApi ollamaApi = new OllamaApi("https://: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");

}
© . This site is unofficial and not affiliated with VMware.