Curator

Clean, filter and deduplicate a sample company dataset with NeMo Curator so a fine-tune trains on the text you intended.

Updated CompTech Team

A fine-tune copies what is in the file. Duplicate policies, empty rows and a footer repeated on every page become answers. NeMo Curator is the step that makes the training file smaller and more honest before Customizer sees it.

This lab starts from a JSONL export of company documents, drops rows that are too short to be a policy, and removes exact duplicates with Curator's exact-deduplication workflow. The output is a chat-format JSONL the next lab trains on.

Prerequisites

  • Python 3.11 or newer, and NeMo Curator installed in an OpenShift AI workbench or a pod with a GPU
  • Ray, which the deduplication workflow starts for you through RayClient
  • A directory raw/ of .jsonl files with a text field

One raw line looks like this. Real exports are messier. Leave the mess in. The lab is the cleaning.

{"id": "hr-14", "text": "Password resets are requested in the IT portal. The portal asks for the employee id and the manager's approval."}

Look at the file, then filter

Count rows, empty texts and very short texts before you change anything. You want a before-and-after count, or you will not know whether the filter did what you think.

jq -r '.text' raw/*.jsonl | wc -l
jq -c 'select((.text | length) < 40)' raw/*.jsonl | wc -l

Write a cleaned JSONL that keeps the identifier, strips whitespace and drops short rows. Forty characters is a starting threshold for this sample, not a rule for every corpus. A table of figures or a one-line title can be short and still worth keeping. Inspect what you dropped.

import json, pathlib
out = pathlib.Path("clean/policy.jsonl")
out.parent.mkdir(exist_ok=True)
kept = 0
with out.open("w") as w:
    for path in pathlib.Path("raw").glob("*.jsonl"):
        for line in path.read_text().splitlines():
            row = json.loads(line)
            text = " ".join(row.get("text", "").split())
            if len(text) < 40:
                continue
            row["text"] = text
            w.write(json.dumps(row, ensure_ascii=False) + "\n")
            kept += 1
print("kept", kept)

Curator ships heuristic filters for the same job at corpus scale: word count, ratio of letters to other characters, and repeated n-grams that mark boilerplate. Use those when the export is too large for a one-process script. The script above is here so you can see every row that disappeared.

Exact duplicates

Exact deduplication hashes each text and groups the hashes. Two copies of the same policy, exported from two systems, collapse to one. Near-copies (the same page with a different footer) need fuzzy deduplication, which uses MinHash and locality-sensitive hashing. Run exact first. It is cheaper, and it makes the fuzzy pass smaller.

The workflow below matches current NeMo Curator: it assigns an id, writes the duplicate ids under results/, and leaves removal as a second step so you can read what it found.

from nemo_curator.core.client import RayClient
from nemo_curator.stages.deduplication.exact.workflow import ExactDeduplicationWorkflow
from nemo_curator.stages.text.deduplication.removal_workflow import TextDuplicatesRemovalWorkflow

ray = RayClient()
ray.start()
exact = ExactDeduplicationWorkflow(
    input_path="clean/",
    output_path="results/",
    text_field="text",
    assign_id=True,
    perform_removal=False,
    input_filetype="jsonl",
)
print(exact.run().metadata)

removal = TextDuplicatesRemovalWorkflow(
    input_path="clean/",
    ids_to_remove_path="results/ExactDuplicateIds",
    output_path="deduped/",
    input_filetype="jsonl",
    input_id_field="_curator_dedup_id",
    ids_to_remove_duplicate_id_field="_curator_dedup_id",
    id_generator_path="results/exact_id_generator.json",
)
removal.run()

result.metadata includes how many duplicates were found. Write that number in the lab notes with the input path. Fuzzy deduplication is the same shape of workflow when you still see near-copies in deduped/.

The file Customizer expects

Supervised fine-tuning wants conversations, not bare paragraphs. Turn each kept policy into one example. The assistant answer must be text that really is in the policy. Do not paraphrase by hand into facts the document does not contain.

{"messages": [
  {"role": "system", "content": "You answer questions about company policy. If the policy does not say, say so."},
  {"role": "user", "content": "How do I reset my password?"},
  {"role": "assistant", "content": "Request the reset in the IT portal. The portal asks for your employee id and your manager's approval."}
]}
wc -l train.jsonl
jq -c 'select(.messages | length < 3)' train.jsonl

The second command should print nothing. Split out a valid.jsonl of questions you will not train on. Evaluator uses that file. If the validation questions were in the training file, the score only tells you the model memorised them.

What's next

  1. Customizer fine-tunes a Llama or Mistral model on train.jsonl.
  2. Evaluator scores that model on valid.jsonl.
  3. The documents you did not turn into training rows still belong in the Retriever index.