Skip to main content
An alert rule is a plugin that decides, for each new article, which alerts it matches. Quivr stores alerts, batches the work, retries and delivers notifications; your rule only answers match, no match or not ready, with evidence for a match. This page starts from the quivr plugin init template, a rule that matches a phrase or a metadata value.

Prerequisites

  • Python 3.12 or later, a quivr-v2 checkout in $QUIVR_REPO, and the quivr command on your PATH (Build your first plugin shows how).

How Quivr calls a rule

When a Version becomes searchable, the worker collects every enabled Subscription that names your rule and sends them in batches: one article (its text Parts and metadata) and a list of evaluations, each a Saved Query expression and a Subscription configuration. Subscriptions that share the same expression and configuration become one evaluation, so a rule that calls a paid service pays once per distinct alert. Your rule answers one decision per evaluation: A decision must depend only on the article, the expression and the configuration. If the rule cannot decide at all, for example because a backend is down, it answers an error: Quivr keeps the evaluations pending and retries. A plugin outage delays alerts; it never turns into a negative decision.

Steps

1

Create the rule

2

Define what an alert looks like

expression_schema in quivr-plugin.yaml is the JSON Schema of the queries your rule understands. The template offers two kinds, told apart by kind:
Quivr validates every Saved Query against this schema when a Subscription is created, so a malformed alert is refused with 422 invalid_expression instead of failing later. configuration_schema does the same for the per-Subscription settings, here case_sensitive. To offer another kind of alert, add a branch to the oneOf and handle it in phrase_alerts/rule.py.
3

Decide

evaluate in phrase_alerts/rule.py receives the article and the batch, and returns one decision per evaluation, built with the SDK’s helpers:
phrase_alerts/rule.py (excerpt)
The explanation is at most 4096 characters, part_keys at most 100 keys of Parts in the request, and details a JSON object of at most 16 KiB. Quivr stores the evidence with the Match and sends it in the webhook.
4

Replay the fixture

fixtures/sample.json holds one article and five evaluations with the decision each one should get:
5

Certify it

Besides your fixture’s expected decisions, the Contract Runner checks that replaying a batch gives the same decisions and that reversing the order of a batch does not change them.
6

Pin it and use it

Pin the plugin on the deployment’s api and worker processes: the API calls it too, to preview alerts. A Subscription then names it by id and version:

Check it worked

Preview an alert before saving it. POST /v0/subscription-previews runs your rule on the most recent articles of a Corpus and returns what it would match, without saving or sending anything:
Then create the Saved Query and the Subscription as in the Quickstart, with your evaluator.