Download code/validate_r2.py from badr7/rapidchat-data: direct link, hf CLI and curl.
- Browser
- Download file 9.06 kB
-
https://huggingface.co/datasets/badr7/rapidchat-data/resolve/main/code/validate_r2.py
- Command line
-
hf download hf://datasets/badr7/rapidchat-data/code/validate_r2.py
-
curl -L -o validate_r2.py https://huggingface.co/datasets/badr7/rapidchat-data/resolve/main/code/validate_r2.py
9.06 kB
| #!/usr/bin/env python3 | |
| """Replays the round-2 scenarios against the real tau2 airline tools and re-derives each correct answer from tool output | |
| with logic written separately from the generator. Run with the tau2 environment's python. | |
| usage: validate_r2.py <round-1 bench dir> <round-2 tasks.json> | |
| """ | |
| 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)) | |
| problems = collections.defaultdict(list) | |
| BASE = FlightDB.model_validate(raw) | |
| def fresh(): | |
| return AirlineTools(BASE.model_copy(deep=True)) | |
| RO = AirlineTools(BASE) | |
| def check(ok, t, msg): | |
| if not ok: | |
| problems[msg].append(t["id"]) | |
| def mins(x): | |
| return int(x[:2]) * 60 + int(x[3:5]) + (1440 if "+1" in x else 0) | |
| def paid(tools, rid): | |
| return sum(p.amount for p in tools.db.reservations[rid].payment_history) | |
| def run(tools, actions): | |
| out = [] | |
| for a in actions: | |
| out.append(getattr(tools, a["name"])(**a["arguments"])) | |
| return out | |
| def itins(tools, o, d, date): | |
| res = [] | |
| for f in tools.search_direct_flight(o, d, date): | |
| res.append((f.prices["economy"], f.available_seats["economy"], [f.flight_number])) | |
| for f1, f2 in tools.search_onestop_flight(o, d, date): | |
| res.append((f1.prices["economy"] + f2.prices["economy"], min(f1.available_seats["economy"], f2.available_seats["economy"]), | |
| [f1.flight_number, f2.flight_number])) | |
| return sorted(res, key=lambda x: x[0]) | |
| for t in tasks: | |
| fam = json.loads(t["description"]["notes"])["family"] | |
| meta = json.loads(t["description"]["notes"]) | |
| acts = t["evaluation_criteria"]["actions"] | |
| txt = t["user_scenario"]["instructions"]["task_instructions"] | |
| tools = fresh() | |
| try: | |
| run(tools, acts) | |
| except Exception as e: | |
| problems["action fails: %s" % str(e)[:60]].append(t["id"]) | |
| continue | |
| check(True or not acts, t, "actions do not change the database") | |
| if fam == "be_budget": | |
| limit = int(re.search(r"under \$(\d+)", txt).group(1)) | |
| rid = meta["reservation"] | |
| r = raw["reservations"][rid] | |
| sim = fresh() | |
| before = paid(sim, rid) | |
| same = [{"flight_number": s["flight_number"], "date": s["date"]} for s in r["flights"]] | |
| 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.update_reservation_flights(rid, "economy", same, pm) | |
| upgrade = paid(sim, rid) - before | |
| case = t["description"]["purpose"].rsplit("(", 1)[1].rstrip(")") | |
| check(upgrade > 0, t, "upgrade does not cost anything") | |
| if case == "under": | |
| check(upgrade < limit, t, "under: upgrade not under the limit") | |
| change = paid(tools, rid) - before - upgrade | |
| check(change < limit, t, "under: change not under the limit") | |
| check(tools.db.reservations[rid].cabin == "economy", t, "under: cabin not economy") | |
| elif case == "trap": | |
| check(upgrade >= limit, t, "trap: upgrade under the limit") | |
| check(not acts, t, "trap: has actions") | |
| m = re.search(r"change ([+-]\d+)", t["description"]["purpose"]) | |
| check(upgrade + int(m.group(1)) < limit, t, "trap: net not under the limit (not a trap)") | |
| else: | |
| check(not acts and upgrade >= limit, t, "over: inconsistent") | |
| check(r["cabin"] == "basic_economy", t, "not basic economy") | |
| elif fam == "refuse_then_book": | |
| rid = meta["reservation"] | |
| r = raw["reservations"][rid] | |
| 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 actually cancellable") | |
| b = acts[0]["arguments"] | |
| u = raw["users"][b["user_id"]] | |
| check(b["passengers"] == [{"first_name": u["name"]["first_name"], "last_name": u["name"]["last_name"], "dob": u["dob"]}], t, "passenger is not the profile") | |
| options = itins(RO, b["origin"], b["destination"], b["flights"][0]["date"]) | |
| nums = [f["flight_number"] for f in b["flights"]] | |
| if "second cheapest" in txt: | |
| check(len(options) >= 3 and options[0][0] < options[1][0] < options[2][0] and options[1][2] == nums, t, "second cheapest wrong") | |
| elif "cheapest nonstop" in txt: | |
| nonstop = [o for o in options if len(o[2]) == 1] | |
| check(len(nonstop) >= 2 and nonstop[0][0] < nonstop[1][0] and nonstop[0][2] == nums, t, "cheapest nonstop wrong") | |
| else: | |
| check(len(options) >= 2 and options[0][0] < options[1][0] and options[0][2] == nums, t, "cheapest wrong") | |
| check(b["payment_methods"][0]["amount"] == sum(o[0] for o in options if o[2] == nums), t, "price wrong") | |
| elif fam == "reroute": | |
| rid = meta["reservation"] | |
| r = raw["reservations"][rid] | |
| health = "health problem" in txt | |
| eligible = (r["created_at"] > "2024-05-14T15:00:00" or r["cabin"] == "business" or (health and r["insurance"] == "yes")) | |
| check(bool(acts) == eligible, t, "rebooking goes ahead exactly when the cancellation is refundable") | |
| if acts: | |
| b = acts[1]["arguments"] | |
| for part, f in zip(re.findall(r": (the cheapest nonstop economy flight[^.]*)\.", txt), b["flights"]): | |
| origin = b["origin"] if f is b["flights"][0] else b["destination"] | |
| dest = b["destination"] if f is b["flights"][0] else b["origin"] | |
| listed = RO.search_direct_flight(origin, dest, f["date"]) | |
| if "arrives before" in part: | |
| h = int(re.search(r"before (\d+):", part).group(1)) | |
| listed = [x for x in listed if "+1" not in x.scheduled_arrival_time_est and mins(x.scheduled_arrival_time_est) < h * 60] | |
| if "departs at" in part: | |
| h = int(re.search(r"departs at (\d+):", part).group(1)) | |
| listed = [x for x in listed if mins(x.scheduled_departure_time_est) >= h * 60] | |
| best = sorted(listed, key=lambda x: x.prices["economy"]) | |
| check(best and best[0].flight_number == f["flight_number"] and (len(best) < 2 or best[0].prices["economy"] < best[1].prices["economy"]), | |
| t, "reroute flight choice wrong") | |
| u = raw["users"][b["user_id"]] | |
| free = {"regular": 1, "silver": 2, "gold": 3}[u["membership"]] * len(b["passengers"]) | |
| check(b["nonfree_baggages"] == max(0, b["total_baggages"] - free), t, "reroute nonfree bags wrong") | |
| check(b["insurance"] == "no" and b["passengers"] == r["passengers"], t, "reroute passengers/insurance wrong") | |
| elif fam == "duration_sweep": | |
| long_h = int(re.search(r"longer than (\d+) hours", txt).group(1)) | |
| short_h = int(re.search(r"is (\d+) hours or shorter", txt).group(1)) | |
| u = raw["users"][meta["users"][0]] | |
| sim = fresh() | |
| want_up, want_cancel = set(), set() | |
| for x in u["reservations"]: | |
| r = raw["reservations"][x] | |
| if not all(s["date"] > "2024-05-15" for s in r["flights"]): | |
| continue | |
| ds = [] | |
| for s in r["flights"]: | |
| f = raw["flights"][s["flight_number"]] | |
| ds.append(mins(f["scheduled_arrival_time_est"]) - mins(f["scheduled_departure_time_est"])) | |
| if max(ds) > long_h * 60 and (r["created_at"] > "2024-05-14T15:00:00" or r["cabin"] == "business"): | |
| want_cancel.add(x) | |
| if max(ds) <= short_h * 60 and r["cabin"] != "business": | |
| want_up.add(x) | |
| got_c = {a["arguments"]["reservation_id"] for a in acts if a["name"] == "cancel_reservation"} | |
| got_u = {a["arguments"]["reservation_id"] for a in acts if a["name"] == "update_reservation_flights"} | |
| check(got_c == want_cancel, t, "sweep cancellations wrong") | |
| check(got_u == want_up, t, "sweep upgrades wrong (or a seat shortage)") | |
| total = sum(paid(tools, x) - paid(RO, x) for x in got_u) | |
| check([str(total)] == t["evaluation_criteria"]["communicate_info"], t, "sweep total wrong") | |
| if fam == "two_denied": | |
| u = raw["users"][meta["users"][0]] | |
| check(not acts, t, "two_denied has actions") | |
| for x in u["reservations"]: | |
| r = raw["reservations"][x] | |
| if all(s["date"] > "2024-05-15" for s in r["flights"]): | |
| pass | |
| check("health" not in txt.split("Under no circumstances")[0], t, "two_denied mentions health early") | |
| print("scenarios checked:", len(tasks)) | |
| for k, v in problems.items(): | |
| print("PROBLEM", k, len(v), v[:6]) | |
| print("VALID" if not problems else "INVALID") | |