Source code for langchain.evaluation.exact_match.base

import string
from typing import Any, List

from langchain.evaluation.schema import StringEvaluator


[docs]class ExactMatchStringEvaluator(StringEvaluator): """计算预测和参考之间的精确匹配。 示例 ---------- >>> evaluator = ExactMatchChain() >>> evaluator.evaluate_strings( prediction="Mindy is the CTO", reference="Mindy is the CTO", ) # 这将返回 {'score': 1.0} >>> evaluator.evaluate_strings( prediction="Mindy is the CTO", reference="Mindy is the CEO", ) # 这将返回 {'score': 0.0}"""
[docs] def __init__( self, *, ignore_case: bool = False, ignore_punctuation: bool = False, ignore_numbers: bool = False, **kwargs: Any, ): super().__init__() self.ignore_case = ignore_case self.ignore_punctuation = ignore_punctuation self.ignore_numbers = ignore_numbers
@property def requires_input(self) -> bool: """ 这个评估器不需要输入。 """ return False @property def requires_reference(self) -> bool: """ 这个评估器需要一个参考。 """ return True @property def input_keys(self) -> List[str]: """获取输入键。 返回: List[str]:输入键。 """ return ["reference", "prediction"] @property def evaluation_name(self) -> str: """获取评估名称。 返回: str:评估名称。 """ return "exact_match" def _evaluate_strings( # type: ignore[arg-type,override] self, *, prediction: str, reference: str, **kwargs: Any, ) -> dict: """评估预测值与参考值之间的精确匹配。 参数: prediction (str): 预测字符串。 reference (Optional[str], optional): 参考字符串。 返回: dict: 包含得分的评估结果。 """ if self.ignore_case: prediction = prediction.lower() reference = reference.lower() if self.ignore_punctuation: prediction = prediction.translate(str.maketrans("", "", string.punctuation)) reference = reference.translate(str.maketrans("", "", string.punctuation)) if self.ignore_numbers: prediction = prediction.translate(str.maketrans("", "", string.digits)) reference = reference.translate(str.maketrans("", "", string.digits)) return {"score": int(prediction == reference)}