Skip to content

Commit

Permalink
Avoid NPE
Browse files Browse the repository at this point in the history
Signed-off-by: Heng Qian <[email protected]>
  • Loading branch information
qianheng-aws committed Jul 17, 2024
1 parent c28de3b commit 39d1bcf
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.Map;

import java.util.NoSuchElementException;
import org.opensearch.action.ActionRequest;
import org.opensearch.client.Client;
import org.opensearch.core.action.ActionListener;
Expand Down Expand Up @@ -67,13 +68,18 @@ public MLModelTool(Client client, String modelId, String responseField) {
this.responseField = responseField;

outputParser = o -> {
List<ModelTensors> mlModelOutputs = (List<ModelTensors>) o;
Map<String, ?> dataAsMap = mlModelOutputs.get(0).getMlModelTensors().get(0).getDataAsMap();
// Return the response field if it exists, otherwise return the whole response a json string.
if (dataAsMap.containsKey(responseField)) {
return dataAsMap.get(responseField);
} else {
return StringUtils.toJson(dataAsMap);
try {
List<ModelTensors> mlModelOutputs = (List<ModelTensors>) o;
Map<String, ?> dataAsMap = mlModelOutputs.getFirst().getMlModelTensors().getFirst()
.getDataAsMap();
// Return the response field if it exists, otherwise return the whole response as json string.
if (dataAsMap.containsKey(responseField)) {
return dataAsMap.get(responseField);
} else {
return StringUtils.toJson(dataAsMap);
}
} catch (Exception e) {
throw new IllegalStateException("LLM returns wrong or empty tensors", e);
}
};
}
Expand Down

0 comments on commit 39d1bcf

Please sign in to comment.