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

Add pre and post process functions for Bedrock Rerank API #3254 #3339

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -17,18 +17,34 @@ public class BedrockRerankPostProcessFunction extends ConnectorPostProcessFuncti

@Override
public void validate(Object input) {

if (!(input instanceof List)) {
throw new IllegalArgumentException("Post process function input is not a List.");
}

List<?> outerList = (List<?>) input;
if (!outerList.isEmpty()) {
if (!(outerList.get(0) instanceof Map)) {
throw new IllegalArgumentException("Post process function input is not a List of Map.");

if (outerList.isEmpty()) {
throw new IllegalArgumentException("Post process function input is empty.");
}

for (Object item : outerList) {
if (!(item instanceof Map)) {
throw new IllegalArgumentException("Rerank result is not a Map.");
}
Map innerMap = (Map) outerList.get(0);

if (innerMap.isEmpty() || !innerMap.containsKey("index") || !innerMap.containsKey("relevanceScore")) {
throw new IllegalArgumentException("The rerank result should contain index and relevanceScore.");
Map<?, ?> innerMap = (Map<?, ?>) item;

if (innerMap.isEmpty()) {
throw new IllegalArgumentException("Rerank result is empty.");
}

if (!innerMap.containsKey("index") || !innerMap.containsKey("relevanceScore")) {
throw new IllegalArgumentException("Rerank result should have both index and relevanceScore.");
}

if (!(innerMap.get("relevanceScore") instanceof BigDecimal || innerMap.get("relevanceScore") instanceof Double)) {
throw new IllegalArgumentException("relevanceScore is not BigDecimal or Double.");
}
}
}
Expand All @@ -37,28 +53,25 @@ public void validate(Object input) {
public List<ModelTensor> process(List<Map<String, Object>> rerankResults) {
List<ModelTensor> modelTensors = new ArrayList<>();

if (rerankResults.size() > 0) {
if (!rerankResults.isEmpty()) {
Double[] scores = new Double[rerankResults.size()];
for (int i = 0; i < rerankResults.size(); i++) {
Integer index = (Integer) rerankResults.get(i).get("index");
Object relevanceScore = rerankResults.get(i).get("relevanceScore");
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());
};
for (Map<?, ?> rerankResult : rerankResults) {
Copy link
Collaborator

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.

Copy link
Contributor Author

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) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed on ededf78

Integer index = (Integer) rerankResult.get("index");
Object relevanceScore = rerankResult.get("relevanceScore");
if (relevanceScore instanceof BigDecimal) {
scores[index] = ((BigDecimal) relevanceScore).doubleValue();
} else if (relevanceScore instanceof Double) {
scores[index] = (Double) relevanceScore;
}
}

for (int i = 0; i < scores.length; i++) {
for (Double score : scores) {
modelTensors
.add(
ModelTensor
.builder()
.name("similarity")
.shape(new long[] { 1 })
.data(new Number[] { scores[i] })
.data(new Number[] { score })
.dataType(MLResultDataType.FLOAT32)
.build()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public BedrockRerankPreProcessFunction() {

@Override
public void validate(MLInput mlInput) {

if (mlInput.getInputDataset() == null) {
throw new IllegalArgumentException("Input dataset cannot be null.");
}

if (!(mlInput.getInputDataset() instanceof TextSimilarityInputDataSet)) {
Copy link
Contributor

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()?

Copy link
Contributor Author

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.");
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated on 4af169a

throw new IllegalArgumentException("This pre_process_function can only support TextSimilarityInputDataSet");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import static org.junit.Assert.assertEquals;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -36,18 +35,53 @@ public void process_WrongInput_NotList() {
function.apply("abc");
}

@Test
public void process_EmptyInput() {
exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage("Post process function input is empty.");
function.apply(Arrays.asList());
}

@Test
public void process_WrongInput_NotCorrectList() {
exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage("Post process function input is not a List of Map.");
exceptionRule.expectMessage("Rerank result is not a Map.");
function.apply(Arrays.asList("abc"));
}

@Test
public void process_EmptyMapInput() {
exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage("Rerank result is empty.");
function.apply(Arrays.asList(Map.of()));
}

@Test
public void process_WrongInput_NotCorrectMap() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process_WrongInput_NotCorrectListOfMapsFormat(){
}

Copy link
Contributor Author

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

exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage("The rerank result should contain index and relevance_score.");
function.apply(Arrays.asList(Map.of("test1", "value1")));
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);
}

@Test
public void process_WrongInput_NotCorrectRelevanceScore() {
exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage("relevanceScore is not BigDecimal or Double.");
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("index", 3, "relevanceScore", "value1")
);
function.apply(rerankResults);
}

Copy link
Contributor

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?

Copy link
Contributor Author

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.

@Test
Expand Down
Loading