Tasks
Module for task-related functions and classes.
ClassificationTask
Bases: BaseTask
A class representing a classification task in the promptolution library.
This class handles the loading and management of classification datasets, as well as the evaluation of predictors on these datasets.
Source code in promptolution/tasks/classification_tasks.py
__init__(df, task_description=None, x_column='x', y_column='y', n_subsamples=30, eval_strategy='full', seed=42, metric=accuracy_score, config=None)
Initialize the ClassificationTask from a pandas DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame containing the data |
required |
task_description
|
str
|
Description of the task |
None
|
x_column
|
str
|
Name of the column containing input texts. Defaults to "x". |
'x'
|
y_column
|
str
|
Name of the column containing labels. Defaults to "y". |
'y'
|
n_subsamples
|
int
|
Number of subsamples to use. No subsampling if None. Defaults to None. |
30
|
eval_strategy
|
str
|
Subsampling strategy to use. Options: - "full": Uses the entire dataset for evaluation. - "evaluated": Uses only previously evaluated datapoints from the cache. - "subsample": Randomly selects n_subsamples datapoints without replacement. - "sequential_block": Uses a block of block_size consecutive datapoints, advancing through blocks sequentially. - "random_block": Randomly selects a block of block_size consecutive datapoints. Defaults to "full". |
'full'
|
seed
|
int
|
Random seed for reproducibility. Defaults to 42. |
42
|
metric
|
Callable
|
Metric to use for evaluation. Defaults to accuracy_score. |
accuracy_score
|
config
|
ExperimentConfig
|
Configuration for the task, overriding defaults. |
None
|
Source code in promptolution/tasks/classification_tasks.py
JudgeTask
Bases: BaseTask
Task that evaluates a predictor using an LLM-as-a-judge, optionally accepting a ground truth.
Source code in promptolution/tasks/judge_tasks.py
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 | |
__init__(df, judge_llm, x_column='x', y_column=None, task_description=None, n_subsamples=30, eval_strategy='full', seed=42, judge_prompt=None, min_score=-5.0, max_score=5.0, config=None)
Initialize the JudgeTask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
The input DataFrame containing the data. |
required |
judge_llm
|
BaseLLM
|
The LLM judging the predictions. |
required |
x_column
|
str
|
Name of the column containing input texts. |
'x'
|
y_column
|
Optional[str]
|
Name of the column containing labels/ground truth (if applicable). |
None
|
task_description
|
Optional[str]
|
Description of the task, parsed to the Judge-LLM and Meta-LLM. |
None
|
n_subsamples
|
int
|
Number of subsamples to use for evaluation. |
30
|
eval_strategy
|
EvalStrategy
|
Subsampling strategy to use for evaluation. |
'full'
|
seed
|
int
|
Random seed for reproducibility. |
42
|
judge_prompt
|
Optional[str]
|
Custom prompt for the judge. Note: The score of the Judge will be extracted inside |
None
|
min_score
|
float
|
Minimum score for evaluation. |
-5.0
|
max_score
|
float
|
Maximum score for evaluation. |
5.0
|
config
|
ExperimentConfig
|
Configuration for the task, overriding defaults. |
None
|
Source code in promptolution/tasks/judge_tasks.py
MultiObjectiveTask
Bases: BaseTask
A task that aggregates evaluations across multiple underlying tasks.
Source code in promptolution/tasks/multi_objective_task.py
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 | |
__init__(tasks, eval_strategy=None)
Initialize with a list of tasks sharing subsampling and seed settings.
Source code in promptolution/tasks/multi_objective_task.py
activate_scalarized_objective()
evaluate(prompts, predictor, system_prompts=None, eval_strategy=None)
Run prediction once, then score via each task's _evaluate.
Source code in promptolution/tasks/multi_objective_task.py
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 | |
RewardTask
Bases: BaseTask
A task that evaluates a predictor using a reward function.
This task takes a DataFrame, a column name for input data, and a reward function. The reward function takes in a prediction as input and returns a scalar reward.
Source code in promptolution/tasks/reward_tasks.py
__init__(df, reward_function, x_column='x', y_column=None, reward_columns=None, task_description=None, n_subsamples=30, eval_strategy='full', seed=42, config=None)
Initialize the RewardTask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame containing the data. |
required |
reward_function
|
Callable
|
Function that takes a prediction, potential keyword arguments from the dataframe, and returns a reward score. Note: The optimizers aim to maximize. |
required |
x_column
|
str
|
Name of the column containing input texts. Defaults to "x". |
'x'
|
y_column
|
str
|
Name of the column containing target texts if available. Defaults to None. |
None
|
reward_columns
|
List[str]
|
Additional dataframe columns to pass as keyword args to reward_function. |
None
|
task_description
|
str
|
Description of the task. |
None
|
n_subsamples
|
int
|
Number of subsamples to use. Defaults to 30. |
30
|
eval_strategy
|
str
|
Subsampling strategy to use. Defaults to "full". |
'full'
|
seed
|
int
|
Random seed for reproducibility. Defaults to 42. |
42
|
config
|
ExperimentConfig
|
Configuration for the task, overriding defaults. |
None
|
Source code in promptolution/tasks/reward_tasks.py
base_task
Base module for tasks.
BaseTask
Bases: ABC
Abstract base class for tasks in the promptolution library.
Source code in promptolution/tasks/base_task.py
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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
__init__(df, x_column, y_column=None, task_description=None, n_subsamples=30, eval_strategy='full', seed=42, config=None)
Initialize the BaseTask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
The input DataFrame containing the data. |
required |
x_column
|
str
|
Name of the column containing input texts. |
required |
y_column
|
Optional[str]
|
Name of the column containing labels/ground truth (if applicable). |
None
|
task_description
|
str
|
Description of the task. |
None
|
n_subsamples
|
int
|
Number of subsamples to use for evaluation. |
30
|
eval_strategy
|
Literal
|
Subsampling strategy ("full", "subsample", "sequential_block", "random_block", "evaluated"). |
'full'
|
seed
|
int
|
Random seed for reproducibility. |
42
|
config
|
ExperimentConfig
|
Configuration for the task, overriding defaults. |
None
|
Source code in promptolution/tasks/base_task.py
activate_scalarized_objective()
evaluate(prompts, predictor, system_prompts=None, eval_strategy=None, block_idx=None)
Evaluate a set of prompts using a given predictor.
This method orchestrates subsampling, prediction, caching, and result collection. Sequences, token costs, raw scores, and aggregated scores are always returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
Union[Prompt, List[Prompt]]
|
A single prompt or a list of prompts to evaluate. Results will be returned in the same order. |
required |
predictor
|
BasePredictor
|
The predictor to evaluate the prompts with. |
required |
system_prompts
|
Optional[Union[str, List[str]]]
|
Optional system prompts to parse to the predictor. |
None
|
eval_strategy
|
Optional[EvalStrategy]
|
Subsampling strategy to use instead of self.eval_strategy. Defaults to None, which uses self.eval_strategy. |
None
|
block_idx
|
Optional[int | list[int]]
|
Specific block index or indices to evaluate, overriding eval_strategy. Defaults to None. |
None
|
Source code in promptolution/tasks/base_task.py
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 | |
get_evaluated_blocks(prompts)
Return mapping of prompt string to evaluated block indices.
Source code in promptolution/tasks/base_task.py
increment_block_idx()
Increment the block index for subsampling.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the eval_strategy does not contain "block". |
Source code in promptolution/tasks/base_task.py
pop_datapoints(n=None, frac=None)
Pop a number of datapoints from the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of datapoints to pop. Defaults to None. |
None
|
frac
|
float
|
Fraction of datapoints to pop. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: DataFrame containing the popped datapoints. |
Source code in promptolution/tasks/base_task.py
reset_block_idx()
Reset the block index for subsampling.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the eval_strategy does not contain "block". |
Source code in promptolution/tasks/base_task.py
set_block_idx(idx)
Set the block index (or indices) for block subsampling strategies.
Source code in promptolution/tasks/base_task.py
subsample(eval_strategy=None, block_idx=None)
Subsample the dataset based on the specified parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eval_strategy
|
EvalStrategy
|
Subsampling strategy to use instead of self.eval_strategy. Defaults to None. |
None
|
block_idx
|
List[int] | None
|
Specific block index or indices to evaluate, overriding eval_strategy. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[List[str], List[str]]
|
Tuple[List[str], List[str]]: Subsampled input data and labels. |
Source code in promptolution/tasks/base_task.py
EvalResult
dataclass
Evaluation outputs including scores, sequences, and costs.
Source code in promptolution/tasks/base_task.py
classification_tasks
Module for classification tasks.
ClassificationTask
Bases: BaseTask
A class representing a classification task in the promptolution library.
This class handles the loading and management of classification datasets, as well as the evaluation of predictors on these datasets.
Source code in promptolution/tasks/classification_tasks.py
__init__(df, task_description=None, x_column='x', y_column='y', n_subsamples=30, eval_strategy='full', seed=42, metric=accuracy_score, config=None)
Initialize the ClassificationTask from a pandas DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame containing the data |
required |
task_description
|
str
|
Description of the task |
None
|
x_column
|
str
|
Name of the column containing input texts. Defaults to "x". |
'x'
|
y_column
|
str
|
Name of the column containing labels. Defaults to "y". |
'y'
|
n_subsamples
|
int
|
Number of subsamples to use. No subsampling if None. Defaults to None. |
30
|
eval_strategy
|
str
|
Subsampling strategy to use. Options: - "full": Uses the entire dataset for evaluation. - "evaluated": Uses only previously evaluated datapoints from the cache. - "subsample": Randomly selects n_subsamples datapoints without replacement. - "sequential_block": Uses a block of block_size consecutive datapoints, advancing through blocks sequentially. - "random_block": Randomly selects a block of block_size consecutive datapoints. Defaults to "full". |
'full'
|
seed
|
int
|
Random seed for reproducibility. Defaults to 42. |
42
|
metric
|
Callable
|
Metric to use for evaluation. Defaults to accuracy_score. |
accuracy_score
|
config
|
ExperimentConfig
|
Configuration for the task, overriding defaults. |
None
|
Source code in promptolution/tasks/classification_tasks.py
judge_tasks
Module for judge tasks.
JudgeTask
Bases: BaseTask
Task that evaluates a predictor using an LLM-as-a-judge, optionally accepting a ground truth.
Source code in promptolution/tasks/judge_tasks.py
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 | |
__init__(df, judge_llm, x_column='x', y_column=None, task_description=None, n_subsamples=30, eval_strategy='full', seed=42, judge_prompt=None, min_score=-5.0, max_score=5.0, config=None)
Initialize the JudgeTask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
The input DataFrame containing the data. |
required |
judge_llm
|
BaseLLM
|
The LLM judging the predictions. |
required |
x_column
|
str
|
Name of the column containing input texts. |
'x'
|
y_column
|
Optional[str]
|
Name of the column containing labels/ground truth (if applicable). |
None
|
task_description
|
Optional[str]
|
Description of the task, parsed to the Judge-LLM and Meta-LLM. |
None
|
n_subsamples
|
int
|
Number of subsamples to use for evaluation. |
30
|
eval_strategy
|
EvalStrategy
|
Subsampling strategy to use for evaluation. |
'full'
|
seed
|
int
|
Random seed for reproducibility. |
42
|
judge_prompt
|
Optional[str]
|
Custom prompt for the judge. Note: The score of the Judge will be extracted inside |
None
|
min_score
|
float
|
Minimum score for evaluation. |
-5.0
|
max_score
|
float
|
Maximum score for evaluation. |
5.0
|
config
|
ExperimentConfig
|
Configuration for the task, overriding defaults. |
None
|
Source code in promptolution/tasks/judge_tasks.py
multi_objective_task
Multi-objective task wrapper that evaluates prompts across multiple tasks.
MultiObjectiveEvalResult
dataclass
Container for per-task evaluation outputs in multi-objective runs.
Source code in promptolution/tasks/multi_objective_task.py
MultiObjectiveTask
Bases: BaseTask
A task that aggregates evaluations across multiple underlying tasks.
Source code in promptolution/tasks/multi_objective_task.py
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 | |
__init__(tasks, eval_strategy=None)
Initialize with a list of tasks sharing subsampling and seed settings.
Source code in promptolution/tasks/multi_objective_task.py
activate_scalarized_objective()
evaluate(prompts, predictor, system_prompts=None, eval_strategy=None)
Run prediction once, then score via each task's _evaluate.
Source code in promptolution/tasks/multi_objective_task.py
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 | |
reward_tasks
Module for Reward tasks.
RewardTask
Bases: BaseTask
A task that evaluates a predictor using a reward function.
This task takes a DataFrame, a column name for input data, and a reward function. The reward function takes in a prediction as input and returns a scalar reward.
Source code in promptolution/tasks/reward_tasks.py
__init__(df, reward_function, x_column='x', y_column=None, reward_columns=None, task_description=None, n_subsamples=30, eval_strategy='full', seed=42, config=None)
Initialize the RewardTask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame containing the data. |
required |
reward_function
|
Callable
|
Function that takes a prediction, potential keyword arguments from the dataframe, and returns a reward score. Note: The optimizers aim to maximize. |
required |
x_column
|
str
|
Name of the column containing input texts. Defaults to "x". |
'x'
|
y_column
|
str
|
Name of the column containing target texts if available. Defaults to None. |
None
|
reward_columns
|
List[str]
|
Additional dataframe columns to pass as keyword args to reward_function. |
None
|
task_description
|
str
|
Description of the task. |
None
|
n_subsamples
|
int
|
Number of subsamples to use. Defaults to 30. |
30
|
eval_strategy
|
str
|
Subsampling strategy to use. Defaults to "full". |
'full'
|
seed
|
int
|
Random seed for reproducibility. Defaults to 42. |
42
|
config
|
ExperimentConfig
|
Configuration for the task, overriding defaults. |
None
|