Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ public <T> T createPayload(String action, Map<String, String> parameters) {
String payload = connectorAction.get().getRequestBody();
payload = fillNullParameters(parameters, payload);
parseParameters(parameters);
payload = removeMissingParameterFields(payload, parameters);
StringSubstitutor substitutor = new StringSubstitutor(parameters, "${parameters.", "}");
payload = substitutor.replace(payload);

Expand All @@ -357,6 +358,30 @@ public <T> T createPayload(String action, Map<String, String> parameters) {
return (T) parameters.get("http_body");
}

/**
* Removes fields from the given JSON payload string that correspond to parameters
* not present in the provided parameter map.
*/
public String removeMissingParameterFields(String payload, Map<String, String> params) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add description for this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need remove the unused field. We should keep consistent behavior with previous code. I.e. if user set a parameter in template and don't provide values, we just keep it. Otherwise it may change the behavior at user side

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand. I’ve updated the code to follow this logic – now it keeps the unused fields consistent with the previous behavior.

if (params == null) {
return payload;
}
Pattern pattern = Pattern.compile("\\s*\"[^\"]+\"\\s*:\\s*(\"?\\$?\\{parameters\\.([^}]+)\\}\"?)\\s*,?");
Matcher matcher = pattern.matcher(payload);
StringBuffer sb = new StringBuffer();

while (matcher.find()) {
String paramName = matcher.group(2); // yyy
if (!params.containsKey(paramName) && !"input".equals(paramName)) {
matcher.appendReplacement(sb, "");
} else {
matcher.appendReplacement(sb, Matcher.quoteReplacement(matcher.group(0)));
}
}
matcher.appendTail(sb);
return sb.toString().replaceAll(",\\s*}", "}").replaceAll(",\\s*]", "]");
}

protected String fillNullParameters(Map<String, String> parameters, String payload) {
List<String> bodyParams = findStringParametersWithNullDefaultValue(payload);
String newPayload = payload;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,24 @@ public void createPayload() {
Assert.assertEquals("{\"input\": \"test input value\"}", predictPayload);
}

@Test
public void createPayload_ExtraParams() {

String requestBody =
"{\"input\": \"${parameters.input}\", \"parameters\": {\"sparseEmbeddingFormat\": \"${parameters.sparseEmbeddingFormat}\", \"content_type\": \"${parameters.content_type}\" }}";
String expected =
"{\"input\": \"test value\", \"parameters\": {\"sparseEmbeddingFormat\": \"WORD\", \"content_type\": \"query\" }}";

HttpConnector connector = createHttpConnectorWithRequestBody(requestBody);
Map<String, String> parameters = new HashMap<>();
parameters.put("input", "test value");
parameters.put("sparseEmbeddingFormat", "WORD");
parameters.put("content_type", "query");
String predictPayload = connector.createPayload(PREDICT.name(), parameters);
connector.validatePayload(predictPayload);
Assert.assertEquals(expected, predictPayload);
}

@Test
public void parseResponse_modelTensorJson() throws IOException {
HttpConnector connector = createHttpConnector();
Expand Down Expand Up @@ -407,4 +425,58 @@ public void parse_WithTenantId() throws IOException {
Assert.assertEquals("test_tenant", connector.getTenantId());
}

@Test
public void removeMissingParameterFields() {
HttpConnector connector = createHttpConnector();
Map<String, String> params = new HashMap<>();
params.put("input", "test value");
params.put("sparseEmbeddingFormat", "WORD");
params.put("content_type", "query");

String payload =
"{\"input\": ${parameters.input}, \"parameters\": {\"sparseEmbeddingFormat\": \"${parameters.sparseEmbeddingFormat}\", \"content_type\": \"${parameters.content_type}\" }}";
String expected =
"{\"input\": ${parameters.input}, \"parameters\": {\"sparseEmbeddingFormat\": \"${parameters.sparseEmbeddingFormat}\", \"content_type\": \"${parameters.content_type}\" }}";
String result = connector.removeMissingParameterFields(payload, params);
Assert.assertEquals(expected, result);
}

@Test
public void removeMissingParameterFields_MissingParameters() {
HttpConnector connector = createHttpConnector();
Map<String, String> params = new HashMap<>();
params.put("input", "test value");

String payload =
"{\"input\": ${parameters.input}, \"parameters\": {\"sparseEmbeddingFormat\": \"${parameters.sparseEmbeddingFormat}\", \"content_type\": \"${parameters.content_type}\"}}";
String expected = "{\"input\": ${parameters.input}, \"parameters\": {}}";
String result = connector.removeMissingParameterFields(payload, params);
Assert.assertEquals(expected, result);
}

@Test
public void removeMissingParameterFields_MissingAll() {
HttpConnector connector = createHttpConnector();
Map<String, String> params = new HashMap<>();

String payload =
"{\"input\": ${parameters.input}, \"parameters\": {\"sparseEmbeddingFormat\": \"${parameters.sparseEmbeddingFormat}\", \"content_type\": \"${parameters.content_type}\"}}";
String expected = "{\"input\": ${parameters.input}, \"parameters\": {}}";
String result = connector.removeMissingParameterFields(payload, params);
Assert.assertEquals(expected, result);
}

@Test
public void removeMissingParameterFields_Nest() {
HttpConnector connector = createHttpConnector();
Map<String, String> params = new HashMap<>();
params.put("input", "test value");

String payload =
"{\"input\": \"${parameters.input}\", \"parameters\": {\"nested\": {\"sparseEmbeddingFormat\": \"${parameters.sparseEmbeddingFormat}\"}}}";
String expected = "{\"input\": \"${parameters.input}\", \"parameters\": {\"nested\": {}}}";
String result = connector.removeMissingParameterFields(payload, params);
Assert.assertEquals(expected, result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.opensearch.ml.engine.algorithms.remote.ConnectorUtils.escapeRemoteInferenceInputData;
import static org.opensearch.ml.engine.algorithms.remote.ConnectorUtils.processInput;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -28,11 +29,14 @@
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.TokenBucket;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.commons.ConfigConstants;
import org.opensearch.commons.authuser.User;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.ml.common.FunctionName;
import org.opensearch.ml.common.connector.Connector;
import org.opensearch.ml.common.connector.ConnectorAction;
Expand All @@ -42,6 +46,7 @@
import org.opensearch.ml.common.dataset.TextDocsInputDataSet;
import org.opensearch.ml.common.dataset.remote.RemoteInferenceInputDataSet;
import org.opensearch.ml.common.input.MLInput;
import org.opensearch.ml.common.input.parameter.MLAlgoParams;
import org.opensearch.ml.common.model.MLGuard;
import org.opensearch.ml.common.output.model.ModelTensorOutput;
import org.opensearch.ml.common.output.model.ModelTensors;
Expand All @@ -50,6 +55,8 @@
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.transport.client.Client;

import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.Builder;

public interface RemoteConnectorExecutor {
Expand Down Expand Up @@ -83,6 +90,7 @@ default void executeAction(String action, MLInput mlInput, ActionListener<MLTask
MLInput
.builder()
.algorithm(FunctionName.TEXT_EMBEDDING)
.parameters(mlInput.getParameters())
.inputDataset(TextDocsInputDataSet.builder().docs(textDocs).build())
.build(),
new ExecutionContext(sequence++),
Expand Down Expand Up @@ -187,6 +195,17 @@ default void preparePayloadAndInvoke(
inputParameters.putAll(((RemoteInferenceInputDataSet) inputDataset).getParameters());
}
parameters.putAll(inputParameters);

MLAlgoParams algoParams = mlInput.getParameters();
if (algoParams != null) {
try {
Map<String, String> parametersMap = getParams(mlInput);
parameters.putAll(parametersMap);
} catch (IOException e) {
actionListener.onFailure(e);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add return here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
}

RemoteInferenceInputDataSet inputData = processInput(action, mlInput, connector, parameters, getScriptService());
if (inputData.getParameters() != null) {
parameters.putAll(inputData.getParameters());
Expand Down Expand Up @@ -227,6 +246,23 @@ && getUserRateLimiterMap().get(user.getName()) != null
}
}

default Map<String, String> getParams(MLInput mlInput) throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make it a static method? it doesn't have dependency on other fields or method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method has been updated to static.

Map<String, String> result = new HashMap<>();
XContentBuilder builder = XContentFactory.jsonBuilder();
mlInput.getParameters().toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.flush();
String json = builder.toString();

ObjectMapper mapper = new ObjectMapper();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make a performance optimization here:

  1. Change the MAPPER here: org.opensearch.ml.common.utils.StringUtils#MAPPER to public.
  2. Use the public MAPPER here for deserialization.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Map<String, Object> tempMap = mapper.readValue(json, Map.class);

HashMap<String, String> paramMap = new HashMap<>();
for (Map.Entry<String, Object> entry : tempMap.entrySet()) {
paramMap.put(entry.getKey(), entry.getValue() != null ? entry.getValue().toString() : null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the Object is a map/list type, the toString by default returns their address in memory instead of a json, it seems not correct here, do you have test cases to cover this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve addressed this issue by updating the code so that all types except basic types and String are serialized to JSON strings. Also added a unit test.

}
return paramMap;
}

default BackoffPolicy getRetryBackoffPolicy(ConnectorClientConfig connectorClientConfig) {
switch (connectorClientConfig.getRetryBackoffPolicy()) {
case EXPONENTIAL_EQUAL_JITTER:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import static org.opensearch.ml.common.connector.HttpConnector.SERVICE_NAME_FIELD;
import static org.opensearch.ml.engine.algorithms.remote.ConnectorUtils.SKIP_VALIDATE_MISSING_PARAMETERS;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
Expand All @@ -39,6 +41,8 @@
import org.opensearch.ml.common.connector.RetryBackoffPolicy;
import org.opensearch.ml.common.dataset.remote.RemoteInferenceInputDataSet;
import org.opensearch.ml.common.input.MLInput;
import org.opensearch.ml.common.input.parameter.textembedding.AsymmetricTextEmbeddingParameters;
import org.opensearch.ml.common.input.parameter.textembedding.SparseEmbeddingFormat;
import org.opensearch.ml.common.output.model.ModelTensors;
import org.opensearch.ml.engine.encryptor.Encryptor;
import org.opensearch.ml.engine.encryptor.EncryptorImpl;
Expand Down Expand Up @@ -169,4 +173,73 @@ public void executePreparePayloadAndInvoke_SkipValidateMissingParameterDefault()
);
assert exception.getMessage().contains("Some parameter placeholder not filled in payload: role");
}

@Test
public void executeGetParams_MissingParameter() {
Map<String, String> parameters = ImmutableMap.of(SERVICE_NAME_FIELD, "sagemaker", REGION_FIELD, "us-west-2");
Connector connector = getConnector(parameters);
AwsConnectorExecutor executor = getExecutor(connector);

RemoteInferenceInputDataSet inputDataSet = RemoteInferenceInputDataSet
.builder()
.parameters(Map.of("input", "${parameters.input}"))
.actionType(PREDICT)
.build();
String actionType = inputDataSet.getActionType().toString();
AsymmetricTextEmbeddingParameters inputParams = AsymmetricTextEmbeddingParameters
.builder()
.sparseEmbeddingFormat(SparseEmbeddingFormat.WORD)
.embeddingContentType(null)
.build();
MLInput mlInput = MLInput
.builder()
.algorithm(FunctionName.TEXT_EMBEDDING)
.parameters(inputParams)
.inputDataset(inputDataSet)
.build();

try {
Map<String, String> paramsMap = executor.getParams(mlInput);
Map<String, String> expectedMap = new HashMap<>();
expectedMap.put("sparse_embedding_format", "WORD");
Assert.assertEquals(expectedMap, paramsMap);
} catch (IOException e) {
e.printStackTrace();
}
}

@Test
public void executeGetParams_PassingParameter() {
Map<String, String> parameters = ImmutableMap.of(SERVICE_NAME_FIELD, "sagemaker", REGION_FIELD, "us-west-2");
Connector connector = getConnector(parameters);
AwsConnectorExecutor executor = getExecutor(connector);

RemoteInferenceInputDataSet inputDataSet = RemoteInferenceInputDataSet
.builder()
.parameters(Map.of("input", "${parameters.input}"))
.actionType(PREDICT)
.build();
String actionType = inputDataSet.getActionType().toString();
AsymmetricTextEmbeddingParameters inputParams = AsymmetricTextEmbeddingParameters
.builder()
.sparseEmbeddingFormat(SparseEmbeddingFormat.WORD)
.embeddingContentType(AsymmetricTextEmbeddingParameters.EmbeddingContentType.PASSAGE)
.build();
MLInput mlInput = MLInput
.builder()
.algorithm(FunctionName.TEXT_EMBEDDING)
.parameters(inputParams)
.inputDataset(inputDataSet)
.build();

try {
Map<String, String> paramsMap = executor.getParams(mlInput);
Map<String, String> expectedMap = new HashMap<>();
expectedMap.put("sparse_embedding_format", "WORD");
expectedMap.put("content_type", "PASSAGE");
Assert.assertEquals(expectedMap, paramsMap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Loading