跳转到内容

传统 NLP 指标

非 LLM 字符串相似度

NonLLMStringSimilarity 指标使用传统的字符串距离度量(如 Levenshtein、Hamming 和 Jaro)来衡量参考文本和响应之间的相似度。此指标可用于评估 responsereference 文本的相似度,而无需依赖大型语言模型 (LLM)。该指标返回一个 0 到 1 之间的分数,其中 1 表示响应与参考文本完全匹配。这是一个基于非大语言模型的指标。

示例

from ragas.metrics.collections import NonLLMStringSimilarity, DistanceMeasure

# Create metric (no LLM/embeddings needed)
scorer = NonLLMStringSimilarity(distance_measure=DistanceMeasure.LEVENSHTEIN)

# Evaluate
result = await scorer.ascore(
    reference="The Eiffel Tower is located in Paris.",
    response="The Eiffel Tower is located in India."
)
print(f"NonLLM String Similarity Score: {result.value}")

输出

NonLLM String Similarity Score: 0.8918918918918919

同步用法

如果你偏好同步代码,可以使用 .score() 方法来代替 .ascore()

result = scorer.score(
    reference="The Eiffel Tower is located in Paris.",
    response="The Eiffel Tower is located in India."
)

配置

您可以从 DistanceMeasure 中选择可用的字符串距离度量。以下是使用 Hamming 距离的示例。

scorer = NonLLMStringSimilarity(distance_measure=DistanceMeasure.HAMMING)

可用的距离度量包括: - DistanceMeasure.LEVENSHTEIN(默认) - DistanceMeasure.HAMMING - DistanceMeasure.JARO - DistanceMeasure.JARO_WINKLER

旧版指标 API

以下示例使用旧版指标 API 模式。对于新项目,我们建议使用上面显示的基于集合的 API。

弃用时间表

此 API 将在 0.4 版本中被弃用,并在 1.0 版本中被移除。请迁移到上面显示的基于集合的 API。

使用 SingleTurnSample 的示例

from ragas.dataset_schema import SingleTurnSample
from ragas.metrics._string import NonLLMStringSimilarity

sample = SingleTurnSample(
    response="The Eiffel Tower is located in India.",
    reference="The Eiffel Tower is located in Paris."
)

scorer = NonLLMStringSimilarity()
await scorer.single_turn_ascore(sample)

输出

0.8918918918918919

使用不同距离度量的示例

from ragas.metrics._string import NonLLMStringSimilarity, DistanceMeasure

scorer = NonLLMStringSimilarity(distance_measure=DistanceMeasure.HAMMING)

BLEU 分数

BleuScore 指标通过将 responsereference进行比较来评估其质量。它根据 n-gram 精度和简洁性惩罚来衡量响应与参考文本之间的相似度。BLEU 分数最初是为评估机器翻译系统而设计的,但它也用于其他自然语言处理任务。BLEU 分数范围为 0 到 1,其中 1 表示响应与参考文本完全匹配。这是一个基于非大语言模型的指标。

示例

from ragas.metrics.collections import BleuScore

# Create metric
scorer = BleuScore()

# Evaluate
result = await scorer.ascore(
    reference="The Eiffel Tower is located in Paris.",
    response="The Eiffel Tower is located in India."
)
print(f"BLEU Score: {result.value}")

输出

BLEU Score: 0.7071067811865478

同步用法

如果你偏好同步代码,可以使用 .score() 方法来代替 .ascore()

result = scorer.score(
    reference="The Eiffel Tower is located in Paris.",
    response="The Eiffel Tower is located in India."
)

配置

您可以使用 kwargs 参数将额外的参数传递给底层的 sacrebleu.corpus_bleu 函数。

scorer = BleuScore(kwargs={"smooth_method": "exp"})

旧版指标 API

以下示例使用旧版指标 API 模式。对于新项目,我们建议使用上面显示的基于集合的 API。

弃用时间表

此 API 将在 0.4 版本中被弃用,并在 1.0 版本中被移除。请迁移到上面显示的基于集合的 API。

使用 SingleTurnSample 的示例

from ragas.dataset_schema import SingleTurnSample
from ragas.metrics import BleuScore

sample = SingleTurnSample(
    response="The Eiffel Tower is located in India.",
    reference="The Eiffel Tower is located in Paris."
)

scorer = BleuScore()
await scorer.single_turn_ascore(sample)

输出

0.7071067811865478

ROUGE 分数

RougeScore 分数是一组用于评估自然语言生成质量的指标。它基于 n-gram 召回率、精确率和 F1 分数来衡量生成的 responsereference 文本之间的重叠度。ROUGE 分数范围为 0 到 1,其中 1 表示响应与参考文本完全匹配。这是一个基于非大语言模型的指标。

示例

from ragas.metrics.collections import RougeScore

# Create metric (no LLM/embeddings needed)
scorer = RougeScore(rouge_type="rougeL", mode="fmeasure")

# Evaluate
result = await scorer.ascore(
    reference="The Eiffel Tower is located in Paris.",
    response="The Eiffel Tower is located in India."
)
print(f"ROUGE Score: {result.value}")

输出

ROUGE Score: 0.8571428571428571

同步用法

如果你偏好同步代码,可以使用 .score() 方法来代替 .ascore()

result = scorer.score(
    reference="The Eiffel Tower is located in Paris.",
    response="The Eiffel Tower is located in India."
)

配置

您可以将 rouge_type 更改为 rouge1rougeL,以分别基于 unigram(一元分词)或最长公共子序列来计算 ROUGE 分数。

scorer = RougeScore(rouge_type="rouge1")

您可以将 mode 更改为 precisionrecallfmeasure,以分别基于精确率、召回率或 F1 分数来计算 ROUGE 分数。

scorer = RougeScore(mode="recall")

旧版指标 API

以下示例使用旧版指标 API 模式。对于新项目,我们建议使用上面显示的基于集合的 API。

弃用时间表

此 API 将在 0.4 版本中被弃用,并在 1.0 版本中被移除。请迁移到上面显示的基于集合的 API。

使用 SingleTurnSample 的示例

from ragas.dataset_schema import SingleTurnSample
from ragas.metrics import RougeScore

sample = SingleTurnSample(
    response="The Eiffel Tower is located in India.",
    reference="The Eiffel Tower is located in Paris."
)

scorer = RougeScore()
await scorer.single_turn_ascore(sample)

输出

0.8571428571428571

完全匹配

ExactMatch 指标检查响应是否与参考文本完全相同。在需要确保生成的响应与预期输出逐字匹配的场景中,此指标非常有用。例如,工具调用中的参数等。如果响应与参考文本完全匹配,则该指标返回 1,否则返回 0。

示例

from ragas.metrics.collections import ExactMatch

# Create metric (no LLM/embeddings needed)
scorer = ExactMatch()

# Evaluate
result = await scorer.ascore(
    reference="Paris",
    response="India"
)
print(f"Exact Match Score: {result.value}")

输出

Exact Match Score: 0.0

同步用法

如果你偏好同步代码,可以使用 .score() 方法来代替 .ascore()

result = scorer.score(
    reference="Paris",
    response="India"
)

旧版指标 API

以下示例使用旧版指标 API 模式。对于新项目,我们建议使用上面显示的基于集合的 API。

弃用时间表

此 API 将在 0.4 版本中被弃用,并在 1.0 版本中被移除。请迁移到上面显示的基于集合的 API。

使用 SingleTurnSample 的示例

from ragas.dataset_schema import SingleTurnSample
from ragas.metrics import ExactMatch

sample = SingleTurnSample(
    response="India",
    reference="Paris"
)

scorer = ExactMatch()
await scorer.single_turn_ascore(sample)

输出

0.0

字符串存在性

StringPresence 指标检查响应是否包含参考文本。在需要确保生成的响应包含某些关键字或短语的场景中,此指标非常有用。如果响应包含参考文本,则该指标返回 1,否则返回 0。

示例

from ragas.metrics.collections import StringPresence

# Create metric (no LLM/embeddings needed)
scorer = StringPresence()

# Evaluate
result = await scorer.ascore(
    reference="Eiffel Tower",
    response="The Eiffel Tower is located in India."
)
print(f"String Presence Score: {result.value}")

输出

String Presence Score: 1.0

同步用法

如果你偏好同步代码,可以使用 .score() 方法来代替 .ascore()

result = scorer.score(
    reference="Eiffel Tower",
    response="The Eiffel Tower is located in India."
)

旧版指标 API

以下示例使用旧版指标 API 模式。对于新项目,我们建议使用上面显示的基于集合的 API。

弃用时间表

此 API 将在 0.4 版本中被弃用,并在 1.0 版本中被移除。请迁移到上面显示的基于集合的 API。

使用 SingleTurnSample 的示例

from ragas.dataset_schema import SingleTurnSample
from ragas.metrics import StringPresence

sample = SingleTurnSample(
    response="The Eiffel Tower is located in India.",
    reference="Eiffel Tower"
)
scorer = StringPresence()
await scorer.single_turn_ascore(sample)

输出

1.0

CHRF 分数

ChrfScore 指标使用字符 n-gram F-score 来评估 responsereference 之间的相似度。与强调精确率的 BLEU 不同,CHRF 同时考虑了精确率和召回率,使其更适用于:

  • 形态丰富的语言
  • 带有意译或灵活措辞的响应

CHRF 分数范围为 0 到 1,其中 1 表示生成的响应与参考文本完全匹配。这是一个基于非大语言模型的指标,完全依赖于确定性比较。

from ragas.dataset_schema import SingleTurnSample
from ragas.metrics import ChrfScore

sample = SingleTurnSample(
    response="The Eiffel Tower is located in India.",
    reference="The Eiffel Tower is located in Paris."
)

scorer = ChrfScore()
await scorer.single_turn_ascore(sample)
输出
0.8048