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

Retrieval-Augmented 代(RAG)评估程序

Retrieval-Augmented 生成 (RAG) 系统尝试生成与地面文档一致的最相关的答案,以响应用户的查询。 用户的查询在地面文档的语料库中触发搜索检索,以便为 AI 模型提供地面上下文以生成响应。

Evaluator Best practice Use when Purpose Output
Document Retrieval Process evaluation 检索质量是 RAG 的瓶颈,并且具有用于调试和参数优化的精确搜索质量指标的查询相关性标签(地面真相) 通过比较检索到的文档与地面真相标签来度量搜索质量指标(Fidelity、NDCG、XDCG、Max Relevance、Holes) 复合:保真度、NDCG、XDCG、最大相关性、孔(通过/失败)
Retrieval Process evaluation 你想要评估检索上下文的文本质量,但你没有基本事实 度量检索的上下文区块与使用 LLM 法官处理查询的相关性 二进制:基于阈值传递/失败(1-5 缩放)
Groundedness System evaluation 你需要一个圆润的基础性定义,它适用于代理输入,并自带 GPT 模型作为 LLM 判断 测量生成的响应与给定上下文的对齐程度(精度方面) 二进制:基于阈值传递/失败(1-5 缩放)
基础专业版 (预览版) System evaluation 你需要一个由Azure AI 内容安全提供支持的严格基础定义,并使用我们的服务模型 使用 Azure AI 内容安全 服务检测响应是否与上下文严格一致 Binary: True/False
Relevance System evaluation 你希望评估 RAG 响应解决查询的方式,但没有基本事实 度量对查询的响应的准确性、完整性和直接相关性 二进制:基于阈值传递/失败(1-5 缩放)
响应完整性(预览版) System evaluation 你想要确保 RAG 响应不会错过关键信息(召回方面)从地面真相 衡量响应与地面真相相比,响应的完全涵盖预期信息的方式 二进制:基于阈值传递/失败(1-5 缩放)

基础性和响应完整性视为:

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

System evaluation

系统评估会检查 RAG 工作流中最终响应的质量。 这些评估程序确保基于提供的上下文和用户查询,AI 生成的内容准确、相关且完整:

  • 基础性 - 响应是否以所提供的上下文为基础,而不制造?
  • 基础专业 - 响应是否严格遵循上下文(Azure AI 内容安全)?
  • 相关性 - 响应是否准确解决用户的查询问题?
  • 响应完整性 (预览版) - 响应是否涵盖来自根本真相的所有关键信息?

Examples:

Process evaluation

进程评估评估 RAG 系统中文档检索步骤的质量。 检索步骤对于为语言模型提供相关上下文至关重要:

  • 检索 - 检索到的上下文区块与查询有何关联?
  • 文档检索 - 检索与地面真相标签(需要 qrels)的匹配程度如何?

有关更多示例,请参阅所有质量计算器示例

使用 RAG 计算器

RAG 评估器评估 AI 系统检索和使用上下文生成地面响应的方式。 每个计算器都需要特定的数据映射和参数:

Evaluator Required inputs Required parameters
Groundedness responsecontext (建议); query 可选用于增强评分;或者 queryresponse 对于代理响应模式 deployment_name
基础专业版 (预览版) queryresponse、、 context (none)
Relevance queryresponse deployment_name
响应完整性(预览版) ground_truthresponse deployment_name
Retrieval querycontext deployment_name
Document Retrieval retrieval_ground_truthretrieved_documents (none)

Example input

测试数据集应包含数据映射中引用的字段:

{"query": "What are the store hours?", "context": "Our store is open Monday-Friday 9am-6pm and Saturday 10am-4pm.", "response": "The store is open weekdays from 9am to 6pm and Saturdays from 10am to 4pm."}
{"query": "What is the return policy?", "context": "Items can be returned within 30 days with original receipt for full refund.", "response": "You can return items within 30 days if you have your receipt."}

Context format

context 字段是包含提供给模型的检索上下文的纯字符串。 对于多区块检索,请使用分隔符将区块连接成单个字符串,例如 \n\n 在区块之间:

{"query": "What is the return policy?", "context": "Items can be returned within 30 days with receipt.\n\nGift items are eligible for store credit only.", "response": "You can return items within 30 days with your receipt."}

Note

对于代理评估 {{sample.output_items}},如果响应包含工具调用消息,则 context 字段是可选的, 计算器可以从工具调用结果中提取上下文。

Configuration example

数据映射语法:

  • {{item.field_name}} 引用测试数据集中的字段(例如 {{item.query}})。
  • {{sample.output_items}} 引用在评估期间生成或检索的代理响应。 使用代理目标或代理响应数据源进行评估时,请使用此功能。 对于代理评估,如果响应包含工具调用, context 则评估程序可以从工具调用结果中提取上下文是可选的。

Tip

为了获得最佳基础性结果,请提供所有三个字段,queryresponse以及context。 该 query 字段是可选的,但可在可用时提高评分准确性。

testing_criteria = [
    {
        "type": "azure_ai_evaluator",
        "name": "groundedness",
        "evaluator_name": "builtin.groundedness",
        "initialization_parameters": {"deployment_name": model_deployment},
        "data_mapping": {
            "context": "{{item.context}}",
            "response": "{{item.response}}",
        },
    },
    {
        "type": "azure_ai_evaluator",
        "name": "relevance",
        "evaluator_name": "builtin.relevance",
        "initialization_parameters": {"deployment_name": model_deployment},
        "data_mapping": {"query": "{{item.query}}", "response": "{{item.response}}"},
    },
    {
        "type": "azure_ai_evaluator",
        "name": "retrieval",
        "evaluator_name": "builtin.retrieval",
        "initialization_parameters": {"deployment_name": model_deployment},
        "data_mapping": {"query": "{{item.query}}", "context": "{{item.context}}"},
    },
]

有关运行评估和配置数据源的详细信息 ,请参阅 SDK 中的运行评估

Example output

这些评估者将分数从1比5返回,其中1分非常差,5分非常出色。 默认传递阈值为 3。 阈值的分数或高于阈值被视为通过。 关键输出字段:

{
    "type": "azure_ai_evaluator",
    "name": "Groundedness",
    "metric": "groundedness",
    "score": 4,
    "label": "pass",
    "reason": "The response is well-grounded in the provided context without fabricating content.",
    "threshold": 3,
    "passed": true
}

Groundedness Pro 使用 Azure AI 内容安全 服务并返回布尔结果,而不是数值分数:

{
    "type": "azure_ai_evaluator",
    "name": "Groundedness Pro",
    "metric": "groundedness_pro",
    "label": "pass",
    "reason": "The response is strictly consistent with the provided context.",
    "passed": true
}

Document retrieval

由于它在 RAG 中的上游角色,检索质量非常重要。 如果检索质量不佳,并且响应需要特定于语料库的知识,则语言模型提供令人满意的答案的可能性较小。 最精确的度量是使用 document_retrieval 计算器来评估检索质量并优化 RAG 的搜索参数。

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

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

文档检索示例

testing_criteria = [
    {
        "type": "azure_ai_evaluator",
        "name": "document_retrieval",
        "evaluator_name": "builtin.document_retrieval",
        "initialization_parameters": {
            "ground_truth_label_min": 1,  # SDK default: 0
            "ground_truth_label_max": 5,  # SDK default: 4
        },
        "data_mapping": {
            "retrieval_ground_truth": "{{item.retrieval_ground_truth}}",
            "retrieved_documents": "{{item.retrieved_documents}}",
        },
    },
]

每个 retrieval_ground_truth 文档包含人为标记的相关性分数:

retrieval_ground_truth = [
    {"document_id": "1", "query_relevance_label": 4},
    {"document_id": "2", "query_relevance_label": 2},
]

包含 retrieved_documents 来自搜索系统的分数:

retrieved_documents = [
    {"document_id": "2", "relevance_score": 45.1},
    {"document_id": "6", "relevance_score": 35.8},
]

文档检索输出

计算器 document_retrieval 返回多个指标以检索质量:

[
    {
        "type": "azure_ai_evaluator",
        "name": "Document Retrieval",
        "metric": "ndcg@3",
        "score": 0.646,
        "label": "pass",
        "passed": true
    },
    {
        "type": "azure_ai_evaluator",
        "name": "Document Retrieval",
        "metric": "fidelity",
        "score": 0.019,
        "label": "fail",
        "passed": false
    },
    # more metrics...
]

计算器还返回xdcg@3top1_relevancetop3_max_relevanceholesholes_ratio指标。