ag2-eval-comparison
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseEvaluation — comparing builds (variants & pairwise)
评估 — 对比不同构建版本(变体与两两对比)
When to use
使用场景
- Rank N models / prompts / configs on a leaderboard →
run_variants - Decide which of two is better, head-to-head → with
run_pairwise(LLM) orpairwise_judge(people)human_pairwise
For running and grading a single agent (scorers, CI, persistence), use .
ag2-evaluation- 在排行榜上对N个模型/提示词/配置进行排名 → 使用
run_variants - 两两对比,判定两者中哪一个更优 → 搭配(LLM自动判定)或
pairwise_judge(人工判定)使用human_pairwiserun_pairwise
如需运行并评估单个Agent(评分器、置信区间、持久化),请使用。
ag2-evaluationInstall
安装
bash
pip install "ag2[openai,tracing]"Required. Run this install before delivering the code. If you cannot run commands, state the exactcommand.pip install
bash
pip install "ag2[openai,tracing]"必须执行。在运行代码前请先执行此安装命令。若无法运行命令,请明确说明该命令。pip install
Leaderboard — run_variants
排行榜 — run_variants
VariantsAgentaxispython
from ag2 import Agent
from ag2.config import OpenAIConfig, GeminiConfig
from ag2.eval import Variants, run_variants
from ag2.eval.scorers import agent_judge
board = await run_variants(
suite,
variants=Variants(
{
"gpt-4o": Agent("a", prompt="Answer helpfully.", config=OpenAIConfig("gpt-4o")),
"flash": Agent("a", prompt="Answer helpfully.", config=GeminiConfig("gemini-3-flash-preview")),
},
axis="config", # label for what was varied (used in summary)
),
scorers=[agent_judge(OpenAIConfig("gpt-4o"), criterion="Helpful and accurate.", key="quality")],
store_dir="runs",
repeats=5, # optional: N runs per variant for stability
)
print(board.summary("quality")) # ranked leaderboard
board.best("quality") # winning variant name (None if tied)
board.leaderboard("quality") # list[LeaderboardRow] — variant, score, n, rank
board.results["gpt-4o"] # each variant's full RunResultVary whatever you like across the agents — set to label it (e.g. , , ). Tied scores share a rank; a 3-way tie usually means the eval isn't discriminating — make it harder, or score quality with a judge.
axis"config""prompt""tools"VariantsAgentaxispython
from ag2 import Agent
from ag2.config import OpenAIConfig, GeminiConfig
from ag2.eval import Variants, run_variants
from ag2.eval.scorers import agent_judge
board = await run_variants(
suite,
variants=Variants(
{
"gpt-4o": Agent("a", prompt="Answer helpfully.", config=OpenAIConfig("gpt-4o")),
"flash": Agent("a", prompt="Answer helpfully.", config=GeminiConfig("gemini-3-flash-preview")),
},
axis="config", # 标记变量维度(用于摘要展示)
),
scorers=[agent_judge(OpenAIConfig("gpt-4o"), criterion="Helpful and accurate.", key="quality")],
store_dir="runs",
repeats=5, # 可选:每个变体运行N次以保证稳定性
)
print(board.summary("quality")) # 排名后的排行榜
board.best("quality") # 获胜变体名称(平局则返回None)
board.leaderboard("quality") # list[LeaderboardRow] — 变体、分数、次数、排名
board.results["gpt-4o"] # 每个变体的完整RunResult可以在Agent之间任意设置变量差异 — 将设置为对应的标签(例如、、)。分数相同的变体共享排名;三方平局通常意味着评估缺乏区分度 — 可以提升评估难度,或使用判定器对质量进行评分。
axis"config""prompt""tools"Head-to-head (LLM) — run_pairwise + pairwise_judge
两两对比(LLM自动) — run_pairwise + pairwise_judge
A comparator picks a winner PER task. shows the pair in BOTH orders and counts a win only if it's consistent — else a tie (cancels position bias):
pairwise_judgepython
from ag2.eval import run_pairwise
from ag2.eval.scorers import pairwise_judge
result = await run_pairwise(
suite, variant_a=agent_v1, variant_b=agent_v2,
comparators=[pairwise_judge(OpenAIConfig("gpt-4o"), criterion="more helpful answer", key="quality")],
store_dir="runs",
)
wr = result.win_rate("quality") # B's win-rate
print(wr.rate, wr.ci, wr.wins, wr.losses, wr.ties) # ties count 0.5; ci is a Wilson 95% interval
print(result.flips("quality")) # int — count of cases where the two orders disagreedvariant_avariant_bAgentcomparators=result.agreement("quality", "human")Agreement.rate.cohen_kappa比较器会针对每个任务选出获胜者。会以两种顺序展示对比对,只有结果一致时才判定为获胜 — 否则判定为平局(消除位置偏差):
pairwise_judgepython
from ag2.eval import run_pairwise
from ag2.eval.scorers import pairwise_judge
result = await run_pairwise(
suite, variant_a=agent_v1, variant_b=agent_v2,
comparators=[pairwise_judge(OpenAIConfig("gpt-4o"), criterion="more helpful answer", key="quality")],
store_dir="runs",
)
wr = result.win_rate("quality") # 变体B的胜率
print(wr.rate, wr.ci, wr.wins, wr.losses, wr.ties) # 平局计0.5;ci为Wilson 95%置信区间
print(result.flips("quality")) # 整数 — 两种顺序下结果不一致的案例数量variant_avariant_bAgentcomparators=result.agreement("quality", "human").rate.cohen_kappaHead-to-head (human) — human_pairwise
两两对比(人工) — human_pairwise
Same unit, decided by a person. The pair is blinded and order-randomized; the default prints it and reads / / . Pass your own async to collect a vote from a UI (returns , , or ):
12tieask(task, response_1, response_2)"1""2""tie"python
from ag2.eval.scorers import human_pairwise
async def ask(task, response_1, response_2) -> str:
return await my_ui.compare(task.inputs["input"], response_1, response_2) # "1" / "2" / "tie"
result = await run_pairwise(suite, variant_a=agent_v1, variant_b=agent_v2,
comparators=[human_pairwise(key="quality", ask=ask)], store_dir="runs")At scale, export a blinded manifest, label it in any tool, import it. is the grade-only twin of (pairs two existing trace sources by ):
evaluate_pairwiserun_pairwisetask_idpython
from ag2.eval import evaluate_pairwise, DirectoryTraceSource
from ag2.eval.scorers import export_pairwise_cases, human_labels
a, b = DirectoryTraceSource("runs/champion"), DirectoryTraceSource("runs/challenger")
await export_pairwise_cases(a, b, criteria=["more helpful"], out="labels.jsonl", suite=suite) # blinded JSONL单元逻辑相同,但由人工判定结果。对比对会被盲化并随机排序;默认情况下会打印内容并读取//输入。可以传入自定义异步函数从UI收集投票结果(返回、或):
12tieask(task, response_1, response_2)"1""2""tie"python
from ag2.eval.scorers import human_pairwise
async def ask(task, response_1, response_2) -> str:
return await my_ui.compare(task.inputs["input"], response_1, response_2) # "1" / "2" / "tie"
result = await run_pairwise(suite, variant_a=agent_v1, variant_b=agent_v2,
comparators=[human_pairwise(key="quality", ask=ask)], store_dir="runs")大规模场景下,可导出盲化清单,在任意工具中标记后再导入。是的仅评分版本(通过配对两个现有跟踪源):
evaluate_pairwiserun_pairwisetask_idpython
from ag2.eval import evaluate_pairwise, DirectoryTraceSource
from ag2.eval.scorers import export_pairwise_cases, human_labels
a, b = DirectoryTraceSource("runs/champion"), DirectoryTraceSource("runs/challenger")
await export_pairwise_cases(a, b, criteria=["more helpful"], out="labels.jsonl", suite=suite) # 盲化JSONL文件a person adds "preferred": "1" | "2" | "tie" per line, then:
人工为每一行添加 "preferred": "1" | "2" | "tie",然后执行:
result = await evaluate_pairwise(a, b, suite=suite, store_dir="runs",
comparators=[human_labels("labels.jsonl", criterion="more helpful", key="helpful")])
The manifest hides which model is which; its `first_variant` field de-blinds it for `human_labels`.result = await evaluate_pairwise(a, b, suite=suite, store_dir="runs",
comparators=[human_labels("labels.jsonl", criterion="more helpful", key="helpful")])
清单会隐藏模型信息;其`first_variant`字段会在`human_labels`中用于解除盲化。Common pitfalls
常见误区
- Judge == a variant's model — self-preference bias; use a different judge model.
- Bare win-rate on few pairs — report (Wilson); a small n straddles 50%.
wr.ci - Passing factories, not agents — (
run_variants) andvariants=Variants({name: Agent(...)})(run_pairwise/variant_a=) takevariant_b=instances, not build callables. Vary the model per task withAgent(amodel_config=) rather than rebuilding the agent. Keepdict[task_id, ModelConfig]'s default swap (don't setpairwise_judge) for unbiased verdicts.swap=False
- 判定模型与变体模型相同 — 存在自我偏好偏差;请使用不同的判定模型。
- 少量对比对仅使用原始胜率 — 请报告(Wilson置信区间);样本量小时结果可能接近50%。
wr.ci - 传入工厂函数而非Agent实例 — (
run_variants)和variants=Variants({name: Agent(...)})(run_pairwise/variant_a=)接收的是**variant_b=实例**,而非构建可调用对象。如需为每个任务设置不同模型,请使用Agent(一个model_config=),而非重新构建Agent。请保留dict[task_id, ModelConfig]的默认交换设置(不要设置pairwise_judge)以获得无偏差结果。swap=False
Going deeper
深入了解
- —
website/docs/user-guide/evaluation/(thevariantsmapping +Variants),axis(comparators, win-rate, blinded labeling)pairwise - — single-agent run/grade, scorers, CI, persistence
ag2-evaluation
- —
website/docs/user-guide/evaluation/(variants映射 +Variants)、axis(比较器、胜率、盲化标记)pairwise - — 单Agent运行/评分、评分器、置信区间、持久化
ag2-evaluation