Lighteval 文档

指标

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

指标

指标

Metric

class lighteval.metrics.Metric

< >

( metric_name: str higher_is_better: bool category: MetricCategory use_case: MetricUseCase sample_level_fn: <built-in function callable> corpus_level_fn: <built-in function callable> )

CorpusLevelMetric

class lighteval.metrics.utils.metric_utils.CorpusLevelMetric

< >

( metric_name: str higher_is_better: bool category: MetricCategory use_case: MetricUseCase sample_level_fn: <built-in function callable> corpus_level_fn: <built-in function callable> )

在整个语料库上计算的指标,计算发生在聚合阶段。

SampleLevelMetric

class lighteval.metrics.utils.metric_utils.SampleLevelMetric

< >

( metric_name: str higher_is_better: bool category: MetricCategory use_case: MetricUseCase sample_level_fn: <built-in function callable> corpus_level_fn: <built-in function callable> )

对每个样本计算指标,然后在语料库上进行聚合。

MetricGrouping

class lighteval.metrics.utils.metric_utils.MetricGrouping

< >

( metric_name: list higher_is_better: dict category: MetricCategory use_case: MetricUseCase sample_level_fn: <built-in function callable> corpus_level_fn: dict )

有些指标一次性一起计算更有优势。例如,如果所有指标的预处理成本都很高且相同,那么只计算一次会更有意义。

CorpusLevelMetricGrouping

class lighteval.metrics.utils.metric_utils.CorpusLevelMetricGrouping

< >

( metric_name: list higher_is_better: dict category: MetricCategory use_case: MetricUseCase sample_level_fn: <built-in function callable> corpus_level_fn: dict )

在整个语料库上计算的指标分组,计算发生在聚合阶段。

SampleLevelMetricGrouping

class lighteval.metrics.utils.metric_utils.SampleLevelMetricGrouping

< >

( metric_name: list higher_is_better: dict category: MetricCategory use_case: MetricUseCase sample_level_fn: <built-in function callable> corpus_level_fn: dict )

指标分组在每个样本上计算,然后在语料库上进行聚合。

语料库指标

CorpusLevelF1Score

class lighteval.metrics.metrics_corpus.CorpusLevelF1Score

< >

( average: str num_classes: int = 2 )

compute

< >

( items: list )

通过使用 scikit-learn 实现,计算所有语料库生成项的指标得分。

CorpusLevelPerplexityMetric

class lighteval.metrics.metrics_corpus.CorpusLevelPerplexityMetric

< >

( metric_type: str )

compute

< >

( items: list )

计算所有语料库生成项的指标得分。

CorpusLevelTranslationMetric

class lighteval.metrics.metrics_corpus.CorpusLevelTranslationMetric

< >

( metric_type: str lang: typing.Literal['zh', 'ja', 'ko', ''] = '' )

compute

< >

( items: list )

通过使用 sacrebleu 实现,计算所有语料库生成项的指标得分。

matthews_corrcoef

lighteval.metrics.metrics_corpus.matthews_corrcoef

< >

( items: list ) float

参数

  • items (list[dict]) — GenerativeCorpusMetricInput 列表

返回

float

分数

使用 scikit-learn 计算马修斯相关系数(文档)。

样本指标

ExactMatches

class lighteval.metrics.metrics_sample.ExactMatches

< >

( aggregation_function: typing.Callable[[list[float]], float] = <built-in function max> normalize_gold: typing.Optional[typing.Callable[[str], str]] = None normalize_pred: typing.Optional[typing.Callable[[str], str]] = None strip_strings: bool = False type_exact_match: str = 'full' )

compute

< >

( golds: list predictions: list **kwargs ) float

参数

  • golds (list[str]) — 参考目标
  • predictions (list[str]) — 预测的字符串

返回

float

当前样本项的聚合分数。

针对单个样本的黄金标准列表和预测列表计算指标。

compute_one_item

< >

( gold: str pred: str ) float

参数

  • gold (str) — 可能的参考之一
  • pred (str) — 可能的预测之一

返回

float

精确匹配分数。匹配则为1,否则为0。

仅比较两个字符串。

F1_score

class lighteval.metrics.metrics_sample.F1_score

< >

( aggregation_function: typing.Callable[[list[float]], float] = <built-in function max> normalize_gold: typing.Optional[typing.Callable[[str], str]] = None normalize_pred: typing.Optional[typing.Callable[[str], str]] = None strip_strings: bool = False )

compute

< >

( golds: list predictions: list **kwargs ) float

参数

  • golds (list[str]) — 参考目标
  • predictions (list[str]) — 预测的字符串

返回

float

当前样本项的聚合分数。

针对单个样本的黄金标准列表和预测列表计算指标。

compute_one_item

< >

( gold: str pred: str ) float

参数

  • gold (str) — 可能的参考之一
  • pred (str) — 可能的预测之一

返回

float

基于词袋的 F1 分数,使用 nltk 计算。

仅比较两个字符串。

LoglikelihoodAcc

class lighteval.metrics.metrics_sample.LoglikelihoodAcc

< >

( logprob_normalization: lighteval.metrics.normalizations.LogProbCharNorm | lighteval.metrics.normalizations.LogProbTokenNorm | lighteval.metrics.normalizations.LogProbPMINorm | None = None )

compute

< >

( gold_ixs: list choices_logprob: list unconditioned_logprob: list[float] | None choices_tokens: list[list[int]] | None formatted_doc: Doc **kwargs ) int

参数

  • gold_ixs (list[int]) — 所有正确选项的索引
  • choices_logprob (list[float]) — 模型所有可能选项的对数概率之和,按选项顺序排列。
  • unconditioned_logprob (list[float] | None) — 用于 PMI 归一化的无条件对数概率,按选项顺序排列。
  • choices_tokens (list[list[int]] | None) — 用于 Token 归一化的分词后选项,按选项顺序排列。
  • formatted_doc (Doc) — 样本的原始文档。用于获取原始选项的长度以进行可能的归一化。

返回

int

评估分数:如果最佳对数概率选项在正确答案中,则为1,否则为0。

计算对数似然准确率:`choices_logprob` 中具有最高对数概率的选项是否存在于 `gold_ixs` 中?

NormalizedMultiChoiceProbability

class lighteval.metrics.metrics_sample.NormalizedMultiChoiceProbability

< >

( log_prob_normalization: lighteval.metrics.normalizations.LogProbCharNorm | lighteval.metrics.normalizations.LogProbTokenNorm | lighteval.metrics.normalizations.LogProbPMINorm | None = None aggregation_function: typing.Callable[[numpy.ndarray], float] = <function max at 0x7f25f039e170> )

compute

< >

( gold_ixs: list choices_logprob: list unconditioned_logprob: list[float] | None choices_tokens: list[list[int]] | None formatted_doc: Doc **kwargs ) float

参数

  • gold_ixs (list[int]) — 所有正确选项的索引
  • choices_logprob (list[float]) — 模型所有可能选项的对数概率之和,按选项顺序排列。
  • unconditioned_logprob (list[float] | None) — 用于 PMI 归一化的无条件对数概率,按选项顺序排列。
  • choices_tokens (list[list[int]] | None) — 用于 Token 归一化的分词后选项,按选项顺序排列。
  • formatted_doc (Doc) — 样本的原始文档。用于获取原始选项的长度以进行可能的归一化。

返回

float

最佳对数概率选项是正确选项的概率。

计算对数似然概率:选择最佳选项的机会。

概率 (Probability)

class lighteval.metrics.metrics_sample.Probability

< >

( normalization: lighteval.metrics.normalizations.LogProbTokenNorm | None = None aggregation_function: typing.Callable[[numpy.ndarray], float] = <function max at 0x7f25f039e170> )

compute

< >

( logprobs: list target_tokens: list **kwargs ) float

参数

  • gold_ixs (list[int]) — 所有正确选项的索引
  • choices_logprob (list[float]) — 模型所有可能选项的对数概率之和,按选项顺序排列。
  • unconditioned_logprob (list[float] | None) — 用于 PMI 归一化的无条件对数概率,按选项顺序排列。
  • choices_tokens (list[list[int]] | None) — 用于 Token 归一化的分词后选项,按选项顺序排列。
  • formatted_doc (Doc) — 样本的原始文档。用于获取原始选项的长度以进行可能的归一化。

返回

float

最佳对数概率选项是正确选项的概率。

计算对数似然概率:选择最佳选项的机会。

召回率 (Recall)

class lighteval.metrics.metrics_sample.Recall

< >

( at: int )

compute

< >

( choices_logprob: list gold_ixs: list **kwargs ) int

参数

  • gold_ixs (list[int]) — 所有正确选项的索引
  • choices_logprob (list[float]) — 模型所有可能选项的对数概率之和,按选项顺序排列。

返回

int

得分:如果顶层预测的选项之一是正确的,则为 1,否则为 0。

在请求的深度级别计算召回率:查看 `n` 个最佳预测选项(具有最高的对数概率),并检查其中是否有实际的正确选项。

MRR

class lighteval.metrics.metrics_sample.MRR

< >

( length_normalization: bool = False )

compute

< >

( choices_logprob: list gold_ixs: list formatted_doc: Doc **kwargs ) float

参数

  • gold_ixs (list[int]) — 所有正确选项的索引
  • choices_logprob (list[float]) — 模型所有可能选项的对数概率之和,按选项顺序排列。
  • formatted_doc (Doc) — 样本的原始文档。用于获取原始选项的长度以进行可能的归一化。

返回

float

MRR 分数。

平均倒数排名。衡量选项排名(按正确性排序)的质量。

ROUGE

class lighteval.metrics.metrics_sample.ROUGE

< >

( methods: str | list[str] multiple_golds: bool = False bootstrap: bool = False normalize_gold: <built-in function callable> = None normalize_pred: <built-in function callable> = None aggregation_function: <built-in function callable> = None tokenizer: object = None )

compute

< >

( golds: list predictions: list **kwargs ) float or dict

参数

  • golds (list[str]) — 参考目标
  • predictions (list[str]) — 预测的字符串

返回

float 或 dict

当前样本各项的聚合分数。如果选择了多个 rouge 函数,则返回一个映射名称和分数的字典。

针对单个样本的一系列正确答案和预测计算指标。

BertScore

class lighteval.metrics.metrics_sample.BertScore

< >

( normalize_gold: <built-in function callable> = None normalize_pred: <built-in function callable> = None )

compute

< >

( golds: list predictions: list **kwargs ) dict

参数

  • golds (list[str]) — 参考目标
  • predictions (list[str]) — 预测的字符串

返回

字典

当前样本各项的分数。

使用 bert scorer 计算精确率、召回率和 f1 分数。

抽取性 (Extractiveness)

class lighteval.metrics.metrics_sample.Extractiveness

< >

( normalize_input: <built-in function callable> = <function remove_braces at 0x7f24fcef9870> normalize_pred: <built-in function callable> = <function remove_braces_and_strip at 0x7f24fcef9900> input_column: str = 'text' )

compute

< >

( predictions: list formatted_doc: Doc **kwargs ) dict[str, float]

参数

  • predictions (list[str]) — 预测的字符串,一个长度为 1 的列表。
  • formatted_doc (Doc) — 格式化的文档。

返回

dict[str, float]

抽取性分数。

计算预测的抽取性。

此方法计算单个预测相对于输入文本的覆盖率、密度和压缩分数。

忠实度 (Faithfulness)

class lighteval.metrics.metrics_sample.Faithfulness

< >

( normalize_input: <built-in function callable> = <function remove_braces at 0x7f24fcef9870> normalize_pred: <built-in function callable> = <function remove_braces_and_strip at 0x7f24fcef9900> input_column: str = 'text' )

compute

< >

( predictions: list formatted_doc: Doc **kwargs ) dict[str, float]

参数

  • predictions (list[str]) — 预测的字符串,一个长度为 1 的列表。
  • formatted_doc (Doc) — 格式化的文档。

返回

dict[str, float]

忠实度分数。

计算预测的忠实度。

SummaCZS(Summary Content Zero-Shot)模型与可配置的粒度和模型变体一起使用。

BLEURT

class lighteval.metrics.metrics_sample.BLEURT

< >

( )

compute

< >

( golds: list predictions: list **kwargs ) float

参数

  • golds (list[str]) — 参考目标
  • predictions (list[str]) — 预测的字符串

返回

float

当前样本各项的分数。

使用存储的 BLEURT 评分器计算当前样本的分数。

BLEU

class lighteval.metrics.metrics_sample.BLEU

< >

( n_gram: int )

compute

< >

( golds: list predictions: list **kwargs ) float

参数

  • golds (list[str]) — 参考目标
  • predictions (list[str]) — 预测的字符串

返回

float

当前样本各项的分数。

计算正确答案和每个预测之间的句子级 BLEU,然后取平均值。

字符串距离 (StringDistance)

class lighteval.metrics.metrics_sample.StringDistance

< >

( metric_types: list[str] | str strip_prediction: bool = True )

compute

< >

( golds: list predictions: list **kwargs ) dict

参数

  • golds (list[str]) — 可能的正确答案列表。如果包含多个项目,则只保留第一个。
  • predictions (list[str]) — 预测的字符串。

返回

字典

计算出的不同分数

根据正确答案和预测计算所有请求的指标。

edit_similarity

< >

( s1 s2 )

计算两个字符串列表之间的编辑相似度。

编辑相似度也用于论文 Lee, Katherine, et al. “Deduplicating training data makes language models better.” arXiv preprint arXiv:2107.06499 (2021) 中。

longest_common_prefix_length

< >

( s1: ndarray s2: ndarray )

计算最长公共前缀的长度。

JudgeLLM

class lighteval.metrics.metrics_sample.JudgeLLM

< >

( judge_model_name: str template: typing.Callable process_judge_response: typing.Callable judge_backend: typing.Literal['litellm', 'openai', 'transformers', 'vllm', 'tgi', 'inference-providers'] short_judge_name: str | None = None response_format: BaseModel = None url: str | None = None hf_provider: str | None = None max_tokens: int | None = None )

JudgeLLMMTBench

class lighteval.metrics.metrics_sample.JudgeLLMMTBench

< >

( judge_model_name: str template: typing.Callable process_judge_response: typing.Callable judge_backend: typing.Literal['litellm', 'openai', 'transformers', 'vllm', 'tgi', 'inference-providers'] short_judge_name: str | None = None response_format: BaseModel = None url: str | None = None hf_provider: str | None = None max_tokens: int | None = None )

compute

< >

( predictions: list formatted_doc: Doc **kwargs )

使用大语言模型作为裁判来计算生成任务的分数。生成任务可以是多轮的,最多两轮,在这种情况下,我们返回第1轮和第2轮的分数。同时返回user_prompt和judgement,这些之后会被聚合器忽略。

JudgeLLMMixEval

class lighteval.metrics.metrics_sample.JudgeLLMMixEval

< >

( judge_model_name: str template: typing.Callable process_judge_response: typing.Callable judge_backend: typing.Literal['litellm', 'openai', 'transformers', 'vllm', 'tgi', 'inference-providers'] short_judge_name: str | None = None response_format: BaseModel = None url: str | None = None hf_provider: str | None = None max_tokens: int | None = None )

compute

< >

( sample_ids: list responses: list formatted_docs: list **kwargs )

使用大语言模型作为裁判来计算生成任务的分数。生成任务可以是多轮的,最多两轮,在这种情况下,我们返回第1轮和第2轮的分数。同时返回user_prompt和judgement,这些之后会被聚合器忽略。

MajAtK

class lighteval.metrics.metrics_sample.MajAtK

< >

( k: int normalize_gold: <built-in function callable> = None normalize_pred: <built-in function callable> = None strip_strings: bool = False type_exact_match: str = 'full' )

compute

< >

( golds: list predictions: list **kwargs ) float

参数

  • golds (list[str]) — 参考目标
  • predictions (list[str]) — k个预测字符串

返回

float

当前样本项的聚合分数。

针对单个样本的正确答案和预测列表计算指标。它对模型预测和正确答案进行归一化(如果需要),并从所有可用答案中选择最频繁的答案,然后将其与正确答案进行比较。

LLM-as-a-Judge

JudgeLM

class lighteval.metrics.llm_as_judge.JudgeLM

< >

( model: str templates: typing.Callable process_judge_response: typing.Callable judge_backend: typing.Literal['litellm', 'openai', 'transformers', 'tgi', 'vllm', 'inference-providers'] url: str | None = None api_key: str | None = None max_tokens: int = 512 response_format: BaseModel = None hf_provider: typing.Optional[typing.Literal['black-forest-labs', 'cerebras', 'cohere', 'fal-ai', 'fireworks-ai', 'inference-providers', 'hyperbolic', 'nebius', 'novita', 'openai', 'replicate', 'sambanova', 'together']] = None )

参数

  • model (str) — 模型的名称。
  • templates (Callable) — 一个函数,它会考虑问题、选项、答案和正确答案,并返回裁判的提示。
  • process_judge_response (Callable) — 用于处理裁判响应的函数。
  • judge_backend (Literal[“openai”, “transformers”, “tgi”, “vllm”]) — 裁判的后端。
  • url (str | None) — OpenAI API的URL。
  • api_key (str | None) — OpenAI API的API密钥(OpenAI或HF密钥)。
  • model (str) — 模型的名称。
  • template (Callable) — 一个函数,它会考虑问题、选项、答案和正确答案,并返回裁判的提示。
  • API_MAX_RETRY (int) — API的最大重试次数。
  • API_RETRY_SLEEP (int) — 重试之间的休眠时间。
  • client (OpenAI | None) — OpenAI客户端。
  • pipe (LLM | AutoModel | None) — Transformers或vllm管道。
  • process_judge_response (Callable) — 用于处理裁判响应的函数。
  • url (str | None) — OpenAI API的URL。
  • api_key (str | None) — OpenAI API的API密钥(OpenAI或HF密钥)。
  • backend (Literal[“openai”, “transformers”, “tgi”, “vllm”]) — 裁判的后端

一个表示使用OpenAI或Transformers库评估答案的裁判类。

方法:evaluate_answer:使用OpenAI API或Transformers库评估答案。lazy_load_client:惰性加载OpenAI客户端或Transformers管道。call_api:调用API获取裁判的响应。call_transformers:调用Transformers管道获取裁判的响应。call_vllm:调用VLLM管道获取裁判的响应。

dict_of_lists_to_list_of_dicts

< >

( dict_of_lists )

参数

  • dict_of_lists — 一个字典,其中每个值都是一个列表。所有列表的长度应相同。

将一个列表字典转换为字典列表。

输出列表中的每个字典将包含输入字典中每个列表的一个元素,其键与输入字典相同。

示例

dict_of_lists_to_list_of_dicts({'k': [1, 2, 3], 'k2': ['a', 'b', 'c']}) [{'k': 1, 'k2': 'a'}, {'k': 2, 'k2': 'b'}, {'k': 3, 'k2': 'c'}]

evaluate_answer

< >

( question: str answer: str options: list[str] | None = None gold: str | None = None )

参数

  • questions (list[str]) — 向被评估模型提出的提示
  • answers (list[str]) — 被评估模型给出的答案
  • references (list[str]) — 参考答案列表

使用Transformers或OpenAI API评估答案。

< > 在 GitHub 上更新