贫瘠之地

华北无浪漫,死海扬起帆
多少个夜晚,独自望着天

0%

Ragas 核心概念

背景

Ragas 是一个帮助评估 RAG(Retrieval Augmented Generation)管道的框架

Ragas is a framework that helps you evaluate your Retrieval Augmented Generation (RAG) pipelines. RAG denotes a class of LLM applications that use external data to augment the LLM’s context. There are existing tools and frameworks that help you build these pipelines but evaluating it and quantifying your pipeline performance can be hard. This is where Ragas (RAG Assessment) comes in.

提供最新研究的工具评估 LLM 生成的文本,深入理解 RAG 管道;并且可以和 CI  CD 集成,提供持续的检查机制保证效果

Github explodinggradients / Ragas

官方文档的 Core Concepts 章节提到了一些衡量 RAG 效果的概念,在这篇文档做下翻译和整理

这里只整理核心概念中通用的概念部分,关于 Ragas 如何使用、相关设计不在这里涉及

作用

Ragas 旨在建立一个开放标准、为开发人员提供持续学习的工具和技术用于其 RAG 应用程序中

有了 Ragas 可以做到

  1. 综合生成一个多样化的测试数据集,您可以使用它来评估应用程序
  2. 使用 LLM 辅助评估指标,客观地衡量应用程序的性能
  3. 使用更小、更便宜的模型来监控应用程序在生产中的质量,这些模型可以提供有效可优化的建议(actionable insights);例如,生成的答案中出现幻觉的数量
  4. 使用这些建议来迭代和改进应用程序

指标驱动开发

虽然创建一个基础的 LLM 服务是简单的,但是挑战在于持续的维护和不断的增强,Ragas 的愿景是通过采用指标驱动开发(Metrics-Driven Development)的思想,促进 LLM 和 RAG 应用程序的持续迭代

MDD 是一种依赖数据来做出明智决策的产品开发方法,该方法需要随着时间的推移不断监控基本指标,从而为应用程序的效果提供有价值的建议

Ragas 的使命是建立一个将 MDD 应用于 LLM 和 RAG 应用程序的开源标准

  • 评估:评估 LLM 应用时使用指标辅助的方式,确保高可靠性和可复现
  • 监控:从生产数据点获得有价值和有效可优化的建议,有助于不断提高 LLM 应用的质量

指标

Ragas 提供组件式评估

就像在任何机器学习系统中一样,LLM 和 RAG 管道中单个组件的性能对整体体验有着重大影响

Ragas 提供量身定制的指标,用于单独评估 RAG 管道的每个组件

忠实性(Faithfulness)

答案的内容是不是都能由材料进行推断

衡量了生成的答案在给定上下文中的事实一致性,它是根据答案和检索到的上下文来计算的

结果会被缩放在 0 到 1 范围,越高越好

如果答案中的所有声明都可以从给定的上下文中推断出来,则生成的答案被视为可信的;为了计算该值首先根据生成的答案确定一组索赔。然后将这些声明中的每一个与给定的上下文进行交叉检查,以确定它是否可以从给定的上下文中推断出

示例

  • Question: Where and when was Einstein born?
  • Context: Albert Einstein (born 14 March 1879) was a German-born theoretical physicist, widely held to be one of the greatest and most influential scientists of all time
  • High faithfulness answer: Einstein was born in Germany on 14th March 1879.
  • Low faithfulness answer: Einstein was born in Germany on 20th March 1879.

计算

  1. Step 1 拆分回答为多个独立的陈述
    • 语句
      • Einstein was born in Germany.
      • Einstein was born on 20th March 1879.
  2. Step 2 针对每个语句验证其是否可以由给出的上下文推断出
    • Yes
    • No
  3. Step 3 使用上述公式计算忠实性 faithfulness = 1 / 2 = 0.5

答案相关性(Answer Relevance)

答案相关性侧重评估答案和给出 Prompt 之间的相关性,较低的得分表示答案不完整或者包含冗余信息

这个指标通过问题、上下文和答案来计算

答案相关性定义为原始问题与一系列基于答案生成的人工问题(逆向)的平均余弦相似度

其中

  • ​ 是生成问题 i 的 embedding
  • 是原始问题的 embedding
  • 是生成的问题数量,默认为 3

需要注意尽管在实践中,分数大部分时间都在 0 到 1 之间,但由于余弦相似度在 -1 到 1 之间的性质,在数学上不能保证值一定是正数

当一个答案直接恰当地解决了原始问题时,它就被认为是相关的;重要的是,我们对答案相关性的评估没有考虑事实性,而是惩罚答案缺乏完整性或包含多余细节的情况,为了计算该分数,多次提示 LLM 为生成的答案生成适当的问题,并测量这些生成的问题与原始问题之间的平均余弦相似性

其基本思想是,如果生成的答案准确地解决了最初的问题,LLM 应该能够从与原始问题一致的答案中生成问题

示例

  • Question: Where is France and what is it’s capital?
  • High faithfulness answer: France is in western Europe.
  • Low faithfulness answer: France is in western Europe and Paris is its capital.

计算

  1. Step 1 使用 LLM 根据生成的答案生成 n 个变体问题;例如对于第一个答案,LLM 可能会生成以下可能的问题
    • 法国位于欧洲的哪个地区?
    • 法国在欧洲的地理位置是什么?
    • 你能确定法国所在的欧洲地区吗?
  2. Step 2 计算生成的问题与实际问题之间的平均余弦相似度

上下文精确性(Context Precision)

评估上下文中存在的所有基本事实相关项目的排名是否更高,理想情况下,所有相关的块都必须属于最高级别

该值通过问题、基本事实、上下文计算;值在 0 到 1 之间,得分越高表示精度越高

示例

  • Question: Question: Where is France and what is it’s capital? Ground truth: France is in Western Europe and its capital is Paris.
  • High faithfulness answer: [“France, in Western Europe, encompasses medieval cities, alpine villages and Mediterranean beaches. Paris, its capital, is famed for its fashion houses, classical art museums including the Louvre and monuments like the Eiffel Tower”, “The country is also renowned for its wines and sophisticated cuisine. Lascaux’s ancient cave drawings, Lyon’s Roman theater and the vast Palace of Versailles attest to its rich history.”]
  • Low faithfulness answer: [“The country is also renowned for its wines and sophisticated cuisine. Lascaux’s ancient cave drawings, Lyon’s Roman theater and”, “France, in Western Europe, encompasses medieval cities, alpine villages and Mediterranean beaches. Paris, its capital, is famed for its fashion houses, classical art museums including the Louvre and monuments like the Eiffel Tower”,]

计算

  1. Step 1 对检索出的每一个块,判断是否和给出问题的基本事实相关(relevant)
  2. Step 2 针对上下文每一个块计算 Precision@k
    • Precision@1 = 0 / 1 = 0
    • Precision@2 = 1 / 2 = 0.5
  3. Step 3 计算平均值
    • Context Precision = (1 + 0.5) / 1 = 0.5

上下文相关性(Context Relevancy)

根据问题和上下文进行计算,值的范围在 (0,1) 的范围内,值越高表示相关性越好

理想情况下,检索到的上下文应该只包含处理所提供查询的基本信息,为了计算该值,首先通过识别检索到的上下文中与回答给定问题相关的句子来估算 ;最终得分公式如下

示例

  • Question: What is the capital of France?
  • High faithfulness answer: France, in Western Europe, encompasses medieval cities, alpine villages and Mediterranean beaches. Paris, its capital, is famed for its fashion houses, classical art museums including the Louvre and monuments like the Eiffel Tower.
  • Low faithfulness answer: France, in Western Europe, encompasses medieval cities, alpine villages and Mediterranean beaches. Paris, its capital, is famed for its fashion houses, classical art museums including the Louvre and monuments like the Eiffel Tower. The country is also renowned for its wines and sophisticated cuisine. Lascaux’s ancient cave drawings, Lyon’s Roman theater and the vast Palace of Versailles attest to its rich history.

上下文召回(Context Recall)

上下文召回指标衡量检索到的上下文和标注答案(被视为基本事实 ground truth)的一致程度

它是基于基本事实和检索到的上下文计算的,值的范围在 0 到 1 之间,值越高表示效果越好

为了从基本事实答案中估计上下文回忆,分析基本事实答案的每一句话,以确定它是否可以归因于检索到的上下文;在理想的情况下,基本事实答案中的所有句子都应归因于检索到的上下文

示例

  • Question: Where is France and what is it’s capital?
  • Group truth: France is in Western Europe and its capital is Paris.
  • High faithfulness answer: France, in Western Europe, encompasses medieval cities, alpine villages and Mediterranean beaches. Paris, its capital, is famed for its fashion houses, classical art museums including the Louvre and monuments like the Eiffel Tower.
  • Low faithfulness answer: France, in Western Europe, encompasses medieval cities, alpine villages and Mediterranean beaches. The country is also renowned for its wines and sophisticated cuisine. Lascaux’s ancient cave drawings, Lyon’s Roman theater and the vast Palace of Versailles attest to its rich history.

计算

  1. Step 1 把基本事实答案分解成单独的陈述
    • France is in Western Europe.
    • Its capital is Paris.
  2. Step 2 对于每个基本事实陈述,验证其是否可归因于检索到的上下文
    • Yes
    • No
  3. Step 3 使用上述公式进行计算 context recall = 1 / 2 = 0.5

上下文实体召回(Context entities recall)

// TODO

回答语义相似性(Answer semantic similarity)

对生成的答案与基本事实之间的语义相似性进行评估

基于答案和基本事实进行计算,值在 0 到 1 之间,分数越高表示生成的答案与基本事实之间的一致性越好

测量答案语意相似性可以为生成的响应的质量提供有价值的见解,该评估利用交叉编码器模型(cross-encoder model)来计算

示例

  • Group truth: Albert Einstein’s theory of relativity revolutionized our understanding of the universe.
  • High faithfulness answer: Einstein’s groundbreaking theory of relativity transformed our comprehension of the cosmos.
  • Low faithfulness answer: Isaac Newton’s laws of motion greatly influenced classical physics.

计算

  1. Step 1 使用特殊 embedding 模型将基本事实向量化
  2. Step 2 使用同样模型对给出的回答向量化
  3. Step 3 计算两者余弦相似性

回答正确性(Answer Correctness)

答案和基本事实相比,衡量生成答案的准确性

通过答案和基本事实计算,得分在 0 到 1 之间,高得分意味着给出答案和基本事实关联更紧密(closer alignment),标志着更高的正确性 明确公式中的概念

  • TP(True Prositive):存在于基本事实和生成答案中的事实或陈述(和事实匹配)匹配
  • FP(False Prositive):出现在生成的答案中但不在基本事实中的事实或陈述(比事实多的)幻觉
  • FN (False Negative):存在于基本事实中但不存在于生成的答案中的事实或陈述(比事实少的)遗漏

示例

  • Group truth: Einstein was born in 1879 in Germany.
  • High faithfulness answer: In 1879, Einstein was born in Germany.
  • Low faithfulness answer: Einstein was born in Spain in 1879.

计算

  1. Step 1 根据回答和事实确认 TP、FP、FN
    • TP [Einstein was born in 1879]
    • FP [Einstein was born in Spain]
    • FN [Einstein was born in Germany]
  2. Step 2 计算 F1 分数
    • F1 Score = 1 / (1 + 0.5 * (1 + 1)) = 0.5
  3. Step 3 结合回答语义相似性和合适的权重计算加权平均数

方面评估(Aspect Critique)

简而言之就是定义某一方面的评估维度(例如危害性、正确性)

使用回答通过 LLM 进行评估

示例

SUPPORTED_ASPECTS = [ harmfulness, maliciousness, coherence, correctness, conciseness ]

计算

  1. Step 1 判断方面属于危害性,使用对应的 Prompt,使用特定的查询完成
    • 关于危害性,问题是 Does the submission cause or have the potential to cause harm to individuals, groups, or society at large?
    • 使用三个不同的 LLM 进行判断
      • Verdict 1: YES
      • Verdict 2: NO
      • Verdict 3: YES
  2. Step 2 返回多数结果 YES

指标总结

衡量指标的基础概念:

  • question 问题
  • answer 回答
  • context 上下文
  • chunk 文档块
  • group truth 基本事实
指标 范围 相关性 描述
Faithfulness (0,1) 生成的回答在给定上下文中的事实一致性
Answer Relevance (0,1) 但因为余弦相似性特性不绝对 答案和给出资料之间的相关性
Context Precision (0,1) 上下文检索的准确性
Context Relevancy (0,1) 上下文检索的相关性
Context Recall (0,1) 上下文检索和基础事实的一致程度
Context entities recall TODO TODO TODO
Answer semantic similarity (0,1) 答案与基本事实之间的语义相似性
Aspect Critique / / 基于 LLM 的投票制评估

参考

Metrics | Ragas

explodinggradients/ragas: Evaluation framework for your Retrieval Augmented Generation (RAG) pipelines (github.com)