"""Class A ingest: RAGTruth TRAIN (upstream ParticleMedia/RAGTruth, MIT; Hub mirror wandb/RAGTruth-processed) -> noul "response supported by context" rows (criteria flipped 50%) + choice "which sentence is unsupported" rows. Only the train parquet is read; the test parquet (= the benchmark's test split) is never opened. Run: PYTHONPATH=src .venv/bin/python -m macjev.v4data.classa_ragtruth_train [--max-rows 12000]""" from __future__ import annotations import argparse, json, re from ..download import fetch from ..paths import ROOT from . import ingest_common as C NAME = "ragtruth_train" OUT_DIR = ROOT / "data" / "synth-v4" / "classA" / NAME REPO = "wandb/RAGTruth-processed" FILE = "data/train-00000-of-00001.parquet" SOURCE = "ParticleMedia/RAGTruth (train; mirror wandb/RAGTruth-processed)" LICENSE = "mit" SENT = re.compile(r"(?<=[.!?])\s+(?=[A-Z0-9\"'(])") def spans(labels, output: str) -> list[str]: out = [] for l in labels or []: if isinstance(l, str): try: l = json.loads(l) except json.JSONDecodeError: out.append(l); continue if isinstance(l, dict): if isinstance(l.get("text"), str): out.append(l["text"]) elif isinstance(l.get("start"), int) and isinstance(l.get("end"), int): out.append(output[l["start"]:l["end"]]) return [s for s in out if s.strip()] def main(argv=None): ap = argparse.ArgumentParser(); ap.add_argument("--max-rows", type=int, default=12000); a = ap.parse_args(argv) import pyarrow.parquet as pq got = fetch(REPO, [FILE]) t = pq.read_table(got["files"][FILE]).to_pylist() rows = [] for i, r in enumerate(t): ctx, out, q = (r.get("context") or ""), (r.get("output") or ""), (r.get("query") or "") if not ctx.strip() or not out.strip(): continue hall = spans(r.get("hallucination_labels"), out) is_hall = bool(hall) gid = "ragtruth:" + C.h(ctx)[:16] split = C.split_for(gid) rng = C.rng_for(NAME, i) state = {"task": r.get("task_type"), "instruction": q[:2000], "context": ctx[:12000], "response": out[:6000]} prov = {"dataset": REPO, "revision": got["revision"], "file": FILE, "line": i, "source_id": r.get("id"), "source_split": "train"} flip = rng.random() < 0.5 ins = ("Every claim in the response is supported by the context." if flip else "The response contains at least one claim that the context does not support.") valid = ["true" if (not is_hall if flip else is_hall) else "false"] rows.append(C.make(id=f"v4A_ragtruth/train/{i}/noul", group_id=gid, split=split, source=SOURCE, category="v4A_rag_hallucination", lang="en", license=LICENSE, state=state, question={"t": "noul", "ins": ins, "crit": None}, valid=valid, provenance=prov, component="di_retrieval", family="ragtruth_support", label_method="source_label", di=[C.di_id("RAGTruth")])) if is_hall: sents = [s.strip() for s in SENT.split(out) if len(s.strip()) > 15] bad = [s for s in sents if any(sp.strip()[:40] in s for sp in hall)] good = [s for s in sents if s not in bad] if bad and len(good) >= 2: opts = [bad[0]] + rng.sample(good, min(3, len(good))) rng.shuffle(opts) names = ["A", "B", "C", "D"][:len(opts)] crit = dict(zip(names, opts)) rows.append(C.make(id=f"v4A_ragtruth/train/{i}/which", group_id=gid, split=split, source=SOURCE, category="v4A_rag_hallucination", lang="en", license=LICENSE, state=state, question={"t": "choice", "ins": "Which sentence of the response makes a claim that the context does not support?", "crit": crit}, valid=[names[opts.index(bad[0])]], provenance=prov, component="di_retrieval", family="ragtruth_support", label_method="source_label", di=[C.di_id("RAGTruth")])) if len(rows) >= a.max_rows: break C.finish(OUT_DIR, rows, builder="macjev.v4data.classa_ragtruth_train-v1", source=SOURCE, license=LICENSE, license_evidence="github.com/ParticleMedia/RAGTruth LICENSE (MIT, 2023 Particle Media); mirror card has no licence field", splits_used=["train"], forbidden_splits=["test"], data_class="A", class_A_benchmark=C.di_id("RAGTruth"), extra={"revision": got["revision"], "mirror_license_field": got["license"]}) if __name__ == "__main__": main()