Skip to content

Commit d81cb7f

Browse files
committed
Fix parser poc
Signed-off-by: Sicheng Song <sicheng.song@outlook.com>
1 parent 98ac6f8 commit d81cb7f

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/remote/ConnectorUtils.java

+13
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
import java.util.*;
1919
import java.util.function.Function;
2020

21+
import org.json.JSONException;
2122
import org.json.JSONObject;
2223
import org.json.JSONArray;
2324
import org.apache.commons.lang3.StringUtils;
2425
import org.apache.commons.text.StringSubstitutor;
2526
import org.opensearch.OpenSearchParseException;
2627
import org.opensearch.OpenSearchStatusException;
28+
import org.opensearch.common.xcontent.XContentFactory;
29+
import org.opensearch.core.xcontent.ToXContent;
2730
import org.opensearch.core.xcontent.XContent;
2831
import org.opensearch.core.xcontent.XContentBuilder;
2932
import org.opensearch.ml.common.connector.Connector;
@@ -265,6 +268,16 @@ public static SdkHttpFullRequest signRequest(
265268
return signer.sign(request, params);
266269
}
267270

271+
public static JSONObject parametersObjectExtractor(MLInput mlInput) throws IOException {
272+
XContentBuilder mlInputXContent = mlInput.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS);
273+
try {
274+
JSONObject mlInputObject = new JSONObject(mlInputXContent.toString());
275+
return (JSONObject) mlInputObject.get("parameters");
276+
} catch (JSONException e) {
277+
throw new OpenSearchParseException("Failed to extract parameters object from MLInput", e.getMessage());
278+
}
279+
}
280+
268281
public static void validateSchema(String schemaString, String instanceString) {
269282
try {
270283
// parse the schema JSON as string

ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/remote/RemoteConnectorExecutor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ private void validateInputSchema(MLInput mlInput) {
168168
String inputSchemaString = getConnector().getModelInterface().get("input");
169169
try {
170170
JSONObject inputSchemaObject = ConnectorUtils.processJsonObject(new JSONObject(inputSchemaString));
171-
System.out.println(mlInput.getInputDataset().toString());
172-
ConnectorUtils.validateSchema(inputSchemaObject.toString(), mlInput.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS).toString());
171+
JSONObject parametersObject = ConnectorUtils.parametersObjectExtractor(mlInput);
172+
ConnectorUtils.validateSchema(inputSchemaObject.toString(), parametersObject.toString());
173173
} catch (IOException | JSONException e) {
174174
throw new IllegalArgumentException("Error validating input schema: " + e.getMessage());
175175
}

0 commit comments

Comments
 (0)