Ragas Ragas
stable · 中文译文

Traditional NLP Metrics(传统 NLP 指标)

Non LLM String Similarity(非 LLM 字符串相似度)

NonLLMStringSimilarity 指标使用传统字符串距离度量(如 Levenshtein、Hamming 和 Jaro)衡量 reference 与 response 之间的相似度。该指标适用于在不依赖大语言模型(LLM)的情况下评估 response 与 reference 文本的相似度。该指标返回 0 到 1 之间的分数,其中 1 表示 response 与 reference 完全匹配。这是一个非 LLM 指标。

示例

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 distance 的示例:

scorer = NonLLMStringSimilarity(distance_measure=DistanceMeasure.HAMMING)

可用的距离度量包括:

  • DistanceMeasure.LEVENSHTEIN(默认)
  • DistanceMeasure.HAMMING
  • DistanceMeasure.JARO
  • DistanceMeasure.JARO_WINKLER

旧版 Metrics API

以下示例使用旧版 metrics API 模式。对于新项目,我们建议使用上文所示的 collections-based API。

弃用时间表

此 API 将在 0.4 版本中弃用,并在 1.0 版本中移除。请迁移到上文所示的 collections-based 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

使用不同 Distance Measure 的示例

from ragas.metrics._string import NonLLMStringSimilarity, DistanceMeasure

scorer = NonLLMStringSimilarity(distance_measure=DistanceMeasure.HAMMING)

BLEU Score

BleuScore 指标用于通过将 response 与 reference 比较来评估质量。它基于 n-gram precision 和 brevity penalty 衡量 response 与 reference 之间的相似度。BLEU score 最初设计用于评估机器翻译系统,但也用于其他自然语言处理任务。BLEU score 范围为 0 到 1,其中 1 表示 response 与 reference 完全匹配。这是一个非 LLM 指标。

示例

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"})

旧版 Metrics API

以下示例使用旧版 metrics API 模式。对于新项目,我们建议使用上文所示的 collections-based API。

弃用时间表

此 API 将在 0.4 版本中弃用,并在 1.0 版本中移除。请迁移到上文所示的 collections-based 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 Score

RougeScore 分数是一组用于评估自然语言生成质量的指标。它基于 n-gram recall、precision 和 F1 score 衡量生成的 response 与 reference 文本之间的重叠。ROUGE score 范围为 0 到 1,其中 1 表示 response 与 reference 完全匹配。这是一个非 LLM 指标。

示例

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 更改为 rouge1 或 rougeL,分别基于 unigram 或最长公共子序列计算 ROUGE score。

scorer = RougeScore(rouge_type="rouge1")

你可以将 mode 更改为 precision、recall 或 fmeasure,分别基于 precision、recall 或 F1 score 计算 ROUGE score。

scorer = RougeScore(mode="recall")

旧版 Metrics API

以下示例使用旧版 metrics API 模式。对于新项目,我们建议使用上文所示的 collections-based API。

弃用时间表

此 API 将在 0.4 版本中弃用,并在 1.0 版本中移除。请迁移到上文所示的 collections-based 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

Exact Match(精确匹配)

ExactMatch 指标检查 response 是否与 reference 文本完全相同。它适用于你需要确保生成的回答与期望输出逐词匹配的场景。例如,工具调用中的参数等。如果 response 与 reference 精确匹配,该指标返回 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"
)

旧版 Metrics API

以下示例使用旧版 metrics API 模式。对于新项目,我们建议使用上文所示的 collections-based API。

弃用时间表

此 API 将在 0.4 版本中弃用,并在 1.0 版本中移除。请迁移到上文所示的 collections-based 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

String Presence(字符串存在性)

StringPresence 指标检查 response 是否包含 reference 文本。它适用于你需要确保生成的回答包含某些关键词或短语的场景。如果 response 包含 reference,该指标返回 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."
)

旧版 Metrics API

以下示例使用旧版 metrics API 模式。对于新项目,我们建议使用上文所示的 collections-based API。

弃用时间表

此 API 将在 0.4 版本中弃用,并在 1.0 版本中移除。请迁移到上文所示的 collections-based 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 Score

CHRFScore 指标使用 character n-gram F-score 评估 response 与 reference 之间的相似度。与强调 precision 的 BLEU 不同,CHRF 同时考虑 precision 和 recall,因此更适合:

  • 形态丰富的语言
  • 带有改写或灵活措辞的回答

CHRF 分数范围为 0 到 1,其中 1 表示生成的回答与 reference 完全匹配。这是一个非 LLM 指标,完全依赖确定性比较。

示例

from ragas.metrics.collections import CHRFScore

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

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

输出:

CHRF Score: 0.8048

同步用法

如果你更喜欢同步代码,可以使用 .score() 方法代替 .ascore():

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

配置

你可以使用 kwargs 参数向底层的 sacrebleu.corpus_chrf 函数传递额外参数:

# Customize character and word order
scorer = CHRFScore(kwargs={"char_order": 4, "word_order": 2})

# Customize beta (recall weight)
scorer = CHRFScore(kwargs={"beta": 3})

旧版 Metrics API

以下示例使用旧版 metrics API 模式。对于新项目,我们建议使用上文所示的 collections-based API。

弃用时间表

此 API 将在 0.4 版本中弃用,并在 1.0 版本中移除。请迁移到上文所示的 collections-based API。

使用 SingleTurnSample 的示例

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