Skip to content

Commit

Permalink
initial default use case addition
Browse files Browse the repository at this point in the history
Signed-off-by: Amit Galitzky <amgalitz@amazon.com>
  • Loading branch information
amitgalitz committed Mar 15, 2024
1 parent 1f6573d commit 74f58bc
Show file tree
Hide file tree
Showing 17 changed files with 819 additions and 19 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ dependencies {

// ZipArchive dependencies used for integration tests
zipArchive group: 'org.opensearch.plugin', name:'opensearch-ml-plugin', version: "${opensearch_build}"

secureIntegTestPluginArchive group: 'org.opensearch.plugin', name:'opensearch-security', version: "${opensearch_build}"

configurations.all {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ private CommonValue() {}
public static final String PROVISION_WORKFLOW = "provision";
/** The field name for workflow steps. This field represents the name of the workflow steps to be fetched. */
public static final String WORKFLOW_STEP = "workflow_step";
/** The param name for default use case, used by the create workflow API */
public static final String USE_CASE = "use_case";

/*
* Constants associated with plugin configuration
Expand Down
120 changes: 120 additions & 0 deletions src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.flowframework.common;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.flowframework.exception.FlowFrameworkException;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Enum encapsulating the different default use cases and templates we have stored
*/
public enum DefaultUseCases {

Check warning on line 23 in src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java#L23

Added line #L23 was not covered by tests

/** defaults file and substitution ready template for OpenAI embedding model */
OPEN_AI_EMBEDDING_MODEL_DEPLOY(

Check warning on line 26 in src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java#L26

Added line #L26 was not covered by tests
"open_ai_embedding_model_deploy",
"defaults/open-ai-embedding-defaults.json",
"substitutionTemplates/deploy-remote-model-template.json"
),
/** defaults file and substitution ready template for cohere embedding model */
COHERE_EMBEDDING_MODEL_DEPLOY(

Check warning on line 32 in src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java#L32

Added line #L32 was not covered by tests
"cohere-embedding_model_deploy",
"defaults/cohere-embedding-defaults.json",
"substitutionTemplates/deploy-remote-model-template-extra-params.json"
),
LOCAL_NEURAL_SPARSE_SEARCH(

Check warning on line 37 in src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java#L37

Added line #L37 was not covered by tests
"local_neural_sparse_search",
"defaults/local-sparse-search-defaults.json",
"substitutionTemplates/neural-sparse-local-template.json"
),
/** defaults file and substitution ready template for cohere embedding model */ // TODO: not finalized
COHERE_EMBEDDING_MODEL_DEPLOY_SEMANTIC_SEARCH(

Check warning on line 43 in src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java#L43

Added line #L43 was not covered by tests
"cohere-embedding_model_deploy_semantic_search",
"defaults/cohere-embedding-defaults.json",
"substitutionTemplates/deploy-remote-model-template-v1.json"
);

private final String useCaseName;
private final String defaultsFile;
private final String substitutionReadyFile;
private static final Logger logger = LogManager.getLogger(DefaultUseCases.class);
private static final Set<String> allResources = Stream.of(values()).map(DefaultUseCases::getDefaultsFile).collect(Collectors.toSet());

Check warning on line 53 in src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java#L52-L53

Added lines #L52 - L53 were not covered by tests

DefaultUseCases(String useCaseName, String defaultsFile, String substitutionReadyFile) {
this.useCaseName = useCaseName;
this.defaultsFile = defaultsFile;
this.substitutionReadyFile = substitutionReadyFile;
}

Check warning on line 59 in src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java#L55-L59

Added lines #L55 - L59 were not covered by tests

/**
* Returns the useCaseName for the given enum Constant
* @return the useCaseName of this use case.
*/
public String getUseCaseName() {
return useCaseName;

Check warning on line 66 in src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java#L66

Added line #L66 was not covered by tests
}

/**
* Returns the defaultsFile for the given enum Constant
* @return the defaultsFile of this for the given useCase.
*/
public String getDefaultsFile() {
return defaultsFile;

Check warning on line 74 in src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java#L74

Added line #L74 was not covered by tests
}

/**
* Returns the substitutionReadyFile for the given enum Constant
* @return the substitutionReadyFile of the given useCase
*/
public String getSubstitutionReadyFile() {
return substitutionReadyFile;

Check warning on line 82 in src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java#L82

Added line #L82 was not covered by tests
}

/**
* Gets the defaultsFile based on the given use case.
* @param useCaseName name of the given use case
* @return the deafultsFile for that usecase
* @throws FlowFrameworkException if the use case doesn't exist in enum
*/
public static String getDefaultsFileByUseCaseName(String useCaseName) throws FlowFrameworkException {
if (useCaseName != null && !useCaseName.isEmpty()) {
for (DefaultUseCases mapping : values()) {
if (useCaseName.equals(mapping.getUseCaseName())) {
return mapping.getDefaultsFile();

Check warning on line 95 in src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java#L95

Added line #L95 was not covered by tests
}
}
}
logger.error("Unable to find defaults file for use case: {}", useCaseName);
throw new FlowFrameworkException("Unable to find defaults file for use case: " + useCaseName, RestStatus.BAD_REQUEST);

Check warning on line 100 in src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java#L99-L100

Added lines #L99 - L100 were not covered by tests
}

/**
* Gets the substitutionReadyFile based on the given use case
* @param useCaseName name of the given use case
* @return the substitutionReadyFile which has the template
* @throws FlowFrameworkException if the use case doesn't exist in enum
*/
public static String getSubstitutionReadyFileByUseCaseName(String useCaseName) throws FlowFrameworkException {
if (useCaseName != null && !useCaseName.isEmpty()) {
for (DefaultUseCases mapping : values()) {
if (mapping.getUseCaseName().equals(useCaseName)) {
return mapping.getSubstitutionReadyFile();

Check warning on line 113 in src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java#L113

Added line #L113 was not covered by tests
}
}
}
logger.error("Unable to find substitution ready file for use case: {}", useCaseName);
throw new FlowFrameworkException("Unable to find substitution ready file for use case: " + useCaseName, RestStatus.BAD_REQUEST);

Check warning on line 118 in src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/DefaultUseCases.java#L117-L118

Added lines #L117 - L118 were not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.flowframework.common.DefaultUseCases;
import org.opensearch.flowframework.common.FlowFrameworkSettings;
import org.opensearch.flowframework.exception.FlowFrameworkException;
import org.opensearch.flowframework.model.Template;
import org.opensearch.flowframework.transport.CreateWorkflowAction;
import org.opensearch.flowframework.transport.WorkflowRequest;
import org.opensearch.flowframework.util.ParseUtils;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestRequest;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -35,6 +38,7 @@

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.flowframework.common.CommonValue.PROVISION_WORKFLOW;
import static org.opensearch.flowframework.common.CommonValue.USE_CASE;
import static org.opensearch.flowframework.common.CommonValue.VALIDATION;
import static org.opensearch.flowframework.common.CommonValue.WORKFLOW_ID;
import static org.opensearch.flowframework.common.CommonValue.WORKFLOW_URI;
Expand Down Expand Up @@ -78,6 +82,7 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
String workflowId = request.param(WORKFLOW_ID);
String[] validation = request.paramAsStringArray(VALIDATION, new String[] { "all" });
boolean provision = request.paramAsBoolean(PROVISION_WORKFLOW, false);
String useCase = request.param(USE_CASE);
// If provisioning, consume all other params and pass to provision transport action
Map<String, String> params = provision
? request.params()
Expand Down Expand Up @@ -112,11 +117,54 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
);
}
try {
XContentParser parser = request.contentParser();
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
Template template = Template.parse(parser);

WorkflowRequest workflowRequest = new WorkflowRequest(workflowId, template, validation, provision, params);
Template template;
Map<String, String> useCaseDefaultsMap = Collections.emptyMap();
if (useCase != null) {
String json = ParseUtils.resourceToString("/" + DefaultUseCases.getSubstitutionReadyFileByUseCaseName(useCase));
String defaultsFilePath = DefaultUseCases.getDefaultsFileByUseCaseName(useCase);
useCaseDefaultsMap = ParseUtils.parseJsonFileToStringToStringMap("/" + defaultsFilePath);

Check warning on line 126 in src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java#L124-L126

Added lines #L124 - L126 were not covered by tests

if (request.hasContent()) {
try {
XContentParser parser = request.contentParser();
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
Map<String, String> userDefaults = ParseUtils.parseStringToStringMap(parser);

Check warning on line 132 in src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java#L130-L132

Added lines #L130 - L132 were not covered by tests
// updates the default params with anything user has given that matches
for (Map.Entry<String, String> userDefaultsEntry : userDefaults.entrySet()) {
if (useCaseDefaultsMap.containsKey(userDefaultsEntry.getKey())) {
useCaseDefaultsMap.put(userDefaultsEntry.getKey(), userDefaultsEntry.getValue());

Check warning on line 136 in src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java#L136

Added line #L136 was not covered by tests
}
}
} catch (Exception ex) {
String errorMessage = "failure parsing request body when a use case is given";
logger.error(errorMessage, ex);
throw new FlowFrameworkException(errorMessage, ExceptionsHelper.status(ex));
}

Check warning on line 143 in src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java#L138-L143

Added lines #L138 - L143 were not covered by tests

}

json = (String) ParseUtils.conditionallySubstitute(json, null, useCaseDefaultsMap);

Check warning on line 147 in src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java#L147

Added line #L147 was not covered by tests

XContentParser parserTestJson = ParseUtils.jsonToParser(json);
ensureExpectedToken(XContentParser.Token.START_OBJECT, parserTestJson.currentToken(), parserTestJson);
template = Template.parse(parserTestJson);

Check warning on line 151 in src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java#L149-L151

Added lines #L149 - L151 were not covered by tests

} else {

Check warning on line 153 in src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java#L153

Added line #L153 was not covered by tests
XContentParser parser = request.contentParser();
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
template = Template.parse(parser);
}

WorkflowRequest workflowRequest = new WorkflowRequest(
workflowId,
template,
validation,
provision,
params,
useCase,
useCaseDefaultsMap
);

return channel -> client.execute(CreateWorkflowAction.INSTANCE, workflowRequest, ActionListener.wrap(response -> {
XContentBuilder builder = response.toXContent(channel.newBuilder(), ToXContent.EMPTY_PARAMS);
Expand All @@ -134,11 +182,14 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
channel.sendResponse(new BytesRestResponse(ExceptionsHelper.status(e), errorMessage));
}
}));

} catch (FlowFrameworkException e) {
logger.error("failed to prepare rest request", e);
return channel -> channel.sendResponse(
new BytesRestResponse(e.getRestStatus(), e.toXContent(channel.newErrorBuilder(), ToXContent.EMPTY_PARAMS))
);
} catch (IOException e) {
} catch (Exception e) {
logger.error("failed to prepare rest request", e);

Check warning on line 192 in src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java#L191-L192

Added lines #L191 - L192 were not covered by tests
FlowFrameworkException ex = new FlowFrameworkException(
"IOException: template content invalid for specified Content-Type.",
RestStatus.BAD_REQUEST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,23 @@ public class WorkflowRequest extends ActionRequest {
*/
private Map<String, String> params;

/**
* use case flag
*/
private String useCase;

/**
* Deafult params map from use case
*/
private Map<String, String> defaultParams;

/**
* Instantiates a new WorkflowRequest, set validation to all, no provisioning
* @param workflowId the documentId of the workflow
* @param template the use case template which describes the workflow
*/
public WorkflowRequest(@Nullable String workflowId, @Nullable Template template) {
this(workflowId, template, new String[] { "all" }, false, Collections.emptyMap());
this(workflowId, template, new String[] { "all" }, false, Collections.emptyMap(), null, Collections.emptyMap());
}

/**
Expand All @@ -65,7 +75,7 @@ public WorkflowRequest(@Nullable String workflowId, @Nullable Template template)
* @param params The parameters from the REST path
*/
public WorkflowRequest(@Nullable String workflowId, @Nullable Template template, Map<String, String> params) {
this(workflowId, template, new String[] { "all" }, true, params);
this(workflowId, template, new String[] { "all" }, true, params, null, Collections.emptyMap());
}

/**
Expand All @@ -75,13 +85,17 @@ public WorkflowRequest(@Nullable String workflowId, @Nullable Template template,
* @param validation flag to indicate if validation is necessary
* @param provision flag to indicate if provision is necessary
* @param params map of REST path params. If provision is false, must be an empty map.
* @param useCase default use case given
* @param defaultParams the params to be used in the substitution based on the default use case.
*/
public WorkflowRequest(
@Nullable String workflowId,
@Nullable Template template,
String[] validation,
boolean provision,
Map<String, String> params
Map<String, String> params,
String useCase,
Map<String, String> defaultParams
) {
this.workflowId = workflowId;
this.template = template;
Expand All @@ -91,6 +105,8 @@ public WorkflowRequest(
throw new IllegalArgumentException("Params may only be included when provisioning.");
}
this.params = params;
this.useCase = useCase;
this.defaultParams = defaultParams;
}

/**
Expand Down Expand Up @@ -150,6 +166,22 @@ public Map<String, String> getParams() {
return Map.copyOf(this.params);
}

/**
* Gets the params map
* @return the params map
*/
public String getUseCase() {
return this.useCase;

Check warning on line 174 in src/main/java/org/opensearch/flowframework/transport/WorkflowRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/transport/WorkflowRequest.java#L174

Added line #L174 was not covered by tests
}

/**
* Gets the params map
* @return the params map
*/
public Map<String, String> getDefaultParams() {
return Map.copyOf(this.defaultParams);

Check warning on line 182 in src/main/java/org/opensearch/flowframework/transport/WorkflowRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/transport/WorkflowRequest.java#L182

Added line #L182 was not covered by tests
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
Expand Down
Loading

0 comments on commit 74f58bc

Please sign in to comment.