forked from opensearch-project/ml-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add preprocess function for batch inferernce jobArn
Signed-off-by: Yaliang Wu <[email protected]>
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
.../common/connector/functions/preprocess/BedrockGetBatchInferenceJobPreProcessFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* | ||
* * Copyright OpenSearch Contributors | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.opensearch.ml.common.connector.functions.preprocess; | ||
|
||
import org.opensearch.ml.common.dataset.TextDocsInputDataSet; | ||
import org.opensearch.ml.common.dataset.remote.RemoteInferenceInputDataSet; | ||
import org.opensearch.ml.common.input.MLInput; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.opensearch.ml.common.utils.StringUtils.convertScriptStringToJsonString; | ||
|
||
/** | ||
* This class provides a pre-processing function for bedrock batch inference job input. | ||
* It takes an instance of {@link MLInput} as input and returns an instance of {@link RemoteInferenceInputDataSet}. | ||
* The input data is expected to be of type {@link RemoteInferenceInputDataSet}, which must have jobArn parameter. | ||
* The function validates the input data and then processes it to create a {@link RemoteInferenceInputDataSet} object. | ||
*/ | ||
public class BedrockGetBatchInferenceJobPreProcessFunction extends ConnectorPreProcessFunction { | ||
|
||
public static final String JOB_ARN = "jobArn"; | ||
|
||
public BedrockGetBatchInferenceJobPreProcessFunction() { | ||
this.returnDirectlyForRemoteInferenceInput = false; | ||
} | ||
|
||
@Override | ||
public void validate(MLInput mlInput) { | ||
if (!(mlInput.getInputDataset() instanceof RemoteInferenceInputDataSet)) { | ||
throw new IllegalArgumentException("Wrong input dataset type"); | ||
} | ||
RemoteInferenceInputDataSet inputData = (RemoteInferenceInputDataSet) mlInput.getInputDataset(); | ||
if (inputData == null) { | ||
throw new IllegalArgumentException("No input dataset provided"); | ||
} | ||
String jobArn = inputData.getParameters().get(JOB_ARN); | ||
if (jobArn == null) { | ||
throw new IllegalArgumentException("No jobArn provided"); | ||
} | ||
} | ||
|
||
/** | ||
* @param mlInput The input data to be processed. | ||
* This method is to escape slash in jobArn. | ||
*/ | ||
@Override | ||
public RemoteInferenceInputDataSet process(MLInput mlInput) { | ||
RemoteInferenceInputDataSet inputData = (RemoteInferenceInputDataSet) mlInput.getInputDataset(); | ||
inputData.getParameters().computeIfPresent(JOB_ARN, (k, jobArn) -> jobArn.replace("/", "%2F")); | ||
return inputData; | ||
} | ||
} |