Our docs got a refresh! Check out the new content and improved navigation. For detailed API reference see our Python SDK docs and TypeScript SDK.
Experiments
Tasks
Creating and using tasks in Patronus experiments
Tasks in Patronus experiments are functions that process each dataset example and produce outputs that will be evaluated. This page covers how to create and use tasks effectively.
def classify_sentiment(row: Row, **kwargs) -> str: # Extract the text to classify text = row.task_input # Simple rule-based sentiment classifier positive_words = ["good", "great", "excellent", "happy", "positive"] negative_words = ["bad", "terrible", "awful", "sad", "negative"] text_lower = text.lower() positive_count = sum(word in text_lower for word in positive_words) negative_count = sum(word in text_lower for word in negative_words) # Classify based on word counts if positive_count > negative_count: return "positive" elif negative_count > positive_count: return "negative" else: return "neutral"
The string output represents a specific classification category, which is a common pattern in text classification tasks.
def selective_task(row: Row, **kwargs): # Skip examples without the required fields if not row.task_input or not row.gold_answer: return None # Process valid examples return f"Processed: {row.task_input}"
Task functions should handle exceptions appropriately:
from patronus import get_loggerfrom patronus.datasets import Rowdef robust_task(row: Row, **kwargs): try: # Attempt to process if row.task_input: return f"Processed: {row.task_input}" else: # Skip if input is missing return None except Exception as e: # Log the error get_logger().exception(f"Error processing row {row.sid}: {e}") # Skip this example return None
If an unhandled exception occurs, the experiment will log the error and skip that example.