From 3d54eb57575be6ed9331f449aa0db16a85e9ae49 Mon Sep 17 00:00:00 2001 From: b4sjoo Date: Wed, 18 Dec 2024 09:32:26 +0000 Subject: [PATCH 1/5] Fix remote model with embedding input issue Signed-off-by: b4sjoo --- .../ml/common/utils/ModelInterfaceUtils.java | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/org/opensearch/ml/common/utils/ModelInterfaceUtils.java b/common/src/main/java/org/opensearch/ml/common/utils/ModelInterfaceUtils.java index 5c5cc5fd99..5f05ee73ff 100644 --- a/common/src/main/java/org/opensearch/ml/common/utils/ModelInterfaceUtils.java +++ b/common/src/main/java/org/opensearch/ml/common/utils/ModelInterfaceUtils.java @@ -52,10 +52,7 @@ public class ModelInterfaceUtils { + " \"texts\"\n" + " ]\n" + " }\n" - + " },\n" - + " \"required\": [\n" - + " \"parameters\"\n" - + " ]\n" + + " }\n" + "}"; private static final String TITAN_TEXT_EMBEDDING_MODEL_INTERFACE_INPUT = "{\n" @@ -72,10 +69,7 @@ public class ModelInterfaceUtils { + " \"inputText\"\n" + " ]\n" + " }\n" - + " },\n" - + " \"required\": [\n" - + " \"parameters\"\n" - + " ]\n" + + " }\n" + "}"; private static final String TITAN_MULTI_MODAL_EMBEDDING_MODEL_INTERFACE_INPUT = "{\n" @@ -92,10 +86,7 @@ public class ModelInterfaceUtils { + " }\n" + " }\n" + " }\n" - + " },\n" - + " \"required\": [\n" - + " \"parameters\"\n" - + " ]\n" + + " }\n" + "}"; private static final String AMAZON_COMPREHEND_DETECTDOMAINANTLANGUAGE_API_INTERFACE_INPUT = "{\n" From b639ffc702006a2826130a7db2cc113a6fb0e6b6 Mon Sep 17 00:00:00 2001 From: b4sjoo Date: Thu, 19 Dec 2024 10:44:40 +0000 Subject: [PATCH 2/5] Add UT Signed-off-by: b4sjoo --- .../opensearch/ml/utils/MLNodeUtilsTests.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java b/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java index 5b12e73d3c..e7c2784590 100644 --- a/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java +++ b/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java @@ -66,6 +66,31 @@ public void testValidateSchema() throws IOException { MLNodeUtils.validateSchema(schema, json); } + @Test + public void testValidateEmbeddingInputWithRemoteSchema() throws IOException { + String schema = "{\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"parameters\": {\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"texts\": {\n" + + " \"type\": \"array\",\n" + + " \"items\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"required\": [\n" + + " \"texts\"\n" + + " ]\n" + + " }\n" + + " }\n" + + "}"; + String json = "{\"text_docs\":[ \"today is sunny\", \"today is sunny\"]}"; + MLNodeUtils.validateSchema(schema, json); + } + @Test public void testProcessRemoteInferenceInputDataSetParametersValueNoParameters() throws IOException { String json = "{\"key1\":\"foo\",\"key2\":123,\"key3\":true}"; From 1f91102987f883dc745f7aa9b03b8efca72bf2b8 Mon Sep 17 00:00:00 2001 From: b4sjoo Date: Thu, 19 Dec 2024 10:45:29 +0000 Subject: [PATCH 3/5] Spotless Signed-off-by: b4sjoo --- .../opensearch/ml/utils/MLNodeUtilsTests.java | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java b/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java index e7c2784590..70cf91696a 100644 --- a/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java +++ b/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java @@ -69,24 +69,24 @@ public void testValidateSchema() throws IOException { @Test public void testValidateEmbeddingInputWithRemoteSchema() throws IOException { String schema = "{\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"parameters\": {\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"texts\": {\n" - + " \"type\": \"array\",\n" - + " \"items\": {\n" - + " \"type\": \"string\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"required\": [\n" - + " \"texts\"\n" - + " ]\n" - + " }\n" - + " }\n" - + "}"; + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"parameters\": {\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"texts\": {\n" + + " \"type\": \"array\",\n" + + " \"items\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"required\": [\n" + + " \"texts\"\n" + + " ]\n" + + " }\n" + + " }\n" + + "}"; String json = "{\"text_docs\":[ \"today is sunny\", \"today is sunny\"]}"; MLNodeUtils.validateSchema(schema, json); } From 9ff2d5dc179af0fa1faca1bcfefb7cc0ab3148bb Mon Sep 17 00:00:00 2001 From: b4sjoo Date: Fri, 20 Dec 2024 13:11:19 +0000 Subject: [PATCH 4/5] Add UT for both embedding and remote cases for all remote embedding schema Signed-off-by: b4sjoo --- .../opensearch/ml/utils/MLNodeUtilsTests.java | 120 +++++++++++++++++- 1 file changed, 119 insertions(+), 1 deletion(-) diff --git a/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java b/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java index 70cf91696a..c0f6d35ee7 100644 --- a/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java +++ b/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java @@ -67,7 +67,7 @@ public void testValidateSchema() throws IOException { } @Test - public void testValidateEmbeddingInputWithRemoteSchema() throws IOException { + public void testValidateEmbeddingInputWithGeneralEmbeddingRemoteSchema() throws IOException { String schema = "{\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" @@ -91,6 +91,124 @@ public void testValidateEmbeddingInputWithRemoteSchema() throws IOException { MLNodeUtils.validateSchema(schema, json); } + @Test + public void testValidateRemoteInputWithGeneralEmbeddingRemoteSchema() throws IOException { + String schema = "{\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"parameters\": {\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"texts\": {\n" + + " \"type\": \"array\",\n" + + " \"items\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"required\": [\n" + + " \"texts\"\n" + + " ]\n" + + " }\n" + + " }\n" + + "}"; + String json = "{\"parameters\": {\"texts\": [\"Hello\",\"world\"]}}"; + MLNodeUtils.validateSchema(schema, json); + } + + @Test + public void testValidateEmbeddingInputWithTitanTextRemoteSchema() throws IOException { + String schema = "{\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"parameters\": {\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"inputText\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " },\n" + + " \"required\": [\n" + + " \"inputText\"\n" + + " ]\n" + + " }\n" + + " }\n" + + "}"; + String json = "{\"text_docs\":[ \"today is sunny\", \"today is sunny\"]}"; + MLNodeUtils.validateSchema(schema, json); + } + + @Test + public void testValidateRemoteInputWithTitanTextRemoteSchema() throws IOException { + String schema = "{\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"parameters\": {\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"inputText\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " },\n" + + " \"required\": [\n" + + " \"inputText\"\n" + + " ]\n" + + " }\n" + + " }\n" + + "}"; + String json = "{\"parameters\": {\"inputText\": \"Say this is a test\"}}"; + MLNodeUtils.validateSchema(schema, json); + } + + @Test + public void testValidateEmbeddingInputWithTitanImageRemoteSchema() throws IOException { + String schema = "{\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"parameters\": {\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"inputText\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"inputImage\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + String json = "{\"text_docs\":[ \"today is sunny\", \"today is sunny\"]}"; + MLNodeUtils.validateSchema(schema, json); + } + + @Test + public void testValidateRemoteInputWithTitanImageRemoteSchema() throws IOException { + String schema = "{\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"parameters\": {\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"inputText\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"inputImage\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + String json = "{{\n" + + " \"parameters\": {\n" + + " \"inputText\": \"Say this is a test\",\n" + + " \"inputImage\": \"/9jk=\"\n" + + " }\n" + + "}"; + MLNodeUtils.validateSchema(schema, json); + } + @Test public void testProcessRemoteInferenceInputDataSetParametersValueNoParameters() throws IOException { String json = "{\"key1\":\"foo\",\"key2\":123,\"key3\":true}"; From 9252146d26b488ece78a5514f0d56fb884a988ba Mon Sep 17 00:00:00 2001 From: b4sjoo Date: Fri, 20 Dec 2024 19:29:24 +0000 Subject: [PATCH 5/5] Remove hardcoded test schema Signed-off-by: b4sjoo --- .../opensearch/ml/utils/MLNodeUtilsTests.java | 117 ++---------------- 1 file changed, 12 insertions(+), 105 deletions(-) diff --git a/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java b/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java index c0f6d35ee7..40bb230bbf 100644 --- a/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java +++ b/plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java @@ -6,6 +6,9 @@ package org.opensearch.ml.utils; import static java.util.Collections.emptyMap; +import static org.opensearch.ml.common.utils.ModelInterfaceUtils.BEDROCK_COHERE_EMBED_ENGLISH_V3_MODEL_INTERFACE; +import static org.opensearch.ml.common.utils.ModelInterfaceUtils.BEDROCK_TITAN_EMBED_MULTI_MODAL_V1_MODEL_INTERFACE; +import static org.opensearch.ml.common.utils.ModelInterfaceUtils.BEDROCK_TITAN_EMBED_TEXT_V1_MODEL_INTERFACE; import static org.opensearch.ml.utils.TestHelper.ML_ROLE; import java.io.IOException; @@ -68,139 +71,43 @@ public void testValidateSchema() throws IOException { @Test public void testValidateEmbeddingInputWithGeneralEmbeddingRemoteSchema() throws IOException { - String schema = "{\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"parameters\": {\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"texts\": {\n" - + " \"type\": \"array\",\n" - + " \"items\": {\n" - + " \"type\": \"string\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"required\": [\n" - + " \"texts\"\n" - + " ]\n" - + " }\n" - + " }\n" - + "}"; + String schema = BEDROCK_COHERE_EMBED_ENGLISH_V3_MODEL_INTERFACE.get("input"); String json = "{\"text_docs\":[ \"today is sunny\", \"today is sunny\"]}"; MLNodeUtils.validateSchema(schema, json); } @Test public void testValidateRemoteInputWithGeneralEmbeddingRemoteSchema() throws IOException { - String schema = "{\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"parameters\": {\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"texts\": {\n" - + " \"type\": \"array\",\n" - + " \"items\": {\n" - + " \"type\": \"string\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"required\": [\n" - + " \"texts\"\n" - + " ]\n" - + " }\n" - + " }\n" - + "}"; + String schema = BEDROCK_COHERE_EMBED_ENGLISH_V3_MODEL_INTERFACE.get("input"); String json = "{\"parameters\": {\"texts\": [\"Hello\",\"world\"]}}"; MLNodeUtils.validateSchema(schema, json); } @Test public void testValidateEmbeddingInputWithTitanTextRemoteSchema() throws IOException { - String schema = "{\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"parameters\": {\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"inputText\": {\n" - + " \"type\": \"string\"\n" - + " }\n" - + " },\n" - + " \"required\": [\n" - + " \"inputText\"\n" - + " ]\n" - + " }\n" - + " }\n" - + "}"; + String schema = BEDROCK_TITAN_EMBED_TEXT_V1_MODEL_INTERFACE.get("input"); String json = "{\"text_docs\":[ \"today is sunny\", \"today is sunny\"]}"; MLNodeUtils.validateSchema(schema, json); } @Test public void testValidateRemoteInputWithTitanTextRemoteSchema() throws IOException { - String schema = "{\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"parameters\": {\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"inputText\": {\n" - + " \"type\": \"string\"\n" - + " }\n" - + " },\n" - + " \"required\": [\n" - + " \"inputText\"\n" - + " ]\n" - + " }\n" - + " }\n" - + "}"; + String schema = BEDROCK_TITAN_EMBED_TEXT_V1_MODEL_INTERFACE.get("input"); String json = "{\"parameters\": {\"inputText\": \"Say this is a test\"}}"; MLNodeUtils.validateSchema(schema, json); } @Test - public void testValidateEmbeddingInputWithTitanImageRemoteSchema() throws IOException { - String schema = "{\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"parameters\": {\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"inputText\": {\n" - + " \"type\": \"string\"\n" - + " },\n" - + " \"inputImage\": {\n" - + " \"type\": \"string\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + public void testValidateEmbeddingInputWithTitanMultiModalRemoteSchema() throws IOException { + String schema = BEDROCK_TITAN_EMBED_MULTI_MODAL_V1_MODEL_INTERFACE.get("input"); String json = "{\"text_docs\":[ \"today is sunny\", \"today is sunny\"]}"; MLNodeUtils.validateSchema(schema, json); } @Test - public void testValidateRemoteInputWithTitanImageRemoteSchema() throws IOException { - String schema = "{\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"parameters\": {\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"inputText\": {\n" - + " \"type\": \"string\"\n" - + " },\n" - + " \"inputImage\": {\n" - + " \"type\": \"string\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; - String json = "{{\n" + public void testValidateRemoteInputWithTitanMultiModalRemoteSchema() throws IOException { + String schema = BEDROCK_TITAN_EMBED_MULTI_MODAL_V1_MODEL_INTERFACE.get("input"); + String json = "{\n" + " \"parameters\": {\n" + " \"inputText\": \"Say this is a test\",\n" + " \"inputImage\": \"/9jk=\"\n"