Optimizers
Module for prompt optimizers.
base_optimizer
Base module for optimizers in the promptolution library.
BaseOptimizer
Bases: ABC
Abstract base class for prompt optimizers.
This class defines the basic structure and interface for prompt optimization algorithms.
Attributes:
Name | Type | Description |
---|---|---|
config |
ExperimentConfig
|
Configuration for the optimizer, overriding defaults. |
prompts |
List[str]
|
List of current prompts being optimized. |
task |
BaseTask
|
The task object used for evaluating prompts. |
callbacks |
List[Callable]
|
List of callback functions to be called during optimization. |
predictor |
The predictor used for prompt evaluation (if applicable). |
Source code in promptolution/optimizers/base_optimizer.py
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 |
|
__init__(predictor, task, initial_prompts, callbacks=None, config=None)
Initialize the optimizer with a configuration and/or direct parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initial_prompts
|
List[str]
|
Initial set of prompts to start optimization with. |
required |
task
|
BaseTask
|
Task object for prompt evaluation. |
required |
callbacks
|
List[Callable]
|
List of callback functions. |
None
|
predictor
|
Predictor for prompt evaluation. |
required | |
config
|
ExperimentConfig
|
Configuration for the optimizer, overriding defaults. |
None
|
Source code in promptolution/optimizers/base_optimizer.py
optimize(n_steps)
Perform the optimization process.
This method should be implemented by concrete optimizer classes to define the specific optimization algorithm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_steps
|
int
|
Number of optimization steps to perform. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
The optimized list of prompts after all steps. |
Source code in promptolution/optimizers/base_optimizer.py
capo
Implementation of the CAPO (Cost-Aware Prompt Optimization) algorithm.
CAPO
Bases: BaseOptimizer
CAPO: Cost-Aware Prompt Optimization.
This class implements an evolutionary algorithm for optimizing prompts in large language models by incorporating racing techniques and multi-objective optimization. It uses crossover, mutation, and racing based on evaluation scores and statistical tests to improve efficiency while balancing performance with prompt length. It is adapted from the paper "CAPO: Cost-Aware Prompt Optimization" by Zehle et al., 2025.
Source code in promptolution/optimizers/capo.py
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 |
|
__init__(predictor, task, meta_llm, initial_prompts=None, crossovers_per_iter=4, upper_shots=5, max_n_blocks_eval=10, test_statistic='paired_t_test', alpha=0.2, length_penalty=0.05, df_few_shots=None, crossover_template=None, mutation_template=None, callbacks=[], config=None)
Initializes the CAPOptimizer with various parameters for prompt evolution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictor
|
BasePredictor
|
The predictor for evaluating prompt performance. |
required |
task
|
BaseTask
|
The task instance containing dataset and description. |
required |
meta_llm
|
BaseLLM
|
The meta language model for crossover/mutation. |
required |
initial_prompts
|
List[str]
|
Initial prompt instructions. |
None
|
crossovers_per_iter
|
int
|
Number of crossover operations per iteration. |
4
|
upper_shots
|
int
|
Maximum number of few-shot examples per prompt. |
5
|
p_few_shot_reasoning
|
float
|
Probability of generating llm-reasoning for few-shot examples, instead of simply using input-output pairs. |
required |
max_n_blocks_eval
|
int
|
Maximum number of evaluation blocks. |
10
|
test_statistic
|
TestStatistics
|
Statistical test to compare prompt performance. Default is "paired_t_test". |
'paired_t_test'
|
alpha
|
float
|
Significance level for the statistical test. |
0.2
|
length_penalty
|
float
|
Penalty factor for prompt length. |
0.05
|
df_few_shots
|
DataFrame
|
DataFrame containing few-shot examples. If None, will pop 10% of datapoints from task. |
None
|
crossover_template
|
str
|
Template for crossover instructions. |
None
|
mutation_template
|
str
|
Template for mutation instructions. |
None
|
callbacks
|
List[Callable]
|
Callbacks for optimizer events. |
[]
|
config
|
ExperimentConfig
|
Configuration for the optimizer. |
None
|
Source code in promptolution/optimizers/capo.py
CAPOPrompt
Represents a prompt consisting of an instruction and few-shot examples.
Source code in promptolution/optimizers/capo.py
__init__(instruction_text, few_shots)
Initializes the Prompt with an instruction and associated examples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instruction_text
|
str
|
The instruction or prompt text. |
required |
few_shots
|
List[str]
|
List of examples as string. |
required |
Source code in promptolution/optimizers/capo.py
__str__()
construct_prompt()
Constructs the full prompt string by replacing placeholders in the template with the instruction and formatted examples.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The constructed prompt string. |
Source code in promptolution/optimizers/capo.py
evoprompt_de
Module for EvoPromptDE optimizer.
EvoPromptDE
Bases: BaseOptimizer
EvoPromptDE: Differential Evolution-based Prompt Optimizer.
This class implements a differential evolution algorithm for optimizing prompts in large language models. It is adapted from the paper "Connecting Large Language Models with Evolutionary Algorithms Yields Powerful Prompt Optimizers" by Guo et al., 2023.
The optimizer uses a differential evolution strategy to generate new prompts from existing ones, with an option to use the current best prompt as a donor.
Attributes:
Name | Type | Description |
---|---|---|
prompt_template |
str
|
Template for generating meta-prompts during evolution. |
donor_random |
bool
|
If False, uses the current best prompt as a donor; if True, uses a random prompt. |
meta_llm |
Language model used for generating child prompts from meta-prompts. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt_template
|
str
|
Template for meta-prompts. |
required |
meta_llm
|
BaseLLM
|
Language model for child prompt generation. |
required |
donor_random
|
bool
|
Whether to use a random donor. Defaults to False. |
False
|
config
|
ExperimentConfig
|
Configuration for the optimizer, overriding defaults. |
None
|
Source code in promptolution/optimizers/evoprompt_de.py
__init__(predictor, task, prompt_template, meta_llm, initial_prompts=None, donor_random=False, callbacks=None, config=None)
Initialize the EvoPromptDE optimizer.
Source code in promptolution/optimizers/evoprompt_de.py
evoprompt_ga
Module for EvoPromptGA optimizer.
EvoPromptGA
Bases: BaseOptimizer
EvoPromptGA: Genetic Algorithm-based Prompt Optimizer.
This class implements a genetic algorithm for optimizing prompts in large language models. It is adapted from the paper "Connecting Large Language Models with Evolutionary Algorithms Yields Powerful Prompt Optimizers" by Guo et al., 2023.
The optimizer uses crossover operations to generate new prompts from existing ones, with different selection methods available for choosing parent prompts.
Attributes:
Name | Type | Description |
---|---|---|
prompt_template |
str
|
Template for generating meta-prompts during crossover. |
meta_llm |
Language model used for generating child prompts from meta-prompts. |
|
selection_mode |
str
|
Method for selecting parent prompts ('random', 'wheel', or 'tour'). |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt_template
|
str
|
Template for meta-prompts. |
required |
meta_llm
|
BaseLLM
|
Language model for child prompt generation. |
required |
selection_mode
|
str
|
Parent selection method. Defaults to "wheel". |
'wheel'
|
Raises:
Type | Description |
---|---|
AssertionError
|
If an invalid selection mode is provided. |
Source code in promptolution/optimizers/evoprompt_ga.py
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 |
|
__init__(predictor, task, prompt_template, meta_llm, initial_prompts=None, selection_mode='wheel', callbacks=None, config=None)
Initialize the EvoPromptGA optimizer.
Source code in promptolution/optimizers/evoprompt_ga.py
opro
Module implementing the OPRO (Optimization by PROmpting) algorithm.
OPRO
Bases: BaseOptimizer
OPRO: Optimization by PROmpting.
Implementation of the technique proposed in "Large Language Models as Optimizers" (Yang et al., 2023: https://arxiv.org/abs/2309.03409).
OPRO works by providing a meta-LLM with task descriptions and previous prompt-score pairs to generate improved prompts for a downstream LLM.
Source code in promptolution/optimizers/opro.py
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 |
|
__init__(predictor, task, prompt_template, meta_llm, initial_prompts=None, max_num_instructions=20, num_instructions_per_step=8, num_few_shots=3, callbacks=None, config=None)
Initialize the OPRO optimizer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictor
|
BasePredictor
|
Predictor for prompt evaluation |
required |
task
|
BaseTask
|
Task object for prompt evaluation |
required |
meta_llm
|
BaseLLM
|
LLM that generates improved prompts |
required |
initial_prompts
|
List[str]
|
Initial set of prompts to start optimization with |
None
|
prompt_template
|
Optional[str]
|
Custom meta prompt template (uses OPRO_TEMPLATE if None) |
required |
max_num_instructions
|
int
|
Maximum previous instructions to include in meta prompt |
20
|
num_instructions_per_step
|
int
|
Number of prompts to generate in each step |
8
|
num_few_shots
|
int
|
Number of few-shot examples to include (0 for none) |
3
|
callbacks
|
List[BaseCallback]
|
List of callback functions |
None
|
config
|
ExperimentConfig
|
"ExperimentConfig" overwriting default parameters |
None
|
Source code in promptolution/optimizers/opro.py
templates
Meta-prompt templates for different prompt optimization methods.