RAG 回答忠实度评估指标
介绍 RAG 评估中的 Faithfulness 指标,它衡量回答与检索上下文在事实层面的一致性,取值 0 到 1,分数越高表示一致性越好;计算时先识别回答中的所有声明,再判断这些声明能否被检索上下文支持。
## 忠诚 ## 忠诚 那个... **忠诚** B. 衡量方法如何在事实上一致 `response` 是和那个 `retrieved context`. 它的范围从0到1,更高的分数表明一致性更好. 答复被考虑进去 **忠诚** 如果它的所有索赔都能够用检索到的上下文作为佐证。 要计算这个: 1 . 确定答复中的所有索赔. 2 . 检查每个索赔是否可从检索的上下文推断出来. 3 使用公式计算忠诚度分数 : \[ \ text{Faithity Scount}} =\ frac\ text{ 被检索的上下文所支持的响应中的索赔数量{ text} 答复中的索赔总数{} \] ### 示例 ``` from openai import AsyncOpenAI from ragas.llms import llm_factory from ragas.metrics.collections import Faithfulness # Setup LLM client = AsyncOpenAI() llm = llm_factory("gpt-4o-mini", client=client) # Create metric scorer = Faithfulness(llm=llm) # Evaluate result = await scorer.ascore( user_input="When was the first super bowl?", response="The first superbowl was held on Jan 15, 1967", retrieved_contexts=[ "The First AFL–NFL World Championship Game was an American football game played on January 15, 1967, at the Los Angeles Memorial Coliseum in Los Angeles." ], ) print(f"Faithfulness Score: {result.value}") ``` 输出 : ``` Faithfulness Score: 1.0 ``` 同步使用 如果你喜欢同步代码,你可以使用 `.score()` 方法代替( Y) `.ascore()`: ``` result = scorer.score( user_input="When was the first super bowl?", response="The first superbowl was held on Jan 15, 1967", retrieved_contexts=[...], ) ``` ### 如何计算 示例 **问题**:爱因斯坦在哪里出生,何时出生? **一. 背景情况**:阿尔伯特·爱因斯坦(出生于14 March 1879)是一位德国出生的理论物理学家,被广泛认为是有史以来最伟大和最有影响力的科学家之一. **忠诚的回答**:爱因斯坦于3月14日出生于德国 1879. **忠诚度低的回答**:爱因斯坦于3月20日出生于德国. 1879. 让我们来检查一下 如何用低忠诚的答案来计算忠诚度: - **Step 1:** 将生成的答案分解为单个语句 。 - 发言: - 声明1:"爱因斯坦出生在德国". - 声明2:"爱因斯坦出生于3月20日1879.". - **Step 2:** 对于每个生成的语句,请检查是否能够从给定上下文推断出来. - 声明 1:是 - 声明 2:无 - **Step 3:** 使用上面描述的公式来计算忠诚度. \[ \text{忠实度} = { \text{1} \over \text{2} } = 0.5 \] ## 遗留量表 API 以下例子使用遗留的度量衡 API 模式。 对于新的项目,我们建议使用上面显示的基于收集的API 。 折旧时间线 这个API将在0.4版本中进行贬值,并在1.0版本中移除. 请迁移到以上显示的基于收藏的API. ### 带有 SingleTurnSample 的示例 ``` from ragas.dataset_schema import SingleTurnSample from ragas.metrics import Faithfulness sample = SingleTurnSample( user_input="When was the first super bowl?", response="The first superbowl was held on Jan 15, 1967", retrieved_contexts=[ "The First AFL–NFL World Championship Game was an American football game played on January 15, 1967, at the Los Angeles Memorial Coliseum in Los Angeles." ], ) scorer = Faithfulness(llm=evaluator_llm) await scorer.single_turn_ascore(sample) ``` 输出 : ``` 1.0 ``` ### 忠于 HHEM-2.1 - 打开 [Vectara 的 HHEM-2.1 - 打开](<https://vectara.com/blog/hhem-2-1-a-better-hallucination-detection-model/>) 是一个分类器模型( T5 ) , 用来检测来自 LLM 生成的文本的幻觉。 这个模型可以在计算忠诚度的第二步中使用, 也就是说, 当债权与特定上下文交叉核对, 以确定是否能够从上下文推断出来。 该模型是自由的, 小型的, 开源的, 这使得它在生产使用案例中非常有效 。 用模型计算忠诚度: ``` from ragas.dataset_schema import SingleTurnSample from ragas.metrics import FaithfulnesswithHHEM sample = SingleTurnSample( user_input="When was the first super bowl?", response="The first superbowl was held on Jan 15, 1967", retrieved_contexts=[ "The First AFL–NFL World Championship Game was an American football game played on January 15, 1967, at the Los Angeles Memorial Coliseum in Los Angeles." ], ) scorer = FaithfulnesswithHHEM(llm=evaluator_llm) await scorer.single_turn_ascore(sample) ``` 您可以通过设置此模式将模型装入指定的设备 `device` 参数并调整批量大小,以便使用 `batch_size` 参数. 默认情况下,该模型被装入CPU上,批量大小为10 : ``` my_device = "cuda:0" my_batch_size = 10 scorer = FaithfulnesswithHHEM(device=my_device, batch_size=my_batch_size) await scorer.single_turn_ascore(sample) ``` 11月 13, 2026 12月 16, 2025 GitHub
查看原始来源