評估測試
測試 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
:上下文資料,例如來自檢索增強生成 (RAG) 的資料,附加到原始輸入中。 -
responseContent
:AI 模型的響應內容,型別為String
RelevancyEvaluator
一種實現是 RelevancyEvaluator
,它使用 AI 模型進行評估。未來版本中將提供更多實現。
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: "
這是一個 JUnit 測試示例,它對載入到向量儲存中的 PDF 文件執行 RAG 查詢,然後評估響應是否與使用者文字相關。
@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 是 Evaluator 介面的另一種實現,旨在根據提供的上下文評估 AI 生成響應的事實準確性。該評估器透過驗證給定陳述(claim)是否在邏輯上得到了提供的上下文(document)的支援,從而幫助檢測和減少 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 模型的響應。
示例
下面是如何將 FactCheckingEvaluator 與基於 Ollama 的 ChatModel(特別是 Bespoke-Minicheck 模型)一起使用的示例:
@Test
void testFactChecking() {
// Set up the Ollama API
OllamaApi ollamaApi = new OllamaApi("https://: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");
}