In previous lessons, you learned how to control timing and manage flows in Colang to make your chatbot more flexible and responsive. However, sometimes the built-in actions and flow logic in Colang aren’t enough. For advanced tasks—like performing complex calculations, calling external APIs, or using specialized Python libraries—you need to extend Colang with custom Python actions.
In this lesson, you’ll learn how to create, use, and manage custom Python actions in your Colang projects. This unlocks the full power of Python within your Colang flows, making your chatbots much more capable and flexible.
Custom Python actions are functions you define in the actions.py
file in the root of your project. These functions are decorated as actions and can be called directly from your Colang flows. This allows you to implement more complex functionality that cannot be achieved with Colang alone. All Python actions run in the context of the Colang interpreter, so they have access to the same runtime environment and resources as your Colang flows.
To make a Python function available to Colang, use the @action
decorator from nemoguardrails.actions
. This registers your function as an action that can be called from Colang flows.
Example:
Key Points:
- Use the
@action
decorator to register your function. - The
name
parameter sets the action’s name (should end withAction
by convention). - Define your function as
async def
. - The function can accept any parameters you need.
Once you’ve defined a Python action, you can call it from a Colang flow using the await
keyword. This pauses the flow until the action completes and returns a result.
Example:
How it works:
await CustomAction(value=5)
calls your Python action and stores the result in$val
.- You can then use
$val
in subsequent steps, such as sending a message to the user.
Some actions may take a long time (e.g., network requests, heavy computation). By default, actions are blocking—the flow waits for them to finish. To run an action in the background and continue the flow, set execute_async=True
in the decorator.
Example:
You can use asynchronous actions by launching it as an action, and then accessing it once it's finished:
start ... as $ref
launches the action and saves a reference.match $ref.Finished()
waits for the action to finish and captures the result.
Python actions can access special parameters for more control and context:
events
: recent conversation eventscontext
: global variables (read or update)config
: configuration fromconfig.yml
llm_task_manager
: LLM task manager objectstate
: state machine state object
Add these as parameters to your function as needed.
Example:
Here, the action gets the LLM model as defined in the config.yaml
:
Example 2:
This returns something like:
- Use the
@action
decorator to define Python functions as Colang actions. - Call actions from Colang flows with
await
, passing parameters as needed. - For long-running tasks, set
execute_async=True
and usestart
/match
orawait
in your flows. - Access special parameters like
context
andevents
for advanced control.
In the next practice, you’ll write your own Python actions, call them from Colang, and see how they work together to build more advanced, agentic AI chatbots.
