你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Retrieval-Augmented 代(RAG)评估程序

Retrieval-Augmented 生成 (RAG) 系统尝试生成与地面文档一致的最相关的答案,以响应用户的查询。 概括而言,用户的查询会触发基于文档的语料库中的搜索检索,以便为 AI 模型提供地面上下文以生成响应。 评估非常重要:

这些计算器涉及三个方面:

  • 检索结果与用户查询的相关性:如果具有特定于查询的文档相关性的标签,请使用 文档检索 ,或者查询相关性判断(qrels)进行更准确的度量。 如果只有检索的上下文,但没有此类标签,并且对更细粒度的度量具有更高的容忍度,请使用 检索
  • 与基础文档相关的生成的响应的一致性:如果要自定义开源大型语言模型判断(LLM-judge)提示中基础性的定义,请使用 “基础 性”。 如果需要简单的定义,请使用 Groundedness Pro
  • 最终响应与查询的相关性:如果没有基本事实,请使用 相关性 。 如果有基本事实,并且不希望响应错过关键信息,请使用 响应完整性

思考基础性和响应完整性的好方法是:

  • 基础性与响应的 精度 方面有关。 它不应在地面上下文之外包含内容。
  • 响应完整性与 响应的召回 方面有关。 与预期的响应或基本事实相比,它不应错过关键信息。

AI 辅助评估器的模型配置

为了在以下代码片段中参考,AI 辅助质量评估器(除了“基础性专业”除外)使用 LLM 判断的模型配置:

import os
from azure.ai.evaluation import AzureOpenAIModelConfiguration
from dotenv import load_dotenv
load_dotenv()

model_config = AzureOpenAIModelConfiguration(
    azure_endpoint=os.environ["AZURE_ENDPOINT"],
    api_key=os.environ.get("AZURE_API_KEY"),
    azure_deployment=os.environ.get("AZURE_DEPLOYMENT_NAME"),
    api_version=os.environ.get("AZURE_API_VERSION"),
)

计算器模型支持

评估程序支持 LLM 判断的 AzureOpenAI 或 OpenAI 推理模型 和非推理模型,具体取决于评估者:

Evaluators 推理模型作为法官 (示例:Azure OpenAI /OpenAI 中的 o 系列模型) 非推理模型作为法官 (示例: gpt-4.1, gpt-4o) 若要为
Intent ResolutionTask AdherenceTool Call AccuracyResponse Completeness Supported Supported 在初始化计算器时设置其他参数is_reasoning_model=True
其他质量评估程序 不支持 Supported --

对于需要优化推理的复杂评估,我们建议使用具有推理性能和成本效益的平衡的强推理模型,例如 o3-mini 之后发布的 o 系列微型模型。

Retrieval

由于它在 RAG 中的上游角色,检索质量非常重要。 如果检索质量不佳,并且响应需要特定于语料库的知识,则语言模型提供令人满意的答案的可能性较小。 RetrievalEvaluator 使用语言模型测量检索结果 的文本质量 ,而无需基本事实,也称为 查询相关性判断

此事实与需要基本事实的度量值、度量fidelityndcgxdcg指标和其他经典信息检索指标相比DocumentRetrievalEvaluator提供价值。 此指标侧重于上下文区块与处理查询的相关性,以及如何在列表顶部显示最相关的上下文区块。 上下文区块编码为字符串。

检索示例

from azure.ai.evaluation import RetrievalEvaluator

retrieval = RetrievalEvaluator(model_config=model_config, threshold=3)
retrieval(
    query="Where was Marie Curie born?", 
    context="Background: 1. Marie Curie was born in Warsaw. 2. Marie Curie was born on November 7, 1867. 3. Marie Curie is a French scientist. ",
)

检索输出

Likert 刻度的数字分数(整数 1 到 5)。 分数越高越好。 给定数值阈值(已设置默认值),计算器还会在分数 >= 阈值时输出传递,否则将失败。 使用原因字段可帮助你了解分数高或低的原因。

{
    "retrieval": 5.0,
    "gpt_retrieval": 5.0,
    "retrieval_reason": "The context contains relevant information that directly answers the query about Marie Curie's birthplace, with the most pertinent information placed at the top. Therefore, it fits the criteria for a high relevance score.",
    "retrieval_result": "pass",
    "retrieval_threshold": 3
}

文档检索

由于它在 RAG 中的上游角色,检索质量非常重要。 如果检索质量不佳,并且响应需要特定于语料库的知识,则语言模型提供令人满意的答案的可能性较小。 请务必用于 DocumentRetrievalEvaluator 评估检索质量,同时优化 RAG 的搜索参数。

  • 文档检索计算器度量 RAG 如何从文档存储中检索正确的文档。 作为对具有基本事实的 RAG 方案的复合计算器,它计算用于调试 RAG 管道的有用搜索质量指标的列表:

    Metric Category Description
    Fidelity 搜索保真度 前 n 个检索的区块如何反映给定查询的内容:从数据集中已知良好文档总数返回的优秀文档数
    NDCG 搜索 NDCG 排名对列表顶部所有相关项的理想顺序有多好
    XDCG 搜索 XDCG 无论对其他索引文档的评分如何,结果在 top-k 文档中有多好
    最大相关性 N 搜索最大相关性 top-k 区块中的最大相关性
    Holes 搜索标签健全性 缺少查询相关性判断或基本事实的文档数
  • 若要在名为 参数扫描的方案中优化 RAG,可以使用这些指标来校准搜索参数以获得最佳的 RAG 结果。 为各种搜索参数(例如搜索算法(矢量、语义)、top_k和想要测试的区块大小等各种搜索参数生成不同的检索结果。 然后,用于 DocumentRetrievalEvaluator 查找产生最高检索质量的搜索参数。

文档检索示例

from azure.ai.evaluation import DocumentRetrievalEvaluator

# These query_relevance_labels are given by your human- or LLM-judges.
retrieval_ground_truth = [
    {
        "document_id": "1",
        "query_relevance_label": 4
    },
    {
        "document_id": "2",
        "query_relevance_label": 2
    },
    {
        "document_id": "3",
        "query_relevance_label": 3
    },
    {
        "document_id": "4",
        "query_relevance_label": 1
    },
    {
        "document_id": "5",
        "query_relevance_label": 0
    },
]
# The min and max of the label scores are inputs to document retrieval evaluator
ground_truth_label_min = 0
ground_truth_label_max = 4

# These relevance scores come from your search retrieval system
retrieved_documents = [
    {
        "document_id": "2",
        "relevance_score": 45.1
    },
    {
        "document_id": "6",
        "relevance_score": 35.8
    },
    {
        "document_id": "3",
        "relevance_score": 29.2
    },
    {
        "document_id": "5",
        "relevance_score": 25.4
    },
    {
        "document_id": "7",
        "relevance_score": 18.8
    },
]

document_retrieval_evaluator = DocumentRetrievalEvaluator(
    # Specify the ground truth label range
    ground_truth_label_min=ground_truth_label_min, 
    ground_truth_label_max=ground_truth_label_max,
    # Optionally override the binarization threshold for pass/fail output
    ndcg_threshold = 0.5,
    xdcg_threshold = 50.0,
    fidelity_threshold = 0.5,
    top1_relevance_threshold = 50.0,
    top3_max_relevance_threshold = 50.0,
    total_retrieved_documents_threshold = 50,
    total_ground_truth_documents_threshold = 50
)
document_retrieval_evaluator(retrieval_ground_truth=retrieval_ground_truth, retrieved_documents=retrieved_documents)   

文档检索输出

除数值分数和holes_ratio数值分数high_is_better=Trueholes,还有high_is_better=False。 给定数值阈值(默认值为 3),如果评分 >= 阈值或失败,计算器也会输出传递

{
    "ndcg@3": 0.6461858173,
    "xdcg@3": 37.7551020408,
    "fidelity": 0.0188438199,
    "top1_relevance": 2,
    "top3_max_relevance": 2,
    "holes": 30,
    "holes_ratio": 0.6000000000000001,
    "holes_higher_is_better": False,
    "holes_ratio_higher_is_better": False,
    "total_retrieved_documents": 50,
    "total_groundtruth_documents": 1565,
    "ndcg@3_result": "pass",
    "xdcg@3_result": "pass",
    "fidelity_result": "fail",
    "top1_relevance_result": "fail",
    "top3_max_relevance_result": "fail",
    # Omitting more fields ...
}

Groundedness

评估响应与上下文的关系非常重要。 AI 模型可以捏造内容或生成不相关的响应。 GroundednessEvaluator 度量生成的响应与给定上下文(基于基础源)的对齐程度,并且不会在上下文之外制造内容。

此指标捕获与地面源的响应对齐 的精度 方面。 较低分数意味着响应与查询无关,或者上下文外部捏造不准确的内容。 此指标是 ResponseCompletenessEvaluator互补的,它捕获响应与预期响应的 召回 方面。

基础性示例

from azure.ai.evaluation import GroundednessEvaluator

groundedness = GroundednessEvaluator(model_config=model_config, threshold=3)
groundedness(
    query="Is Marie Curie is born in Paris?", 
    context="Background: 1. Marie Curie is born on November 7, 1867. 2. Marie Curie is born in Warsaw.",
    response="No, Marie Curie is born in Warsaw."
)

地面输出

Likert 刻度的数字分数(整数 1 到 5)。 分数越高越好。 给定数值阈值(默认值为 3),如果评分 >= 阈值或失败,计算器也会输出传递。 使用原因字段了解分数高或低的原因。

{
    "groundedness": 5.0,  
    "gpt_groundedness": 5.0,
    "groundedness_reason": "The RESPONSE accurately answers the QUERY by confirming that Marie Curie was born in Warsaw, which is supported by the CONTEXT. It does not include any irrelevant or incorrect information, making it a complete and relevant answer. Thus, it deserves a high score for groundedness.",
    "groundedness_result": "pass", 
    "groundedness_threshold": 3
}

有据性 Pro

AI 系统可以在给定上下文之外捏造内容或生成无关的响应。 由 Azure AI 内容安全提供支持, GroundednessProEvaluator 检测生成的文本响应在 Retrieval-Augmented 生成问答方案中的给定上下文是一致的还是准确的。 它检查响应是否紧贴在上下文中,以便回答查询,避免猜测或制造。 它输出二进制标签。

Groundedness Pro 示例

from azure.ai.evaluation import GroundednessProEvaluator
from azure.identity import DefaultAzureCredential
import os
from dotenv import load_dotenv
load_dotenv()

## Using Azure AI Foundry Hub
azure_ai_project = {
    "subscription_id": os.environ.get("AZURE_SUBSCRIPTION_ID"),
    "resource_group_name": os.environ.get("AZURE_RESOURCE_GROUP"),
    "project_name": os.environ.get("AZURE_PROJECT_NAME"),
}
## Using Azure AI Foundry Development Platform, example: AZURE_AI_PROJECT=https://your-account.services.ai.azure.com/api/projects/your-project
azure_ai_project = os.environ.get("AZURE_AI_PROJECT")

groundedness_pro = GroundednessProEvaluator(azure_ai_project=azure_ai_project, credential=DefaultAzureCredential())
groundedness_pro(
    query="Is Marie Curie is born in Paris?", 
    context="Background: 1. Marie Curie is born on November 7, 1867. 2. Marie Curie is born in Warsaw.",
    response="No, Marie Curie is born in Warsaw."
)

Groundedness Pro 输出

如果响应中的所有内容完全位于上下文True中,则标签字段返回布尔分数,否则返回布尔分数False。 使用原因字段详细了解分数背后的判断。

{
    "groundedness_pro_reason": "All Contents are grounded",
    "groundedness_pro_label": True
}

Relevance

AI 模型可以生成与用户查询无关的响应。 评估最终响应非常重要。 若要解决此问题,可以使用 RelevanceEvaluator此方法来衡量响应处理查询的效率。 它基于给定的查询评估响应的准确性、完整性和直接相关性。 更高的分数意味着更好的相关性。

相关性示例

from azure.ai.evaluation import RelevanceEvaluator

relevance = RelevanceEvaluator(model_config=model_config, threshold=3)
relevance(
    query="Is Marie Curie is born in Paris?", 
    response="No, Marie Curie is born in Warsaw."
)

相关性输出

Likert 刻度的数字分数(整数 1 到 5)。 分数越高越好。 给定数值阈值(默认值为 3),如果评分 >= 阈值或失败,计算器也会输出传递。 使用原因字段可帮助你了解分数高或低的原因。

{
    "relevance": 4.0,
    "gpt_relevance": 4.0, 
    "relevance_reason": "The RESPONSE accurately answers the QUERY by stating that Marie Curie was born in Warsaw, which is correct and directly relevant to the question asked.",
    "relevance_result": "pass", 
    "relevance_threshold": 3
}

响应完整性

AI 系统可以在给定上下文之外捏造内容或生成无关的响应。 给定地面真实响应, ResponseCompletenessEvaluator 捕获响应与预期响应一致的 召回 方面。 此计算器是互补的 GroundednessEvaluator,它捕获与地面源的 响应对齐的精度 方面。

响应完整性示例

from azure.ai.evaluation import ResponseCompletenessEvaluator

response_completeness = ResponseCompletenessEvaluator(model_config=model_config, threshold=3)
response_completeness(
    response="Based on the retrieved documents, the shareholder meeting discussed the operational efficiency of the company and financing options.",
    ground_truth="The shareholder meeting discussed the compensation package of the company CEO."
)

响应完整性输出

Likert 刻度的数字分数(整数 1 到 5)。 分数越高越好。 给定数值阈值(默认值为 3),如果评分 >= 阈值或失败,计算器也会输出传递。 使用原因字段了解分数高或低的原因。

{
    "response_completeness": 1,
    "response_completeness_result": "fail",
    "response_completeness_threshold": 3,
    "response_completeness_reason": "The response does not contain any relevant information from the ground truth, which specifically discusses the CEO's compensation package. Therefore, it is considered fully incomplete."
}