# -*- coding: utf-8 -*-"""The base class for _metric in evaluation."""fromabcimportABC,abstractmethodfromdataclassesimportdataclass,fieldfromenumimportEnumfromtypingimportAnyfrom.._utils._commonimport_get_timestampfrom.._utils._mixinimportDictMixinfrom..typesimportJSONSerializableObject
[文档]@dataclassclassMetricResult(DictMixin):"""The result of a _metric."""name:str"""The metric name."""result:str|float|int"""The metric result."""created_at:str=field(default_factory=_get_timestamp)"""The timestamp when the metric result was created."""message:str|None=field(default_factory=lambda:None)"""An optional message for the metric result, can be used to provide additional information or context about the result."""metadata:dict[str,JSONSerializableObject]|None=field(default=None)"""Optional metadata for the metric result, can be used to store additional information related to the metric result."""
[文档]classMetricType(str,Enum):"""The metric type enum."""CATEGORY="category""""The metric result is a category, e.g. "pass" or "fail"."""NUMERICAL="numerical""""The metric result is a numerical value, e.g. 0.95 or 100."""
[文档]classMetricBase(ABC):"""The base class for _metric in evaluation."""
[文档]def__init__(self,name:str,metric_type:MetricType,description:str|None=None,categories:list[str]|None=None,)->None:"""Initialize the _metric object. Args: name (`str`): The name of the metric. metric_type (`MetricType`): The type of the metric, can be either "category" or "numerical", which will determine how to display the result. description (`str`): The description of the metric. categories (`list[str] | None`, optional): The candidate categories. If `metric_type` is "category", the categories must be provided, otherwise it should be `None`. """self.name=nameself.metric_type=metric_typeself.description=descriptionifmetric_type==MetricType.CATEGORYandcategoriesisNone:raiseValueError("Categories must be provided for category metrics.",)self.categories=categories
[文档]@abstractmethoddef__call__(self,*args:Any,**kwargs:Any,)->MetricResult:"""The call function to calculate the _metric result"""