config
Configuration schema & runtime parameters for metrics
| TYPE ALIAS | DESCRIPTION |
|---|---|
PlainDictSchema |
A plain dictionary mapping configuration parameters to their types. |
PydanticSchema |
A |
TypedDictSchema |
A |
| CLASS | DESCRIPTION |
|---|---|
InvalidConfigSchema |
Exception raised when an invalid schema is provided to the |
| FUNCTION | DESCRIPTION |
|---|---|
config_schema |
Decorator for defining the expected schema of a metric's runtime parameters. |
Type Aliases
PlainDictSchema
A plain dictionary mapping configuration parameters to their types.
All parameters defined in a plain dictionary schema are considered required.
Info
The dictionary's values must be type objects that serve as runtime annotations. Make sure you're passing the expected types rather than the parameter values themselves.
TypedDictSchema
TypedDictSchema = type[TypedDictType]
A TypedDict subclass defining configuration parameters and their types.
PydanticSchema
A pydantic.BaseModel subclass.
defining configuration parameters, their types, and optionally validation logic and
default values.
Classes
InvalidConfigSchema
flowchart TD
evallm.metrics.config.InvalidConfigSchema[InvalidConfigSchema]
click evallm.metrics.config.InvalidConfigSchema href "" "evallm.metrics.config.InvalidConfigSchema"
Exception raised when an invalid schema is provided to the @config_schema
decorator.
Functions
config_schema
config_schema(
*, schema: PlainDictSchema | TypedDictSchema | PydanticSchema
) -> Callable[[type[BaseMetric[Any]]], type[BaseMetric[Any]]]
Decorator for defining the expected schema of a metric's runtime parameters.
The specified schema is converted to a pydantic model and set as the value of the
decorated classes'
config_schema class
variable. This can subsequently be used for validating runtime parameters that are
provided on instantiation. (see also:
BaseMetric.config)
Example
Specifying a config schema
...as a plain dictionary:
...as a TypedDict subclass:
from typing import TypedDict, NotRequired
class CustomConfig(TypedDict):
# required parameter
keywords: list[str]
# optional parameter with default value of None
min_keyword_counts: NotRequired[dict[str, int]]
schema: TypedDictSchema = CustomConfig # (2)!
...as a pydantic.BaseModel subclass:
from pydantic import BaseModel, Field, field_validator
class ConfigWithValidation(BaseModel):
# required parameter with validation
keywords: list[str] = Field(..., min_length=1)
# optional parameter with validation
threshold: float | None = Field(default=None, ge=0, le=1)
@field_validator("keywords")
@classmethod
def validate_keywords(cls, keywords: list[str]) -> list[str]:
if not all(k.startswith("foo") for k in keywords):
raise ValueError("All keywords must start with 'foo'.")
return keywords
schema: PydanticSchema = ConfigWithValidation # (3)!
Decorating a metric class with a config schema
@register_metric
@config_schema(schema=schema) # (4)!
class KeywordRatios(BaseMetric[dict[str, float]]):
def evaluate(self, text: str) -> dict[str, float]:
counts: dict[str, int] = {}
for keyword in self.config.get("keywords"): # (5)!
counts[keyword] = text.count(keyword)
ratios: dict[str, float] = {}
for keyword, count in counts.items():
kw_ratio: float = counts[keyword] / sum(counts.values())
if (threshold := self.config.get("threshold")) is not None: # (6)!
if kw_ratio < threshold:
kw_ratio = 0.0
ratios[keyword] = kw_ratio
return self.result(ratios)
- A plain dictionary is the simplest way to define a schema, it will however assume all parameters as required.
- A
TypedDictsubclass allows you to specify optional parameters usingtyping.NotRequiredand provides better readability. - A
pydantic.BaseModelsubclass provides the most flexibility, allowing you to define complex validation logic and default values. The defined model will be used as-is. - The
@config_schemadecorator should be applied before the@register_metricdecorator. - You can access validated configuration values in the metric's
evaluate(...)method viaself.config.get(<parameter_name>). - Remember to appropriately handle parameters that you defined as optional in the schema!
| PARAMETER | DESCRIPTION |
|---|---|
schema
|
The schema to enforce for the decorated metric class. Must be one of the following:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
metric_cls
|
The decorated metric class.
TYPE:
|
-
API Reference
evallmmetrics
Source code in src/evallm/metrics/config.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 | |