评估数据代理(预览版)

通过使用 Fabric SDK 进行评估,你可以程序化测试数据代理对自然语言问题的响应能力。 通过简单的 Python 接口,你可以定义真实案例、运行评估和分析结果——所有这些都在你的笔记本环境中完成。 这个过程帮助你验证准确性、调试错误,并在部署到生产环境前自信地改进代理。

重要

此功能目前为预览版。

先决条件

安装数据代理 SDK

要开始程序化评估你的 Fabric 数据代理,安装 Fabric 数据代理 Python SDK。 此 SDK 提供了与数据代理交互、运行评估和日志结果所需的工具和方法。 通过在笔记本中运行以下命令安装最新版本:

%pip install -U fabric-data-agent-sdk

此步骤确保 SDK 中提供最新的功能和修补程序。

加载基本事实数据集

要评估你的 Fabric 数据代理,你需要一组样本问题以及预期答案。 利用这些问题验证代理对现实问题的准确回应。

用pandas数据框架直接在代码中定义这些问题:

import pandas as pd

# Define a sample evaluation set with user questions and their expected answers.
# You can modify the question/answer pairs to match your scenario.
df = pd.DataFrame(
    columns=["question", "expected_answer"],
    data=[
        ["Show total sales for Canadian Dollar for January 2013", "46,117.30"],
        ["What is the product with the highest total sales for Canadian Dollar in 2013", "Mountain-200 Black, 42"],
        ["Total sales outside of the US", "19,968,887.95"],
        ["Which product category had the highest total sales for Canadian Dollar in 2013", "Bikes (Total Sales: 938,654.76)"]
    ]
)

或者,如果你已有评估数据集,可以从包含列 question 和 expected_answer的 CSV 文件加载:

# Load questions and expected answers from a CSV file
input_file_path = "/lakehouse/default/Files/Data/Input/curated_2.csv"
df = pd.read_csv(input_file_path)

此数据集用作针对数据代理运行自动评估以评估准确性和覆盖范围的输入。

评估数据代理

下一步是使用函数 evaluate_data_agent 进行评估。 此函数将代理的响应与预期结果进行比较,并存储评估指标。

注释

此步骤需要一个已发布到你正在评估的阶段的数据代理(production 或 sandbox)。 如果你还没有,请参见创建 Fabric 数据代理。

from fabric.dataagent.evaluation import evaluate_data_agent

# Name of your data agent
data_agent_name = "AgentEvaluation"

# (Optional) Name of the workspace if the data agent is in a different workspace
workspace_name = None

# (Optional) Name of the output table to store evaluation results (default: "evaluation_output")
# Two tables will be created:
# - "<table_name>": contains summary results (e.g., accuracy)
# - "<table_name>_steps": contains detailed reasoning and step-by-step execution
table_name = "demo_evaluation_output"

# Specify the data agent stage: "production" (default) or "sandbox"
data_agent_stage = "production"

# Run the evaluation and get the evaluation ID
try:
    evaluation_id = evaluate_data_agent(
        df,
        data_agent_name,
        workspace_name=workspace_name,
        table_name=table_name,
        data_agent_stage=data_agent_stage
    )
    print(f"Unique ID for the current evaluation run: {evaluation_id}")
except Exception as e:
    print(f"Evaluation failed: {e}")

运行结束后,你会看到类似以下文本的输出:

Unique ID for the current evaluation run: <evaluation-id>

获取评估摘要

运行评估后,你可以使用函数 get_evaluation_summary 获取结果的高级总结。 该功能可帮助您了解数据代理的整体表现,包括有多少条回复与预期答案相符等指标。

from fabric.dataagent.evaluation import get_evaluation_summary

# Retrieve a summary of the evaluation results
summary_df = get_evaluation_summary(table_name)

显示数据代理评估结果摘要的屏幕截图。

默认情况下,该函数会寻找一个名为 evaluation_output的表。 如果你在评估时指定了自定义表名(比如 demo_evaluation_output),就把这个名字作为 table_name 参数传递出来。

返回的数据帧包括聚合指标,例如正确、不正确或不清楚的响应数。 此结果可帮助你快速评估代理的准确性,并确定改进方面。

检查详细的评估结果

要深入了解数据代理对每个具体问题的回应,可以使用该 get_evaluation_details 功能。 此函数返回评估运行的详细细分,包括实际代理响应、它们是否与预期答案匹配,以及指向评估线程的链接(仅对运行评估的用户可见)。

from fabric.dataagent.evaluation import get_evaluation_details

# Table name used during evaluation
table_name = "demo_evaluation_output"

# Whether to return all evaluation rows (True) or only failures (False)
get_all_rows = False

# Whether to print a summary of the results
verbose = True

# Retrieve evaluation details for a specific run
eval_details = get_evaluation_details(
    evaluation_id,
    table_name,
    get_all_rows=get_all_rows,
    verbose=verbose
)

显示特定数据代理评估结果详细信息的屏幕截图。

自定义评估提示

默认情况下,Fabric SDK 使用内置提示来评估数据代理的实际答案是否符合预期答案。 不过,你可以通过使用 critic_prompt 参数,提供自己更细致或领域特定的评估提示。

自定义提示应包含占位符 {query}, {expected_answer}以及 {actual_answer}。 评估过程会针对每个问题动态替换这些占位符。

from fabric.dataagent.evaluation import evaluate_data_agent

# Define a custom prompt for evaluating agent responses
critic_prompt = """
    Given the following query, expected answer, and actual answer, please determine if the actual answer is equivalent to expected answer. If they are equivalent, respond with 'yes'.

    Query: {query}

    Expected Answer:
    {expected_answer}

    Actual Answer:
    {actual_answer}

    Is the actual answer equivalent to the expected answer?
"""

# Name of the data agent
data_agent_name = "AgentEvaluation"

# Run evaluation using the custom critic prompt
evaluation_id = evaluate_data_agent(df, data_agent_name, critic_prompt=critic_prompt)

此功能在以下情况下特别有用:

  • 你想对何种情况算作匹配采用更宽松或更严格的标准。
  • 你预期的答案和实际答案形式可能不同,但语义上仍然相当。
  • 你需要捕捉领域特定的细微差别,以便正确评估答案。

诊断按钮

诊断 按钮允许 你下载数据代理配置和执行步骤的完整快照。 导出过程包括数据源设置、应用指令、所用示例查询以及数据代理生成响应时所采取的底层步骤等细节。

在与 Microsoft 支持部门 合作或排查意外行为时使用此功能。 通过查看下载的文件,你可以准确看到数据代理如何处理你的请求、应用了哪些配置以及潜在问题发生在哪里。 这种透明度使得调试和优化数据代理性能变得更容易。

数据代理中诊断按钮的截图。

Troubleshooting

問题 原因 解决方案
找不到数据代理 data_agent_name 或 workspace_name 不正确,或者该代理尚未发布。 验证代理名称和工作区,并确保代理已发布到指定的 data_agent_stage 中。
结果为空或缺失 表名与在 evaluate_data_agent 期间使用的名称不一致。 将相同的 table_name 数据传递给 get_evaluation_summary 和 get_evaluation_details。
message_url 无法访问 评估线程仅对执行评估的用户可见。 用你自己的身份重新运行评估以访问线程链接。
自定义提示没有任何效果或错误 critic_prompt缺少必需的占位符。 在提示中包含 {query}、 {expected_answer}和 {actual_answer} 。
权限或容量错误 缺少F2或更高容量,或者缺少对数据源的读取权限。 确认前提条件,包括容量和数据源读取权限。

后续步骤