Data Quality: The First Principle of Fine-Tuning

In model fine-tuning, there is a repeatedly validated conclusion: a high-quality small dataset is far superior to a low-quality large dataset. The LIMA paper even demonstrated that using only 1,000 carefully crafted instruction examples can bring a model close to GPT-4 performance. Data construction is not a matter of "the more, the better," but an art of "the finer, the better." A good instruction example should have: clear and unambiguous instructions, accurate and complete responses, consistent formatting, and coverage of real-world usage scenarios.

Data Sources and Collection

Common sources for instruction fine-tuning data: Human-written (highest quality but costly, suitable for seed datasets), Filtered from existing datasets (real user conversations like ShareGPT/WildChat, broad coverage but require cleaning), Self-Instruct generation (using a strong model to generate instruction-response pairs, efficient but may introduce model bias), Reverse-engineered from documents/codebases (converting document passages into instruction format). In practice, projects often use a mix: 50% human seeds + 30% Self-Instruct + 20% real user data.

Data Cleaning Pipeline

import re, json
from openai import OpenAI

client = OpenAI(api_key="your-deepseek-api-key", base_url="https://api.deepseek.com")

class DataCleaner:
    def __init__(self):
        self.filters = []

    def clean(self, samples):
        cleaned = []
        for s in samples:
            # 1. Remove empty and too-short content
            if not s.get("instruction") or len(s["instruction"]) < 10:
                continue
            if not s.get("output") or len(s["output"]) < 20:
                continue
            # 2. Deduplicate (based on instruction similarity)
            if self._is_duplicate(s, cleaned):
                continue
            # 3. Format cleaning
            s["instruction"] = self._normalize(s["instruction"])
            s["output"] = self._normalize(s["output"])
            cleaned.append(s)
        return cleaned

    def _is_duplicate(self, sample, existing, threshold=0.9):
        for e in existing[-100:]:  # Only check the last 100 entries
            if sample["instruction"] == e["instruction"]:
                return True
        return False

    def _normalize(self, text):
        text = re.sub(r"\s+", " ", text)
        return text.strip()

    def quality_score(self, sample):
        """Use DeepSeek to score data quality"""
        prompt = f"""Evaluate instruction fine-tuning data quality (0-100):
Instruction: {sample['instruction']}
Response: {sample['output'][:500]}
Scoring dimensions: clarity, accuracy, completeness, usefulness
Return JSON: {{"score":85,"issues":[]}}"""
        resp = client.chat.completions.create(model="deepseek-chat",
            messages=[{"role":"user","content":prompt}])
        return json.loads(resp.choices[0].message.content)

cleaner = DataCleaner()
raw = [{"instruction": "Explain what machine learning is", "output": "Machine learning is..."}]
print(f"After cleaning: {len(cleaner.clean(raw))} entries")

The Art of Data Mixing

Different types of instruction data need a reasonable mix, which directly determines the balance of model capabilities: General Q&A 40% (ensures basic conversational ability), Code generation 20% (programming ability), Reasoning/Math 15% (logical ability), Creative writing 10% (text generation diversity), Safety/Alignment 10% (refusing inappropriate requests), Specific domain 5% (vertical scenario capability). The mix is not fixed—if your model is primarily for code scenarios, you can increase code generation to 40% and correspondingly reduce the general Q&A ratio.

Data Augmentation Strategies

When data is insufficient, you can apply data augmentation techniques: Instruction rewriting (rewrite the same intent in 5-10 different phrasings), Response diversification (generate responses of different styles and lengths for the same instruction), Increasing difficulty (add extra constraints to basic instructions, e.g., upgrade "implement in Python" to "implement in Python with O(n) time complexity"), Reverse generation (given a high-quality response, generate a matching instruction in reverse). However, note that augmentation may introduce noise; after each augmentation, re-evaluate quality.

Real Case of Data Quality Issues

In a project to build a fine-tuning dataset for a medical consultation model, we deeply experienced the decisive role of data quality. The initial dataset contained 12,000 doctor-patient conversations scraped from online forums, which after initial screening left 8,000. After the first round of fine-tuning, the model performed terribly—it gave seemingly professional but actually incorrect medical advice. In-depth investigation revealed three typical problems: Format inconsistency (in 30% of conversations, the doctor's reply was mixed with the patient's next question due to HTML parsing errors during scraping), Content pollution (15% of "doctor" replies actually came from non-professional comments incorrectly labeled as best answers), Distribution bias (80% of data came from dermatology and pediatrics, leaving the model almost ignorant of cardiovascular and cerebrovascular issues). After three rounds of rigorous data cleaning—manual review of 1,000 seed data, automatic scoring of the remaining data using evaluation criteria annotated by medical experts, and retaining only data with scores >80—the final dataset was reduced to 2,800 entries. But it was this 2,800-entry high-quality dataset that trained a model scoring 4.2/5 in blind evaluation by real doctors, far surpassing the model trained on 8,000 dirty data (2.1/5). This case again demonstrates the critical importance of data quality.

This confirms the conclusion of the LIMA paper: quality is far more important than quantity.

Data Diversity: Avoiding Model "Subject Bias"

Insufficient data diversity is the second leading cause of fine-tuning failure (the first is poor data quality). A classic negative example: a team fine-tuned a model with 5,000 "customer service dialogues" and found that the model responded to any question like a customer service representative—even when asked "Explain relativity," it replied, "Dear user, regarding your question about relativity, I have recorded it for you..." This is "role fixation" caused by insufficient data diversity. How to avoid it: Instruction diversity (the dataset should include various instruction types such as Q&A, writing, code, reasoning, translation), Style diversity (responses should not have only one style; there should be concise and detailed, formal and casual), Domain diversity (even if the model primarily serves the financial sector, it should include 5-10% of data from other domains to prevent overfitting).

Automation and Quality Control in Data Annotation

High-quality instruction data often requires manual annotation, but pure manual annotation is too costly (3-5 yuan per item) and slow. We adopted a hybrid approach of "AI-assisted annotation + manual review": Round 1: AI generates candidates (DeepSeek generates 5 candidate responses based on seed examples and task descriptions) → Round 2: AI initial screening (another DeepSeek instance evaluates the 5 candidates, selects the best 2, and points out their respective pros and cons) → Round 3: Manual final review (annotators choose the best from the 2 candidates or provide modification suggestions). This process increased annotation efficiency by 4 times (from 20 to 80 items per day) while maintaining high quality (manual final review found that AI initial screening had an accuracy of about 82%, meaning only 18% required manual re-selection). Annotation consistency is monitored via Fleiss' Kappa coefficient—when it falls below 0.7, annotators are retrained.

Want to orchestrate this skill chain yourself?

Open in Skill Chain →