Datasets:
File size: 4,870 Bytes
06c7e2f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | """Example of a custom task with automatic discovery."""
from ..base_task import BaseTask, TaskConfig
import re
from typing import List, Dict
class MathTask(BaseTask):
"""A task for simple math problems."""
TASK_NAME = "math" # This is all that's needed for automatic registration!
def __init__(self, config: TaskConfig):
super().__init__(config)
def _load_data(self):
"""Generate synthetic math problems for testing."""
import pandas as pd
import random
random.seed(42) # For reproducibility
problems = []
# Generate 20 simple arithmetic problems
for _ in range(20):
a, b = random.randint(1, 20), random.randint(1, 20)
op = random.choice(['+', '-', '*'])
if op == '+':
answer = a + b
elif op == '-':
answer = a - b
else: # multiplication
answer = a * b
problems.append({
self.config.input_column: f"{a} {op} {b}",
self.config.output_column: str(answer)
})
self.data = pd.DataFrame(problems)
def get_icl_examples(self, num_examples: int = 10, shuffle: bool = True, seed: int = None, fresh: bool = True) -> List[Dict[str, str]]:
"""Generate simple arithmetic examples for ICL.
Note: `fresh` is ignored for synthetic/generated tasks (there's no stable dataset indices to track).
"""
import random
if seed is not None:
random.seed(seed)
examples = []
for _ in range(num_examples):
a, b = random.randint(1, 10), random.randint(1, 10)
examples.append({"input": f"{a} + {b}", "output": str(a + b)})
if shuffle:
# shuffle in-place for diversity
random.shuffle(examples)
return examples[:num_examples]
def evaluate(self, predictions: List[str], split: str = "test", **kwargs) -> Dict[str, float]:
"""Evaluate math predictions."""
correct = 0
total = len(predictions)
if total == 0:
return {"accuracy": 0.0}
# Get the targets for this split
data_split = self.data if split == "test" else self.data # For now, use same data
targets = data_split[self.config.output_column].tolist()
if len(predictions) != len(targets):
raise ValueError(f"Number of predictions ({len(predictions)}) doesn't match targets ({len(targets)})")
detailed_results = []
for pred, target in zip(predictions, targets):
result = self.evaluate_response(pred, str(target))
if result["correct"]:
correct += 1
detailed_results.append(result)
accuracy = correct / total
return {
"accuracy": accuracy,
"correct_count": correct,
"total_count": total,
"detailed_results": detailed_results
}
def evaluate_response(self, response: str, target: str) -> dict:
"""Evaluate a math response with number extraction."""
def extract_number(text: str) -> float:
"""Extract the first number from text."""
# Look for numbers (including decimals)
match = re.search(r'-?\d+\.?\d*', text.strip())
if match:
return float(match.group())
return None
response_num = extract_number(response)
target_num = extract_number(target)
if response_num is None or target_num is None:
return {
"correct": False,
"error": "Could not extract numbers",
"response_extracted": response_num,
"target_extracted": target_num
}
# Check if they're equal (with small tolerance for floating point)
correct = abs(response_num - target_num) < 0.001
return {
"correct": correct,
"response_extracted": response_num,
"target_extracted": target_num,
"original_response": response,
"original_target": target
}
def generate_prompt(self, row: dict, demonstrations: list = None) -> str:
"""Generate a prompt for math problems."""
prompt = "Solve the following math problem:\n\n"
if demonstrations:
prompt += "Here are some examples:\n"
for demo in demonstrations:
prompt += f"Problem: {demo['input']}\nAnswer: {demo['target']}\n\n"
prompt += "Now solve this problem:\n"
prompt += f"Problem: {row[self.config.input_column]}\nAnswer:"
return prompt
|