BaseMetric

class opik.evaluation.metrics.BaseMetric(name: str, track: bool = True)

Bases: ABC

Abstract base class for all metrics. When creating a new metric, you should inherit from this class and implement the abstract methods.

Parameters:
  • name – The name of the metric.

  • track – Whether to track the metric. Defaults to True.

Example

>>> from opik.evaluation.metrics import base_metric, score_result
>>>
>>> class MyCustomMetric(base_metric.BaseMetric):
>>>     def __init__(self, name: str, track: bool = True):
>>>         self.name = name
>>>         self.track = track
>>>
>>>     def score(self, input: str, output: str, **ignored_kwargs: Any):
>>>         # Add you logic here
>>>
>>>         return score_result.ScoreResult(
>>>             value=0,
>>>             name=self.name,
>>>             reason="Optional reason for the score"
>>>         )
abstract score(*args: Any, **kwargs: Any) ScoreResult | List[ScoreResult]

Public method that can be called independently.

async ascore(*args: Any, **kwargs: Any) ScoreResult | List[ScoreResult]

Async public method that can be called independently.