-
Notifications
You must be signed in to change notification settings - Fork 140
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
Add pre and post process functions for Bedrock Rerank API #3254 #3339
base: main
Are you sure you want to change the base?
Add pre and post process functions for Bedrock Rerank API #3254 #3339
Conversation
…project#3254 Signed-off-by: tkykenmt <[email protected]>
Apply Spotless |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor feedback on validation/converting scores.
} | ||
List<?> outerList = (List<?>) input; | ||
if (!outerList.isEmpty()) { | ||
if (!(outerList.get(0) instanceof Map)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make this iteratively? That way we check all the items for having a map and a valid index and relevance score key
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, will be updated on the next commit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated on 4af169a
scores[index] = switch (relevanceScore) { | ||
case BigDecimal bd -> bd.doubleValue(); | ||
case Double d -> d; | ||
case null -> throw new IllegalArgumentException("relevanceScore is null"); | ||
default -> throw new IllegalArgumentException("Unexpected type for relevanceScore: " + | ||
relevanceScore.getClass().getName()); | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is nice but I believe it will cause issues on backports without this feature what if we exctracted this into a method and do manual if-else checks?
Something like
private Double toDouble(Object score) {
if (relevanceScore instanceof BigDecimal) {
return ((BigDecimal) relevanceScore).doubleValue();
} else if (relevanceScore instanceof Double) {
return (Double) relevanceScore;
} else if (relevanceScore == null) {
throw new IllegalArgumentException("relevanceScore is null");
}
throw new IllegalArgumentException("Unexpected type for relevanceScore: " +
relevanceScore.getClass().getName());
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, will be updated on the next commit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated on 4af169a
|
||
@Override | ||
public void validate(MLInput mlInput) { | ||
if (!(mlInput.getInputDataset() instanceof TextSimilarityInputDataSet)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also check for null before getInputDataset()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, will be updated on the next commit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've checked null check is already implemented in apply method in superclass, ConnectorPreProcessFunction and ConnectorPostProcessFunction. apply method is wrapping validate method.
Thus, I think it's not necessary to implement nullcheck in validate method again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just added following validation in validate method.
if (mlInput.getInputDataset() == null) {
throw new IllegalArgumentException("Input dataset cannot be null.");
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated on 4af169a
exceptionRule.expectMessage("The rerank result should contain index and relevance_score."); | ||
function.apply(Arrays.asList(Map.of("test1", "value1"))); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe lets make a null test? just so we can understand?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
null test can be implemented by referring superclass, but writing superclass's test case in test class for extended class can lead build failure when superclass is updated.
Map.of("index", 2, "relevanceScore", 0.7711548805236816), | ||
Map.of("index", 0, "relevanceScore", 0.0025114635936915874), | ||
Map.of("index", 1, "relevanceScore", 2.4876489987946115e-05), | ||
Map.of("index", 3, "relevanceScore", 6.339210358419223e-06) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently this will pass when just the first is in correct format but does not check the rest. Like mentioned early if you can change the validation to check each entry is in the right format
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me add a test case to check a list having incorrect map. will update on the next commit.
@Test
public void process_WrongInput_NotCorrectMap() {
exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage("Rerank result should have both index and relevanceScore.");
List<Map<String, Object>> rerankResults = List
.of(
Map.of("index", 2, "relevanceScore", 0.7711548805236816),
Map.of("index", 0, "relevanceScore", 0.0025114635936915874),
Map.of("index", 1, "relevanceScore", 2.4876489987946115e-05),
Map.of("test1", "value1")
);
function.apply(rerankResults);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated on 4af169a
Signed-off-by: tkykenmt <[email protected]>
Signed-off-by: tkykenmt <[email protected]>
Looks like there is a build failure on a test I thought I fixed but I can see there is a different underlying problem within the IT involving encryption
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the code changes It covers all the possible scenarios I can think of.
exceptionRule.expectMessage("Rerank result is empty."); | ||
function.apply(Arrays.asList(Map.of())); | ||
} | ||
|
||
@Test | ||
public void process_WrongInput_NotCorrectMap() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
process_WrongInput_NotCorrectListOfMapsFormat(){
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated on the commit 8a4fdb2
Are you planning to update the corresponding blueprint in a separate PR? |
…ect#3339 Signed-off-by: tkykenmt <[email protected]>
@dhrubo-os https://github.com/opensearch-project/ml-commons/tree/main/docs/remote_inference_blueprints |
@dhrubo-os |
|
||
if (!rerankResults.isEmpty()) { | ||
Double[] scores = new Double[rerankResults.size()]; | ||
for (Map<?, ?> rerankResult : rerankResults) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need to cast the elements as Map? We defined this as parameter: List<Map<String, Object>> rerankResults
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your review. I agree with you. For the logic, we don't need to cast as Map but just specify data type for enhanced loop as follows.
for (Map rerankResult : rerankResults) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed on ededf78
Signed-off-by: tkykenmt <[email protected]>
Description
Amazon Bedrock introduced Rerank model support. OpenSearch can invoke Rerank models on Bedrock by writing custom pre and post processing function, but pre-built function is good for performance.
Related Issues
Resolves #3254
Check List
--signoff
.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.