-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add XContent classes representing Template JSON
Signed-off-by: Daniel Widdis <[email protected]>
- Loading branch information
Showing
7 changed files
with
298 additions
and
16 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/java/org/opensearch/flowframework/template/Template.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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.template; | ||
|
||
import org.opensearch.Version; | ||
import org.opensearch.core.xcontent.ToXContentObject; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.flowframework.workflow.Workflow; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
/** | ||
* The Template is the central data structure which configures workflows. This object is used to parse JSON communicated via REST API. | ||
*/ | ||
public class Template implements ToXContentObject { | ||
|
||
// TODO: Some of thse are placeholders based on the template design | ||
// Current code is only using user inputs and workflows | ||
private String name = null; | ||
private String description = null; | ||
private String useCase = null; | ||
private String[] operations = null; // probably an ENUM actually | ||
private Version templateVersion = null; | ||
private Version[] compatibilityVersion = null; | ||
private Map<String, String> userInputs = new HashMap<>(); | ||
private Map<String, Workflow> workflows = new HashMap<>(); | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
XContentBuilder xContentBuilder = builder.startObject(); | ||
xContentBuilder.field("name", this.name); | ||
xContentBuilder.field("description", this.description); | ||
xContentBuilder.field("use_case", this.useCase); | ||
xContentBuilder.field("operations", this.operations); | ||
|
||
xContentBuilder.startObject("version"); | ||
xContentBuilder.field("template", this.templateVersion); | ||
xContentBuilder.startArray("compatibility"); | ||
for (Version v : this.compatibilityVersion) { | ||
xContentBuilder.value(v); | ||
} | ||
xContentBuilder.endArray(); | ||
xContentBuilder.endObject(); | ||
|
||
xContentBuilder.startObject("user_inputs"); | ||
for (Entry<String, String> e : userInputs.entrySet()) { | ||
xContentBuilder.field(e.getKey(), e.getValue()); | ||
} | ||
xContentBuilder.endObject(); | ||
|
||
xContentBuilder.startObject("workflows"); | ||
for (Entry<String, Workflow> e : workflows.entrySet()) { | ||
xContentBuilder.field(e.getKey(), e.getValue(), params); | ||
} | ||
xContentBuilder.endObject(); | ||
|
||
return xContentBuilder.endObject(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/org/opensearch/flowframework/template/WorkflowNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.template; | ||
|
||
import org.opensearch.core.xcontent.ToXContentFragment; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
/** | ||
* This represents a process node (step) in a workflow graph in the {@link Template}. | ||
* It will have a one-to-one correspondence with a {@link ProcessNode}, | ||
* where its type is used to determine the correct {@link WorkflowStep} object, | ||
* and its inputs are used to populate the {@link WorkflowData} input. | ||
*/ | ||
public class WorkflowNode implements ToXContentFragment { | ||
|
||
private static final String INPUTS_FIELD = "inputs"; | ||
private static final String TYPE_FIELD = "type"; | ||
private static final String ID_FIELD = "id"; | ||
|
||
private String id = null; // unique id | ||
private String type = null; // maps to a WorkflowStep | ||
private Map<String, String> inputs = new HashMap<>(); // maps to WorkflowData | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
XContentBuilder xContentBuilder = builder.startObject(); | ||
xContentBuilder.field(ID_FIELD, this.id); | ||
xContentBuilder.field(TYPE_FIELD, this.type); | ||
|
||
xContentBuilder.startObject(INPUTS_FIELD); | ||
for (Entry<String, String> e : inputs.entrySet()) { | ||
xContentBuilder.field(e.getKey(), e.getValue()); | ||
} | ||
xContentBuilder.endObject(); | ||
|
||
return xContentBuilder.endObject(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/org/opensearch/flowframework/workflow/Workflow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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.workflow; | ||
|
||
import org.opensearch.core.xcontent.ToXContentFragment; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.flowframework.template.WorkflowEdge; | ||
import org.opensearch.flowframework.template.WorkflowNode; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
/** | ||
* This represents an object in the workflows section of a {@link Template}. | ||
*/ | ||
public class Workflow implements ToXContentFragment { | ||
|
||
private static final String USER_PARAMS_FIELD = "user_params"; | ||
private static final String NODES_FIELD = "nodes"; | ||
private static final String EDGES_FIELD = "edges"; | ||
|
||
private Map<String, String> userParams = new HashMap<>(); | ||
private WorkflowNode[] nodes = null; | ||
private WorkflowEdge[] edges = null; | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
XContentBuilder xContentBuilder = builder.startObject(); | ||
|
||
xContentBuilder.startObject(USER_PARAMS_FIELD); | ||
for (Entry<String, String> e : userParams.entrySet()) { | ||
xContentBuilder.field(e.getKey(), e.getValue()); | ||
} | ||
xContentBuilder.endObject(); | ||
|
||
xContentBuilder.startArray(NODES_FIELD); | ||
for (WorkflowNode n : nodes) { | ||
xContentBuilder.value(n); | ||
} | ||
xContentBuilder.endArray(); | ||
|
||
xContentBuilder.startArray(EDGES_FIELD); | ||
for (WorkflowEdge e : edges) { | ||
xContentBuilder.value(e); | ||
} | ||
xContentBuilder.endArray(); | ||
|
||
return xContentBuilder.endObject(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
{ | ||
"name": "semantic-search", | ||
"description": "My semantic search use case", | ||
"use_case": "SEMANTIC_SEARCH", | ||
"operations": [ | ||
"PROVISION", | ||
"INGEST", | ||
"QUERY" | ||
], | ||
"version": { | ||
"template": "1.0", | ||
"compatibility": [ | ||
"2.9", | ||
"3.0" | ||
] | ||
}, | ||
"user_inputs": { | ||
"index_name": "my-knn-index", | ||
"index_settings": { | ||
} | ||
}, | ||
"workflows": { | ||
"provision": { | ||
"steps": [{ | ||
"id": "create_index", | ||
"inputs": { | ||
"name": "user_inputs.index_name", | ||
"settings": "user_inputs.index_settings" | ||
} | ||
}, | ||
{ | ||
"id": "create_ingest_pipeline", | ||
"inputs": { | ||
"name": "my-ingest-pipeline", | ||
"description": "some description", | ||
"processors": [{ | ||
"type": "text_embedding", | ||
"model_id": "my-existing-model-id", | ||
"input_field": "text_passage", | ||
"output_field": "text_embedding" | ||
}] | ||
} | ||
} | ||
], | ||
"edges": [{ | ||
"source": "create_index", | ||
"dest": "create_ingest_pipeline" | ||
}] | ||
}, | ||
"ingest": { | ||
"user_params": { | ||
"document": {} | ||
}, | ||
"steps": [{ | ||
"id": "ingest_index", | ||
"inputs": { | ||
"index": "user_inputs.index_name", | ||
"ingest_pipeline": "my-ingest-pipeline", | ||
"document": "user_params.document" | ||
} | ||
}] | ||
}, | ||
"query": { | ||
"user_params": { | ||
"plaintext": "string" | ||
}, | ||
"nodes": [{ | ||
"id": "transform_query", | ||
"inputs": { | ||
"template": "neural-search-template-1", | ||
"plaintext": "user_params.plaintext" | ||
} | ||
}, | ||
{ | ||
"id": "query_index", | ||
"inputs": { | ||
"index": "user_inputs.index_name", | ||
"query": "{{output-from-prev-step}}.query", | ||
"search_request_processors": [], | ||
"search_response_processors": [] | ||
} | ||
} | ||
], | ||
"edges": [{ | ||
"source": "transform_query", | ||
"dest": "query_index" | ||
}] | ||
} | ||
} | ||
} |