Skip to main content

API Reference

This section provides comprehensive API documentation for the Model Train Protocol (MTP) package.

Core Classes

Protocol

The main Protocol class for creating model training protocols.

class Protocol:
def __init__(self, name: str, inputs: int, encrypt: bool = True, state_machine: bool = False):
"""
Initialize the Model Train Protocol (MTP)

:param name: The name of the protocol
:param inputs: The number of input lines in each Instruction sample (minimum 1)
:param encrypt: Whether to encrypt tokens with hashed keys (default: True)
:param state_machine: Enables state machine protocols (default: False). See the [State Machines](/training/mtp/state-machines/overview/) section for details.
"""

def add_context(self, context: str):
"""
Adds a line of context to the model.

:param context: Context string (must not exceed 300 characters)
"""

def add_instruction(self, instruction: Instruction):
"""Adds an Instruction (and its components) to the protocol."""

def save(self, name: str | None = None, path: str | None = None):
"""Saves the training protocol to a JSON file."""

def template(self, path: str | None = None):
"""Create a usage template JSON file for the model."""

Tokens

Token

Basic token class for representing concepts, actions, or entities.

class Token:
def __init__(self, value: str, key: str = None, desc: str = None):
"""Initialize a Token with value, optional key, and optional description."""

NumToken

Token that can be associated with numerical values within a specified range.

class NumToken(Token):
def __init__(self, value: str, min_value: int | float, max_value: int | float, key: str = None, desc: str = None):
"""Initialize a NumToken for numerical data with required range constraints."""

NumListToken

Token that can be associated with a list of numerical values with a fixed length.

class NumListToken(Token):
def __init__(self, value: str, min_value: int | float, max_value: int | float, length: int, key: str = None, desc: str = None):
"""Initialize a NumListToken for numerical list data with required range and length constraints."""

TokenSet

Groups multiple tokens together to define input patterns.

class TokenSet:
def __init__(self, tokens: Sequence[Token]):
"""Initialize a TokenSet with a list of tokens."""

def create_snippet(self, string: str, numbers: list = None, number_lists: list = None):
"""
Create a snippet from this TokenSet.

:param string: The text content for the snippet (must not exceed 300 characters)
:param numbers: Numeric value(s) for NumTokens (if applicable)
:param number_lists: List of numbers for NumListTokens (if applicable)
"""

Instructions

InstructionInput

Defines the input structure for an instruction.

class InstructionInput:
def __init__(self, tokensets: List[TokenSet]):
"""Initialize an InstructionInput with tokensets."""

InstructionOutput

Defines the output structure for an instruction.

class InstructionOutput:
def __init__(self, tokenset: TokenSet, final: FinalToken | List[FinalToken] | None = None):
"""Initialize an InstructionOutput with tokenset and optional final tokens."""

Instruction

Combines InstructionInput and InstructionOutput to create a complete instruction.

class Instruction:
def __init__(self, input: InstructionInput, output: InstructionOutput, context: List[str] | None = None, name: str | None = None):
"""Initialize an Instruction with input, output, optional context, and optional name."""

def add_sample(self, input_snippets: List[Union[str, Snippet]], output_snippet: Union[Snippet, str], value: Union[int, float, None] = None, final: FinalToken | None = None):
"""Add a training sample to the instruction."""

def add_guardrail(self, guardrail: Guardrail, tokenset_index: int):
"""Add a guardrail to the instruction, applying it to a specific TokenSet in the input."""

Guardrails

Safety mechanisms for user interactions.

class Guardrail:
def __init__(self, good_prompt: str, bad_prompt: str, bad_output: str):
"""Initialize a Guardrail with prompt descriptions and bad output."""

def add_sample(self, bad_prompt_example: str):
"""Add an example of a bad prompt."""