Skip to main content

Context

Context is a foundational layer of the Model Train Protocol (MTP) system. It provides the background information and domain knowledge that helps the model understand the training data and respond appropriately in the intended context.

What is Context?

Context in MTP refers to the background information, domain knowledge, and setting that establishes the foundation for all training data. It helps the model understand:

  • The domain or subject area (e.g., "medical diagnosis", "creative writing", "customer service")
  • The setting or environment (e.g., "Alice in Wonderland", "modern office", "medieval fantasy")
  • Key concepts and terminology specific to the domain
  • The tone and style expected in responses
  • Any constraints or guidelines for the model's behavior

Why Context Matters

Context serves several critical functions:

  1. Domain Understanding: Provides the model with essential background knowledge about the subject matter
  2. Consistency: Ensures all training examples are interpreted within the same conceptual framework
  3. Appropriate Responses: Helps the model generate responses that are contextually appropriate
  4. Reduced Ambiguity: Clarifies ambiguous terms and concepts that might have different meanings in different contexts

Adding Context to Your Protocol

There are two ways to provide context in MTP:

  1. General Context: Add context to the entire protocol using the add_context() method. This context applies to all instructions and provides overall domain knowledge and background information.

  2. Instruction-Specific Context: Add context to individual instructions using the context parameter in Instruction. This allows you to provide specific background information that is relevant only to particular instructions.

You can use both approaches together - general context for overall domain knowledge and instruction-specific context for situation-specific details.

Instruction Context Snippets

Context snippets, set as instruction_context_snippets when initializing the protocol, refers to how many snippets of context are provided to each Instruction. Context snippets does NOT refer to the amount of total context that your model has.

Each Instruction in the protocol must have the same number of context snippets as specified when initializing the protocol.

This is not to be confused with the context added to the protocol using the add_context() method, which can be any number of lines.

A minimum of 2 instruction_context_snippets are required.

General Context Usage

import model_train_protocol as mtp

# Initialize the protocol
protocol = mtp.Protocol(name="my_model", instruction_context_snippets=2)

# Add general context that applies to all instructions
protocol.add_context("The Cheshire Cat is a fictional character from Lewis Carroll's 'Alice's Adventures in Wonderland'.")
protocol.add_context("The Cheshire Cat is known for its distinctive mischievous grin and its ability to disappear and reappear at will.")
protocol.add_context("The Cat often speaks in riddles and philosophical musings, adding a whimsical and enigmatic element to the story.")
important

Protocol Context Limits:

  • Minimum: At least 10 total context lines across all instructions (protocol context + instruction context combined)
  • Character Limit: Each protocol context line must not exceed 300 characters
  • No Maximum: There is no maximum number of protocol context lines - you can add as many as needed

Instruction-Specific Context Usage

# Create Input and Output
instruction_input = mtp.InstructionInput(
tokensets=[cat_pondering, alice_talk]
)

instruction_output = mtp.InstructionOutput(
tokenset=cat_grinning,
final=mtp.FinalToken("Continue")
)

# Add context specific to individual instructions
instruction = mtp.Instruction(
input=instruction_input,
output=instruction_output,
context=[
"Alice was beginning to get very tired of sitting by her sister on the bank.",
"The Cheshire Cat appeared in the tree, grinning mysteriously."
]
)

This instruction-specific context provides additional background information that is relevant only to this particular instruction, supplementing the general context provided to the protocol.

Context Examples

Storytelling Context

# Fantasy storytelling context
protocol.add_context("This is a medieval fantasy world with magic, dragons, and heroic quests.")
protocol.add_context("The story follows a young mage learning to control their powers.")
protocol.add_context("Magic is governed by ancient laws and requires both knowledge and willpower.")

Educational Context

# Educational tutoring context
protocol.add_context("You are a patient and encouraging math tutor for middle school students.")
protocol.add_context("Explain concepts clearly using simple language and relatable examples.")
protocol.add_context("Always show your work step-by-step and encourage questions.")

Customer Service Context

# Customer service context
protocol.add_context("You are a helpful customer service representative for a tech company.")
protocol.add_context("Always be polite, professional, and solution-oriented.")
protocol.add_context("If you cannot solve a problem, escalate to a specialist.")

Context Validation

The MTP system validates context to ensure quality and consistency:

Protocol Context Validation

  • Minimum Requirement: The total number of context lines (protocol context + all instruction contexts combined) must be at least 10
  • Character Limit: Each protocol context line added via add_context() must not exceed 300 characters
  • No Maximum Lines: There is no limit on the number of protocol context lines you can add

Instruction Context Validation

  • Maximum Lines: Each instruction can have a maximum of 10 context lines
  • Character Limit: Each instruction context line must not exceed 300 characters

For more details on instruction context limits, see Instructions.

Best Practices for Context

  1. Be Specific: Provide clear, specific information about the domain and setting
  2. Be Comprehensive: Include all relevant background information the model needs
  3. Be Consistent: Ensure all context lines work together to create a coherent framework
  4. Consider Your Audience: Tailor the context to the intended use case and user base
  5. More Is Better: The more context you provide, the better your model will perform (within the character limits)
  6. Respect Limits: Keep protocol context lines under 300 characters and instruction context to a maximum of 10 lines

Example: Complete Context Setup

Here's a complete example of setting up context for a creative writing assistant:

import model_train_protocol as mtp

# Initialize the protocol
protocol = mtp.Protocol(name="creative_writing_assistant", instruction_context_snippets=2)

# Add comprehensive context
protocol.add_context("You are a creative writing assistant specializing in fantasy and science fiction.")
protocol.add_context("Your role is to help writers develop compelling characters, engaging plots, and immersive worlds.")
protocol.add_context("You provide constructive feedback, creative suggestions, and writing techniques.")
protocol.add_context("You encourage experimentation while maintaining narrative coherence and reader engagement.")
protocol.add_context("Your responses should be inspiring, detailed, and actionable for writers of all skill levels.")

# Now you can proceed with defining tokens, tokensets, and instructions
# that all work within this creative writing context

Next Steps

After establishing your context, you can proceed to:

  • Tokens - Define the fundamental building blocks within your context
  • TokenSets - Create meaningful patterns that fit your domain
  • Instructions - Teach the model how to respond appropriately in your context