""" STEP 1 — THE PASSAGES A WORKSPACE WOULD TEACH ITS EMBEDDER FROM. Documents only: see the guarantee in `common.py`. Stratified by source with square-root weights (Slack's 285k messages would otherwise drown Confluence's 5k pages), at most `--per-doc` passages from a document, the rest chosen at random, seeded. uv run python sample.py --passages 30000 --out $SELF_TUNE_DATA/passages.jsonl [--exclude pool-doc-ids.txt] [--seed 215] `--exclude` names document ids kept OUT of training (the held-out variant); ids only, never read as text. """ import argparse import json import math import random from common import corpus_documents, load_exclusions, passages, source_of def main(): ap = argparse.ArgumentParser() ap.add_argument("--passages", type=int, default=30000) ap.add_argument("--per-doc", type=int, default=2) ap.add_argument("--min-chars", type=int, default=200) ap.add_argument("--exclude", default="") ap.add_argument("--seed", type=int, default=215) ap.add_argument("--out", required=True) a = ap.parse_args() rng = random.Random(a.seed) excluded = load_exclusions(a.exclude) docs = {d: p for d, p in corpus_documents().items() if d not in excluded} by_source = {} for d, p in docs.items(): by_source.setdefault(source_of(p), []).append(d) weights = {s: math.sqrt(len(ids)) for s, ids in by_source.items()} total = sum(weights.values()) out, counts = [], {} for source, ids in sorted(by_source.items()): want = round(a.passages * weights[source] / total) rng.shuffle(ids) got = 0 for d in ids: if got >= want: break chunks = [c for c in passages(docs[d]) if len(c) >= a.min_chars] rng.shuffle(chunks) for i, text in enumerate(chunks[: a.per_doc]): out.append({"pid": f"{d}#{i}", "doc": d, "source": source, "text": text}) got += 1 counts[source] = got rng.shuffle(out) with open(a.out, "w", encoding="utf-8") as f: for row in out: f.write(json.dumps(row, ensure_ascii=False) + "\n") print(json.dumps({"passages": len(out), "documents": len({r["doc"] for r in out}), "excluded": len(excluded), "by_source": counts})) if __name__ == "__main__": main()