Develop a Custom Alerter
Learning how to develop a custom alerter.
Base Abstraction
The base abstraction for alerters is very basic, as it only defines two abstract methods that subclasses should implement:
post()takes a string, posts it to the desired chat service, and returnsTrueif the operation succeeded, elseFalse.ask()does the same aspost(), but after sending the message, it waits until someone approves or rejects the operation from within the chat service (e.g., by sending "approve" / "reject" to the bot as a response).ask()then only returnsTrueif the operation succeeded and was approved, elseFalse.
The ask() method is particularly useful for implementing human-in-the-loop workflows. When implementing this method, you should:
Wait for user responses containing approval keywords (like
"approve","yes","ok","LGTM")Wait for user responses containing disapproval keywords (like
"reject","no","cancel","stop")Return
Trueonly when explicit approval is receivedReturn
Falsefor disapproval, timeout, or any errorsConsider implementing configurable approval/disapproval keywords via parameters
Then base abstraction looks something like this:
from abc import ABC
from typing import Optional
from zenml.stack import StackComponent
from zenml.alerter import BaseAlerterStepParameters
class BaseAlerter(StackComponent, ABC):
"""Base class for all ZenML alerters."""
def post(
self, message: str, params: Optional[BaseAlerterStepParameters]
) -> bool:
"""Post a message to a chat service."""
return True
def ask(
self, question: str, params: Optional[BaseAlerterStepParameters]
) -> bool:
"""Post a message to a chat service and wait for approval."""
return TrueBuilding your own custom alerter
Creating your own custom alerter can be done in four steps:
Create a class that inherits from the
BaseAlerterand implement thepost()andask()methods.If you need to configure your custom alerter, you can also implement a config object.
Optionally, you can create custom parameter classes to support configurable approval/disapproval keywords:
Finally, you can bring the implementation and the configuration together in a new flavor object.
Once you are done with the implementation, you can register your new flavor through the CLI. Please ensure you point to the flavor class via dot notation:
For example, if your flavor class MyAlerterFlavor is defined in flavors/my_flavor.py, you'd register it by doing:
Afterward, you should see the new custom alerter flavor in the list of available alerter flavors:
Last updated
Was this helpful?