Optimization by PROmpting (OPRO)
About

Optimization by PROmpting (OPRO) starts with a seed prompt. At each iteration, a collection of past prompt candidates and scores and a random sample of input and output pairs from the validation set are formatted into a metaprompt. The metaprompt is sent to the language model multiple times, each time asking it to provide a new candidate prompt that improves the instructions for the task. These new candidates are scored and the iterations continue until the maximum depth is reached or the score threshold is exceeded.
Citation
@misc{yang2024largelanguagemodelsoptimizers,
title={Large Language Models as Optimizers},
author={Chengrun Yang and Xuezhi Wang and Yifeng Lu and Hanxiao Liu and Quoc V. Le and Denny Zhou and Xinyun Chen},
year={2024},
eprint={2309.03409},
archivePrefix={arXiv},
primaryClass={cs.LG},
url={https://arxiv.org/abs/2309.03409},
}
Source
OPROOptimizer
Bases: BaseOptimizer
OPRO Optimizer.
Based on Optimization by PROmpting from Yang, et. al. 2024
@misc{yang2024largelanguagemodelsoptimizers,
title={Large Language Models as Optimizers},
author={Chengrun Yang and Xuezhi Wang and Yifeng Lu and Hanxiao Liu and Quoc V. Le and Denny Zhou and Xinyun Chen},
year={2024},
eprint={2309.03409},
archivePrefix={arXiv},
primaryClass={cs.LG},
url={https://arxiv.org/abs/2309.03409},
}
Source code in src/prompt_optimizer/optimizers/opro.py
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 | |
__init__(*, client, seed_prompts, validation_set, max_depth, evaluator, output_path=None, input_field, output_field, num_candidates_per_step=20, num_exemplars=3, max_demonstration_prompts=20, score_threshold=None, **kwargs)
Initialize the APE optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
ClientType
|
Language model client to use for prompt generation and feedback. |
required |
seed_prompts
|
list[Prompt]
|
List of prompts to seed generation. |
required |
validation_set
|
ValidationSetType
|
Set of examples to evaluate the prompt on. |
required |
max_depth
|
int
|
Maximum iteration depth for prompt generation. |
required |
evaluator
|
Callable[[Prompt, ValidationSetType], ScoreType]
|
Function that takes a prompt and the validation data and returns a score. |
required |
output_path
|
Union[str, Path]
|
Path to store run results. Should be a .jsonl file path. If None, no outputs will be written to disk. Defaults to None. |
None
|
input_field
|
str
|
Field in the validation set that represents the input. Used in candidate generation in the "input:" field. |
required |
output_field
|
str
|
Field in the validation set that represents the output. Used in candidate generation in the "output:" field. |
required |
num_candidates_per_step
|
int
|
Number of candidates to create at each step. Defaults to 20. |
20
|
num_exemplars
|
int
|
Number of exemplars from the validation set to provide in the metaprompt. A random sample of input and output pairs of this size will be provided to the LLM during candidate generation. Defaults to 3. |
3
|
max_demonstration_prompts
|
int
|
Maximum number of demostration prompts to provide in the metaprompt. Defaults to 20. |
20
|
score_threshold
|
float
|
Threshold for early convergence. If a prompt exceeds this score after any iteration, the optimization loop immediately ends. If set to None, the optimization loop will not terminate early. Defaults to None. |
None
|
kwargs
|
Additional keyword arguments. |
{}
|
Source code in src/prompt_optimizer/optimizers/opro.py
check_early_convergence(*, all_prompts)
Detect early convergence.
Source code in src/prompt_optimizer/optimizers/opro.py
generate_prompt_candidates(*, prompts, validation_set)
Generate prompt candidates.
Source code in src/prompt_optimizer/optimizers/opro.py
get_all_prompts(include_candidates=False)
Get all the prompts from the latest training run.
The default behavior returns a list of lists, where each internal list contains the retained candidates after one iteration step. Setting include_candidates to True will also include all generated candidate prompts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_candidates
|
bool
|
Whether to include all the candidate prompts in the output. If True, candidate prompts from each iteration will be included. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
list[list[Prompt]]
|
list[list[Prompt]]: List of lists where each list contains the prompts from each iteration. E.g. list[0] contains prompts from the first iteration, list[1] the second, etc. If include_candidates is False, each inner list contains only the retained prompts at each iteration. If include_candidates is True, each inner list contains all candidate prompts at each iteration, including those that were discarded. |
Source code in src/prompt_optimizer/optimizers/base.py
run()
Run the optimization pipeline.
Source code in src/prompt_optimizer/optimizers/base.py
save_prompts(output_path)
Save prompts in jsonl format.
Source code in src/prompt_optimizer/optimizers/base.py
select_best_prompt(all_prompts)
Select the top scoring prompt.
Source code in src/prompt_optimizer/optimizers/opro.py
select_prompt_candidates(*, prompts, validation_set)
Select prompt candidates.