Loading...
Loading...
Compare AG2 agents, models, or prompts to decide which is better. run_variants scores several named agents on one suite and ranks them on a leaderboard (Variants holds a mapping of named Agent instances plus an axis label). run_pairwise with pairwise_judge does head-to-head LLM comparison using a dual-order position swap (a win counts only if it survives the swap, else a tie), reporting win-rate with a Wilson 95% CI, wins, losses, ties, flips, and agreement (Cohen's kappa). human_pairwise collects a person's blinded vote inline, or via an exported manifest with export_pairwise_cases and human_labels. Use when the user wants to A/B test prompts or models, run a leaderboard, pick a winner, judge head-to-head, measure win-rate, or collect human preference labels. For running and grading a single agent, see ag2-evaluation.
npx skill4agent add ag2ai/ag2-skills ag2-eval-comparisonrun_variantsrun_pairwisepairwise_judgehuman_pairwiseag2-evaluationpip install "ag2[openai,tracing]"Required. Run this install before delivering the code. If you cannot run commands, state the exactcommand.pip install
VariantsAgentaxisfrom 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 RunResultaxis"config""prompt""tools"pairwise_judgefrom 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_kappa12tieask(task, response_1, response_2)"1""2""tie"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_idfrom 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
# a person adds "preferred": "1" | "2" | "tie" per line, then:
result = await evaluate_pairwise(a, b, suite=suite, store_dir="runs",
comparators=[human_labels("labels.jsonl", criterion="more helpful", key="helpful")])first_varianthuman_labelswr.cirun_variantsvariants=Variants({name: Agent(...)})run_pairwisevariant_a=variant_b=Agentmodel_config=dict[task_id, ModelConfig]pairwise_judgeswap=Falsewebsite/docs/user-guide/evaluation/variantsVariantsaxispairwiseag2-evaluation