Base Workflow
Index your data and provide your app with a default knowledge base
The base workflow acts as the default knowledge base for your app and provides the basic structure for indexing your data. It gives you a "head start" when working with your data — by pre-indexing your inputs for search and by providing a default embedding for your custom models.
Click here to learn more about the base workflow functionality.
Let's demonstrate how you can update the default workflow for your app.
Create a Workflow
You may need to start by creating a workflow for your app.
Below is an example of how to create a workflow with a custom model. Note that you can also create a workflow using the models available publicly on the Clarifai Community platform.
- Python
- JavaScript (REST)
- NodeJS
- Java
- PHP
- cURL
###################################################################################
# In this section, we set the user authentication, app ID, and the details we want
# to use to create a workflow. Change these strings to run your own example.
###################################################################################
USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change these to create your own workflow
WORKFLOW_ID = 'my-new-workflow-id'
EMBED_MODEL_ID = 'YOUR_EMBED_MODEL_ID'
EMBED_MODEL_VERSION_ID = 'YOUR_EMBED_MODEL_VERSION_ID'
WORKFLOWNODE_ID = 'my-custom-model'
CUSTOM_MODEL_ID = 'YOUR_CUSTOM_MODEL_ID'
CUSTOM_MODEL_VERSION_ID = 'YOUR_CUSTOM_MODEL_VERSION_ID'
##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)
metadata = (('authorization', 'Key ' + PAT),)
userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)
post_workflows_response = stub.PostWorkflows(
service_pb2.PostWorkflowsRequest(
user_app_id=userDataObject,
workflows=[
resources_pb2.Workflow(
id=WORKFLOW_ID,
nodes=[
resources_pb2.WorkflowNode(
id="embed",
model=resources_pb2.Model(
id=EMBED_MODEL_ID,
model_version=resources_pb2.ModelVersion(
id=EMBED_MODEL_VERSION_ID
)
)
),
resources_pb2.WorkflowNode(
id=WORKFLOWNODE_ID,
model=resources_pb2.Model(
id=CUSTOM_MODEL_ID,
model_version=resources_pb2.ModelVersion(
id=CUSTOM_MODEL_VERSION_ID
)
),
node_inputs=[
resources_pb2.NodeInput(node_id="embed")
]
),
]
)
]
),
metadata=metadata
)
if post_workflows_response.status.code != status_code_pb2.SUCCESS:
print(post_workflows_response.status)
raise Exception("Post workflows failed, status: " + post_workflows_response.status.description)
<!--index.html file-->
<script>
//////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and the details we want
// to use to create a workflow. Change these strings to run your own example.
/////////////////////////////////////////////////////////////////////////////////////////
const USER_ID = "YOUR_USER_ID_HERE";
// Your PAT (Personal Access Token) can be found in the Account's Security section
const PAT = "YOUR_PAT_HERE";
const APP_ID = "YOUR_APP_ID_HERE";
// Change these to create your own custom workflow
const WORKFLOW_ID = "my-new-workflow-id";
const EMBED_MODEL_ID = "YOUR_EMBED_MODEL_ID";
const EMBED_MODEL_VERSION_ID = "YOUR_EMBED_MODEL_VERSION_ID";
const WORKFLOWNODE_ID = "my-custom-model";
const CUSTOM_MODEL_ID = "YOUR_CUSTOM_MODEL_ID";
const CUSTOM_MODEL_VERSION_ID = "YOUR_CUSTOM_MODEL_VERSION_ID";
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
const raw = JSON.stringify({
"user_app_id": {
"user_id": USER_ID,
"app_id": APP_ID
},
"workflows": [{
"id": WORKFLOW_ID,
"nodes": [
{
"id": "embed",
"model": {
"id": EMBED_MODEL_ID,
"model_version": {
"id": EMBED_MODEL_VERSION_ID
}
}
},
{
"id": WORKFLOWNODE_ID,
"model": {
"id": CUSTOM_MODEL_ID,
"model_version": {
"id": CUSTOM_MODEL_VERSION_ID
}
},
"node_inputs": [
{
"node_id": "embed"
}
]
}
]
}]
});
const requestOptions = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Authorization': 'Key ' + PAT
},
body: raw
};
fetch(`https://api.clarifai.com/v2/workflows`, requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
</script>
//index.js file
//////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and the details we want
// to use to create a workflow. Change these strings to run your own example.
/////////////////////////////////////////////////////////////////////////////////////
const USER_ID = 'YOUR_USER_ID_HERE';
// Your PAT (Personal Access Token) can be found in the Account's Security section
const PAT = 'YOUR_PAT_HERE';
const APP_ID = 'YOUR_APP_ID_HERE';
// Change these to create your own workflow
const WORKFLOW_ID = 'my-new-workflow-id';
const EMBED_MODEL_ID = 'YOUR_EMBED_MODEL_ID';
const EMBED_MODEL_VERSION_ID = 'YOUR_EMBED_MODEL_VERSION_ID';
const WORKFLOWNODE_ID = 'my-custom-model';
const CUSTOM_MODEL_ID = 'YOUR_CUSTOM_MODEL_ID';
const CUSTOM_MODEL_VERSION_ID = 'YOUR_CUSTOM_MODEL_VERSION_ID';
/////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
/////////////////////////////////////////////////////////////////////////////
const { ClarifaiStub, grpc } = require("clarifai-nodejs-grpc");
const stub = ClarifaiStub.grpc();
// This will be used by every Clarifai endpoint call
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key " + PAT);
stub.PostWorkflows(
{
user_app_id: {
"user_id": USER_ID,
"app_id": APP_ID
},
workflows: [
{
id: WORKFLOW_ID,
nodes: [
{
id: "embed",
model: {
id: EMBED_MODEL_ID,
model_version: {
id: EMBED_MODEL_VERSION_ID
}
}
},
{
id: WORKFLOWNODE_ID,
model: {
id: CUSTOM_MODEL_ID,
model_version: {
id: CUSTOM_MODEL_VERSION_ID
}
},
node_inputs: [
{ node_id: "embed" }
]
}
]
}
]
},
metadata,
(err, response) => {
if (err) {
throw new Error(err);
}
if (response.status.code !== 10000) {
console.log(response.status);
throw new Error("Post workflows failed, status: " + response.status.description);
}
}
);
package com.clarifai.example;
import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.StatusCode;
import com.clarifai.channel.ClarifaiChannel;
import com.clarifai.credentials.ClarifaiCallCredentials;
public class ClarifaiExample {
//////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and the details we want
// to use to create a workflow. Change these strings to run your own example.
/////////////////////////////////////////////////////////////////////////////////////
static final String USER_ID = "YOUR_USER_ID_HERE";
//Your PAT (Personal Access Token) can be found in the portal under Authentication
static final String PAT = "YOUR_PAT_HERE";
static final String APP_ID = "YOUR_APP_ID_HERE";
// Change these to create your own workflow
static final String WORKFLOW_ID = "my-new-workflow-id";
static final String EMBED_MODEL_ID = "YOUR_EMBED_MODEL_ID";
static final String EMBED_MODEL_VERSION_ID = "YOUR_EMBED_MODEL_VERSION_ID";
static final String WORKFLOWNODE_ID = "my-custom-model";
static final String CUSTOM_MODEL_ID = "YOUR_CUSTOM_MODEL_ID";
static final String CUSTOM_MODEL_VERSION_ID = "YOUR_CUSTOM_MODEL_VERSION_ID";
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
.withCallCredentials(new ClarifaiCallCredentials(PAT));
MultiWorkflowResponse postWorkflowsResponse = stub.postWorkflows(
PostWorkflowsRequest.newBuilder()
.setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID).setAppId(APP_ID))
.addWorkflows(
Workflow.newBuilder()
.setId(WORKFLOW_ID)
.addNodes(
WorkflowNode.newBuilder()
.setId("embed")
.setModel(
Model.newBuilder()
.setId(EMBED_MODEL_ID)
.setModelVersion(
ModelVersion.newBuilder()
.setId(EMBED_MODEL_VERSION_ID)
)
)
)
.addNodes(
WorkflowNode.newBuilder()
.setId(WORKFLOWNODE_ID)
.setModel(
Model.newBuilder()
.setId(CUSTOM_MODEL_ID)
.setModelVersion(
ModelVersion.newBuilder()
.setId(CUSTOM_MODEL_VERSION_ID)
)
)
.addNodeInputs(NodeInput.newBuilder().setNodeId("embed"))
)
)
.build()
);
if (postWorkflowsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
throw new RuntimeException("Post workflows failed, status: " + postWorkflowsResponse.getStatus());
}
}
}
<?php
require __DIR__ . "/vendor/autoload.php";
//////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and the details we want
// to use to create a workflow. Change these strings to run your own example.
/////////////////////////////////////////////////////////////////////////////////////////
$USER_ID = "YOUR_USER_ID_HERE";
// Your PAT (Personal Access Token) can be found in the Account's Security section
$PAT = "YOUR_PAT_HERE";
$APP_ID = "YOUR_APP_ID_HERE";
// Change these to create your own custom workflow
$WORKFLOW_ID = "my-new-workflow-id";
$EMBED_MODEL_ID = "YOUR_EMBED_MODEL_ID";
$EMBED_MODEL_VERSION_ID = "YOUR_EMBED_MODEL_VERSION_ID";
$WORKFLOWNODE_ID = "my-custom-model";
$CUSTOM_MODEL_ID = "YOUR_CUSTOM_MODEL_ID";
$CUSTOM_MODEL_VERSION_ID = "YOUR_CUSTOM_MODEL_VERSION_ID";
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
use Clarifai\ClarifaiClient;
use Clarifai\Api\PostWorkflowsRequest;
use Clarifai\Api\Workflow;
use Clarifai\Api\WorkflowNode;
use Clarifai\Api\NodeInput;
use Clarifai\Api\Model;
use Clarifai\Api\ModelVersion;
use Clarifai\Api\Status\StatusCode;
use Clarifai\Api\UserAppIDSet;
$client = ClarifaiClient::grpc();
$metadata = ["Authorization" => ["Key " . $PAT]];
$userDataObject = new UserAppIDSet([
"user_id" => $USER_ID,
"app_id" => $APP_ID
]);
// Let's make a RPC call to the Clarifai platform. It uses the opened gRPC client channel to communicate a
// request and then wait for the response
[$response, $status] = $client->PostWorkflows(
// The request object carries the request along with the request status and other metadata related to the request itself
new PostWorkflowsRequest([
"user_app_id" => $userDataObject,
"workflows" => [
new Workflow([
"id"=> $WORKFLOW_ID,
"nodes" => [
new WorkflowNode([
"id" => "embed",
"model" => new Model([
"id" => $EMBED_MODEL_ID,
"model_version" => new ModelVersion([
"id" => $EMBED_MODEL_VERSION_ID
])
])
]),
new WorkflowNode([
"id" => $WORKFLOWNODE_ID,
"model"=> new Model([
"id" => $CUSTOM_MODEL_ID,
"model_version" => new ModelVersion([
"id" => $CUSTOM_MODEL_VERSION_ID
])
]),
"node_inputs" => [
new NodeInput([
"node_id"=> "embed"
])
]
])
]
])
]
]),
$metadata
)->wait();
// A response is returned and the first thing we do is check the status of it
// A successful response will have a status code of 0; otherwise, there is some error
if ($status->code !== 0) {
throw new Exception("Error: {$status->details}");
}
// In addition to the RPC response status, there is a Clarifai API status that reports if the operation was a success or failure
// (not just that the communication was successful)
if ($response->getStatus()->getCode() != StatusCode::SUCCESS) {
print $response->getStatus()->getDetails();
throw new Exception("Failure response: " . $response->getStatus()->getDescription());
}
?>
curl -X POST "https://api.clarifai.com/v2/users/YOUR_USER_ID_HERE/apps/YOUR_APP_ID_HERE/workflows" \
-H "Authorization: Key YOUR_PAT_HERE" \
-H "Content-Type: application/json" \
--data-raw '{
"workflows": [
{
"id": "my-new-workflow-id",
"nodes": [
{
"id": "embed",
"model": {
"id": "YOUR_EMBED_MODEL_ID_HERE",
"model_version": {
"id": "YOUR_EMBED_MODEL_VERSION_ID_HERE"
}
}
},
{
"id": "my-custom-model",
"model": {
"id": "YOUR_CUSTOM_MODEL_ID_HERE",
"model_version": {
"id": "YOUR_CUSTOM_MODEL_VERSION_ID_HERE"
}
},
"node_inputs": [
{
"node_id": "embed"
}
]
}
]
}
]
}'
Update Your Base Workflow
After creating a workflow, you can then use it to update the default workflow for your app. You can also use a publicly available workflow to update your default workflow.
Below is an example of how to update your base workflow.
Updating the base workflow will re-index your app, processing all inputs through the new base workflow. This may take some time, and could incur costs. You could avoid the costs by deleting all your inputs before updating the base workflow.
- Python
- NodeJS
- Java
- cURL
########################################################################
# In this section, we set the user authentication, app ID, and default
# workflow ID. Change these strings to run your own example.
########################################################################
USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change this to update your default workflow
DEFAULT_WORKFlOW_ID = 'auto-annotation-workflow-id'
##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)
metadata = (('authorization', 'Key ' + PAT),)
userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)
patch_apps_response = stub.PatchApps(
service_pb2.PatchAppsRequest(
user_app_id=userDataObject,
action="overwrite",
apps=[
resources_pb2.App(
id=APP_ID,
default_workflow_id=DEFAULT_WORKFlOW_ID
)
]
),
metadata=metadata
)
if patch_apps_response.status.code != status_code_pb2.SUCCESS:
print(patch_apps_response.status)
raise Exception("Patch apps failed, status: " + patch_apps_response.status.description)
//index.js file
/////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and default
// workflow ID. Change these strings to run your own example.
/////////////////////////////////////////////////////////////////////////////
const USER_ID = 'YOUR_USER_ID_HERE';
// Your PAT (Personal Access Token) can be found in the Account's Security section
const PAT = 'YOUR_PAT_HERE';
const APP_ID = 'YOUR_APP_ID_HERE';
// Change this to update your default workflow
const DEFAULT_WORKFlOW_ID = 'auto-annotation-workflow-id';
/////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
/////////////////////////////////////////////////////////////////////////////
const { ClarifaiStub, grpc } = require("clarifai-nodejs-grpc");
const stub = ClarifaiStub.grpc();
// This will be used by every Clarifai endpoint call
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key " + PAT);
stub.PatchApps(
{
user_app_id: {
"user_id": USER_ID,
"app_id": APP_ID
},
action: "overwrite",
apps: [
{
id: APP_ID,
default_workflow_id: DEFAULT_WORKFlOW_ID
}
]
},
metadata,
(err, response) => {
if (err) {
throw new Error(err);
}
if (response.status.code !== 10000) {
console.log(response.status);
throw new Error("Patch apps failed, status: " + response.status.description);
}
}
);
package com.clarifai.example;
import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.StatusCode;
import com.clarifai.channel.ClarifaiChannel;
import com.clarifai.credentials.ClarifaiCallCredentials;
public class ClarifaiExample {
/////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and default
// workflow ID. Change these strings to run your own example.
/////////////////////////////////////////////////////////////////////////////
static final String USER_ID = "YOUR_USER_ID_HERE";
//Your PAT (Personal Access Token) can be found in the portal under Authentication
static final String PAT = "YOUR_PAT_HERE";
static final String APP_ID = "YOUR_APP_ID_HERE";
// Change this to update your default workflow
static final String DEFAULT_WORKFlOW_ID = "auto-annotation-workflow-id";
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
.withCallCredentials(new ClarifaiCallCredentials(PAT));
MultiAppResponse patchAppsResponse = stub.patchApps(
PatchAppsRequest.newBuilder()
.setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID).setAppId(APP_ID))
.setAction("overwrite")
.addApps(
App.newBuilder()
.setId(APP_ID)
.setDefaultWorkflowId(DEFAULT_WORKFlOW_ID)
).build()
);
if (patchAppsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
throw new RuntimeException("Patch apps failed, status: " + patchAppsResponse.getStatus());
}
}
}
curl -X PATCH "https://api.clarifai.com/v2/users/YOUR_USER_ID_HERE/apps/" \
-H "Authorization: Key YOUR_PAT_HERE" \
-H "Content-Type: application/json" \
--data-raw '{
"action": "overwrite",
"apps": [
{
"id": "YOUR_APP_ID_HERE",
"default_workflow_id": "auto-annotation-workflow-ID"
}
]
}'