Commit 17a4d0b
authored
feat: online evaluation rules example (SDK & REST) (#33)
## What
New `scripts/online_eval_rules/` example: create and manage Opik
**online evaluation rules** (automation rule evaluators) from code —
Python SDK and raw REST side by side, across every common rule type.
## Why
Online eval rules auto-score traces/threads/spans as they arrive
(LLM-as-judge or user-defined Python metric). This example provisions
and manages them programmatically (project onboarding, CI, bulk setup)
instead of clicking through the UI.
## Highlights
- **One payload, two surfaces** — `build_payload()` produces the JSON
the REST API accepts; `via_sdk()` maps it to the typed
`AutomationRuleEvaluatorWrite_*` union, `via_rest()` sends it with
`requests`. `--surface` picks the live path.
- **Dry-run shows both** — prints the SDK call *and* an equivalent
`curl` from the same payload, so the two never drift. Runs with no
credentials.
- **Rule types**: `llm_as_judge`, `user_defined_metric_python`,
`trace_thread_llm_as_judge`, `span_llm_as_judge`,
`span_user_defined_metric_python`.
- **Full lifecycle**: create, list, get, update (sampling rate /
enabled), delete.
- Project resolved-or-created by name; actionable error when an LLM
provider key is missing for judge rules.
- Tests in `test_create_online_eval_rules.py` (271 lines).
## Notes
- `update` always uses REST (SDK update-union types differ) — carries
`project_ids` forward so the evaluators PATCH endpoint doesn't 400 on
`"At least one project must be specified"`.
- `.gitignore`: ignore `*.log`.
## Test plan
- `uv run create-online-eval-rules create-llm-judge --name relevance
--dry-run` — prints SDK snippet + curl, no creds.
- Live (creds set): create each rule type, then `list` / `get` / `update
--sampling-rate 0.2 --disabled` / `update --enabled` / `delete --yes`.
- `uv run pytest` in `scripts/online_eval_rules/`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)9 files changed
Lines changed: 918 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
0 commit comments