Skip to main content

Create a workflow

Workflows decide what happens to each file added to a Knowledge: which text is extracted, which resources are created, and what gets chunked and embedded for search. This guide shows how to write a step, connect steps into a workflow, and make that workflow the default for a Knowledge.

Settings → Workflows holds two kinds of object:

  • A step is one Python script. It receives a file and its options, does the work, and can return text for the Knowledge to chunk and embed.
  • A workflow is a graph that connects steps, control-flow nodes and built-in operations. A Knowledge runs it on every file when it is the Knowledge's Default workflow.

A Knowledge that keeps the Classic pipeline as its default runs no graph: its processing rule picks one step for each file. See Workflows and processing rules for how the pieces fit.

Before you begin​

  • Role: Create and Update on the Workflows row of your role. See Manage roles and users.
  • A Knowledge with a few files, to test the step and run the workflow on.
The Workflows tab with the Workflows, Steps, Control flow and Builtins lists on the left and the selected step's code editor on the right

Create a step​

  1. Go to Settings → Workflows.
  2. Next to Steps, select +, then From scratch. On an empty page, select Add new. Import JSON loads a step exported from another environment instead.
  3. Replace New workflow with a name that says what the step does, for example Markdown to resource. Processing rules and workflows call the step by this name.
  4. Optionally, enter a description.
  5. Write the step's code in the editor. The next section has an example.
  6. Select Save.

A new step is switched on. Only switched-on steps can run and appear where you pick a step.

Write the step code​

This step reads a text file, creates a resource for it in the file's Knowledge, and returns the text so the Knowledge chunks and embeds it:

#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "rational-client",
# ]
# ///

from rational_client.core import File, Knowledge
from rational_client.utils import run


def process(document: File, options: dict):
text = document.get_data().decode("utf-8")

knowledge = Knowledge(document.knowledge_id)
resource = knowledge.create_resource(
name=document.name,
category="Document",
file_id=document.id,
)

# Resource ID -> text to chunk and embed
return {str(resource.resource_id): text}


run(process)
  • The header lists the packages the step needs. Add any package your code imports to dependencies.
  • run(process) calls process with the file being processed and the step's options.
  • The rational-client package gives the step access to the Knowledge, for example to create resources.
  • The returned dictionary maps each resource ID to its Markdown or plain text. Rational AI chunks and embeds that text with the Knowledge's settings. Return nothing if the step creates no searchable text.

Tag the step's capabilities​

Capabilities (Select capabilities) records the kinds of annotation the step produces, such as images or tables. The tags appear next to the step's name wherever you pick a step, for example when you set up auto-annotation. They describe the step and don't change what it does.

Test the step​

  1. Save the step. A step that was never saved can't run.
  2. Select Test run (the play icon).
  3. In Input (JSON), enter the input. "file_id" is loaded as the document, and any other key is passed as an option. Use {} for a step that takes no input.
  4. Select Run.

Output shows the result and how long the run took. If the run fails, it shows the Error and the Traceback / STDERR.

The step's ⋮ menu also holds Download (the .py file), Export (JSON), Workflow Settings, and Delete. See Import, export, and set timeouts.

Connect steps into a workflow​

  1. In Settings → Workflows, select + next to Workflows (Add workflow). Rational AI creates Untitled workflow, switched off, and opens it.
  2. Build the graph in one of two ways:
    • On the Graph canvas, drag steps from Steps, and nodes from Control flow and Builtins, onto the canvas. Drag from one port to another to connect two nodes. Press Del to remove the selected node or connection.
    • In the code editor, write the graph with the workflow builder, starting from f = Flow(). Select a node under Control flow or Builtins to see its Usage, Parameters, Inputs, and Outputs.
  3. Select Save. Rational AI compiles the code and redraws the graph. If the code doesn't compile, the error shows in a message and nothing is saved.
  4. In the list, turn on the workflow's switch.

A Knowledge can select a workflow only while it is switched on.

Make a workflow the default for a Knowledge​

  1. Go to Settings → Knowledge and open the Knowledge.
  2. Select the Sources tab.
  3. In Default workflow, select your workflow. The list shows the switched-on workflows that belong to this Knowledge or to the whole workspace. Classic pipeline routes each file with the Default processing rule instead.
  4. Select Save.

The workflow now runs on every file added to the Knowledge: uploads in the Control Room, files brought in by a source sync, and reprocessing requests that don't name another workflow. When you reprocess a Knowledge from the Control Room, you can still pick a different Workflow for that run.

Deleting a workflow leaves every Knowledge that used it as the default without one.

Next steps​