Lighteval 文档
指标
并获得增强的文档体验
开始使用
指标
指标
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 )
通过使用 scikit-learn 实现,计算所有语料库生成项的指标得分。
CorpusLevelPerplexityMetric
计算所有语料库生成项的指标得分。
CorpusLevelTranslationMetric
class lighteval.metrics.metrics_corpus.CorpusLevelTranslationMetric
< 来源 >( metric_type: str lang: typing.Literal['zh', 'ja', 'ko', ''] = '' )
通过使用 sacrebleu 实现,计算所有语料库生成项的指标得分。
matthews_corrcoef
lighteval.metrics.metrics_corpus.matthews_corrcoef
< 来源 >( items: list ) → 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
针对单个样本的黄金标准列表和预测列表计算指标。
compute_one_item
< 来源 >( gold: str pred: str ) → float
仅比较两个字符串。
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
针对单个样本的黄金标准列表和预测列表计算指标。
compute_one_item
< 来源 >( gold: str pred: str ) → float
仅比较两个字符串。
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)
compute
< 源代码 >( choices_logprob: list gold_ixs: list **kwargs ) → int
在请求的深度级别计算召回率:查看 `n` 个最佳预测选项(具有最高的对数概率),并检查其中是否有实际的正确选项。
MRR
compute
< 源代码 >( choices_logprob: list gold_ixs: list formatted_doc: Doc **kwargs ) → float
平均倒数排名。衡量选项排名(按正确性排序)的质量。
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
针对单个样本的一系列正确答案和预测计算指标。
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
使用 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]
计算预测的抽取性。
此方法计算单个预测相对于输入文本的覆盖率、密度和压缩分数。
忠实度 (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]
计算预测的忠实度。
SummaCZS(Summary Content Zero-Shot)模型与可配置的粒度和模型变体一起使用。
BLEURT
compute
< 源代码 >( golds: list predictions: list **kwargs ) → float
使用存储的 BLEURT 评分器计算当前样本的分数。
BLEU
compute
< 源代码 >( golds: list predictions: list **kwargs ) → float
计算正确答案和每个预测之间的句子级 BLEU,然后取平均值。
字符串距离 (StringDistance)
class lighteval.metrics.metrics_sample.StringDistance
< source >( metric_types: list[str] | str strip_prediction: bool = True )
compute
< source >( golds: list predictions: list **kwargs ) → dict
根据正确答案和预测计算所有请求的指标。
计算两个字符串列表之间的编辑相似度。
编辑相似度也用于论文 Lee, Katherine, et al. “Deduplicating training data makes language models better.” arXiv preprint arXiv:2107.06499 (2021) 中。
计算最长公共前缀的长度。
JudgeLLM
class lighteval.metrics.metrics_sample.JudgeLLM
< source >( 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
< source >( 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 )
使用大语言模型作为裁判来计算生成任务的分数。生成任务可以是多轮的,最多两轮,在这种情况下,我们返回第1轮和第2轮的分数。同时返回user_prompt和judgement,这些之后会被聚合器忽略。
JudgeLLMMixEval
class lighteval.metrics.metrics_sample.JudgeLLMMixEval
< source >( 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 )
使用大语言模型作为裁判来计算生成任务的分数。生成任务可以是多轮的,最多两轮,在这种情况下,我们返回第1轮和第2轮的分数。同时返回user_prompt和judgement,这些之后会被聚合器忽略。
MajAtK
class lighteval.metrics.metrics_sample.MajAtK
< source >( 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
< source >( golds: list predictions: list **kwargs ) → float
针对单个样本的正确答案和预测列表计算指标。它对模型预测和正确答案进行归一化(如果需要),并从所有可用答案中选择最频繁的答案,然后将其与正确答案进行比较。
LLM-as-a-Judge
JudgeLM
class lighteval.metrics.llm_as_judge.JudgeLM
< source >( 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
< source >( 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
< source >( question: str answer: str options: list[str] | None = None gold: str | None = None )
使用Transformers或OpenAI API评估答案。