T5 Task Formatting › Building a Task Formatter
Language AI HandbookAll chapters T5 Task Formatting › Building a Task FormatterOne section · Sections in this chapter9 / 15 ↓
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
The following utility class handles task formatting consistently, encapsulating the formatting conventions we have discussed into a clean interface for preparing inputs across different task types. In production systems, centralized formatting logic like this is needed for consistency: all parts of the system that construct T5 inputs should go through the same formatter to ensure they use the same prefix conventions.
class T5TaskFormatter:
"""Format various NLP tasks for T5 text-to-text processing."""
@staticmethod
def sentiment(text: str) -> str:
"""Format text for sentiment classification."""
return f"sst2 sentence: {text}"
@staticmethod
def translation(text: str, source_lang: str, target_lang: str) -> str:
"""Format text for translation."""
return f"translate {source_lang} to {target_lang}: {text}"
@staticmethod
def summarization(text: str, max_words: int = None) -> str:
"""Format text for summarization with optional length control."""
if max_words:
return f"summarize to {max_words} words: {text}"
return f"summarize: {text}"
@staticmethod
def qa(question: str, context: str = None) -> str:
"""Format for question answering (open or closed book)."""
if context:
return f"question: {question} context: {context}"
return f"question: {question}"
@staticmethod
def ner(text: str) -> str:
"""Format text for named entity recognition."""
return f"extract entities: {text}"
@staticmethod
def nli(premise: str, hypothesis: str) -> str:
"""Format for natural language inference."""
return f"mnli premise: {premise} hypothesis: {hypothesis}"
@staticmethod
def grammar_correction(text: str) -> str:
"""Format for grammar and spelling correction."""
return f"correct: {text}"
@staticmethod
def similarity(sentence1: str, sentence2: str) -> str:
"""Format for semantic similarity."""
return f"stsb sentence1: {sentence1} sentence2: {sentence2}"# Using the formatter
formatter = T5TaskFormatter()
formatted_examples = [
formatter.sentiment("This book was incredibly engaging!"),
formatter.translation("Good morning", "English", "Spanish"),
formatter.qa(
"What color is the sky?", "The sky appears blue during the day."
),
formatter.nli("All birds can fly.", "Penguins are birds."),
]Formatted task examples: sst2 sentence: This book was incredibly engaging! translate English to Spanish: Good morning question: What color is the sky? context: The sky appears blue during the day. mnli premise: All birds can fly. hypothesis: Penguins are birds.
Each formatted string follows a consistent pattern: a task-specific prefix followed by the input content. This consistency is key to T5's ability to route inputs to the appropriate behavior. The formatter class encapsulates these patterns, which makes it easy to ensure correct formatting across an application. By centralizing the formatting logic, we also make it easier to update formats if needed during iterative development. Changing the formatter updates all usages automatically, preventing the subtle inconsistencies that arise when formatting logic is scattered across a codebase.