forked from deepjavalibrary/djl-serving
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Workflow Templates (deepjavalibrary#1594)
- Loading branch information
Showing
9 changed files
with
201 additions
and
8 deletions.
There are no files selected for viewing
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
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,42 @@ | ||
# Workflow Templates | ||
|
||
Workflow templates are a tool to make it easier to register similar workflows. | ||
|
||
## Registering a workflow with a template | ||
|
||
To register a new workflow using a template, the template must first be registered (see below). | ||
Then, you can use the management API to register the workflow. | ||
To do so, call register while specifying the template using the `template` query parameter. | ||
Then, specify all template replacement values using additional query parameters. | ||
|
||
## Available templates | ||
|
||
### adapter | ||
|
||
The adapter template is used for a simple workflow that reflects an [adapter model](adapters.md). | ||
|
||
Parameters: | ||
|
||
- template=`adapter` | ||
- adapter - The adapter name | ||
- url - The adapter URL | ||
- model - The model URL | ||
|
||
Example: | ||
|
||
`POST /workflows?template=adapter&adapter={adapterName}&url={adapterUrl}&model={modelUrl}` | ||
|
||
## Adding new templates | ||
|
||
To add a new template, begin by creating the template JSON file. | ||
This mostly matches the standard format of a [workflow](workflows.md). | ||
However, your template can indicate variable sections of the template to be replaced. | ||
This is done by prefixing the name to replace with a `$`. | ||
So, a parameter `param` would replace the value `$param` within the template. | ||
This replacement is directly in place, so if your parameter is a string you will still have to surround it with quotation marks. | ||
|
||
Then, you must register your new workflow template. | ||
There are two options for doing this. | ||
First, you can add it to the classpath as a resource with path `workflowTemplates/{workflowTemplateName}.json`. | ||
Alternatively, you can register it from a plugin by calling `WorkflowTemplates.register(..)`. | ||
Once this is done, you will be able to begin creating workflows with your template. |
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
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
71 changes: 71 additions & 0 deletions
71
serving/src/main/java/ai/djl/serving/workflow/WorkflowTemplates.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,71 @@ | ||
/* | ||
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance | ||
* with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
package ai.djl.serving.workflow; | ||
|
||
import ai.djl.util.ClassLoaderUtils; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** A class for managing and using {@link WorkflowDefinition} templates. */ | ||
public final class WorkflowTemplates { | ||
|
||
private static final Map<String, URI> TEMPLATES = new ConcurrentHashMap<>(); | ||
|
||
private WorkflowTemplates() {} | ||
|
||
/** | ||
* Registers a new workflow template. | ||
* | ||
* @param name the template name | ||
* @param template the template location | ||
*/ | ||
public static void register(String name, URI template) { | ||
TEMPLATES.put(name, template); | ||
} | ||
|
||
/** | ||
* Constructs a {@link WorkflowDefinition} using a registered template. | ||
* | ||
* @param templateName the template name | ||
* @param templateReplacements a map of replacements to be applied to the template | ||
* @return the new {@link WorkflowDefinition} based off the template | ||
* @throws IOException if it fails to load the template file for parsing | ||
*/ | ||
public static WorkflowDefinition template( | ||
String templateName, Map<String, String> templateReplacements) throws IOException { | ||
URI uri = TEMPLATES.get(templateName); | ||
|
||
if (uri == null) { | ||
URL fromResource = | ||
ClassLoaderUtils.getResource("workflowTemplates/" + templateName + ".json"); | ||
if (fromResource != null) { | ||
try { | ||
uri = fromResource.toURI(); | ||
} catch (URISyntaxException ignored) { | ||
} | ||
} | ||
} | ||
|
||
if (uri == null) { | ||
throw new IllegalArgumentException( | ||
"The workflow template " + templateName + " could not be found"); | ||
} | ||
|
||
return WorkflowDefinition.parse(null, uri, templateReplacements); | ||
} | ||
} |
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,18 @@ | ||
{ | ||
"name": "$adapter", | ||
"version": "0.1", | ||
"models": { | ||
"m": "$model" | ||
}, | ||
"configs": { | ||
"adapters": { | ||
"$adapter": { | ||
"model": "m", | ||
"src": "$url" | ||
} | ||
} | ||
}, | ||
"workflow": { | ||
"out": ["adapter", "$adapter", "in"] | ||
} | ||
} |
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