Re:ゼロから始めるML生活

どちらかといえばエミリア派です

RAGの評価をRagasを使ってやってみる

この前はPhoenixを使ってRAGの実験管理をしてみました。 とはいうものの、Phoenixに事前定義された機能で評価をしただけなので、今回改めてRAGアプリケーションの精度評価について考えてみようと思います。

RAGの評価周りでよく知られたツールとしてRagasがありますが、今回はこちらを使いながら評価について勉強してみようと思います。

Ragas

この記事の本題であるRAGの評価について入っていきたいと思います。

github.com

docs.ragas.io

Ragasで用いる評価指標

RagasではRetrievalとGenerationそれぞれで評価指標を定めています。

  • Generation
    • faithfulness: 与えられたコンテキストに対する回答と事実の一貫性
    • answer relevancy: 生成された回答が与えられたプロンプトに対してどれだけ適切であるか
  • Retrieval
    • context precision
    • context recall

Metrics | Ragas より引用

もちろんRagasで考えられている評価指標以外にも考えることはあるかと思いますが、RAGアプリケーションを継続的に改善していくにはRetrievalとGenerationを切り分けて評価を確認することが良さそうに思えます。

基本的な評価指標

Ragasで提供されている指標が今のところ下記のような感じで、この中から必要な評価指標を呼び出して使用します。

  • Generation
    • Faithfulness
    • Answer relevancy
  • Retrieval
    • Context precision
    • Context recall
    • Context relevancy
    • Context entity recall

docs.ragas.io

Faithfulness

This measures the factual consistency of the generated answer against the given context. It is calculated from answer and retrieved context. The answer is scaled to (0,1) range. Higher the better.

与えられたコンテキストに対する生成された答えの事実の一貫性を表します。正解が正しいかではなく、コンテキストと生成された回答との間での一貫性なので注意が必要です。

Answer relevancy

The evaluation metric, Answer Relevancy, focuses on assessing how pertinent the generated answer is to the given prompt. A lower score is assigned to answers that are incomplete or contain redundant information and higher scores indicate better relevancy. This metric is computed using the question, the context and the answer.

生成された回答が与えられたプロンプトにどれだけ適切であるかを表しており、不完全だったり冗長な回答にはスコアが下がるようになっているようです。

Context recall, Context precision

Context Precision is a metric that evaluates whether all of the ground-truth relevant items present in the contexts are ranked higher or not. Ideally all the relevant chunks must appear at the top ranks. This metric is computed using the question, ground_truth and the contexts, with values ranging between 0 and 1, where higher scores indicate better precision.

回答の根拠となるコンテキストの関連項目がすべて上位にランクされているかどうかを評価する指標であり、質問、ground truth、コンテキストを比較して求めているようです。

Context Relevancy

This metric gauges the relevancy of the retrieved context, calculated based on both the question and contexts. The values fall within the range of (0, 1), with higher values indicating better relevancy.

検索されたコンテキストと質問との関連性を測定しているようです。

Context entities recall

This metric gives the measure of recall of the retrieved context, based on the number of entities present in both ground_truths and contexts relative to the number of entities present in the ground_truths alone. Simply put, it is a measure of what fraction of entities are recalled from ground_truths. This metric is useful in fact-based use cases like tourism help desk, historical QA, etc. This metric can help evaluate the retrieval mechanism for entities, based on comparison with entities present in ground_truths, because in cases where entities matter, we need the contexts which cover them.

ground_truthsとコンテキストに存在するエンティティの数に基づいて、ground_truthsだけに存在するエンティティの数に対する、検索されたコンテキストに含まれるエンティティの数を表しているようです。

やってみる

大体評価指標についてわかったので実際にやってみます。

評価

今回はwikipedia からデータを取得して、おそらくLLMだけでは答えられないような質問をしてRAGの評価を指定校と思います。

用意した評価用データセットはこんな感じにしました。

questions = [
    "ゆるキャンの高校はどこ?",
    "ゆるキャンの3期はいつから放送?",
    "ダンジョン飯の主人公は誰ですか?",
    "ダンジョン飯のアニメはいつから放送していますか?",
]

ground_truths = [
    "本栖高校",
    "2024年4月",
    "ライオス・トーデン",
    "2024年1月",
]

# 質問からの文脈と応答の生成
contexts = []
answers = []
for question in questions:
    response = query_engine.query(question)
    contexts.append([x.node.get_content() for x in response.source_nodes])
    answers.append(str(response))

これに対して評価を行うとこのような形になります。

from datasets import Dataset
from ragas import evaluate
from ragas.metrics import (
    faithfulness,
    answer_correctness,
    answer_relevancy,
    context_recall,
    context_precision,
)
# データセットの準備
ds = Dataset.from_dict(
    {
        "question": questions,
        "answer": answers,
        "contexts": contexts,
        "ground_truth": ground_truths,
    }
)

# 評価
result = evaluate(
    ds,
    [faithfulness, answer_correctness, answer_relevancy, context_recall, context_precision]
)
print(result)

出力としてはこんな感じです。

{'faithfulness': 0.7500, 'answer_correctness': 0.5322, 'answer_relevancy': 0.8860, 'context_recall': 0.7500, 'context_precision': 0.8750}

評価用の質問、ground truthのデータを適切に用意する必要はありますが、それさえできれば統一的な方法でRAGの評価ができるということがわかりました。

今回使用したnotebook

参考文献

感想

ドキュメントをそのまま動かすと動かない箇所がたくさんあったので、実際に使うときはソースコードと見比べる必要があるかもしれませんね。 とりあえず、RAGの評価の概念と使い方については大体わかったので、その点では良かったです。