Skip to content

Lark Notification ​

image

If you wish to receive immediate Lark notifications upon training completion or errors, the Lark Notification plugin is highly recommended.

Improve the Plugin

SwanLab plugins are open-source. You can view the GitHub source code. Suggestions and PRs are welcome!

Preparation ​

Reference Documentation

• Custom Bot API Guide • Using Bots in Lark Groups

  1. In a Lark group, click the "···" - "Settings" in the top-right corner.
  1. Click "Group Bots".
  1. Click "Add Bot".
  1. Add a "Custom Bot".
  1. Copy the "Webhook URL" and "Signature".

At this point, your preparation is complete.

Basic Usage ​

Using the Lark notification plugin is straightforward. Simply initialize a LarkCallback object:

python
from swanlab.plugin.notification import LarkCallback

lark_callback = LarkCallback(
    webhook_url="https://open.larkoffice.com/open-apis/bot/v2/hook/xxxx",
    secret="xxxx",
)

Then pass the lark_callback object into the callbacks parameter of swanlab.init:

python
swanlab.init(callbacks=[lark_callback])

This way, when training completes or an error occurs (triggering swanlab.finish()), you will receive a Lark notification.

Custom Notifications ​

You can also use the send_msg method of the LarkCallback object to send custom Lark messages.

This is particularly useful for notifying you when certain metrics reach specific thresholds!

python
if accuracy > 0.95:
    # Send a custom notification
    lark_callback.send_msg(
        content=f"Current Accuracy: {accuracy}",  # Notification content
    )

Register plugins externally ​

If you are using the integration of SwanLab with other frameworks and thus find it difficult to locate swanlab.init, you can use the swanlab.register_callbacks method to pass in plugins externally:

python
import swanlab

# Equivalent to swanlab.init(callbacks=[...])
swanlab.register_callbacks([...])

Limitations ​

• The training completion/error notification of the Lark notification plugin relies on the on_stop lifecycle callback of SwanKitCallback. Therefore, if your process is abruptly killed or the training machine shuts down unexpectedly, the on_stop callback will not be triggered, and no Lark notification will be sent.

• A more robust solution will be available with the launch of SwanLab's Platform Open API. Stay tuned!