Skip to content

About

Zero-dependency MinHash + LSH near-duplicate detector for LLM datasets (JSONL/text). Clean fine-tuning & RAG data locally — no embeddings, no API keys.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

29 Commits

Folders and files

Repository files navigation

minhash-dedupe

Find and remove near-duplicate documents in LLM datasets — locally, with zero dependencies and no embeddings or API keys.

minhash-dedupe demo

Near-duplicate examples are silent poison in LLM pipelines. In fine-tuning data they cause overfitting and inflate the apparent dataset size; in RAG corpora they crowd out diverse context and waste tokens; across train/eval splits they leak answers and contaminate your benchmarks. Exact-match dedup misses them because the texts differ by a word or two.

minhash-dedupe catches them using the classic MinHash + LSH recipe — the same technique used by large-scale dataset curation pipelines — implemented in pure ESM JavaScript with no dependencies. It runs anywhere Node 18+ runs, needs no GPU, no Python, and no embedding service.

Features

  • Near-duplicate detection via MinHash signatures and estimated Jaccard similarity.
  • LSH banding prefilter so you avoid the O(n²) all-pairs comparison on large datasets.
  • Transitive clustering (union-find) — A≈B, B≈C ⇒ one cluster, one kept representative.
  • Works on any dataset shape: plain text, {"text": ...}, {"prompt": ...}, or OpenAI/Anthropic-style {"messages": [...]} chat data.
  • CLI and library APIs. Print a report or write a cleaned file.
  • SimHash fingerprints included as a fast alternative for very short texts.
  • Zero dependencies, tested with node --test (57 unit tests).

Install

# Run without installing
npx minhash-dedupe data.jsonl --field text

# Or install globally
npm install -g minhash-dedupe

Or clone and use directly (no build step):

git clone https://github.com/Ayubjon/minhash-dedupe.git
cd minhash-dedupe
node bin/cli.js examples/sample.jsonl --field text -t 0.6 -k 2

CLI usage

minhash-dedupe <input> [options]

Input:
  A .jsonl file (one JSON object per line) or a .txt file (one document per line).
  Use "-" to read from stdin.

Options:
  -f, --field <path>     Field to compare (dotted path; "messages" for chat data).
                         Auto-detected (text/prompt/content/...) when omitted.
  -t, --threshold <num>  Jaccard similarity to treat as duplicate (default 0.7).
  -k, --shingle <num>    Shingle size (default 3).
      --mode <word|char> Shingle mode (default word).
  -n, --hashes <num>     Number of MinHash permutations (default 128).
  -o, --out <file>       Write deduplicated JSONL/TXT here (drops duplicates).
      --keep-dupes <f>   Also write the removed records to this file.
      --json             Print the raw result as JSON instead of a report.
      --no-strip         Do not strip punctuation before shingling.
  -h, --help             Show this help.

Examples

# Report near-duplicates in a fine-tuning file
minhash-dedupe data.jsonl --field text --threshold 0.8

# Deduplicate OpenAI chat data and write a clean file
minhash-dedupe chat.jsonl --field messages -o clean.jsonl --keep-dupes removed.jsonl

# Character shingles for short strings, reading from stdin
cat docs.txt | minhash-dedupe - --mode char -k 5

Example run on the bundled sample:

$ minhash-dedupe examples/sample.jsonl --field text -t 0.6 -k 2

minhash-dedupe report
==========================================
Documents scanned : 8
Similarity threshold: 0.6
Candidate pairs   : 3
Confirmed pairs   : 3
Duplicate clusters: 1
Kept              : 6  (removed 2, 25.0%)

Duplicates (dropped -> kept representative):
  #1  ~  #0   (similarity 0.66)
  #4  ~  #0   (similarity 1.00)

Library usage

import { dedupe, dedupeRecords, parseJsonl } from 'minhash-dedupe';

// Plain text documents
const docs = [
  'The quick brown fox jumps over the lazy dog near the river.',
  'The quick brown fox jumps over the lazy dog beside the river.', // near-dup
  'Neural networks learn representations from raw data.',
];

const result = dedupe(docs, { threshold: 0.7, k: 2 });
console.log(result.keptIndices);       // indices to keep
console.log(result.duplicates);        // [{ index, representative, similarity }]
console.log(result.clusters);          // groups of duplicate indices

// JSONL records — keeps the original objects, annotates the removed ones
const rows = parseJsonl(jsonlText);
const { kept, removed } = dedupeRecords(rows, { field: 'text', threshold: 0.8 });

Lower-level building blocks are exported too: shingles, minhashSignature, estimateJaccard, exactJaccard, chooseBands, candidatePairs, clusterPairs, simhash, hammingDistance.

How it works

  1. Shingling — each document becomes a set of overlapping word (or character) k-grams.
  2. MinHash — each set is reduced to a fixed-length signature of numHashes minimum hashes. The fraction of matching positions between two signatures estimates their Jaccard similarity.
  3. LSH banding — signatures are split into bands; documents that match in any band become candidate pairs. This prunes the comparison space so you never compare every pair. The band split is auto-tuned to your threshold (biased for recall).
  4. Verification — each candidate pair's estimated Jaccard is checked against your threshold; only confirmed pairs survive.
  5. Clustering — confirmed pairs are merged transitively with union-find. The lowest-index member of each cluster is kept; the rest are reported as duplicates.

Choosing parameters

  • --threshold: higher = stricter (fewer removals). 0.9+ ≈ "almost exact", 0.6–0.8 catches paraphrases and edits.
  • --shingle (k): smaller k (2–3) is more sensitive to short overlaps; larger k (4–5) demands longer shared runs. Use --mode char with larger k for code or short strings.
  • --hashes: more permutations = more accurate Jaccard estimates at higher cost. 128 is a good default; 256 for tighter accuracy.

Development

npm test   # runs node --test over the unit suite

Support

If this tool saved you time, an optional tip is always welcome (never expected):

Please send only on the Ethereum (ERC-20) network. Other networks may result in lost funds.

License

MIT © 2026 Ayubjon

About

Zero-dependency MinHash + LSH near-duplicate detector for LLM datasets (JSONL/text). Clean fine-tuning & RAG data locally — no embeddings, no API keys.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages