Download code/validate_r3.py from badr7/rapidchat-data: direct link, hf CLI and curl.
- Browser
- Download file 7.5 kB
-
https://huggingface.co/datasets/badr7/rapidchat-data/resolve/main/code/validate_r3.py
- Command line
-
hf download hf://datasets/badr7/rapidchat-data/code/validate_r3.py
-
curl -L -o validate_r3.py https://huggingface.co/datasets/badr7/rapidchat-data/resolve/main/code/validate_r3.py
7.5 kB
| #!/usr/bin/env python3 | |
| """Replays the round-3 gold actions with the real tau2 airline tools and re-derives each answer independently. | |
| usage: validate_r3.py <round-1 bench dir> <round-3 tasks.json> (run with the tau2 environment's python) | |
| """ | |
| import collections | |
| import json | |
| import re | |
| import sys | |
| from loguru import logger | |
| logger.remove() | |
| from tau2.domains.airline.data_model import FlightDB | |
| from tau2.domains.airline.tools import AirlineTools | |
| bench, tasks_path = sys.argv[1:3] | |
| raw = json.load(open(bench + "/db.json")) | |
| tasks = json.load(open(tasks_path)) | |
| BASE = FlightDB.model_validate(raw) | |
| RO = AirlineTools(BASE) | |
| problems = collections.defaultdict(list) | |
| def fresh(): | |
| return AirlineTools(BASE.model_copy(deep=True)) | |
| def check(ok, t, msg): | |
| if not ok: | |
| problems[msg].append(t["id"]) | |
| def paid(tools, rid): | |
| return sum(p.amount for p in tools.db.reservations[rid].payment_history) | |
| def options(o, d, date, cabin): | |
| res = [] | |
| for f in RO.search_direct_flight(o, d, date): | |
| res.append((f.prices[cabin], [f.flight_number])) | |
| for f1, f2 in RO.search_onestop_flight(o, d, date): | |
| res.append((f1.prices[cabin] + f2.prices[cabin], [f1.flight_number, f2.flight_number])) | |
| return sorted(res, key=lambda x: x[0]) | |
| FREE = {"regular": {"basic_economy": 0, "economy": 1, "business": 2}, "silver": {"basic_economy": 1, "economy": 2, "business": 3}, | |
| "gold": {"basic_economy": 2, "economy": 3, "business": 4}} | |
| for t in tasks: | |
| meta = json.loads(t["description"]["notes"]) | |
| fam = meta["family"] | |
| acts = t["evaluation_criteria"]["actions"] | |
| txt = t["user_scenario"]["instructions"]["task_instructions"] | |
| tools = fresh() | |
| try: | |
| for a in acts: | |
| getattr(tools, a["name"])(**a["arguments"]) | |
| except Exception as e: | |
| problems["action fails: %s" % str(e)[:70]].append(t["id"]) | |
| continue | |
| if fam == "be_net": | |
| limit = int(re.search(r"\$(\d+)", txt.split("What counts")[0].split("\n\n")[-1]).group(1)) | |
| rid = meta["reservation"] | |
| r = raw["reservations"][rid] | |
| check(r["cabin"] == "basic_economy", t, "not basic economy") | |
| card = re.search(r"ending in (\d{4})", txt).group(1) | |
| pm = [p for p in raw["users"][r["user_id"]]["payment_methods"].values() if p.get("last_four") == card][0]["id"] | |
| sim = fresh() | |
| before = paid(sim, rid) | |
| sim.update_reservation_flights(rid, "economy", [{"flight_number": s["flight_number"], "date": s["date"]} for s in r["flights"]], pm) | |
| upgrade = paid(sim, rid) - before | |
| case = t["description"]["purpose"].rsplit("(", 1)[1].rstrip(")") | |
| m = re.search(r"change ([+-]\d+) = net ([+-]\d+)", t["description"]["purpose"]) | |
| change, net = int(m.group(1)), int(m.group(2)) | |
| check(upgrade > 0 and upgrade + change == net, t, "upgrade/net arithmetic") | |
| if acts: | |
| real_net = paid(tools, rid) - before | |
| check(real_net == net, t, "replayed net differs") | |
| check(real_net <= limit, t, "go-ahead but net over the limit") | |
| if case == "net_ok_upgrade_over": | |
| check(upgrade > limit, t, "net_ok case but upgrade fits") | |
| check(tools.db.reservations[rid].cabin == "economy", t, "cabin not economy after") | |
| else: | |
| check(case == "net_over" and net > limit, t, "no actions but net is within the limit") | |
| elif fam in ("be_willing", "be_nooffer"): | |
| rid = meta["reservation"] | |
| r = raw["reservations"][rid] | |
| check(r["cabin"] == "basic_economy", t, "not basic economy") | |
| para = [x for x in txt.split("\n\n") if "$" in x][0] | |
| limit = int(re.search(r"\$(\d+)", para).group(1)) | |
| card = re.search(r"ending in (\d{4})", txt).group(1) | |
| pm = [p for p in raw["users"][r["user_id"]]["payment_methods"].values() if p.get("last_four") == card][0]["id"] | |
| sim = fresh() | |
| before = paid(sim, rid) | |
| sim.update_reservation_flights(rid, "economy", [{"flight_number": s["flight_number"], "date": s["date"]} for s in r["flights"]], pm) | |
| upgrade = paid(sim, rid) - before | |
| m = re.search(r"upgrade ([+-]\d+) then change ([+-]\d+)", t["description"]["purpose"]) | |
| check(upgrade == int(m.group(1)), t, "upgrade amount differs") | |
| change = int(m.group(2)) | |
| if fam == "be_nooffer": | |
| check(not acts and upgrade >= limit, t, "nooffer: upgrade under the limit or has actions") | |
| elif acts: | |
| step = paid(tools, rid) - before - upgrade | |
| check(step == change and step <= limit, t, "willing: change step over limit") | |
| else: | |
| check(change > limit, t, "willing: no actions but change step within limit") | |
| elif fam == "rank_pick": | |
| b = acts[0]["arguments"] | |
| u = raw["users"][b["user_id"]] | |
| cabin = b["cabin"] | |
| opts = options(b["origin"], b["destination"], b["flights"][0]["date"], cabin) | |
| nums = [f["flight_number"] for f in b["flights"]] | |
| if "nonstop flight in" in txt: | |
| opts = [o for o in opts if len(o[1]) == 1] | |
| rank = 1 if "second cheapest" in txt else 0 | |
| check(len(opts) > rank + 1 and opts[rank][1] == nums and len({o[0] for o in opts[: rank + 2]}) == rank + 2, t, "rank pick wrong") | |
| pax = len(b["passengers"]) | |
| check(b["passengers"][0] == {"first_name": u["name"]["first_name"], "last_name": u["name"]["last_name"], "dob": u["dob"]}, t, "first passenger not profile") | |
| if pax == 2: | |
| check(b["passengers"][1] in u["saved_passengers"], t, "companion not saved") | |
| nonfree = max(0, b["total_baggages"] - FREE[u["membership"]][cabin] * pax) | |
| check(b["nonfree_baggages"] == nonfree, t, "nonfree bags") | |
| check(b["payment_methods"][0]["amount"] == opts[rank][0] * pax + 50 * nonfree, t, "total wrong") | |
| check(b["insurance"] == "no", t, "insurance") | |
| elif fam == "rebook_pay": | |
| rid = meta["reservation"] | |
| r = raw["reservations"][rid] | |
| health = "health problem" in txt | |
| ok = (r["created_at"] > "2024-05-14T15:00:00" or r["cabin"] == "business" or (health and r["insurance"] == "yes") | |
| or any(raw["flights"][s["flight_number"]]["dates"][s["date"]]["status"] == "cancelled" for s in r["flights"])) | |
| check(ok, t, "cancellation not refundable") | |
| b = acts[1]["arguments"] | |
| check(b["payment_methods"][0]["payment_id"] == r["payment_history"][0]["payment_id"], t, "not the original payment") | |
| check(b["insurance"] == "no" and b["passengers"] == r["passengers"], t, "insurance/passengers") | |
| u = raw["users"][b["user_id"]] | |
| pax = len(b["passengers"]) | |
| check(b["nonfree_baggages"] == max(0, b["total_baggages"] - FREE[u["membership"]]["economy"] * pax), t, "nonfree bags") | |
| elif fam == "refuse_then_book": | |
| r = raw["reservations"][meta["reservation"]] | |
| ok_cancel = (r["created_at"] > "2024-05-14T15:00:00" or r["cabin"] == "business" | |
| or any(raw["flights"][s["flight_number"]]["dates"][s["date"]]["status"] == "cancelled" for s in r["flights"])) | |
| check(not ok_cancel, t, "refusal scenario is cancellable") | |
| elif fam == "two_denied": | |
| check(not acts, t, "two_denied has actions") | |
| print("scenarios checked:", len(tasks), collections.Counter(json.loads(t["description"]["notes"])["family"] for t in tasks)) | |
| for k, v in problems.items(): | |
| print("PROBLEM", k, len(v), v[:6]) | |
| print("VALID" if not problems else "INVALID") | |