Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Enhance validation for create connector API #3579

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -6,13 +6,39 @@
package org.opensearch.ml.common.connector;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.BEDROCK_BATCH_JOB_ARN;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.BEDROCK_EMBEDDING;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.BEDROCK_RERANK;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.BEDROCK_V2_EMBEDDING_BINARY;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.BEDROCK_V2_EMBEDDING_FLOAT;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.COHERE_EMBEDDING;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.COHERE_RERANK;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.COHERE_V2_EMBEDDING_BINARY;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.COHERE_V2_EMBEDDING_FLOAT32;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.COHERE_V2_EMBEDDING_INT8;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.COHERE_V2_EMBEDDING_UBINARY;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.COHERE_V2_EMBEDDING_UINT8;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.DEFAULT_EMBEDDING;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.DEFAULT_RERANK;
import static org.opensearch.ml.common.connector.MLPostProcessFunction.OPENAI_EMBEDDING;
import static org.opensearch.ml.common.connector.MLPreProcessFunction.IMAGE_TO_COHERE_MULTI_MODAL_EMBEDDING_INPUT;
import static org.opensearch.ml.common.connector.MLPreProcessFunction.TEXT_DOCS_TO_BEDROCK_EMBEDDING_INPUT;
import static org.opensearch.ml.common.connector.MLPreProcessFunction.TEXT_DOCS_TO_COHERE_EMBEDDING_INPUT;
import static org.opensearch.ml.common.connector.MLPreProcessFunction.TEXT_DOCS_TO_DEFAULT_EMBEDDING_INPUT;
import static org.opensearch.ml.common.connector.MLPreProcessFunction.TEXT_DOCS_TO_OPENAI_EMBEDDING_INPUT;
import static org.opensearch.ml.common.connector.MLPreProcessFunction.TEXT_IMAGE_TO_BEDROCK_EMBEDDING_INPUT;
import static org.opensearch.ml.common.connector.MLPreProcessFunction.TEXT_SIMILARITY_TO_BEDROCK_RERANK_INPUT;
import static org.opensearch.ml.common.connector.MLPreProcessFunction.TEXT_SIMILARITY_TO_COHERE_RERANK_INPUT;
import static org.opensearch.ml.common.connector.MLPreProcessFunction.TEXT_SIMILARITY_TO_DEFAULT_INPUT;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.apache.commons.text.StringSubstitutor;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
Expand All @@ -35,6 +61,13 @@ public class ConnectorAction implements ToXContentObject, Writeable {
public static final String REQUEST_BODY_FIELD = "request_body";
public static final String ACTION_PRE_PROCESS_FUNCTION = "pre_process_function";
public static final String ACTION_POST_PROCESS_FUNCTION = "post_process_function";
public static final String OPENAI = "openai";
public static final String COHERE = "cohere";
public static final String BEDROCK = "bedrock";
public static final String SAGEMAKER = "sagemaker";
public static final List<String> SUPPORTED_REMOTE_SERVERS_FOR_DEFAULT_ACTION_TYPES = List.of(SAGEMAKER, OPENAI, BEDROCK, COHERE);

private static final String INBUILT_FUNC_PREFIX = "connector.";

private ActionType actionType;
private String method;
Expand Down Expand Up @@ -185,6 +218,155 @@ public static ConnectorAction parse(XContentParser parser) throws IOException {
.build();
}

public void validatePrePostProcessFunctions(Map<String, String> parameters) {
StringSubstitutor substitutor = new StringSubstitutor(parameters, "${parameters.", "}");
String endPoint = substitutor.replace(url);
String remoteServer = getRemoteServerFromURL(endPoint);
validatePreProcessFunctions(remoteServer);
validatePostProcessFunctions(remoteServer);
}

private void validatePreProcessFunctions(String remoteServer) {
if (isInBuiltFunction(preProcessFunction)) {
switch (remoteServer) {
case OPENAI:
if (!TEXT_DOCS_TO_OPENAI_EMBEDDING_INPUT.equals(preProcessFunction)) {
throw new IllegalArgumentException(
"LLM service is " + OPENAI + ", so PreProcessFunction should be " + TEXT_DOCS_TO_OPENAI_EMBEDDING_INPUT
);
}
break;
case COHERE:
if (!(TEXT_DOCS_TO_COHERE_EMBEDDING_INPUT.equals(preProcessFunction)
|| IMAGE_TO_COHERE_MULTI_MODAL_EMBEDDING_INPUT.equals(preProcessFunction)
|| TEXT_SIMILARITY_TO_COHERE_RERANK_INPUT.equals(preProcessFunction))) {
throw new IllegalArgumentException(
"LLM service is "
+ COHERE
+ ", so PreProcessFunction should be "
+ TEXT_DOCS_TO_COHERE_EMBEDDING_INPUT
+ " or "
+ IMAGE_TO_COHERE_MULTI_MODAL_EMBEDDING_INPUT
+ " or "
+ TEXT_SIMILARITY_TO_COHERE_RERANK_INPUT
);
}
break;
case BEDROCK:
if (!(TEXT_DOCS_TO_BEDROCK_EMBEDDING_INPUT.equals(preProcessFunction)
|| TEXT_IMAGE_TO_BEDROCK_EMBEDDING_INPUT.equals(preProcessFunction)
|| TEXT_SIMILARITY_TO_BEDROCK_RERANK_INPUT.equals(preProcessFunction))) {
throw new IllegalArgumentException(
"LLM service is "
+ BEDROCK
+ ", so PreProcessFunction should be "
+ TEXT_DOCS_TO_BEDROCK_EMBEDDING_INPUT
+ " or "
+ TEXT_IMAGE_TO_BEDROCK_EMBEDDING_INPUT
+ " or "
+ TEXT_SIMILARITY_TO_BEDROCK_RERANK_INPUT
);
}
break;
case SAGEMAKER:
if (!(TEXT_DOCS_TO_DEFAULT_EMBEDDING_INPUT.equals(preProcessFunction)
|| TEXT_SIMILARITY_TO_DEFAULT_INPUT.equals(preProcessFunction))) {
throw new IllegalArgumentException(
"LLM service is "
+ SAGEMAKER
+ ", so PreProcessFunction should be "
+ TEXT_DOCS_TO_DEFAULT_EMBEDDING_INPUT
+ " or "
+ TEXT_SIMILARITY_TO_DEFAULT_INPUT
);
}
}
}
}

private void validatePostProcessFunctions(String remoteServer) {
if (isInBuiltFunction(postProcessFunction)) {
switch (remoteServer) {
case OPENAI:
if (!OPENAI_EMBEDDING.equals(postProcessFunction)) {
throw new IllegalArgumentException(
"LLM service is " + OPENAI + ", so PostProcessFunction should be " + OPENAI_EMBEDDING
);
}
break;
case COHERE:
if (!(COHERE_EMBEDDING.equals(postProcessFunction)
|| COHERE_RERANK.equals(postProcessFunction)
|| COHERE_V2_EMBEDDING_FLOAT32.equals(postProcessFunction)
|| COHERE_V2_EMBEDDING_INT8.equals(postProcessFunction)
|| COHERE_V2_EMBEDDING_UINT8.equals(postProcessFunction)
|| COHERE_V2_EMBEDDING_BINARY.equals(postProcessFunction)
|| COHERE_V2_EMBEDDING_UBINARY.equals(postProcessFunction))) {
throw new IllegalArgumentException(
"LLM service is "
+ COHERE
+ ", so PostProcessFunction should be "
+ COHERE_EMBEDDING
+ " or "
+ COHERE_RERANK
+ " or "
+ COHERE_V2_EMBEDDING_FLOAT32
+ " or "
+ COHERE_V2_EMBEDDING_INT8
+ " or "
+ COHERE_V2_EMBEDDING_UINT8
+ " or "
+ COHERE_V2_EMBEDDING_BINARY
+ " or "
+ COHERE_V2_EMBEDDING_UBINARY
);
}
break;
case BEDROCK:
if (!(BEDROCK_EMBEDDING.equals(postProcessFunction)
|| BEDROCK_BATCH_JOB_ARN.equals(postProcessFunction)
|| BEDROCK_RERANK.equals(postProcessFunction)
|| BEDROCK_V2_EMBEDDING_FLOAT.equals(postProcessFunction)
|| BEDROCK_V2_EMBEDDING_BINARY.equals(postProcessFunction))) {
throw new IllegalArgumentException(
"LLM service is "
+ BEDROCK
+ ", so PostProcessFunction should be "
+ BEDROCK_EMBEDDING
+ " or "
+ BEDROCK_BATCH_JOB_ARN
+ " or "
+ BEDROCK_RERANK
+ " or "
+ BEDROCK_V2_EMBEDDING_FLOAT
+ " or "
+ BEDROCK_V2_EMBEDDING_BINARY
);
}
break;
case SAGEMAKER:
if (!(DEFAULT_EMBEDDING.equals(postProcessFunction) || DEFAULT_RERANK.equals(postProcessFunction))) {
throw new IllegalArgumentException(
"LLM service is "
+ SAGEMAKER
+ ", so PostProcessFunction should be "
+ DEFAULT_EMBEDDING
+ " or "
+ DEFAULT_RERANK
);
}
}
}
}

private boolean isInBuiltFunction(String function) {
return (function != null && function.startsWith(INBUILT_FUNC_PREFIX));
}

public static String getRemoteServerFromURL(String url) {
return SUPPORTED_REMOTE_SERVERS_FOR_DEFAULT_ACTION_TYPES.stream().filter(url::contains).findFirst().orElse("");
}

public enum ActionType {
PREDICT,
EXECUTE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public HttpConnector(
String tenantId
) {
validateProtocol(protocol);
if (actions != null) {
for (ConnectorAction action : actions) {
action.validatePrePostProcessFunctions(parameters);
}
}
this.name = name;
this.description = description;
this.version = version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public MLCreateConnectorInput(
if (credential == null || credential.isEmpty()) {
throw new IllegalArgumentException("Connector credential is null or empty list");
}
if (actions != null) {
for (ConnectorAction action : actions) {
action.validatePrePostProcessFunctions(parameters);
}
}
}
this.name = name;
this.description = description;
Expand Down
Loading
Loading