Collection
Metric collections and helpers for dynamic per-field evaluation.
Functions:
| Name | Description |
|---|---|
_expand_field_by_key_values |
Expand nested dict-like fields into generated top-level fields. |
Classes:
| Name | Description |
|---|---|
MetricCollection |
Aggregate multiple child metrics behind one metric interface. |
MetricCollectionWithFieldDiscoveryAndGrouping |
Lazily create per-field metrics while discovering or expanding fields. |
MetricCollection(metrics=None, sort_fields=False)
Bases: Metric, Generic[T]
A metric that aggregates multiple sub-metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
dict[str, T] | None
|
Optional mapping of metric names to metric instances. |
None
|
sort_fields
|
bool
|
Whether computed results should be emitted in sorted field order. |
False
|
Source code in src/kibad_llm/metrics/collection.py
25 26 27 28 29 30 31 32 33 34 | |
add_metric(name, metric)
Adds a new metric to the collection.
Source code in src/kibad_llm/metrics/collection.py
36 37 38 39 40 | |
reset()
Resets all sub-metrics.
Source code in src/kibad_llm/metrics/collection.py
42 43 44 45 | |
MetricCollectionWithFieldDiscoveryAndGrouping(metric_class, fields=None, subfield_keys=None, subfield_values=None, sort_fields=False, field_overrides=None, **kwargs)
Bases: MetricCollection[T2], Generic[T2]
A metric collection that discovers fields dynamically and can group nested entries.
This collection creates per-field metrics lazily during _update. Fields can either
be taken from an explicit allowlist or, on each update, discovered from the union of
prediction and reference keys. Additionally, configured dict-like fields can be expanded
into generated top-level fields such as field.A&B before the underlying single-field
metrics are updated. During that expansion, the configured grouping keys are used to derive
the generated field names and are removed from the scored payload, while subfield_values
can optionally restrict which of the remaining nested values are compared. Additional keyword
arguments passed to __init__ are forwarded to each lazily created per-field metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_class
|
type[T2]
|
Metric class used to instantiate field-specific metrics. |
required |
fields
|
list[str] | None
|
Optional allowlist of fields to evaluate. If omitted, fields are discovered from the union of keys present in each prediction/reference pair. |
None
|
subfield_keys
|
dict[str, list[str]] | None
|
Optional mapping describing how nested entries are split into generated fields. |
None
|
subfield_values
|
dict[str, list[str]] | None
|
Optional mapping restricting which nested values are kept after field expansion. |
None
|
sort_fields
|
bool
|
Whether computed results should be emitted in sorted field order. |
False
|
field_overrides
|
dict[str, dict[str, Any]] | None
|
Optional mapping of field names to keyword arguments for each per-field metric. |
None
|
**kwargs
|
Additional keyword arguments forwarded to each created metric instance. |
{}
|
Source code in src/kibad_llm/metrics/collection.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |