传统文本相似度指标在RAG评估中的应用

介绍非LLM字符串相似度、BLEU、ROUGE、精确匹配、字符串存在性及CHRF等传统指标,说明它们如何基于编辑距离、n-gram精度与召回衡量生成文本与参考文本的接近程度。

## 传统的 NLP 计量 ## 非 LLM 字符串相似性 `NonLLMStringSimilarity` 使用传统的字符串距离测量方法,例如Levensshtein、Hamming和Jaro,衡量参考和反应之间的相似性。 `response` 页:1 `reference` 文本不依赖大语言模型(LLMs). ribal返回一个在0和1之间的分数,其中1表示反应和引用之间的完美匹配. 这是基于非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()` 方法代替( Y) `.ascore()`: ``` result = scorer.score( reference="The Eiffel Tower is located in Paris.", response="The Eiffel Tower is located in India.", ) ``` ### 配置 您可以从可用的字符串距离测量中选择 `DistanceMeasure`以下是使用汉明距离的例子: ``` 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` 衡量标准用于评估质量 `response` 通过比较它与 `reference`. 它测量反应与基于n-g精度和简洁度的引用的相似性. BLEU分数最初是为了评价机器翻译系统,但也用于其他自然语言处理任务. BLEU分数范围从0到1,其中1表示反应与引用的完美匹配,这是一个基于非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()` 方法代替( Y) `.ascore()`: ``` result = scorer.score( reference="The Eiffel Tower is located in Paris.", response="The Eiffel Tower is located in India.", ) ``` ### 配置 您可以将更多参数传递到下面的 `sacrebleu.corpus_bleu` 函数使用 `kwargs` 参数 : ``` 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` 分数是用来评价自然语言代数质量的一套衡量标准。它测量生成者之间的重叠。 `response` 页:1 `reference` 文本基于n-gram recall,精度,以及F1分数. ROUGE分数范围从0到1,其中1表示反应和引用之间的完美匹配. 这是基于非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()` 方法代替( Y) `.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分数. ``` scorer = RougeScore(rouge_type="rouge1") ``` 你可以改变 `mode` 改为: `precision`, `recall`,或 `fmeasure` 以精度、回忆或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` 如果回复与参考文本完全相同, 则测量检查。 在需要确保生成的回复与预期输出的 word-for-word 匹配的情景中, 这样做很有用 。 例如, 工具调用中的参数等。 如果回复与参考文本完全匹配, 测量返回 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()` 方法代替( Y) `.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()` 方法代替( Y) `.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` 度量衡评价 a 的相似性 `response` 备注a `reference` 使用 **字符 n 克 F 分数**. 与强调精度的BLEU不同,CHRF对两者都有说明. **准确性和召回性**,使其更适合: - 神经丰富的语言 - 以引文或灵活措词作出的答复 CHRF的分数范围从0到1,其中1表示生成的响应与引用之间完美匹配,这是一个非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()` 方法代替( Y) `.ascore()`: ``` result = scorer.score( reference="The Eiffel Tower is located in Paris.", response="The Eiffel Tower is located in India.", ) ``` ### 配置 您可以将更多参数传递到下面的 `sacrebleu.corpus_chrf` 函数使用 `kwargs` 参数 : ``` # Customize character and word order scorer = CHRFScore(kwargs={"char_order": 4, "word_order": 2}) # Customize beta (recall weight) scorer = CHRFScore(kwargs={"beta": 3}) ``` ### 遗留量表 API 以下例子使用遗留的度量衡 API 模式。 对于新的项目,我们建议使用上面显示的基于收集的API 。 折旧时间线 这个API将在0.4版本中进行贬值,并在1.0版本中移除. 请迁移到以上显示的基于收藏的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 ``` 11月 13, 2026 12月 16, 2025 GitHub

查看原始来源