Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix full_response false and no output mapping exceptions #2944

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ private static Map<String, String> getDefaultOutputMapping(Integer mappingIndex,
Map<String, String> outputMapping;
if (processOutputMap == null || processOutputMap.size() == 0) {
outputMapping = new HashMap<>();
outputMapping.put(DEFAULT_OUTPUT_FIELD_NAME, "$." + DEFAULT_OUTPUT_FIELD_NAME);
outputMapping.put(DEFAULT_OUTPUT_FIELD_NAME, null);
} else {
outputMapping = processOutputMap.get(mappingIndex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,158 @@ public void onFailure(Exception e) {
verify(client, times(1)).execute(any(), any(), any());
}

/**
* Tests create processor with one_to_one is false
* with custom prompt
* with many to one prediction, 5 documents in hits are calling 1 prediction tasks
* with full response path false and no output mapping is provided
* @throws Exception if an error occurs during the test
*/
public void testProcessResponseManyToOneWithCustomPromptFullResponsePathFalse() throws Exception {

String documentField = "text";
String modelInputField = "context";
List<Map<String, String>> inputMap = new ArrayList<>();
Map<String, String> input = new HashMap<>();
input.put(modelInputField, documentField);
inputMap.add(input);

Map<String, String> modelConfig = new HashMap<>();
modelConfig
.put(
"prompt",
"\\n\\nHuman: You are a professional data analyst. You will always answer question based on the given context first. If the answer is not directly shown in the context, you will analyze the data and find the answer. If you don't know the answer, just say I don't know. Context: ${parameters.context}. \\n\\n Human: please summarize the documents \\n\\n Assistant:"
);
MLInferenceSearchResponseProcessor responseProcessor = new MLInferenceSearchResponseProcessor(
"model1",
inputMap,
null,
modelConfig,
DEFAULT_MAX_PREDICTION_TASKS,
PROCESSOR_TAG,
DESCRIPTION,
false,
"remote",
false,
false,
false,
"{ \"parameters\": ${ml_inference.parameters} }",
client,
TEST_XCONTENT_REGISTRY_FOR_QUERY,
false
);

SearchRequest request = getSearchRequest();
String fieldName = "text";
SearchResponse response = getSearchResponse(5, true, fieldName);
Map<String, Object> predictionResult = ImmutableMap.of("response", "here is a summary of the documents");

ModelTensor modelTensor = ModelTensor.builder().dataAsMap(predictionResult).build();
ModelTensors modelTensors = ModelTensors.builder().mlModelTensors(Arrays.asList(modelTensor)).build();
ModelTensorOutput mlModelTensorOutput = ModelTensorOutput.builder().mlModelOutputs(Arrays.asList(modelTensors)).build();

doAnswer(invocation -> {
ActionListener<MLTaskResponse> actionListener = invocation.getArgument(2);
actionListener.onResponse(MLTaskResponse.builder().output(mlModelTensorOutput).build());
return null;
}).when(client).execute(any(), any(), any());

ActionListener<SearchResponse> listener = new ActionListener<>() {
@Override
public void onResponse(SearchResponse newSearchResponse) {
assertEquals(newSearchResponse.getHits().getHits().length, 5);
assertEquals(newSearchResponse.getHits().getHits()[0].getSourceAsMap().get("inference_results"), predictionResult);
assertEquals(newSearchResponse.getHits().getHits()[1].getSourceAsMap().get("inference_results"), predictionResult);
assertEquals(newSearchResponse.getHits().getHits()[2].getSourceAsMap().get("inference_results"), predictionResult);
assertEquals(newSearchResponse.getHits().getHits()[3].getSourceAsMap().get("inference_results"), predictionResult);
}

@Override
public void onFailure(Exception e) {
throw new RuntimeException(e);
}

};
responseProcessor.processResponseAsync(request, response, responseContext, listener);
verify(client, times(1)).execute(any(), any(), any());
}

/**
* Tests create processor with one_to_one is false
* with custom prompt
* with many to one prediction, 5 documents in hits are calling 1 prediction tasks
* with full response path true and no output mapping is provided
* @throws Exception if an error occurs during the test
*/
public void testProcessResponseManyToOneWithCustomPromptFullResponsePathTrue() throws Exception {

String documentField = "text";
String modelInputField = "context";
List<Map<String, String>> inputMap = new ArrayList<>();
Map<String, String> input = new HashMap<>();
input.put(modelInputField, documentField);
inputMap.add(input);

Map<String, String> modelConfig = new HashMap<>();
modelConfig
.put(
"prompt",
"\\n\\nHuman: You are a professional data analyst. You will always answer question based on the given context first. If the answer is not directly shown in the context, you will analyze the data and find the answer. If you don't know the answer, just say I don't know. Context: ${parameters.context}. \\n\\n Human: please summarize the documents \\n\\n Assistant:"
);
MLInferenceSearchResponseProcessor responseProcessor = new MLInferenceSearchResponseProcessor(
"model1",
inputMap,
null,
modelConfig,
DEFAULT_MAX_PREDICTION_TASKS,
PROCESSOR_TAG,
DESCRIPTION,
false,
"remote",
true,
false,
false,
"{ \"parameters\": ${ml_inference.parameters} }",
client,
TEST_XCONTENT_REGISTRY_FOR_QUERY,
false
);

SearchRequest request = getSearchRequest();
String fieldName = "text";
SearchResponse response = getSearchResponse(5, true, fieldName);
Map<String, Object> predictionResult = ImmutableMap.of("response", "here is a summary of the documents");
ModelTensor modelTensor = ModelTensor.builder().dataAsMap(predictionResult).build();
ModelTensors modelTensors = ModelTensors.builder().mlModelTensors(Arrays.asList(modelTensor)).build();
ModelTensorOutput mlModelTensorOutput = ModelTensorOutput.builder().mlModelOutputs(Arrays.asList(modelTensors)).build();
Map<String, Object> fullPredictionResult = generateInferenceResult("here is a summary of the documents");

doAnswer(invocation -> {
ActionListener<MLTaskResponse> actionListener = invocation.getArgument(2);
actionListener.onResponse(MLTaskResponse.builder().output(mlModelTensorOutput).build());
return null;
}).when(client).execute(any(), any(), any());

ActionListener<SearchResponse> listener = new ActionListener<>() {
@Override
public void onResponse(SearchResponse newSearchResponse) {
assertEquals(newSearchResponse.getHits().getHits().length, 5);
assertEquals(newSearchResponse.getHits().getHits()[0].getSourceAsMap().get("inference_results"), fullPredictionResult);
assertEquals(newSearchResponse.getHits().getHits()[1].getSourceAsMap().get("inference_results"), fullPredictionResult);
assertEquals(newSearchResponse.getHits().getHits()[2].getSourceAsMap().get("inference_results"), fullPredictionResult);
assertEquals(newSearchResponse.getHits().getHits()[3].getSourceAsMap().get("inference_results"), fullPredictionResult);
}

@Override
public void onFailure(Exception e) {
throw new RuntimeException(e);
}

};
responseProcessor.processResponseAsync(request, response, responseContext, listener);
verify(client, times(1)).execute(any(), any(), any());
}

/**
* Tests create processor with one_to_one is true
* with no mapping provided
Expand Down Expand Up @@ -401,23 +553,23 @@ public void onResponse(SearchResponse newSearchResponse) {
assertEquals(newSearchResponse.getHits().getHits().length, 5);
assertEquals(
newSearchResponse.getHits().getHits()[0].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[1].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[2].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[3].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[4].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
}

Expand Down Expand Up @@ -482,23 +634,23 @@ public void onResponse(SearchResponse newSearchResponse) {
assertEquals(newSearchResponse.getHits().getHits().length, 5);
assertEquals(
newSearchResponse.getHits().getHits()[0].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[1].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[2].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[3].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[4].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
}

Expand Down Expand Up @@ -1893,23 +2045,23 @@ public void onResponse(SearchResponse newSearchResponse) {
assertEquals(newSearchResponse.getHits().getHits().length, 5);
assertEquals(
newSearchResponse.getHits().getHits()[0].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[1].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[2].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[3].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[4].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
}

Expand Down Expand Up @@ -1973,23 +2125,23 @@ public void onResponse(SearchResponse newSearchResponse) {
assertEquals(newSearchResponse.getHits().getHits().length, 5);
assertEquals(
newSearchResponse.getHits().getHits()[0].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[1].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[2].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[3].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
assertEquals(
newSearchResponse.getHits().getHits()[4].getSourceAsMap().get(DEFAULT_OUTPUT_FIELD_NAME).toString(),
"[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]"
"{inference_results=[{output=[{dataAsMap={response=[0.0, 1.0, 2.0, 3.0, 4.0]}}]}]}"
);
}

Expand Down Expand Up @@ -3054,6 +3206,27 @@ private static SearchRequest getSearchRequest() {
return request;
}

private static Map<String, Object> generateInferenceResult(String response) {
Map<String, Object> inferenceResult = new HashMap<>();
List<Map<String, Object>> inferenceResults = new ArrayList<>();

Map<String, Object> outputMap = new HashMap<>();
List<Map<String, Object>> outputs = new ArrayList<>();

Map<String, Object> responseOutput = new HashMap<>();
Map<String, String> dataAsMap = new HashMap<>();
dataAsMap.put("response", response);
responseOutput.put("dataAsMap", dataAsMap);

outputs.add(responseOutput);
outputMap.put("output", outputs);

inferenceResults.add(outputMap);
inferenceResult.put("inference_results", inferenceResults);

return inferenceResult;
}

/**
* Helper method to create an instance of the MLInferenceSearchResponseProcessor with the specified parameters in
* single pair of input and output mapping.
Expand Down
Loading