Develop a Custom Alerter
Learning how to develop a custom alerter.
Before diving into the specifics of this component type, it is beneficial to familiarize yourself with our general guide to writing custom component flavors in ZenML. This guide provides an essential understanding of ZenML's component flavor concepts.
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 returnsTrue
if 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 returnsTrue
if the operation succeeded and was approved, elseFalse
.
Then base abstraction looks something like this:
This is a slimmed-down version of the base implementation. To see the full docstrings and imports, please check the source code on GitHub.
Building your own custom alerter
Creating your own custom alerter can be done in three steps:
Create a class that inherits from the
BaseAlerter
and implement thepost()
andask()
methods.If you need to configure your custom alerter, you can also implement a config object.
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:
ZenML resolves the flavor class by taking the path where you initialized zenml (via zenml init
) as the starting point of resolution. Therefore, please ensure you follow the best practice of initializing zenml at the root of your repository.
If ZenML does not find an initialized ZenML repository in any parent directory, it will default to the current working directory, but usually, it's better to not have to rely on this mechanism and initialize zenml at the root.
Afterward, you should see the new custom alerter flavor in the list of available alerter flavors:
It is important to draw attention to when and how these abstractions are coming into play in a ZenML workflow.
The MyAlerterFlavor class is imported and utilized upon the creation of the custom flavor through the CLI.
The MyAlerterConfig class is imported when someone tries to register/update a stack component with the
my_alerter
flavor. Especially, during the registration process of the stack component, the config will be used to validate the values given by the user. AsConfig
objects are inherentlypydantic
objects, you can also add your own custom validators here.The MyAlerter only comes into play when the component is ultimately in use.
The design behind this interaction lets us separate the configuration of the flavor from its implementation. This way we can register flavors and components even when the major dependencies behind their implementation are not installed in our local setting (assuming the MyAlerterFlavor
and the MyAlerterConfig
are implemented in a different module/path than the actual MyAlerter
).
Last updated