|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.common.connector.functions.postprocess; |
| 7 | + |
| 8 | +import static org.junit.Assert.assertEquals; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Rule; |
| 16 | +import org.junit.Test; |
| 17 | +import org.junit.rules.ExpectedException; |
| 18 | +import org.opensearch.ml.common.output.model.ModelTensor; |
| 19 | + |
| 20 | +public class RemoteMlCommonsPassthroughPostProcessFunctionTest { |
| 21 | + @Rule |
| 22 | + public ExpectedException exceptionRule = ExpectedException.none(); |
| 23 | + |
| 24 | + RemoteMlCommonsPassthroughPostProcessFunction function; |
| 25 | + |
| 26 | + @Before |
| 27 | + public void setUp() { |
| 28 | + function = new RemoteMlCommonsPassthroughPostProcessFunction(); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void process_WrongInput_NotMapOrList() { |
| 33 | + exceptionRule.expect(IllegalArgumentException.class); |
| 34 | + exceptionRule.expectMessage("Post process function input must be a Map or List"); |
| 35 | + function.apply("abc", null); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Tests processing of ML-Commons response containing sparse vector data with rank features. |
| 40 | + * Validates that sparse vectors with dataAsMap containing token-score pairs are correctly parsed. |
| 41 | + */ |
| 42 | + @Test |
| 43 | + public void process_MLCommonsResponse_RankFeatures() { |
| 44 | + Map<String, Object> rankFeatures = Map |
| 45 | + .of("increasingly", 0.028670792, "achievements", 0.4906937, "nation", 0.15371077, "hello", 0.35982144, "today", 3.0966291); |
| 46 | + Map<String, Object> innerDataAsMap = Map.of("response", Arrays.asList(rankFeatures)); |
| 47 | + Map<String, Object> output = Map.of("name", "output", "dataAsMap", innerDataAsMap); |
| 48 | + Map<String, Object> inferenceResult = Map.of("output", Arrays.asList(output)); |
| 49 | + Map<String, Object> input = Map.of("inference_results", Arrays.asList(inferenceResult)); |
| 50 | + |
| 51 | + List<ModelTensor> result = function.apply(input, null); |
| 52 | + |
| 53 | + assertEquals(1, result.size()); |
| 54 | + ModelTensor tensor = result.get(0); |
| 55 | + assertEquals("output", tensor.getName()); |
| 56 | + assertEquals(innerDataAsMap, tensor.getDataAsMap()); |
| 57 | + |
| 58 | + // Verify the nested sparse data structure |
| 59 | + Map<String, Object> dataAsMap = (Map<String, Object>) tensor.getDataAsMap(); |
| 60 | + List<Map<String, Object>> response = (List<Map<String, Object>>) dataAsMap.get("response"); |
| 61 | + assertEquals(1, response.size()); |
| 62 | + assertEquals(0.35982144, (Double) response.get(0).get("hello"), 0.0001); |
| 63 | + assertEquals(3.0966291, (Double) response.get(0).get("today"), 0.0001); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Tests processing of ML-Commons response containing dense vector data with numerical arrays. |
| 68 | + * Validates that dense vectors with data_type, shape, and data fields are correctly parsed. |
| 69 | + */ |
| 70 | + @Test |
| 71 | + public void process_MLCommonsResponse_DenseVector() { |
| 72 | + Map<String, Object> output = Map |
| 73 | + .of( |
| 74 | + "name", |
| 75 | + "sentence_embedding", |
| 76 | + "data_type", |
| 77 | + "FLOAT32", |
| 78 | + "shape", |
| 79 | + Arrays.asList(3L), |
| 80 | + "data", |
| 81 | + Arrays.asList(0.5400895, -0.19082281, 0.4996347) |
| 82 | + ); |
| 83 | + Map<String, Object> inferenceResult = Map.of("output", Arrays.asList(output)); |
| 84 | + Map<String, Object> input = Map.of("inference_results", Arrays.asList(inferenceResult)); |
| 85 | + |
| 86 | + List<ModelTensor> result = function.apply(input, null); |
| 87 | + |
| 88 | + assertEquals(1, result.size()); |
| 89 | + ModelTensor tensor = result.get(0); |
| 90 | + assertEquals("sentence_embedding", tensor.getName()); |
| 91 | + assertEquals(3, tensor.getShape().length); |
| 92 | + assertEquals(3L, tensor.getShape()[0]); |
| 93 | + assertEquals(3, tensor.getData().length); |
| 94 | + assertEquals(0.5400895, tensor.getData()[0].doubleValue(), 0.0001); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Tests processing of ML-Commons response with multiple output tensors in a single inference result. |
| 99 | + * Ensures all outputs are processed and returned as separate ModelTensor objects. |
| 100 | + */ |
| 101 | + @Test |
| 102 | + public void process_MLCommonsResponse_MultipleOutputs() { |
| 103 | + Map<String, Object> output1 = Map.of("name", "output1", "result", "result1"); |
| 104 | + Map<String, Object> output2 = Map.of("name", "output2", "result", "result2"); |
| 105 | + Map<String, Object> inferenceResult = Map.of("output", Arrays.asList(output1, output2)); |
| 106 | + Map<String, Object> input = Map.of("inference_results", Arrays.asList(inferenceResult)); |
| 107 | + |
| 108 | + List<ModelTensor> result = function.apply(input, null); |
| 109 | + |
| 110 | + assertEquals(2, result.size()); |
| 111 | + assertEquals("output1", result.get(0).getName()); |
| 112 | + assertEquals("result1", result.get(0).getResult()); |
| 113 | + assertEquals("output2", result.get(1).getName()); |
| 114 | + assertEquals("result2", result.get(1).getResult()); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Tests edge case where ML-Commons response has empty inference_results array. |
| 119 | + * Should return empty list without errors. |
| 120 | + */ |
| 121 | + @Test |
| 122 | + public void process_MLCommonsResponse_EmptyInferenceResults() { |
| 123 | + Map<String, Object> input = Map.of("inference_results", Arrays.asList()); |
| 124 | + |
| 125 | + List<ModelTensor> result = function.apply(input, null); |
| 126 | + |
| 127 | + assertEquals(0, result.size()); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Tests edge case where inference result lacks the expected "output" field. |
| 132 | + * Should skip processing and return empty list. |
| 133 | + */ |
| 134 | + @Test |
| 135 | + public void process_MLCommonsResponse_NoOutputField() { |
| 136 | + Map<String, Object> inferenceResult = Map.of("other_field", "value"); |
| 137 | + Map<String, Object> input = Map.of("inference_results", Arrays.asList(inferenceResult)); |
| 138 | + |
| 139 | + List<ModelTensor> result = function.apply(input, null); |
| 140 | + |
| 141 | + assertEquals(0, result.size()); |
| 142 | + } |
| 143 | +} |
0 commit comments