0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Trouble in the training data
Brightcart's training data is nearly ready: curated real chats, expert-written examples and verified synthetic conversations. Dev opens it for a final look, and finds trouble. One template reply appears thirty times. A customer's phone number slipped through. In some examples, the customer's words are labelled as the assistant's. And a Spanish conversation is tagged as English. Any of these could quietly damage the fine-tuned model. Today, the final step before training: inspecting, deduplicating, cleaning, filtering and formatting the data, in a pipeline you can rerun and trust.
Season Five, Module Twenty, final stop: data processing.
0:57Inspect your data first
Before any automatic processing, look at your data. Plot simple statistics: lengths, number of turns, languages, topics, sources, dates. List the most repeated replies, which is how that thirty-times template jumps out. Find the outliers: empty replies, enormous ones, garbled text. And read a random sample of examples yourself, every single time. Experienced engineers will tell you that half an hour of simply looking at data catches problems that weeks of training would only reveal after the damage is done.
1:33Deduplication: hashes, shingles and Jaccard
Duplicates come in two kinds. Exact duplicates are easy: compute a fingerprint, called a hash, of each normalised text, and keep one copy of each. Near-duplicates are trickier: the same message with an extra comma or word. A common method splits each text into overlapping groups of three words, called shingles, and measures the share they have in common, the Jaccard similarity. "Where is my order B-4127 it has not arrived" and the same message ending "yet" share most of their shingles: a similarity of zero point eight eight, so they're near-duplicates.
Compare either with "my kettle arrived dented", and the similarity is zero. With millions of examples, comparing every pair would take forever, so techniques like MinHash with locality-sensitive hashing quickly sort likely-similar texts into the same buckets, and only those are compared. Why bother? Duplicates make the model over-learn some examples, like that template, encourage memorisation, and can sneak test examples into training.
2:46Cleaning and filtering data
Next, clean and filter, as a cascade of stages. Remove personal data: patterns catch emails, phone numbers and card numbers, and a name-recognition model catches names, patterns catch addresses, each replaced with a consistent placeholder like "phone". Remove abusive or unsafe content, unless it's deliberately part of a safety dataset. Strip boilerplate, like email signatures and automatic replies. Drop replies that cite outdated policy versions. Detect each example's language and fix wrong tags. And filter by length and quality, using quality-scoring models, and perplexity from episode fifty-four as a warning sign for gibberish or boilerplate.
3:31Formatting data for training: chat templates
Then format everything for training. Convert every example into exactly the same chat template the model will see in production, with system, user and assistant roles. Validate the roles: turns should alternate, and the customer's words must never be labelled as the assistant's, as happened in Dev's sample. Mask the loss so the model learns only from the assistant's replies. Tokenise and check lengths against the maximum, and handle long examples carefully. Cutting them off mid-reply would teach the model to stop mid-sentence.
And validate any structured outputs against their schema.
4:14Train-test splits and data leakage
Now split the data, carefully. A random split can put different messages from the same conversation, or the same customer, into both training and test sets, and the test score will look better than it really is: leakage again, from episode eight. Instead, split by group: all of a customer's examples go to the same side. Where time matters, train on older data and test on newer. And finally, decontaminate: remove any training example that's a near-duplicate of something in your evaluation sets.
4:49A reproducible data pipeline
All of this belongs in a pipeline written as code, not a series of manual edits. Every run produces a new, versioned dataset, logs how many examples each stage removed, and comes with a datasheet: where the data came from and under what consent, which filters and thresholds were used, the known gaps and biases, and what the data is for. Every fine-tuned model records which dataset version trained it. That makes results reproducible, problems traceable, and it means that when a customer asks for their data to be deleted, the next dataset version can honour it.
5:30Try it yourself: words vs shingles
Compare two messages: "can I return my lamp" and "can I return the lamp please". First, using sets of single words, then using three-word shingles. Pause and estimate both. With single words, they share four of seven distinct words: a Jaccard similarity of about zero point five seven. With three-word shingles, they share just one of six: about zero point one seven. Shingles care about word order and context, so they flag only genuine near-copies, like template replies, rather than any two messages about returning a lamp.
6:17MinHash: why near-duplicate search needs a trick
How do you find near-duplicates among forty thousand chats? Comparing every pair means almost eight hundred million comparisons. Instead, each chat gets a short signature, say a hundred and twenty-eight special hash values, chosen so that the share of matching values estimates the Jaccard similarity, typically to within about four hundredths. Then signatures are sorted into buckets, so only chats that land in the same bucket get compared. Hundreds of millions of comparisons shrink to a manageable list of likely candidates.
6:54Pause & Predict: split by message or conversation?
To create training and test sets, should Brightcart shuffle individual messages, or split whole conversations? Pause. Whole conversations, and ideally whole customers. Shuffle individual messages, and turns from the same chat end up on both sides, so the model has effectively seen the test conversation during training, and the scores flatter it. That's leakage again, in a new disguise. And as an extra safety check, run near-duplicate detection across the two sets, and remove anything in the test set that closely matches the training data.
7:37What inspecting the raw data revealed
Before any processing, look at the data. When Brightcart first inspected the raw export of forty thousand chats, the dashboard immediately showed problems. A spike of completely empty replies, caused by a logging bug. A long tail of five-thousand-token monsters, where customers pasted whole email chains. One template reply repeated three hundred times. And a slice of garbled text from an encoding error. Each discovery becomes a rule: drop empty replies, split or remove the monsters, keep just a few copies of each template, and fix the encoding.
Five minutes of looking saves weeks of confusion.
8:21Exact duplicates: normalise, then hash
Exact duplicates are easy to find, once you normalise. "Thanks, your refund is on its way!" and the same sentence in lower case, with an extra space and no exclamation mark, look different to a computer. So first lowercase everything, collapse spaces and strip stray punctuation, then compute a short fingerprint, a hash, of each message. Identical fingerprints mean duplicates. It handles millions of messages in seconds, and catches the obvious copies before the cleverer near-duplicate search.
8:54Filters with reasons
Every filter is a decision, so each one is written down with its reason. Unsupported languages are removed or moved to their own set. Messages under five tokens, like "ok" or "thanks", are removed. Anything still containing personal data after redaction is fixed or dropped. Rude agent replies are removed, but abusive customer messages are kept when they're paired with excellent handling, because that's exactly what the model needs to learn. And replies quoting outdated policies go. Then someone reads a sample of what each filter removed, to make sure it isn't throwing away good data.
9:35Formatting chats into the chat template
Finally, each chat is converted into the exact format the model will be trained on: Brightcart's system prompt, then alternating customer and assistant turns, in the model's chat template. Internal notes, agent names and timestamps are stripped out, because the model should never learn to write "customer is being difficult" in a reply. Every example is checked: it must parse correctly and fit within the training length, and long chats are trimmed at turn boundaries, never mid-sentence.
10:09Reproducible, end to end
The whole pipeline lives in code: inspect, deduplicate, filter, redact, format and split, all driven by one configuration file with fixed random seeds. The output is a versioned dataset with a fingerprint. Run the pipeline again on the same inputs, and you get exactly the same fingerprint. That means anyone can reproduce the dataset behind any model, and the data card can be generated automatically from the pipeline's own logs.
10:41Pause & Predict: how many copies of a template?
In the raw export, one template reply appeared three hundred times. Should Brightcart keep all of them, none, or a few? Pause. A few, perhaps three, each in a different conversation. Keeping all three hundred would over-weight that one sentence, and encourage the model to repeat it word for word everywhere. Removing it entirely would lose a perfectly good answer. Deduplication isn't about deleting; it's about giving every pattern its fair share.
11:18Process the evaluation data too
The evaluation data needs the same care. Brightcart runs the golden set through redaction and formatting, and, crucially, checks it against every training set for exact and near-duplicates. If any test item, or a close copy of it, has slipped into the training data, it's removed, and the check is recorded on both data cards. A clean training set paired with a leaky test set still produces a score you can't trust.
11:48Using a model to score data quality
At scale, a model can help score quality too. A small classifier, or a validated judge, rates each remaining reply against Brightcart's guidelines: correct, complete, polite and properly formatted. The best replies are kept, borderline ones go to a person, and poor ones are dropped. Then comes the audit: someone reads fifty random kept replies and fifty random dropped ones. If short but perfect answers are being thrown away, the scorer has a length bias, and its threshold or training needs fixing.
12:24Myth vs Fact
Myth: data cleaning is boring busywork. Fact: decisions about what to remove, how to deduplicate and how to split directly shape how the model behaves, and whether your evaluation scores can be trusted at all.
12:41Recap
Let's recap. Always inspect data first, with statistics and by reading samples. Remove exact duplicates with hashes, and near-duplicates with shingles and Jaccard similarity, or MinHash at scale. Clean out personal data, harmful content, boilerplate and outdated material, and fix languages and lengths. Format everything in the production chat template, with valid roles and loss masks. Split by customer or conversation, decontaminate against evaluation sets, and run it all as a versioned, documented pipeline. One sentence to remember: look, dedupe, scrub, format, and split as if the test depended on it, because it does.
13:27Next: inference speed and accelerators
Mission complete. Brightcart's training dataset is clean, deduplicated, free of personal data, correctly formatted, safely split and fully documented, ready for the fine-tune. That completes Module Twenty, dataset engineering. Next, the speed and cost of every answer. Customers say the assistant sometimes feels slow. Where does the time actually go, and what hardware is doing the work? Inference metrics and accelerators, next. See you on the map.