Create custom action

Create your own custom action

To create your custom actions, you can use the action.py file you've used in the previous section. After completing this tutorial, you'll have two actions in one action package.

Each action is defined by its function and needs its own devdata JSON file with the appropriate input values to be able to run in VS Code.

Let's say you want to create a very simple action that returns a greeting, including the recipient's name (such as “Hi Dave!”).

Write the action

Create a new function for the action

In the action.py file, add the following code at the bottom:

@action
def greet(name: str) -> str:
    """Greets a person including their name.
    Args:
        name (str): Name of the person to greet.
    Returns:
        str: The greeting
    """
    return f"Hello, my friend {name}!"

Define the input data in JSON

  1. Under the devdata folder, create a new file named input_greet.json.
    • The part between input_ and .json must match exactly the name of the corresponding function.
  2. Open the input_greet.json file.
  3. Add the following JSON into the file to define the name variable value:
{
    "name": "Dave"
}

Run your new action

Make sure you saved both the action.py and input_greet.json files.

By now, you have two actions in the same package. That means you need to select which action to run.

You can't use the ▷ Run Action button in VSC, because it always runs only the first action it finds, no questions asked. Use the VSC's Command Palette instead:

  1. Type Run action and select Sema4.ai: Run Action (from Action Package).
  2. Select the Basic Template Package: greet option.

That's it. After the action finishes, you see the output of the greet action in the Terminal pane:

================================ Running: greet ================================
greet status: PASS
result:
"Hello, my friend Dave!"
================================================================================

Congratulations, you've created your very own first action!

If you didn't get the expected result, see your debugging options in the previous article.

Connect your action with external data provider

Connecting your action with an external data provider enables your agent to access and process data from various platforms, expanding its capabilities beyond internal datasets. We will be adding tutorials for connecting to external data providers soon!

Explore actions provided by Sema4.ai

Now that you know how to build your own actions, it's time to push your skills forward:

Need to customize a pre-built action?

You can adapt a pre-built action to suit your unique business requirements.

There are a few points to keep in mind:

  • We suggest you rename the action to avoid confusion of having two actions of the same name.
  • If you already use the pre-built action in an agent, you can't adjust it in place. You need to remove the pre-built action and add the customized version anew.
  • If you develop an agent using the action in VS Code, you need to move the pre-built action to the My Actions folder. Customize the action there and update your agent configuration accordingly.