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

Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -12,6 +12,7 @@

import org.opensearch.ml.common.connector.functions.postprocess.BedrockBatchJobArnPostProcessFunction;
import org.opensearch.ml.common.connector.functions.postprocess.BedrockEmbeddingPostProcessFunction;
import org.opensearch.ml.common.connector.functions.postprocess.BedrockRerankPostProcessFunction;
import org.opensearch.ml.common.connector.functions.postprocess.CohereRerankPostProcessFunction;
import org.opensearch.ml.common.connector.functions.postprocess.EmbeddingPostProcessFunction;
import org.opensearch.ml.common.output.model.ModelTensor;
Expand All @@ -23,6 +24,7 @@ public class MLPostProcessFunction {
public static final String BEDROCK_EMBEDDING = "connector.post_process.bedrock.embedding";
public static final String BEDROCK_BATCH_JOB_ARN = "connector.post_process.bedrock.batch_job_arn";
public static final String COHERE_RERANK = "connector.post_process.cohere.rerank";
public static final String BEDROCK_RERANK = "connector.post_process.bedrock.rerank";
public static final String DEFAULT_EMBEDDING = "connector.post_process.default.embedding";
public static final String DEFAULT_RERANK = "connector.post_process.default.rerank";

Expand All @@ -35,19 +37,22 @@ public class MLPostProcessFunction {
BedrockEmbeddingPostProcessFunction bedrockEmbeddingPostProcessFunction = new BedrockEmbeddingPostProcessFunction();
BedrockBatchJobArnPostProcessFunction batchJobArnPostProcessFunction = new BedrockBatchJobArnPostProcessFunction();
CohereRerankPostProcessFunction cohereRerankPostProcessFunction = new CohereRerankPostProcessFunction();
BedrockRerankPostProcessFunction bedrockRerankPostProcessFunction = new BedrockRerankPostProcessFunction();
JSON_PATH_EXPRESSION.put(OPENAI_EMBEDDING, "$.data[*].embedding");
JSON_PATH_EXPRESSION.put(COHERE_EMBEDDING, "$.embeddings");
JSON_PATH_EXPRESSION.put(DEFAULT_EMBEDDING, "$[*]");
JSON_PATH_EXPRESSION.put(BEDROCK_EMBEDDING, "$.embedding");
JSON_PATH_EXPRESSION.put(BEDROCK_BATCH_JOB_ARN, "$");
JSON_PATH_EXPRESSION.put(COHERE_RERANK, "$.results");
JSON_PATH_EXPRESSION.put(BEDROCK_RERANK, "$.results");
JSON_PATH_EXPRESSION.put(DEFAULT_RERANK, "$[*]");
POST_PROCESS_FUNCTIONS.put(OPENAI_EMBEDDING, embeddingPostProcessFunction);
POST_PROCESS_FUNCTIONS.put(COHERE_EMBEDDING, embeddingPostProcessFunction);
POST_PROCESS_FUNCTIONS.put(DEFAULT_EMBEDDING, embeddingPostProcessFunction);
POST_PROCESS_FUNCTIONS.put(BEDROCK_EMBEDDING, bedrockEmbeddingPostProcessFunction);
POST_PROCESS_FUNCTIONS.put(BEDROCK_BATCH_JOB_ARN, batchJobArnPostProcessFunction);
POST_PROCESS_FUNCTIONS.put(COHERE_RERANK, cohereRerankPostProcessFunction);
POST_PROCESS_FUNCTIONS.put(BEDROCK_RERANK, bedrockRerankPostProcessFunction);
POST_PROCESS_FUNCTIONS.put(DEFAULT_RERANK, cohereRerankPostProcessFunction);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.function.Function;

import org.opensearch.ml.common.connector.functions.preprocess.BedrockEmbeddingPreProcessFunction;
import org.opensearch.ml.common.connector.functions.preprocess.BedrockRerankPreProcessFunction;
import org.opensearch.ml.common.connector.functions.preprocess.CohereEmbeddingPreProcessFunction;
import org.opensearch.ml.common.connector.functions.preprocess.CohereMultiModalEmbeddingPreProcessFunction;
import org.opensearch.ml.common.connector.functions.preprocess.CohereRerankPreProcessFunction;
Expand All @@ -28,6 +29,7 @@ public class MLPreProcessFunction {
public static final String TEXT_IMAGE_TO_BEDROCK_EMBEDDING_INPUT = "connector.pre_process.bedrock.multimodal_embedding";
public static final String TEXT_DOCS_TO_DEFAULT_EMBEDDING_INPUT = "connector.pre_process.default.embedding";
public static final String TEXT_SIMILARITY_TO_COHERE_RERANK_INPUT = "connector.pre_process.cohere.rerank";
public static final String TEXT_SIMILARITY_TO_BEDROCK_RERANK_INPUT = "connector.pre_process.bedrock.rerank";
public static final String TEXT_SIMILARITY_TO_DEFAULT_INPUT = "connector.pre_process.default.rerank";

public static final String PROCESS_REMOTE_INFERENCE_INPUT = "pre_process_function.process_remote_inference_input";
Expand All @@ -38,6 +40,7 @@ public class MLPreProcessFunction {
OpenAIEmbeddingPreProcessFunction openAIEmbeddingPreProcessFunction = new OpenAIEmbeddingPreProcessFunction();
BedrockEmbeddingPreProcessFunction bedrockEmbeddingPreProcessFunction = new BedrockEmbeddingPreProcessFunction();
CohereRerankPreProcessFunction cohereRerankPreProcessFunction = new CohereRerankPreProcessFunction();
BedrockRerankPreProcessFunction bedrockRerankPreProcessFunction = new BedrockRerankPreProcessFunction();
MultiModalConnectorPreProcessFunction multiModalEmbeddingPreProcessFunction = new MultiModalConnectorPreProcessFunction();
CohereMultiModalEmbeddingPreProcessFunction cohereMultiModalEmbeddingPreProcessFunction =
new CohereMultiModalEmbeddingPreProcessFunction();
Expand All @@ -49,6 +52,7 @@ public class MLPreProcessFunction {
PRE_PROCESS_FUNCTIONS.put(TEXT_DOCS_TO_BEDROCK_EMBEDDING_INPUT, bedrockEmbeddingPreProcessFunction);
PRE_PROCESS_FUNCTIONS.put(TEXT_SIMILARITY_TO_DEFAULT_INPUT, cohereRerankPreProcessFunction);
PRE_PROCESS_FUNCTIONS.put(TEXT_SIMILARITY_TO_COHERE_RERANK_INPUT, cohereRerankPreProcessFunction);
PRE_PROCESS_FUNCTIONS.put(TEXT_SIMILARITY_TO_BEDROCK_RERANK_INPUT, bedrockRerankPreProcessFunction);
}

public static boolean contains(String functionName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.ml.common.connector.functions.postprocess;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.opensearch.ml.common.output.model.MLResultDataType;
import org.opensearch.ml.common.output.model.ModelTensor;

public class BedrockRerankPostProcessFunction extends ConnectorPostProcessFunction<List<Map<String, Object>>> {

@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()) {
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<?, ?>) 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.");
}
}
}

@Override
public List<ModelTensor> process(List<Map<String, Object>> rerankResults) {
List<ModelTensor> modelTensors = new ArrayList<>();

if (!rerankResults.isEmpty()) {
Double[] scores = new Double[rerankResults.size()];
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 (Double score : scores) {
modelTensors
.add(
ModelTensor
.builder()
.name("similarity")
.shape(new long[] { 1 })
.data(new Number[] { score })
.dataType(MLResultDataType.FLOAT32)
.build()
);
}
}
return modelTensors;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.ml.common.connector.functions.preprocess;

import static org.opensearch.ml.common.utils.StringUtils.convertScriptStringToJsonString;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.opensearch.ml.common.dataset.TextSimilarityInputDataSet;
import org.opensearch.ml.common.dataset.remote.RemoteInferenceInputDataSet;
import org.opensearch.ml.common.input.MLInput;

public class BedrockRerankPreProcessFunction extends ConnectorPreProcessFunction {

public BedrockRerankPreProcessFunction() {
this.returnDirectlyForRemoteInferenceInput = true;
}

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

@Override
public RemoteInferenceInputDataSet process(MLInput mlInput) {
TextSimilarityInputDataSet inputData = (TextSimilarityInputDataSet) mlInput.getInputDataset();
String queryText = inputData.getQueryText();
List<String> textDocs = inputData.getTextDocs();

List<Map<String, Object>> queries = new ArrayList<Map<String, Object>>();
queries.add(Map.of("textQuery", Map.of("text", queryText), "type", "TEXT"));

List<Map<String, Object>> sources = new ArrayList<Map<String, Object>>();
inputData.getTextDocs().forEach(textDoc -> {
sources.add(Map.of("inlineDocumentSource", Map.of("textDocument", Map.of("text", textDoc), "type", "TEXT"), "type", "INLINE"));
});

Map<String, Object> processedResult = Map
.of("parameters", Map.of("queries", queries, "sources", sources, "numberOfResults", textDocs.size()));

return RemoteInferenceInputDataSet.builder().parameters(convertScriptStringToJsonString(processedResult)).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.ml.common.connector.functions.postprocess;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.opensearch.ml.common.output.model.ModelTensor;

public class BedrockRerankPostProcessFunctionTest {
@Rule
public ExpectedException exceptionRule = ExpectedException.none();

BedrockRerankPostProcessFunction function;

@Before
public void setUp() {
function = new BedrockRerankPostProcessFunction();
}

@Test
public void process_WrongInput_NotList() {
exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage("Post process function input is not a List.");
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("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("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
public void process_CorrectInput() {
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", 6.339210358419223e-06)
Copy link
Contributor

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

Copy link
Contributor Author

@tkykenmt tkykenmt Jan 8, 2025

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

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

);
List<ModelTensor> result = function.apply(rerankResults);
assertEquals(4, result.size());
assertEquals(1, result.get(0).getData().length);
assertEquals(0.0025114635936915874, result.get(0).getData()[0]);
assertEquals(2.4876489987946115e-05, result.get(1).getData()[0]);
assertEquals(0.7711548805236816, result.get(2).getData()[0]);
assertEquals(6.339210358419223e-06, result.get(3).getData()[0]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.ml.common.connector.functions.preprocess;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;

import org.json.JSONArray;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.opensearch.ml.common.FunctionName;
import org.opensearch.ml.common.dataset.TextDocsInputDataSet;
import org.opensearch.ml.common.dataset.TextSimilarityInputDataSet;
import org.opensearch.ml.common.dataset.remote.RemoteInferenceInputDataSet;
import org.opensearch.ml.common.input.MLInput;

public class BedrockRerankPreProcessFunctionTest {
@Rule
public ExpectedException exceptionRule = ExpectedException.none();

BedrockRerankPreProcessFunction function;

TextSimilarityInputDataSet textSimilarityInputDataSet;
TextDocsInputDataSet textDocsInputDataSet;

@Before
public void setUp() {
function = new BedrockRerankPreProcessFunction();
textSimilarityInputDataSet = TextSimilarityInputDataSet.builder().queryText("test").textDocs(Arrays.asList("hello")).build();
textDocsInputDataSet = TextDocsInputDataSet.builder().docs(Arrays.asList("hello", "world")).build();
}

@Test
public void process_NullInput() {
exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage("Preprocess function input can't be null");
function.apply(null);
}

@Test
public void process_WrongInput() {
exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage("This pre_process_function can only support TextSimilarityInputDataSet");
MLInput mlInput = MLInput.builder().algorithm(FunctionName.TEXT_EMBEDDING).inputDataset(textDocsInputDataSet).build();
function.apply(mlInput);
}

@Test
public void process_CorrectInput() {
MLInput mlInput = MLInput.builder().algorithm(FunctionName.TEXT_SIMILARITY).inputDataset(textSimilarityInputDataSet).build();
RemoteInferenceInputDataSet dataSet = function.apply(mlInput);
assertEquals(3, dataSet.getParameters().size());

JSONArray expectedSources = new JSONArray(
"[{\"type\": \"INLINE\", \"inlineDocumentSource\": {\"type\": \"TEXT\", \"textDocument\": {\"text\": \"hello\"}}}]"
);
JSONArray actualSources = new JSONArray(dataSet.getParameters().get("sources"));
assertTrue(expectedSources.getJSONObject(0).similar(actualSources.getJSONObject(0)));

JSONArray expectedQueries = new JSONArray("[{\"textQuery\": {\"text\": \"test\"}, \"type\": \"TEXT\"}]");
JSONArray actualQueries = new JSONArray(dataSet.getParameters().get("queries"));
assertTrue(expectedQueries.getJSONObject(0).similar(actualQueries.getJSONObject(0)));

assertEquals("1", dataSet.getParameters().get("numberOfResults"));
}
}
Loading