Ragas Ragas
stable · 中文译文
中文译文 · 原文:https://docs.ragas.io/en/stable/howtos/cli/agent_evals/ · 许可证 Apache-2.0

Agent 评测 Quickstart

agent_evals 模板提供一套设置,用于评测求解数学问题的 AI agent,并使用正确性指标。

创建项目

ragas quickstart agent_evals
cd agent_evals

安装依赖

uv sync

设置 API Key

export OPENAI_API_KEY="your-openai-key"

运行评测

uv run python evals.py

项目结构

agent_evals/
├── README.md              # Project documentation
├── pyproject.toml         # Project configuration
├── agent.py               # Math solving agent implementation
├── evals.py               # Evaluation workflow
├── __init__.py            # Python package marker
└── evals/
    ├── datasets/          # Test datasets
    ├── experiments/       # Evaluation results
    └── logs/              # Execution logs

评测内容

该模板评测 AI agent 求解数学表达式的能力:

  • Agent:使用工具逐步求解数学问题
  • 测试用例:如 (2 + 3) * (6 - 2)、100 / 5 + 3 * 2 这类数学表达式
  • 指标:二元正确性(正确为 1.0,错误为 0.0)

理解代码

Agent(agent.py)

实现带计算器工具的数学求解 agent:

from agent import get_default_agent

math_agent = get_default_agent()
result = math_agent.solve("15 - 3 / 4")

评测(evals.py)

在各种数学问题上测试该 agent:

@numeric_metric(name="correctness", allowed_values=(0.0, 1.0))
def correctness_metric(prediction: float, actual: float):
    result = 1.0 if abs(prediction - actual) < 1e-5 else 0.0
    return MetricResult(value=result, reason=f"Prediction: {prediction}, Actual: {actual}")

下一步