評価テスト

AI アプリケーションをテストするには、生成されたコンテンツを評価して、AI モデルが幻覚的なレスポンスを生成していないことを確認する必要があります。

レスポンスを評価する 1 つの方法は、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 として

関連性評価ツール

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:
.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 ( テスト [GitHub] (英語) を参照) と RetrievalAugmentationAdvisor ( テスト [GitHub] (英語) を参照) の機能をテストする統合テストがいくつかあります。

カスタムテンプレート

RelevancyEvaluator はデフォルトのテンプレートを使用して AI モデルに評価を促します。.promptTemplate() ビルダーメソッドを介して独自の PromptTemplate オブジェクトを提供することで、この動作をカスタマイズできます。

カスタム PromptTemplate では、任意の TemplateRenderer 実装を使用できます(デフォルトでは、StringTemplate (英語) エンジンに基づく StPromptTemplate を使用します)。重要な要件として、テンプレートには以下のプレースホルダが含まれている必要があります。

  • ユーザーの質問を受け取るための query プレースホルダー。

  • AI モデルのレスポンスを受信するための response プレースホルダー。

  • コンテキスト情報を受け取るための context プレースホルダー。

FactCheckingEvaluator

FactCheckingEvaluator は、提供されたコンテキストに対する AI 生成レスポンスの事実上の正確性を評価するために設計された、評価インターフェースの別の実装です。この評価は、指定されたステートメント (主張) が提供されたコンテキスト (ドキュメント) によって論理的にサポートされているかどうかを検証することにより、AI 出力の幻覚を検出して軽減できます。

「クレーム」と「ドキュメント」は、評価のために AI モデルに提示されます。Bespoke の Minicheck など、この目的専用のより小型で効率的な AI モデルが利用可能で、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("http://localhost:11434");

  ChatModel chatModel = new OllamaChatModel(ollamaApi,
				OllamaOptions.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");

}