MultiClassifierInstruction
MultiClassifierInstruction defines the single instruction used in a multi-classification protocol. It maps one or more input TokenSets to a JSON response containing one value for every classification in the state_map.
Key characteristics:
- Output is created automatically from the
state_map - Final token is always
<NON> - Samples provide a JSON string instead of a plain response snippet
MultiClassifierInstruction Parameters
class MultiClassifierInstruction:
def __init__(self, input: InstructionInput, state_map: Dict[str, List[str]], context: List[str] | None = None):
- input: An
InstructionInputinstance defining the input TokenSets - state_map: A dictionary mapping classification labels to their list of acceptable values
- context: Optional list of strings providing background context for the instruction. This context helps the model understand the domain and situation. Can be
Noneor an empty list if no context is needed.
Creating a MultiClassifierInstruction
# Create a TokenSet for the input pattern
alice_talk = mtp.TokenSet(tokens=(tree, english, alice, talk))
# Create the InstructionInput
instruction_input = mtp.InstructionInput(
tokensets=[alice_talk]
)
# Define the classifications and their acceptable values
state_map = {
"emotion": ["curious", "afraid", "confused", "amused"],
"intent": ["question", "statement", "exclamation"],
}
# Create the MultiClassifierInstruction
multi_classifier_instruction = mtp.MultiClassifierInstruction(
input=instruction_input,
state_map=state_map,
context=[
"The Cheshire Cat classifies each line Alice speaks by the emotion she expresses and the intent of the line.",
"Each response is a JSON object with exactly the keys 'emotion' and 'intent'."
]
)
Adding Samples
Each sample maps input snippets to a JSON string containing exactly the keys defined in the state_map.
add_sample() parameters:
- input_snippets: List of snippets or strings that will be added to the instruction. Must match the number of TokenSets in the Input. Each snippet must not exceed 300 characters.
- output_snippet: A JSON string containing exactly the
state_mapkeys, each set to one of that key's acceptable values.
import json
# Build the JSON response with json.dumps()
multi_classifier_instruction.add_sample(
input_snippets=["What a curious feeling, I must be shutting up like a telescope!"],
output_snippet=json.dumps({"emotion": "curious", "intent": "exclamation"})
)
# Or pass the JSON as a string directly
multi_classifier_instruction.add_sample(
input_snippets=["Which way ought I to go from here?"],
output_snippet='{"emotion": "confused", "intent": "question"}'
)
multi_classifier_instruction.add_sample(
input_snippets=["Oh dear, I do hope this fall will ever come to an end."],
output_snippet=json.dumps({"emotion": "afraid", "intent": "statement"})
)
A minimum of 3 samples is required. Provide more samples to cover every value in the state_map.
Adding the Instruction to the Protocol
protocol.add_instruction(multi_classifier_instruction)
Adding a MultiClassifierInstruction to a protocol makes it a multi-classification model. No additional protocol parameter is required.
Next Steps
- Multi-Classification Overview - Understand multi-classification protocols
- Input - Define input TokenSets
- Creating a Model - Build and submit the model
Databiomes