Fine-tuning basics (TypeScript) · Chapter 4 of 20
Duplicates and conflicts
By the end of this chapter you can find messages that say the same thing, keep one copy of each with the tag most of the desk agreed on, set aside the ones the desk disagreed about, and show with a number what duplicates do to a test score. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The data
- 4 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of Fine-tuning basics (TypeScript) are free. This one comes with the all labs plan.
Why you need this
After chapter 3 every message is clean, and many of them are the same. Customers send a message twice when the first one seems stuck. They send it again a minute later with ?? after it. Hundreds of customers write the same short things: "ho gaya thanks", "office ka number kya hai". And Chulha's own WhatsApp broadcast comes back, forwarded by customers who did not read it.
Irfan has two worries. The first is about training: a message seen twenty times counts twenty times, so the model leans towards whatever is common, and a slip of the desk repeated is a slip learned. The second is about testing, and it is worse: if a test message is also in training, the test score measures memory, not skill. Mangala does not want a number that looks good because the exam paper was in the textbook.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
dedupe-key.ts: one key for the same message
Export dedupeKey(text): from a cleaned message, keep only a to z, 0 to 9 and spaces (every other character becomes a space), turn runs of spaces into one, and trim. dedupeKey('cylinder kab aayega ??') is 'cylinder kab aayega'; dedupeKey('rasta band hai kya, delivery nhi aayi') is 'rasta band hai kya delivery nhi aayi'.
Trap: a mark between two words must leave a space, not glue them together.
- Task 2
dedupe.ts: one row per message, with the majority's tag
Export dedupe(rows) returning { kept, conflicts, removed }: group the rows by dedupeKey(row.text), groups in the order their first row appears. In each group count the votes per tag. If the top two tags have the same count, the group is a conflict: add its key to conflicts and keep none of its rows. Otherwise keep the group's first row, with its tag set to the tag most rows had. removed is rows.length minus kept.length.
Under if (import.meta.main), dedupe cleanRows() (given in cleaned.ts) and print:
N rows: K kept, R removed, C conflicts dropped
- Task 3
conflicts.ts: the conflicts, for Mangala
Dedupe cleanRows() with your dedupe. For each conflict key, in the order dedupe returns them, count the tags of the cleaned rows whose dedupeKey(text) is that key, and print one line:
KEY: TAG N, TAG N (most rows first; the same count keeps the order of first appearance)
then one line: C conflicts for Mangala
- Task 4 · interview question
Interview: open-book.ts, what do duplicates do to a test score?
Irfan: "If a test message is also in training, the test is an open-book exam. How open was ours?" Export overlap(train, test): the number of test rows whose dedupeKey(text) some train row has.
Under if (import.meta.main), split twice the same way, test as every fifth row (index i % 5 === 0) and train the rest: first the rows of cleanRows(), then the kept rows of dedupe(cleanRows()). Print:
before dedupe: N of T test messages are also in train (P%)
after dedupe: N of T test messages are also in train (P%)
(P with one decimal)
What you will learn
- A dedupe key: when two messages count as the same.
- Majority tags: duplicates as votes, and the tag most of them have.
- Conflicts: messages tagged two ways, equally; dropped, and listed for a person.
- Open-book tests: how duplicates inflate a score, measured.
More in The data
Build "Duplicates and conflicts" yourself
Chulha, a Nagpur startup, tags WhatsApp messages for 40 LPG gas agencies, and a missed gas leak is not an option. You fine-tune chota, a tiny transformer, until it beats the best prompt while forgetting little. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Fine-tuning basics (TypeScript) are free. This one comes with the all labs plan.