評価テスト

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

1 つの実装は、評価に AI モデルを使用する RelevancyEvaluator です。今後のリリースでは、さらに多くの実装が利用可能になる予定です。

RelevancyEvaluator は入力 (userText) と AI モデルの出力 (chatResponse) を使用して次の質問をします。

Your task is to evaluate if the response for the query
is in line with the context information provided.\n
You have two options to answer. Either YES/ NO.\n
Answer - YES, if the response for the query
is in line with context information otherwise NO.\n
Query: \n {query}\n
Response: \n {response}\n
Context: \n {context}\n
Answer: "

以下は、ベクトルストアにロードされた PDF ドキュメントに対して RAG クエリを実行し、レスポンスがユーザーテキストに関連しているかどうかを評価する JUnit テストの例です。

@Test
void testEvaluation() {

    dataController.delete();
    dataController.load();

    String userText = "What is the purpose of Carina?";

    ChatResponse response = ChatClient.builder(chatModel)
            .build().prompt()
            .advisors(new QuestionAnswerAdvisor(vectorStore))
            .user(userText)
            .call()
            .chatResponse();
    String responseContent = response.getResult().getOutput().getContent();

    var relevancyEvaluator = new RelevancyEvaluator(ChatClient.builder(chatModel));

    EvaluationRequest evaluationRequest = new EvaluationRequest(userText,
            (List<Content>) response.getMetadata().get(QuestionAnswerAdvisor.RETRIEVED_DOCUMENTS), responseContent);

    EvaluationResponse evaluationResponse = relevancyEvaluator.evaluate(evaluationRequest);

    assertTrue(evaluationResponse.isPass(), "Response is not relevant to the question");

}

上記のコードは、ここにあるサンプルアプリケーションからのものです。

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

}