Self-Instruct › Instruction Generation

Michael BrenndoerferUpdated February 5, 20267 min read
Language AI HandbookAll chapters
Part 36 of 67Instruction Tuning
Chapter 268Self-Instruct › Instruction Generation
Reading section 4 of 14 · Instruction Generation · 7 minSection 4 of 14 · 7 min

Book contents

Section 4 of 14 · 7 min read

Self-Instruct › Instruction GenerationOne section · Sections in this chapter4 / 14 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

With seed tasks established, the model generates new instructions through prompted generation. The approach samples several existing tasks from the pool and asks the model to create novel instructions inspired by, but different from, the examples. This in-context learning approach uses the model's ability to identify patterns and generate variations that maintain the same structure while introducing new content. The technique requires no special training objective: we simply ask the model to continue a numbered list of instructions, and it does exactly that.

The prompt structure is carefully designed to encourage diversity while maintaining quality. By showing the model a numbered list of existing instructions and asking it to continue the list, we tap into the model's natural tendency to generate coherent continuations. This is a direct application of the few-shot learning capability we studied in Part XXVIII. The model recognizes that it is creating items in a series and attempts to match the style and format as well as the diversity level of the demonstrated items. If the demonstrated items vary widely in topic and structure, the model's continuations will similarly vary.

A key design decision in the original paper was using a mix of human-authored seeds and machine-generated tasks in the in-context demonstration. This blend serves two purposes. The human seeds ensure a quality floor: the model always has access to carefully crafted examples as anchors. The machine-generated tasks provide additional diversity and demonstrate that the range of acceptable instructions extends beyond the narrow set of hand-crafted examples. Over time, as the pool grows, the proportion of machine-generated demonstrations increases, but the human seeds remain available for sampling.

The sampling strategy for selecting which tasks to include as demonstrations significantly affects what kinds of instructions the model produces. Purely random sampling tends to reflect the current distribution of the task pool, which means popular task types get over-represented in demonstrations and thus in new generations. More sophisticated sampling strategies can counteract this tendency by deliberately seeking out underrepresented task types or recently discovered categories that the model should explore further.

In[8]:
Code
def create_instruction_generation_prompt(sampled_tasks, num_to_generate=8):
    """
    Create a prompt for generating new instructions.

    Samples existing tasks and formats them as few-shot examples,
    then asks for new, diverse instructions.
    """
    prompt = """Come up with a series of tasks:

"""
    # Add sampled existing tasks as demonstrations
    for i, task in enumerate(sampled_tasks, 1):
        prompt += f"{i}. {task['instruction']}\n"

    # Request new instructions
    next_num = len(sampled_tasks) + 1
    prompt += f"{next_num}."

    return prompt
In[9]:
Code
import random

# Simulate instruction generation with sampled context
sampled = random.sample(seed_tasks, 3)
generation_prompt = create_instruction_generation_prompt(sampled)
Out[10]:
Console
=== Instruction Generation Prompt ===
Come up with a series of tasks:

1. Classify the sentiment of the given movie review.
2. Rewrite the sentence to make it more formal.
3. Convert the following temperature from Celsius to Fahrenheit.
4.

The sampling strategy for selecting demonstration tasks is important for diversity. In practice, rather than purely random uniform sampling, you can use a balanced approach that considers multiple factors.

  • Recency bias: Slightly favor recently added tasks to explore new directions. This helps the generation process branch out from recently discovered instruction patterns rather than always returning to the same seed tasks.
  • Diversity sampling: Ensure sampled tasks span different categories. If all demonstrations are sentiment classification tasks, the model will likely generate more sentiment classification tasks.
  • Human seed inclusion: Always include some original seed tasks to maintain quality anchoring. The human-written examples serve as a quality floor, preventing drift toward lower-quality patterns.

When the model generates instructions, it often produces multiple candidates at once. The prompt asks for several new instructions, and the model continues generating until it hits a stop condition or maximum length. This batch generation approach is more efficient than requesting one instruction at a time, and it provides the model with a longer rolling context that encourages diversity: if the model has already generated a classification task in this batch, it tends to generate something different next.

In[11]:
Code
def parse_generated_instructions(model_output):
    """
    Extract individual instructions from model output.

    The model typically generates numbered lists like:
    4. Instruction one
    5. Instruction two
    """
    instructions = []
    lines = model_output.strip().split("\n")

    for line in lines:
        line = line.strip()
        # Match numbered items like "4. Instruction text"
        if line and line[0].isdigit():
            # Remove the number and period
            parts = line.split(".", 1)
            if len(parts) > 1:
                instruction = parts[1].strip()
                if instruction:
                    instructions.append(instruction)

    return instructions
In[12]:
Code
# Example model output
mock_model_output = """4. Summarize the main argument of the given paragraph.
5. Write a product description for a smartwatch.
6. Identify the logical fallacy in the given statement.
7. Translate the English sentence to Spanish.
8. Suggest three alternative titles for the given book."""

parsed_instructions = parse_generated_instructions(mock_model_output)
Out[13]:
Console
Parsed instructions:
  1. Summarize the main argument of the given paragraph.
  2. Write a product description for a smartwatch.
  3. Identify the logical fallacy in the given statement.
  4. Translate the English sentence to Spanish.
  5. Suggest three alternative titles for the given book.

The parsing function successfully extracts the core instruction text from the raw model output, removing numbering and whitespace. This step normalizes the data into a structured list ready for filtering and instance generation. The simplicity of this parsing reflects a key design principle of Self-Instruct: use straightforward, predictable formats that are easy to both generate and parse. Complex formats introduce parsing failures and model confusion; simple numbered lists are reliable to the minor formatting variations that naturally occur in model outputs.

Balancing Human and Machine Demonstrations

One of the subtler aspects of the Self-Instruct prompting strategy is deciding how to mix human-authored seed tasks with machine-generated tasks in the in-context demonstrations. Including only human seeds produces high-quality but potentially narrow demonstrations. Including only machine-generated tasks risks drifting toward lower-quality patterns over time. The original paper found that always including some human seeds alongside machine-generated tasks struck the best balance.

In practice, you can implement this mixed sampling as a two-stage draw: first, always sample a fixed number of human seeds (typically 6 out of 8 demonstration slots), then fill the remaining slots from the broader machine-generated pool. This ensures that quality anchors are always present while still benefiting from the diversity of the larger pool. As the pool grows, the machine-generated tasks contribute increasingly varied demonstrations, but the human seeds provide a constant quality reference point.

The number of demonstrations to include in each prompt also matters. Too few demonstrations and the model lacks sufficient context to identify the desired format and diversity level. Too many demonstrations and the prompt approaches the model's context window limit, leaving less room for the generated output. The original Self-Instruct paper used 8 demonstrations per prompt, a compromise that fits comfortably within the context window while giving enough examples to establish clear patterns.

Instruction Diversity Through Prompt Variants

An advanced technique for improving instruction diversity is using multiple prompt templates that frame the generation task differently. Rather than always asking the model to "come up with a series of tasks," you might sometimes ask it to "write instructions for tasks a teacher might assign" or "list tasks that would require looking something up in a reference book." These different framings encourage the model to explore different regions of the instruction space.

The framing effect in large language models is powerful and well-documented. The same underlying knowledge manifests very differently depending on how the question is posed. A model asked to generate "creative writing tasks" will produce different instructions than one asked to generate "analytical reasoning tasks," even though both are drawing on the same pre-trained knowledge. Systematic variation of prompt framing can therefore significantly increase the coverage of the final instruction dataset without requiring additional seed tasks.