评估 AI 工作流
本教程演示了如何使用 Ragas 评估 AI 工作流,此处以一个简单的自定义电子邮件支持分类工作流为例。在本教程结束时,您将学会如何使用评估驱动开发来评估和迭代工作流。
flowchart LR
A["Email Query"] --> B["Rule based Info Extractor"]
B --> C["Template + LLM Response"]
C --> D["Email Reply"]
我们将从测试一个简单的工作流开始,该工作流从电子邮件中提取必要信息,将其路由到正确的模板,并使用大语言模型(LLM)生成响应。
接下来,我们将为工作流编写一些示例电子邮件查询和预期输出,然后将它们转换为 CSV 文件。
import pandas as pd
dataset_dict = [
{
"email": "Hi, I'm getting error code XYZ-123 when using version 2.1.4 of your software. Please help!",
"pass_criteria": "category Bug Report; product_version 2.1.4; error_code XYZ-123; response references both version and error code"
},
{
"email": "I need to dispute invoice #INV-2024-001 for 299.99 dollars. The charge seems incorrect.",
"pass_criteria": "category Billing; invoice_number INV-2024-001; amount 299.99; response references invoice and dispute process"
}]
pd.DataFrame(dataset_dict).to_csv("datasets/test_dataset.csv", index=False)
为了评估我们工作流的性能,我们将定义一个基于大语言模型的指标,该指标将我们工作流的输出与通过标准进行比较,并据此输出通过/失败。
from ragas.metrics import DiscreteMetric
my_metric = DiscreteMetric(
name="response_quality",
prompt="Evaluate the response based on the pass criteria: {pass_criteria}. Does the response meet the criteria? Return 'pass' or 'fail'.\nResponse: {response}",
allowed_values=["pass", "fail"],
)
接着,我们将编写评估实验循环,该循环将在测试数据集上运行我们的工作流,并使用该指标进行评估,然后将结果存储在 CSV 文件中。
from ragas import experiment
@experiment()
async def run_experiment(row):
response = workflow_client.process_email(
row["email"]
)
score = my_metric.score(
llm=llm,
response=response.get("response_template", " "),
pass_criteria=row["pass_criteria"]
)
experiment_view = {
**row,
"response": response.get("response_template", " "),
"score": score.value,
"score_reason": score.reason,
}
return experiment_view
现在,每当您对工作流进行更改时,都可以运行该实验,并查看其对工作流性能的影响。然后将其与之前的结果进行比较,以了解性能是提升了还是下降了。
端到端运行示例
-
设置你的 OpenAI API 密钥
-
运行实验
太棒了!你已成功使用Ragas运行了你的第一次评估。现在你可以通过打开 experiments/experiment_name.csv 文件来检查结果。