QuotedSpansAlignment
是什么: 该指标衡量模型答案中的引语片段(quoted spans)有多大比例原样出现在检索到的来源里。分数范围是 [0, 1],其中 1.0 表示每个引语片段都有证据支持,0.0 表示来源中找不到任何引语片段。
为什么: 用户对精确引语格外信任。当模型引用的事实并不存在于证据中时,可靠性会被削弱。该指标帮助发现引用漂移:答案里的引语短语没有得到支持。
现代 Collections API(推荐)
from ragas.metrics.collections import QuotedSpansAlignment
metric = QuotedSpansAlignment()
result = await metric.ascore(
response='The study found that "machine learning improves accuracy".',
retrieved_contexts=["Machine learning improves accuracy by 15%."]
)
print(f"Score: {result.value}") # 1.0
print(f"Reason: {result.reason}") # "Matched 1/1 quoted spans"
参数:
name:指标名称(默认:"quoted_spans_alignment")casefold:匹配前是否把文本转成小写做归一化(默认:True)min_span_words:一个引语片段的最少词数(默认:3)
输入:
response: str– 包含引语片段的模型回答retrieved_contexts: List[str]– 用于核对的来源段落列表
输出: 一个 MetricResult,包含:
value:[0, 1] 的分数reason:已匹配 / 总片段数的说明
说明:
- 实现会通过折叠空白和转小写来归一化文本。
- 默认忽略短于三个词的片段;可用
min_span_words调整。 - 如果回答中没有找到任何引语片段,分数为 1.0(没有需要核验的内容)。
旧版 API(已弃用)
警告: 旧版
quoted_spans_alignment函数已弃用。 请改用ragas.metrics.collections中的QuotedSpansAlignment。
输入形状:
answers: List[str]– 模型答案列表(长度 N)sources: List[List[str]]– 长度为 N 的列表,每个元素是来源段落列表
输出: 包含以下内容的字典:
{
"citation_alignment_quoted_spans": float, # score in [0,1]
"matched": float, # number of spans found in sources
"total": float # total number of spans considered
}
说明:
- 如果所有答案中都没有找到引语片段,分数定义为 0.0,且
total = 0。