Accept response as a String instead of ChatResponse in EvaluationRequest
- Update docs
This commit is contained in:
committed by
Mark Pollack
parent
9956367fc8
commit
b5cc9db88a
@@ -1,6 +1,5 @@
|
||||
package org.springframework.ai.evaluation;
|
||||
|
||||
import org.springframework.ai.chat.model.ChatResponse;
|
||||
import org.springframework.ai.model.Content;
|
||||
|
||||
import java.util.List;
|
||||
@@ -20,12 +19,12 @@ public class EvaluationRequest {
|
||||
|
||||
private final List<Content> dataList;
|
||||
|
||||
private final ChatResponse chatResponse;
|
||||
private final String responseContent;
|
||||
|
||||
public EvaluationRequest(String userText, List<Content> dataList, ChatResponse chatResponse) {
|
||||
public EvaluationRequest(String userText, List<Content> dataList, String responseContent) {
|
||||
this.userText = userText;
|
||||
this.dataList = dataList;
|
||||
this.chatResponse = chatResponse;
|
||||
this.responseContent = responseContent;
|
||||
}
|
||||
|
||||
public String getUserText() {
|
||||
@@ -36,14 +35,14 @@ public class EvaluationRequest {
|
||||
return dataList;
|
||||
}
|
||||
|
||||
public ChatResponse getChatResponse() {
|
||||
return chatResponse;
|
||||
public String getResponseContent() {
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EvaluationRequest{" + "userText='" + userText + '\'' + ", dataList=" + dataList + ", chatResponse="
|
||||
+ chatResponse + '}';
|
||||
+ responseContent + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -53,12 +52,12 @@ public class EvaluationRequest {
|
||||
if (!(o instanceof EvaluationRequest that))
|
||||
return false;
|
||||
return Objects.equals(userText, that.userText) && Objects.equals(dataList, that.dataList)
|
||||
&& Objects.equals(chatResponse, that.chatResponse);
|
||||
&& Objects.equals(responseContent, that.responseContent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(userText, dataList, chatResponse);
|
||||
return Objects.hash(userText, dataList, responseContent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ public class RelevancyEvaluator implements Evaluator {
|
||||
@Override
|
||||
public EvaluationResponse evaluate(EvaluationRequest evaluationRequest) {
|
||||
|
||||
var response = doGetResponse(evaluationRequest);
|
||||
var response = evaluationRequest.getResponseContent();
|
||||
var context = doGetSupportingData(evaluationRequest);
|
||||
|
||||
String evaluationResponse = this.chatClientBuilder.build()
|
||||
@@ -52,10 +52,6 @@ public class RelevancyEvaluator implements Evaluator {
|
||||
return new EvaluationResponse(passing, score, "", Collections.emptyMap());
|
||||
}
|
||||
|
||||
protected String doGetResponse(EvaluationRequest evaluationRequest) {
|
||||
return evaluationRequest.getChatResponse().getResult().getOutput().getContent();
|
||||
}
|
||||
|
||||
protected String doGetSupportingData(EvaluationRequest evaluationRequest) {
|
||||
List<Content> data = evaluationRequest.getDataList();
|
||||
String supportingData = data.stream()
|
||||
|
||||
@@ -26,21 +26,21 @@ public class EvaluationRequest {
|
||||
|
||||
private final List<Content> dataList;
|
||||
|
||||
private final ChatResponse chatResponse;
|
||||
private final String responseContent;
|
||||
|
||||
public EvaluationRequest(String userText, List<Content> dataList, ChatResponse chatResponse) {
|
||||
public EvaluationRequest(String userText, List<Content> dataList, String responseContent) {
|
||||
this.userText = userText;
|
||||
this.dataList = dataList;
|
||||
this.chatResponse = chatResponse;
|
||||
this.responseContent = responseContent;
|
||||
}
|
||||
|
||||
...
|
||||
}
|
||||
----
|
||||
|
||||
* `userText`: The raw input from the user.
|
||||
* `userText`: The raw input from the user as a `String`
|
||||
* `dataList`: Contextual data, such as from Retrieval Augmented Generation, appended to the raw input.
|
||||
* `chatResponse`: The AI model's response.
|
||||
* `responseContent`: The AI model's response content as a `String`
|
||||
|
||||
== RelevancyEvaluator
|
||||
|
||||
@@ -73,17 +73,17 @@ void testEvaluation() {
|
||||
|
||||
String userText = "What is the purpose of Carina?";
|
||||
|
||||
ChatResponse response = ChatClient.builder(chatModel)
|
||||
String responseContent = ChatClient.builder(chatModel)
|
||||
.build().prompt()
|
||||
.advisors(new QuestionAnswerAdvisor(vectorStore, SearchRequest.defaults()))
|
||||
.user(userText)
|
||||
.call()
|
||||
.chatResponse();
|
||||
.content();
|
||||
|
||||
var relevancyEvaluator = new RelevancyEvaluator(ChatClient.builder(chatModel));
|
||||
|
||||
EvaluationRequest evaluationRequest = new EvaluationRequest(userText,
|
||||
(List<Content>) response.getMetadata().get(QuestionAnswerAdvisor.RETRIEVED_DOCUMENTS), response);
|
||||
(List<Content>) response.getMetadata().get(QuestionAnswerAdvisor.RETRIEVED_DOCUMENTS), responseContent);
|
||||
|
||||
EvaluationResponse evaluationResponse = relevancyEvaluator.evaluate(evaluationRequest);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user