Advanced about 5 h
The picture
01
Collect
real + synthetic
02
Clean
filters, PII
03
Dedupe
exact + fuzzy
04
Decontaminate
vs eval sets
05
Document
data card
Data is the lever
Across fine-tuning and pre-training, cleaner, more diverse, correctly labelled data beats more data. A few hundred excellent examples can outperform tens of thousands of noisy ones, so curation is usually the best use of your time.
A curation pipeline
- Provenance and licence. Record where every example came from and whether you may train on it. Remove personal data and secrets.
- Heuristic filters. Drop empty, truncated, wrong-language, boilerplate and extreme-length items.
- Deduplication. Exact (hash) and near-duplicate (MinHash) removal.
- Quality scoring. A small classifier or an LLM rubric to rank or filter, with a human audit of the thresholds.
- Decontamination. Remove anything overlapping your evaluation data.
- Balance. Check coverage across topics, lengths, difficulty and edge cases.
- Document the result in a data card.
Near-duplicate removal
from datasketch import MinHash, MinHashLSH
def shingles(text, n=5):
toks = text.lower().split()
return {" ".join(toks[i:i + n]) for i in range(max(len(toks) - n + 1, 1))}
def minhash(text, num_perm=128):
m = MinHash(num_perm=num_perm)
for s in shingles(text):
m.update(s.encode("utf-8"))
return m
def dedupe(texts, threshold=0.8):
lsh, keep = MinHashLSH(threshold=threshold, num_perm=128), []
for i, t in enumerate(texts):
m = minhash(t)
if not lsh.query(m):
lsh.insert(str(i), m)
keep.append(i)
return keep
kept = dedupe(corpus)
print(f"kept {len(kept)} of {len(corpus)}")
Decontamination
def ngrams(text, n=13):
t = text.lower().split()
return {" ".join(t[i:i + n]) for i in range(max(len(t) - n + 1, 1))}
eval_grams = set().union(*(ngrams(x["prompt"] + " " + x["answer"]) for x in eval_set))
def contaminated(example):
return bool(ngrams(example["prompt"] + " " + example["answer"]) & eval_grams)
clean_train = [x for x in train if not contaminated(x)]
Also split by source or entity, not randomly, when near-duplicates are likely: all variants of one customer's invoices go to either train or test, never both.
Synthetic data
A strong model can generate training examples, which is how many instruction datasets were built. Done well it is powerful; done carelessly it bakes in errors.
def synth(seed_topic: str, n: int = 5):
out = llm(f"""Write {n} diverse, realistic user requests about: {seed_topic}.
Vary length, tone and difficulty; include two tricky edge cases. Return a JSON list of strings.""", 1200)
return json.loads(out)
def keep(prompt, answer, verify):
return verify(prompt, answer) # unit test, schema check, second-model check, or human
Principles:
- Verify. Prefer outputs that can be checked (code that runs, JSON that validates, maths that matches).
- Diversify. Seed with varied topics, personas and formats, or you get thousands of near-identical examples.
- Keep humans in the loop on a sample, and on the hard cases.
- Mix with real data. Training only on model outputs risks model collapse: repeated generations narrow the distribution and lose rare cases.
- Mind licences of the generating model.
A data card
Write down: purpose, sources, collection dates, filters applied, known gaps and biases, dedup and decontamination results, licences and intended use. Future you, and your reviewers, will need it.
Common mistakes
- Random train/test split when near-duplicates exist, which inflates scores.
- Never reading a sample of the final dataset.
- Generating 50,000 synthetic examples from one prompt.
Exercises
- Run the dedupe on a real corpus and read ten flagged pairs to check the threshold.
- Generate 200 synthetic examples, verify automatically, then audit 30 by hand and report the error rate.
You are done when you can…
- Deduplicate a text dataset with MinHash and report how many near-duplicates you found
- Check a training set for overlap with an evaluation set
- Generate synthetic data with a verification step and explain the risk of model collapse