File size: 2,051 Bytes
58d9a58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Show an observed search for a DMV-Hard question and the evidence the agent actually saw."""
import argparse
from pathlib import Path

import pyarrow.parquet as pq


def read(table, filters):
    folder = Path(__file__).resolve().parents[1] / "data" / table
    return pq.read_table(folder / "dmv-hard.parquet", filters=filters or None).to_pylist()


def show(seed=None, step=None):
    questions = read("dmv_questions", [("question_type", "=", "pattern")] if step is None else [])
    if seed is not None:
        questions = [row for row in questions if row["seed"] == seed]
    if step is not None:
        questions = [row for row in questions if row["step"] == step]
    for question in sorted(questions, key=lambda r: (r["seed"], r["step"])):
        filters = [(k, "=", question[k]) for k in ("cell_id", "seed", "step")]
        searches = read("dmv_searches", filters)
        candidates = read("dmv_candidates", filters)
        for search in sorted(searches, key=lambda r: (r["phase"], r["call"])):
            shown = sorted(
                [r for r in candidates if (r["phase"], r["call"]) == (search["phase"], search["call"])],
                key=lambda r: r["rank"],
            )
            if not shown:
                continue
            print(f"Seed {question['seed']}, step {question['step']}, {question['question_type']}")
            print(f"Search ({search['phase']}, call {search['call']}): {search['query']}")
            for item in shown[:3]:
                print(f"  {item['rank']}. Page {item['product_key']} | {item['retrieval_score']:g} | {item['shown_swatch_text']}")
            print(f"Agent answered page {question['executed_product_key']}. Correct: {question['answered_correctly']}.")
            return
    raise SystemExit("No matching question with an observed ranked response.")


if __name__ == "__main__":
    cli = argparse.ArgumentParser(description=__doc__)
    cli.add_argument("--seed", type=int)
    cli.add_argument("--step", type=int)
    args = cli.parse_args()
    show(args.seed, args.step)