|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.rest; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Locale; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import org.junit.Before; |
| 16 | +import org.opensearch.ml.common.FunctionName; |
| 17 | +import org.opensearch.ml.common.dataset.TextDocsInputDataSet; |
| 18 | +import org.opensearch.ml.common.input.MLInput; |
| 19 | +import org.opensearch.ml.common.utils.StringUtils; |
| 20 | + |
| 21 | +import lombok.SneakyThrows; |
| 22 | + |
| 23 | +public class RestBedRockInferenceIT extends MLCommonsRestTestCase { |
| 24 | + private static final String AWS_ACCESS_KEY_ID = System.getenv("AWS_ACCESS_KEY_ID"); |
| 25 | + private static final String AWS_SECRET_ACCESS_KEY = System.getenv("AWS_SECRET_ACCESS_KEY"); |
| 26 | + private static final String AWS_SESSION_TOKEN = System.getenv("AWS_SESSION_TOKEN"); |
| 27 | + private static final String GITHUB_CI_AWS_REGION = "us-west-2"; |
| 28 | + |
| 29 | + @SneakyThrows |
| 30 | + @Before |
| 31 | + public void setup() throws IOException, InterruptedException { |
| 32 | + RestMLRemoteInferenceIT.disableClusterConnectorAccessControl(); |
| 33 | + Thread.sleep(20000); |
| 34 | + } |
| 35 | + |
| 36 | + public void test_bedrock_embedding_model() throws Exception { |
| 37 | + // Skip test if key is null |
| 38 | + if (AWS_ACCESS_KEY_ID == null || AWS_SECRET_ACCESS_KEY == null || AWS_SESSION_TOKEN == null) { |
| 39 | + return; |
| 40 | + } |
| 41 | + String templates = Files |
| 42 | + .readString( |
| 43 | + Path |
| 44 | + .of( |
| 45 | + RestMLPredictionAction.class |
| 46 | + .getClassLoader() |
| 47 | + .getResource("org/opensearch/ml/rest/templates/BedRockConnectorBodies.json") |
| 48 | + .toURI() |
| 49 | + ) |
| 50 | + ); |
| 51 | + Map<String, Object> templateMap = StringUtils.gson.fromJson(templates, Map.class); |
| 52 | + for (Map.Entry<String, Object> templateEntry : templateMap.entrySet()) { |
| 53 | + String bedrockEmbeddingModelName = "bedrock embedding model " + randomAlphaOfLength(5); |
| 54 | + String testCaseName = templateEntry.getKey(); |
| 55 | + String errorMsg = String.format(Locale.ROOT, "Failing test case name: %s", testCaseName); |
| 56 | + String modelId = registerRemoteModel( |
| 57 | + String |
| 58 | + .format( |
| 59 | + StringUtils.gson.toJson(templateEntry.getValue()), |
| 60 | + GITHUB_CI_AWS_REGION, |
| 61 | + AWS_ACCESS_KEY_ID, |
| 62 | + AWS_SECRET_ACCESS_KEY, |
| 63 | + AWS_SESSION_TOKEN |
| 64 | + ), |
| 65 | + bedrockEmbeddingModelName, |
| 66 | + true |
| 67 | + ); |
| 68 | + |
| 69 | + TextDocsInputDataSet inputDataSet = TextDocsInputDataSet.builder().docs(List.of("hello", "world")).build(); |
| 70 | + MLInput mlInput = MLInput.builder().inputDataset(inputDataSet).algorithm(FunctionName.TEXT_EMBEDDING).build(); |
| 71 | + Map inferenceResult = predictTextEmbeddingModel(modelId, mlInput); |
| 72 | + assertTrue(errorMsg, inferenceResult.containsKey("inference_results")); |
| 73 | + List output = (List) inferenceResult.get("inference_results"); |
| 74 | + assertEquals(errorMsg, 2, output.size()); |
| 75 | + assertTrue(errorMsg, output.get(0) instanceof Map); |
| 76 | + assertTrue(errorMsg, output.get(1) instanceof Map); |
| 77 | + validateOutput(errorMsg, (Map) output.get(0)); |
| 78 | + validateOutput(errorMsg, (Map) output.get(1)); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private void validateOutput(String errorMsg, Map<String, Object> output) { |
| 83 | + assertTrue(errorMsg, output.containsKey("output")); |
| 84 | + assertTrue(errorMsg, output.get("output") instanceof List); |
| 85 | + List outputList = (List) output.get("output"); |
| 86 | + assertEquals(errorMsg, 1, outputList.size()); |
| 87 | + assertTrue(errorMsg, outputList.get(0) instanceof Map); |
| 88 | + assertTrue(errorMsg, ((Map<?, ?>) outputList.get(0)).get("data") instanceof List); |
| 89 | + assertEquals(errorMsg, 1536, ((List) ((Map<?, ?>) outputList.get(0)).get("data")).size()); |
| 90 | + } |
| 91 | +} |
0 commit comments