🌌 Worklets

Worklets are where you define your application's business logic. A Worklet is a DAG of code and model execution that is backed by an API endpoint. It allows you to think in terms of your application's business logic and flow, rather than worrying about Kubernetes, Docker, Flask, and DB instances. Once you create your worklet, you can then invoke it through its auto-generated API endpoint, on a set schedule, or by connecting it to a Kafka source (more on that below).
Worklets are comprised of nodes of different types, including custom python code, model invocation, decision node, and actions such as a Slack node.
In the example below, we want to operationalize a zero-shot classification model. The worklet receives English text as input, pre-processes it, runs the model, and stores the results in the database.
Simple worklet example

How does data flow through a worklet?

As the flow suggests, the output of each node is fed as the input to the next node. The worklet's input becomes the first node's input, and the final node's output is the complete worklet's output.
Inputs and outputs must be JSON-serializable. For instance, dicts, lists, strings, numbers, and booleans are all valid.

How are worklets invoked?

You can invoke a worklet in three different ways:
  1. 1.
    While developing and testing, the easiest way to invoke the worklet is through the UI. Click on the blue play button and use the "Testing" section.
  2. 2.
    You can call the worklet through its auto-generated API endpoint. To see the worklet invocation API, click on the blue play button and follow the "Run this worklet" section.
  3. 3.
    And finally you can set the worklet to execute on a set schedule. Again click on the blue play button and follow the "Run this worklet" instructions.

⭐ Nodes

Nodes are units of work that comprise a worklet. Some node types do all the heavy lifting for you and simply need to be configured. For example when adding an Invoke model node, you need to select which of your ML models want it to invoke. Or when selecting a Slack node, you need to give it a Slack webhook URL.
Two types of nodes give you lots of flexibility in terms of functionality: Code block and Decision nodes.

Code block

BaseTen allows you to write your own Python code as part of your worklet. Simply add a Code block node to your worklet. You'll notice that the placeholder function's definition looks like:
def do(node_input, env, context):
Let's dig into what these three arguments are.


This is the input to the node. If this node is the first in the worklet, then node_input is what the worklet was called with. Otherwise node_input is the return value of the previous node in the worklet.


Exclusively relying on node input/output for the execution of the worklet would cause headaches. What if the final node needs to access the first node's input? Would you have to ensure that the input is passed along all the way to the end? No.
That's where env comes in. It's an empty dictionary to which you can add key/value pairs (again, JSON-serializable values only). env gets threaded through the worklet execution, which means that the first node could set something on it and all subsequent nodes would have access to that data.


context provides two sets of functionality:
  1. 1.
    Access to external data connections (e.g. Snowflake, Redis, and Postgres)
Visit those sections to see how the context parameter can help.

Decision node

Decision nodes are very similar to Code block in that you write custom Python code with access to the same node_input, env, context args; however, Decision nodes' output must be a boolean, which will determine which of its two child nodes should be called next.
Exception to the rule: Since a node's output becomes the subsequent node's input, you can imagine that the boolean result of the Decision node will become the input to its subsequent node. That would be somewhat useless, so we made an exception: The input to the Decision node becomes the input to the subsequent node.


While debugging a Code block or Decision node, it might be handy to log some information. You can use Python's print statements, like the following example shows:
Logging within code nodes
Last modified 6mo ago