File size: 16,423 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""Base task class and configuration. When implementing a new task, please put it under tasks/implementations and inherit from BaseTask."""

import json
import csv
import pandas as pd
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import Dict, List, Any, Optional, Union, Callable
from pathlib import Path


@dataclass
class TaskConfig:
    """Configuration for a task."""
    name: str # the task name 
    description: Optional[str] = None # optional description
    data_path: Optional[str] = None # path to the main data file (CSV, JSON, etc.) for non-memory tasks
    data_format: str = "csv"  # 'csv', 'json', 'jsonl', 'memory'
    input_column: str = "input" # name of the input column in the data
    output_column: str = "output" # name of the output column in the data
    demonstrations_path: Optional[str] = None # path to demonstrations file (JSON, CSV, TXT)
    num_demonstrations: int = 5 # number of demonstrations to include in prompt. Will be max(num available, num_demonstrations)
    prompt_template: Optional[str] = None # optional prompt template with placeholders
    evaluation_metrics: List[str] = field(default_factory=lambda: ["accuracy"])
    metadata: Dict[str, Any] = field(default_factory=dict)
    # For in-memory data (when migrating from hardcoded tasks)
    in_memory_data: Optional[List[Dict[str, Any]]] = None # option to just pass in-memory data (e.g. just a list)
    in_memory_demonstrations: Optional[Dict[str, List[str]]] = None
    # Optional override for the number of new tokens to generate during eval.
    # When set on the task, evaluate_model() will use this value instead of its
    # own max_new_tokens argument (which comes from the CLI default of 100).
    max_new_tokens: Optional[int] = None


class BaseTask(ABC):
    """Base class for all tasks"""
    
    def __init__(self, config: TaskConfig):
        self.config = config
        self.data = None
        self.demonstrations = None
        # track which example indices have been used for ICL sampling
        self._icl_used_indices = set()
        self._load_data()
        self._load_demonstrations()
    
    def _load_data(self):
        """Load the main task data from the specified path and format or use in-memory data."""
        # Check if we have in-memory data first
        if self.config.in_memory_data is not None:
            self.data = pd.DataFrame(self.config.in_memory_data)
            return
            
        # If no data_path provided, let subclass handle data loading
        if not self.config.data_path:
            # Subclasses should override this method to provide their own data
            # If they don't override it properly, self.data will remain None
            return
            
        # Otherwise load from file
        data_path = Path(self.config.data_path)
        
        if not data_path.exists():
            raise FileNotFoundError(f"Data file not found: {data_path}")
        
        if self.config.data_format.lower() == 'csv':
            self.data = pd.read_csv(data_path)
        elif self.config.data_format.lower() == 'json':
            with open(data_path, 'r', encoding='utf-8') as f:
                data = json.load(f)
                self.data = pd.DataFrame(data)
        elif self.config.data_format.lower() == 'jsonl':
            data = []
            with open(data_path, 'r', encoding='utf-8') as f:
                for line in f:
                    data.append(json.loads(line.strip()))
            self.data = pd.DataFrame(data)
        else:
            raise ValueError(f"Unsupported data format: {self.config.data_format}")
    
    def _load_demonstrations(self):
        """Load demonstration examples from file, in-memory data, or use defaults."""
        # Check for in-memory demonstrations first
        if self.config.in_memory_demonstrations is not None:
            self.demonstrations = self.config.in_memory_demonstrations
            return
            
        # Otherwise try to load from file
        if self.config.demonstrations_path:
            demo_path = Path(self.config.demonstrations_path)
            if demo_path.exists():
                if demo_path.suffix == '.json':
                    with open(demo_path, 'r', encoding='utf-8') as f:
                        self.demonstrations = json.load(f)
                elif demo_path.suffix == '.csv':
                    self.demonstrations = pd.read_csv(demo_path)
                else:
                    # Try to load as text file with examples
                    with open(demo_path, 'r', encoding='utf-8') as f:
                        self.demonstrations = f.read().strip()
    @property
    def supports_icl(self) -> bool:
        """Check if this task supports ICL format"""
        return hasattr(self, 'get_icl_examples') or self.config.in_memory_data is not None

    def get_icl_examples(
        self,
        num_examples: int = 10,
        shuffle: bool = True,
        seed: Optional[int] = None,
        fresh: bool = True,
    ) -> List[Dict[str, str]]:
        """Return ICL-formatted examples (dicts with 'input' and 'output').

        Args:
            num_examples: number of examples to return.
            shuffle: whether to shuffle available examples before selection.
            seed: optional random seed for reproducibility.
            fresh: if True, prefer examples that haven't been returned before
                   (tracks indices in ``self._icl_used_indices``).
        """
        rows = self.get_split("test")
        if not rows:
            return []

        n = len(rows)
        indices = list(range(n))

        if shuffle:
            import random
            if seed is not None:
                random.seed(seed)
            random.shuffle(indices)

        if fresh:
            # choose indices not seen before; if insufficient, wrap around deterministically
            available = [i for i in indices if i not in self._icl_used_indices]
            if len(available) < num_examples:
                # include previously used indices to fill up
                available = available + [i for i in indices if i not in available]
            selected = available[:min(num_examples, n)]
            self._icl_used_indices.update(selected)
        else:
            selected = indices[:min(num_examples, n)]

        examples: List[Dict[str, str]] = []
        for i in selected:
            item = rows[i]
            examples.append({
                "input": item.get(self.config.input_column, ""),
                "output": str(item.get(self.config.output_column, "")),
            })
        return examples

    def reset_icl_tracking(self) -> None:
        """Reset internal tracking of used ICL examples so sampling starts fresh."""
        self._icl_used_indices.clear()

    def get_split(self, split: str = "test") -> List[Dict[str, Any]]:
        """Get data split as a list of dictionaries."""
        if split not in ["train", "val", "test", "all"]:
            raise ValueError(f"Invalid split: {split}. Must be one of ['train', 'val', 'test', 'all']")
        
        # For now, return all data as test split
        # Subclasses can override this for proper train/val/test splits
        if split == "all" or split == "test":
            # Handle both DataFrame and list data
            if hasattr(self.data, 'to_dict'):
                return self.data.to_dict('records')
            elif isinstance(self.data, list):
                return self.data
            else:
                return list(self.data)
        else:
            # Return empty for train/val if not implemented
            return []
    
    def build_prompt(self, instance: Dict[str, Any], num_shots: int = 5) -> str:
        """Build a prompt for a given instance.
        
        Args:
            instance: The instance to build a prompt for
            num_shots: Number of in-context learning examples to include (default: 5)
        
        Returns:
            The formatted prompt string
        """
        prompt = ""
        
        # Add demonstrations if available
        if self.demonstrations is not None:
            prompt += self._format_demonstrations()
            prompt += "\n\n"
        # If task supports ICL generation, use that
        elif hasattr(self, 'get_icl_examples') and num_shots > 0:
            try:
                icl_examples = self.get_icl_examples(num_examples=num_shots)
                if icl_examples:
                    prompt += self._format_icl_examples(icl_examples)
                    prompt += "\n\n"
            except Exception as e:
                # If ICL generation fails, continue without examples
                pass
        
        # Add the instance input
        if self.config.prompt_template:
            prompt += self.config.prompt_template.format(**instance)
        else:
            prompt += f"Input: {instance[self.config.input_column]}\nOutput:"
        
        return prompt
    
    def _format_demonstrations(self) -> str:
        """Format demonstration examples into a string."""
        if isinstance(self.demonstrations, str):
            return self.demonstrations
        elif isinstance(self.demonstrations, pd.DataFrame):
            demo_text = ""
            for _, row in self.demonstrations.head(self.config.num_demonstrations).iterrows():
                demo_text += f"Input: {row[self.config.input_column]}\nOutput: {row[self.config.output_column]}\n\n"
            return demo_text.strip()
        elif isinstance(self.demonstrations, list):
            demo_text = ""
            for demo in self.demonstrations[:self.config.num_demonstrations]:
                demo_text += f"Input: {demo[self.config.input_column]}\nOutput: {demo[self.config.output_column]}\n\n"
            return demo_text.strip()
        elif isinstance(self.demonstrations, dict):
            # Handle category-based demonstrations (like in the notebook)
            return ""  # Will be handled by subclasses that need category-specific demos
        else:
            return ""
    
    def _format_icl_examples(self, examples: List[Dict[str, Any]]) -> str:
        """Format ICL examples into a string.
        
        Args:
            examples: List of example dictionaries with input/output keys
            
        Returns:
            Formatted string of examples
        """
        if not examples:
            return ""
        
        demo_text = "\n"
        for ex in examples:
            # Try different column names
            input_val = ex.get(self.config.input_column) or ex.get('input') or ex.get('question')
            output_val = ex.get(self.config.output_column) or ex.get('output') or ex.get('answer')
            
            if input_val is not None and output_val is not None:
                demo_text += f"Input: {input_val}\nOutput: {output_val}\n\n"
        
        return demo_text.strip()
    
    @abstractmethod
    def evaluate(self, predictions: List[str], split: str = "test", **kwargs) -> Dict[str, float]:
        """Evaluate predictions against ground truth."""
        pass
    
    def preprocess_prediction(self, prediction: str) -> str:
        """Preprocess a model prediction before evaluation."""
        # Default implementation: strip whitespace and take first line
        return prediction.strip().split('\n')[0] if prediction else ""
    
    def get_ground_truth(self, split: str = "test") -> List[str]:
        """Get ground truth answers for a split."""
        data = self.get_split(split)
        return [item[self.config.output_column] for item in data]


class SimpleTask(BaseTask):
    """A simple task implementation with basic exact match evaluation."""
    
    def evaluate(self, predictions: List[str], split: str = "test", **kwargs) -> Dict[str, float]:
        """Evaluate predictions with exact match accuracy."""
        ground_truth = self.get_ground_truth(split)
        
        if len(predictions) != len(ground_truth):
            raise ValueError(f"Prediction count ({len(predictions)}) doesn't match ground truth count ({len(ground_truth)})")
        
        # Preprocess predictions
        processed_predictions = [self.preprocess_prediction(pred) for pred in predictions]
        
        # Calculate exact match accuracy
        correct = sum(1 for pred, gt in zip(processed_predictions, ground_truth) 
                     if pred.lower().strip() == gt.lower().strip())
        accuracy = correct / len(ground_truth)
        
        results = {
            "accuracy": accuracy,
            "correct": correct,
            "total": len(ground_truth)
        }
        
        # Add per-category accuracy if available
        data = self.get_split(split)
        if any("category" in item for item in data):
            category_results = {}
            for pred, gt, item in zip(processed_predictions, ground_truth, data):
                category = item.get("category", "unknown")
                if category not in category_results:
                    category_results[category] = {"correct": 0, "total": 0}
                
                category_results[category]["total"] += 1
                if pred.lower().strip() == gt.lower().strip():
                    category_results[category]["correct"] += 1
            
            # Calculate per-category accuracy
            for category, stats in category_results.items():
                results[f"accuracy_{category}"] = stats["correct"] / stats["total"]
        
        return results


# Factory function for creating tasks from config files
def create_task_from_config(config_path: str, task_class: type = SimpleTask) -> BaseTask:
    """Create a task instance from a configuration file."""
    config_path = Path(config_path)
    
    if config_path.suffix == '.json':
        with open(config_path, 'r', encoding='utf-8') as f:
            config_dict = json.load(f)
    else:
        raise ValueError(f"Unsupported config format: {config_path.suffix}")
    
    config = TaskConfig(**config_dict)
    return task_class(config)


# Factory function for creating tasks from hardcoded data (for migration)
def create_task_from_data(
    name: str,
    data: List[Dict[str, Any]],
    demonstrations: Optional[Dict[str, List[str]]] = None,
    description: str = "Task created from hardcoded data",
    task_class: type = SimpleTask,
    **kwargs
) -> BaseTask:
    """Create a task instance from in-memory data."""
    config = TaskConfig(
        name=name,
        description=description,
        data_format="memory",
        in_memory_data=data,
        in_memory_demonstrations=demonstrations,
        **kwargs
    )
    return task_class(config)


# Utility function to convert notebook-style data to standardized format
def convert_notebook_data_to_standard(
    csv_path: str,
    category_demonstrations: Dict[str, List[str]],
    output_csv_path: Optional[str] = None,
    output_demo_json_path: Optional[str] = None
) -> tuple[str, str]:
    """
    Convert notebook-style hardcoded data to standardized CSV and JSON files.
    
    Args:
        csv_path: Path to the existing CSV data file
        category_demonstrations: Dictionary mapping categories to demonstration examples
        output_csv_path: Path for output CSV (defaults to input path)
        output_demo_json_path: Path for demonstration JSON file
        
    Returns:
        Tuple of (csv_path, demo_json_path)
    """
    import pandas as pd
    import json
    from pathlib import Path
    
    # Read the CSV data
    data = pd.read_csv(csv_path)
    
    # Set default output paths
    if output_csv_path is None:
        output_csv_path = csv_path
    if output_demo_json_path is None:
        base_path = Path(csv_path).parent
        output_demo_json_path = base_path / f"{Path(csv_path).stem}_demonstrations.json"
    
    # Save demonstrations to JSON
    with open(output_demo_json_path, 'w', encoding='utf-8') as f:
        json.dump(category_demonstrations, f, indent=2, ensure_ascii=False)
    
    # Ensure CSV has the right format (it likely already does)
    if output_csv_path != csv_path:
        data.to_csv(output_csv_path, index=False)
    
    print(f"Converted data saved to:")
    print(f"  CSV: {output_csv_path}")
    print(f"  Demonstrations: {output_demo_json_path}")
    
    return str(output_csv_path), str(output_demo_json_path)