评估一个人工智能工作流
本教程演示如何使用 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)
为了评估我们工作流的性能,我们将定义一个基于 llm 的度量,该度量将比较我们工作流的输出与通过标准,并根据比较结果输出通过/未通过。
from ragas_experimental.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
文件来检查结果。