-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add bedrock batch job post process function; enhance remote job statu…
…s parsing (#2955) Signed-off-by: Yaliang Wu <[email protected]>
- Loading branch information
Showing
12 changed files
with
344 additions
and
30 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
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
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
40 changes: 40 additions & 0 deletions
40
...arch/ml/common/connector/functions/postprocess/BedrockBatchJobArnPostProcessFunction.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,40 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.connector.functions.postprocess; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.opensearch.ml.common.output.model.ModelTensor; | ||
|
||
public class BedrockBatchJobArnPostProcessFunction extends ConnectorPostProcessFunction<Map<String, String>> { | ||
public static final String JOB_ARN = "jobArn"; | ||
public static final String PROCESSED_JOB_ARN = "processedJobArn"; | ||
|
||
@Override | ||
public void validate(Object input) { | ||
if (!(input instanceof Map)) { | ||
throw new IllegalArgumentException("Post process function input is not a Map."); | ||
} | ||
Map<String, String> jobInfo = (Map<String, String>) input; | ||
if (!(jobInfo.containsKey(JOB_ARN))) { | ||
throw new IllegalArgumentException("job arn is missing."); | ||
} | ||
} | ||
|
||
@Override | ||
public List<ModelTensor> process(Map<String, String> jobInfo) { | ||
List<ModelTensor> modelTensors = new ArrayList<>(); | ||
Map<String, String> processedResult = new HashMap<>(); | ||
processedResult.putAll(jobInfo); | ||
String jobArn = jobInfo.get(JOB_ARN); | ||
processedResult.put(PROCESSED_JOB_ARN, jobArn.replace("/", "%2F")); | ||
modelTensors.add(ModelTensor.builder().name("response").dataAsMap(processedResult).build()); | ||
return modelTensors; | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
.../ml/common/connector/functions/postprocess/BedrockBatchJobArnPostProcessFunctionTest.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,58 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.connector.functions.postprocess; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.opensearch.ml.common.connector.functions.postprocess.BedrockBatchJobArnPostProcessFunction.JOB_ARN; | ||
import static org.opensearch.ml.common.connector.functions.postprocess.BedrockBatchJobArnPostProcessFunction.PROCESSED_JOB_ARN; | ||
|
||
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 BedrockBatchJobArnPostProcessFunctionTest { | ||
|
||
@Rule | ||
public ExpectedException exceptionRule = ExpectedException.none(); | ||
|
||
BedrockBatchJobArnPostProcessFunction function; | ||
|
||
@Before | ||
public void setUp() { | ||
function = new BedrockBatchJobArnPostProcessFunction(); | ||
} | ||
|
||
@Test | ||
public void process_WrongInput_NotMap() { | ||
exceptionRule.expect(IllegalArgumentException.class); | ||
exceptionRule.expectMessage("Post process function input is not a Map."); | ||
function.apply("abc"); | ||
} | ||
|
||
@Test | ||
public void process_WrongInput_NotContainJobArn() { | ||
exceptionRule.expect(IllegalArgumentException.class); | ||
exceptionRule.expectMessage("job arn is missing."); | ||
function.apply(Map.of("test", "value")); | ||
} | ||
|
||
@Test | ||
public void process_CorrectInput() { | ||
String jobArn = "arn:aws:bedrock:us-east-1:12345678912:model-invocation-job/w1xtlm0ik3e1"; | ||
List<ModelTensor> result = function.apply(Map.of(JOB_ARN, jobArn)); | ||
assertEquals(1, result.size()); | ||
assertEquals(jobArn, result.get(0).getDataAsMap().get(JOB_ARN)); | ||
assertEquals( | ||
"arn:aws:bedrock:us-east-1:12345678912:model-invocation-job%2Fw1xtlm0ik3e1", | ||
result.get(0).getDataAsMap().get(PROCESSED_JOB_ARN) | ||
); | ||
} | ||
} |
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
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
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
Oops, something went wrong.