Ragas Ragas
stable · 中文译文

Rubric-Based Evaluation(基于量规的评估)

基于量规的评估指标允许你使用自定义评分标准评估 LLM 回答。Ragas 提供两类量规指标:

  1. DomainSpecificRubrics:对数据集中的所有样本使用同一量规(在初始化时设置)
  2. InstanceSpecificRubrics:每个样本可以有自己独特的量规(每次评估时传入)

量规由每个分数的描述组成,通常范围为 1 到 5。回答由 LLM 根据量规中指定的描述进行评估和打分。

Domain-Specific Rubrics(领域特定量规)

当你想对所有样本应用相同的评估标准时,使用 DomainSpecificRubrics。这适用于评分标准保持不变的领域范围评估。

示例

from openai import AsyncOpenAI
from ragas.llms.base import llm_factory
from ragas.metrics.collections import DomainSpecificRubrics

# Setup
client = AsyncOpenAI()
llm = llm_factory("gpt-4o-mini", client=client)

# Reference-free evaluation (default)
metric = DomainSpecificRubrics(llm=llm)
result = await metric.ascore(
    user_input="What's the longest river in the world?",
    response="The longest river in the world is the Nile, stretching approximately 6,650 kilometers through northeastern Africa.",
)
print(f"Score: {result.value}, Feedback: {result.reason}")

# Reference-based evaluation
metric_with_ref = DomainSpecificRubrics(llm=llm, with_reference=True)
result = await metric_with_ref.ascore(
    user_input="What's the longest river in the world?",
    response="The longest river in the world is the Nile.",
    reference="The Nile is a major north-flowing river in northeastern Africa.",
)

自定义量规

你可以定义自己的量规来定制评分标准:

from ragas.metrics.collections import DomainSpecificRubrics

my_custom_rubrics = {
    "score1_description": "Answer and ground truth are completely different",
    "score2_description": "Answer and ground truth are somewhat different",
    "score3_description": "Answer and ground truth are somewhat similar",
    "score4_description": "Answer and ground truth are similar",
    "score5_description": "Answer and ground truth are exactly the same",
}

metric = DomainSpecificRubrics(llm=llm, rubrics=my_custom_rubrics, with_reference=True)

配合检索上下文

该指标还支持使用检索到的上下文进行评估:

result = await metric.ascore(
    user_input="What's the longest river in the world?",
    response="Based on the context, the Nile is the longest river.",
    retrieved_contexts=[
        "Scientists debate whether the Amazon or the Nile is the longest river.",
        "The Nile River was central to Ancient Egyptians' wealth and power.",
    ],
)

便捷类

为了意图更清晰,使用便捷类:

from ragas.metrics.collections import (
    RubricsScoreWithoutReference,
    RubricsScoreWithReference,
)

# Reference-free
metric_no_ref = RubricsScoreWithoutReference(llm=llm)

# Reference-based
metric_with_ref = RubricsScoreWithReference(llm=llm)

默认量规

无 Reference 量规(默认)

分数 描述
1 The response is entirely incorrect and fails to address any aspect of the user input.
2 The response contains partial accuracy but includes major errors or significant omissions.
3 The response is mostly accurate but lacks clarity, thoroughness, or minor details.
4 The response is accurate and clear, with only minor omissions or slight inaccuracies.
5 The response is completely accurate, clear, and thoroughly addresses the user input.

基于 Reference 的量规

分数 描述
1 The response is entirely incorrect, irrelevant, or does not align with the reference.
2 The response partially matches the reference but contains major errors or omissions.
3 The response aligns with the reference overall but lacks sufficient detail or clarity.
4 The response is mostly accurate, aligns closely with the reference with minor issues.
5 The response is fully accurate, completely aligns with the reference, clear and detailed.

Instance-Specific Rubrics(实例特定量规)

当你的不同样本需要不同评估标准时,使用 InstanceSpecificRubrics。这在以下情况很有用:

  • 不同问题需要不同的评估标准
  • 你想根据特定任务需求定制评分
  • 评估标准在你的数据集中各不相同

示例

from openai import AsyncOpenAI
from ragas.llms.base import llm_factory
from ragas.metrics.collections import InstanceSpecificRubrics

# Setup
client = AsyncOpenAI()
llm = llm_factory("gpt-4o-mini", client=client)

metric = InstanceSpecificRubrics(llm=llm)

# Each sample can have its own rubrics
email_rubrics = {
    "score1_description": "The email is unprofessional or inappropriate",
    "score2_description": "The email lacks proper formatting or tone",
    "score3_description": "The email is acceptable but could be improved",
    "score4_description": "The email is professional with minor issues",
    "score5_description": "The email is highly professional and well-written",
}

result = await metric.ascore(
    user_input="Write a professional email declining a meeting invitation",
    response="Dear John, Thank you for the invitation...",
    rubrics=email_rubrics,
)
print(f"Score: {result.value}, Feedback: {result.reason}")

# Different rubrics for a different type of task
code_rubrics = {
    "score1_description": "The code doesn't work or has critical bugs",
    "score2_description": "The code has significant issues or is poorly structured",
    "score3_description": "The code works but lacks optimization or best practices",
    "score4_description": "The code is good with minor improvements possible",
    "score5_description": "The code is excellent, efficient, and follows best practices",
}

result = await metric.ascore(
    user_input="Write a function to sort a list",
    response="def sort_list(arr): return sorted(arr)",
    rubrics=code_rubrics,
)

配合 Reference 和上下文

result = await metric.ascore(
    user_input="Explain the water cycle",
    response="The water cycle involves evaporation, condensation, and precipitation.",
    reference="The water cycle describes how water evaporates from surfaces, rises into the atmosphere, condenses into clouds, and falls as precipitation.",
    retrieved_contexts=["Water cycle information from encyclopedia..."],
    rubrics={
        "score1_description": "Explanation is completely wrong",
        "score2_description": "Explanation has major inaccuracies",
        "score3_description": "Explanation is partially correct",
        "score4_description": "Explanation is mostly correct",
        "score5_description": "Explanation is comprehensive and accurate",
    },
)

旧版 API

已弃用

以下旧版 API 已弃用。请改用 ragas.metrics.collections.DomainSpecificRubrics 或 ragas.metrics.collections.InstanceSpecificRubrics。

from ragas import evaluate
from datasets import Dataset

from ragas.metrics import rubrics_score_without_reference, rubrics_score_with_reference

rows = {
    "question": [
        "What's the longest river in the world?",
    ],
    "ground_truth": [
        "The Nile is a major north-flowing river in northeastern Africa.",
    ],
    "answer": [
        "The longest river in the world is the Nile, stretching approximately 6,650 kilometers (4,130 miles) through northeastern Africa.",
    ],
    "contexts": [
        [
            "Scientists debate whether the Amazon or the Nile is the longest river in the world.",
            "The Nile River was central to the Ancient Egyptians' rise to wealth and power.",
        ],
    ]
}

dataset = Dataset.from_dict(rows)

result = evaluate(
    dataset,
    metrics=[
        rubrics_score_without_reference,
        rubrics_score_with_reference
    ],
)

使用旧版 API 的自定义量规:

from ragas.metrics._domain_specific_rubrics import RubricsScore

my_custom_rubrics = {
    "score1_description": "answer and ground truth are completely different",
    "score2_description": "answer and ground truth are somewhat different",
    "score3_description": "answer and ground truth are somewhat similar",
    "score4_description": "answer and ground truth are similar",
    "score5_description": "answer and ground truth are exactly the same",
}

rubrics_score = RubricsScore(rubrics=my_custom_rubrics)