Skip to main content

Output

The Output component of an Instruction defines how the model should respond. It specifies the response TokenSet pattern and the final tokens that indicate completion or action.

InstructionOutput

The InstructionOutput class defines the output structure for an instruction. It specifies the TokenSet that defines the model's response pattern and the final tokens that indicate completion or action.

InstructionOutput Parameters

class InstructionOutput:
def __init__(self, tokenset: TokenSet, final: FinalToken | List[FinalToken] | None = None):
  • tokenset: Required TokenSet that defines the model's structured response pattern. This TokenSet cannot contain NumTokens or NumListTokens. To include numeric output, use a FinalNumToken instead.
  • final: Optional FinalToken or list of FinalToken instances that represent the final action or result. E.g. "Continue", "End", "Vanish". Defaults to <NON>. Can be a single FinalToken, a list of FinalTokens, or None.

What's Allowed in InstructionOutput

  • Basic Tokens only: The response TokenSet can only contain Basic Tokens. NumTokens and NumListTokens are not allowed in output TokenSets.
  • FinalToken or FinalNumToken: The final parameter can be a single FinalToken, a list of FinalTokens, or None (defaults to <NON>).
  • Multiple final options: You can provide a list of FinalTokens to allow the model to choose from multiple actions.
  • Numeric output via FinalNumToken: To include numeric values in the response, use a FinalNumToken as the final token. The numeric value is provided separately when adding samples using the value parameter.

Output Constraints

The response TokenSet in InstructionOutput has specific constraints:

  • No NumTokens or NumListTokens: The response TokenSet cannot contain NumTokens or NumListTokens. These are only allowed in input TokenSets.
  • Numeric Output: To include numeric values in the response, use a FinalNumToken as the final token. The numeric value is provided separately when adding samples using the value parameter.

Creating InstructionOutput

# Create a response TokenSet (must contain only Basic Tokens)
cat_grinning = mtp.TokenSet(tokens=(tree, cat, grin))

# Create an InstructionOutput with a single final token
simple_output = mtp.InstructionOutput(
tokenset=cat_grinning,
final=mtp.FinalToken("Continue")
)

# Create an InstructionOutput with multiple final token options
multi_output = mtp.InstructionOutput(
tokenset=cat_grinning,
final=[mtp.FinalToken("Appear"), mtp.FinalToken("Vanish")]
)

# Create an InstructionOutput with a FinalNumToken for numeric responses
numeric_output = mtp.InstructionOutput(
tokenset=cat_grinning,
final=mtp.FinalNumToken("Madness", min_value=0, max_value=10)
)

# Create an InstructionOutput without a final token (defaults to <NON>)
default_output = mtp.InstructionOutput(
tokenset=cat_grinning,
final=None
)

Adding Samples to InstructionOutput

When adding samples to an instruction, the output_snippet parameter can be:

  • String: For TokenSets without numeric tokens, you can pass a string directly. The system will automatically convert it to a snippet.
  • Snippet: You can create a snippet explicitly using TokenSet.create_snippet() if needed.

If an InstructionOutput uses a FinalNumToken, you must provide the value parameter with a numeric value within the FinalNumToken's range.

# For output TokenSets without numeric tokens, use strings directly
instruction.add_sample(
input_snippets=["Why do I keep vanishing?"],
output_snippet="Because it amuses me." # String is automatically converted
)

# For output with FinalNumToken, provide the value parameter
numeric_output = mtp.InstructionOutput(
tokenset=cat_grinning,
final=mtp.FinalNumToken("Madness", min_value=0, max_value=10)
)

instruction.add_sample(
input_snippets=["How do you know I am mad?"],
output_snippet="You must be, or you would not have come here.",
value=7 # Required: numeric value for the FinalNumToken
)

# For output with multiple final token options, specify which one to use
multi_output = mtp.InstructionOutput(
tokenset=cat_grinning,
final=[mtp.FinalToken("Appear"), mtp.FinalToken("Vanish")]
)

instruction.add_sample(
input_snippets=["Then it doesn't matter which way you go."],
output_snippet="Oh sure, if you only walk long enough.",
final=mtp.FinalToken("Appear") # Required: specify which final token to use
)

Output Validation

The MTP system ensures that:

  • InstructionOutput TokenSets do not contain NumTokens or NumListTokens
  • Final tokens are appropriate for the instruction type
  • Numeric values are within FinalNumToken ranges when used
  • When multiple final tokens are provided, each sample specifies which one to use