Component · L5
Synthetic Data
github.com/juanmmm21/synthetic-data-generator
An automated engine to generate and filter high-quality synthetic instruction datasets using LLMs.
- Lines
- 260
- Files
- 6
- Test files
- 1
- Commits
- 9
- Started
- 26 Jun 2026
- Last commit
- 6 Jul 2026
- Python 100%
● Where it sits
Layer 5 of AI Core — mlops & quality.
L1 Data & input
L2 Representation & retrieval
L3 Inference & control
L4 Agents
L5 MLOps & quality
↳ Flagship
README · 3 min read
Commits per day
Automated engine for generating, semantically mutating, and filtering high-fidelity synthetic datasets. This module facilitates the structured creation of training data in two critical language model alignment formats: Instruction Tuning and Direct Preference Optimization (DPO).
The module solves the problem of scarce clean training data within the local infrastructure through deterministic linguistic expansion algorithms and strict duplicate control.
Generation Methodology and Workflow
The ingestion and refinement of synthetic samples follows a multi-step architecture designed to maximize the entropy (diversity) of the final dataset.
flowchart TD
Topics[Seed Semantic Topics: RAG, Embeddings, DB] --> Mutator[Prompt Mutator: Self-Instruct Style]
subgraph Semantic Mutation
Mutator --> Choice{Random Selection}
Choice -->|Action Verb| Verbs[write, develop, optimize...]
Choice -->|Technical Synonym| Syns[RAG pipeline, HNSW, similarity...]
Choice -->|Modifier| Mods[with strict typing, step by step...]
Verbs & Syns & Mods --> Concat[Sentence Structures]
end
Concat --> CandidatePrompt[Candidate Prompt]
CandidatePrompt --> DedupFilter[Jaccard Diversity Filter]
subgraph N-Gram Deduplication
DedupFilter --> TriGram[Extract Character Tri-grams]
TriGram --> Overlap{Jaccard Overlap > Threshold?}
Overlap -->|Yes - Redundant| Reject[Discard Prompt]
Overlap -->|No - Unique| Accept[Accept Prompt]
end
Accept --> OutputGen[Synthetic Response Generator]
subgraph Sample Creation
OutputGen --> SFT[Instruction Tuning Sample: Instruction + Chosen]
OutputGen --> DPO[DPO Sample: Prompt + Chosen + Rejected]
end
SFT --> SFTFile[synthetic_instruction_dataset.json]
DPO --> DPOFile[synthetic_dpo_dataset.json]1. Semantic Input Mutation (Self-Instruct Methodology)
To avoid creating datasets with homogeneous directives that induce overfitting bias in the LLM, the SyntheticDataGenerator module implements a linguistic mutation engine:
- Vocabulary Expansion: For each seed topic (RAG, embeddings, databases), equivalent technical synonyms, action verbs, and format or style modifiers are defined.
- Structural Combinatorics: The mutator pseudo-randomly combines these grammatical pieces, injecting them into directive templates (questions, code requirements, tutorial requests), producing a diversified collection of unique prompts from a small number of basic topics.
2. N-Gram Similarity Diversity Filter
Each generated prompt passes through a heuristic deduplication filter before being added to the final dataset:
- Tri-gram Decomposition: The candidate prompt and each of the previously accepted prompts are lowercased, stripped of spaces, and segmented into 3-character sequences (tri-grams). Let be the set of tri-grams of text .
- Jaccard Coefficient: The degree of lexical overlap is computed:
- Threshold Cutoff: If (where is the
dedup_threshold, defaults to0.50), the instruction is considered semantically redundant and immediately discarded to force thematic diversity in the corpus.
3. DPO Preference Pair Generation
To support DPO (Direct Preference Optimization) training, the generator associates each accepted prompt with a pair of contrasted responses:
- Chosen Response: Represents optimal behavior. It is a response with high technical rigor, correct data typing, free of placeholders, and with detailed theoretical explanations.
- Rejected Response: Simulates anomalous or hallucinated behavior (logic errors in the code, incorrect conceptual explanations, non-existent dependencies, or empty comments).
By training an LLM with this pair of examples, the DPO loss algorithm maximizes the probability of the preferred behavior and minimizes that of the rejected one, achieving a more stable safety alignment than traditional RLHF techniques.
Connection to the Ecosystem
This module acts as a synthetic data provider for training and evaluation:
- llm-qlora-finetuner: Directly consumes the JSON files generated by this module (
synthetic_instruction_dataset.jsonandsynthetic_dpo_dataset.json) to perform local parametric (LoRA) training of causal models. - llm-eval-harness: Uses the high-quality instructions and response pairs to establish benchmarks and measure accuracy deviations in the LLM after fine-tuning.
- dataset-version-control: Manages change control of the generated JSON files, recording semantic differences and versioning the datasets to ensure training traceability.
Project Structure
generator.py: Defines theInstructionSampleandDPOSamplePydantic models, and the mainSyntheticDataGeneratorclass with the mutator and Jaccard filters.test_generator.py: Unit test suite that verifies the Jaccard overlap calculation, automatic deduplication, and the generated Instruction and DPO data formats.example.py: Interactive demo run that generates 12 fine-tuning samples and DPO interactions, saving the results to disk in JSON format.
Installation and Usage
1. Activate the Local Environment and Install Dependencies
Make sure to enable the virtual environment before installing:
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
2. Run Quality Tests
.venv/bin/python -m unittest test_generator.py
3. Generate Synthetic Datasets
To run the generator and export the JSON files in the subproject:
.venv/bin/python example.py
The script will compile the dataset and generate the output files synthetic_instruction_dataset.json and synthetic_dpo_dataset.json, ready to be consumed by the training pipeline.