tool-routing-data / validate.py
stanley-nv's picture
Upload dataset
249f72a verified
Raw History Blame Contribute Delete
2.79 kB
"""Validate the dataset files: schema, labels, duplicates inside / across splits. Usage: python validate.py [dataset_dir]"""
import collections, json, re, sys
from pathlib import Path
root = Path(sys.argv[1] if len(sys.argv) > 1 else Path(__file__).parent)
LABELS = {"wiki", "websearch", "weather", "news", "files", "shell", "other"}
norm = lambda q: re.sub(r"[^a-z0-9]+", " ", q.lower()).strip()
errors = []
def read(p): return [json.loads(l) for l in open(root / p, encoding="utf-8") if l.strip()]
splits = {}
for name, path in (("train", "routing/train.jsonl"), ("validation", "routing/validation.jsonl"), ("test", "routing/test.jsonl"), ("test2", "routing/test2.jsonl"), ("synthetic", "synthetic/templates.jsonl")):
rows = read(path); splits[name] = rows; seen = set()
for i, r in enumerate(rows):
if set(r) != {"query", "label", "source", "style"}: errors.append(f"{path}:{i} bad keys {sorted(r)}")
if r.get("label") not in LABELS: errors.append(f"{path}:{i} bad label {r.get('label')!r}")
if not isinstance(r.get("query"), str) or not r["query"].strip(): errors.append(f"{path}:{i} empty query")
n = norm(r["query"])
if n in seen: errors.append(f"{path}:{i} duplicate inside split: {r['query']!r}")
seen.add(n)
print(f"routing {name:10s} {len(rows):6d} rows", dict(sorted(collections.Counter(r["label"] for r in rows).items())))
keys = {n: {norm(r["query"]) for r in rows} for n, rows in splits.items()}
for a in splits:
for b in splits:
if a < b and keys[a] & keys[b]: errors.append(f"routing leakage {a} / {b}: {len(keys[a] & keys[b])} shared queries")
names = collections.Counter()
for split in ("train", "validation", "test"):
rows = read(f"tools/{split}.jsonl")
for i, t in enumerate(rows):
if set(t) != {"name", "descriptions", "queries", "source"}: errors.append(f"tools/{split}:{i} bad keys {sorted(t)}")
if not re.fullmatch(r"[a-z][a-z0-9_]*", t["name"]): errors.append(f"tools/{split}:{i} bad name {t['name']!r}")
if len(t["descriptions"]) < 2 or any(not d.strip() for d in t["descriptions"]): errors.append(f"tools/{split}:{i} needs >= 2 non-empty descriptions")
if len(t["queries"]) < 5: errors.append(f"tools/{split}:{i} too few queries")
names[t["name"]] += 1
print(f"tools {split:10s} {len(rows):6d} tools, {sum(len(t['queries']) for t in rows)} queries")
errors += [f"tool name used in several places: {n}" for n, c in names.items() if c > 1]
pt = json.load(open(root / "priority_tools.json", encoding="utf-8"))
if set(pt) != LABELS: errors.append(f"priority_tools.json keys {sorted(pt)}")
print("\n" + ("OK: no problems found" if not errors else f"{len(errors)} problem(s):\n " + "\n ".join(errors[:30])))
sys.exit(1 if errors else 0)