dspy.MIPROv2
MIPROv2
(Multiprompt Instruction PRoposal Optimizer Version 2) is an prompt optimizer capable of optimizing both instructions and few-shot examples jointly. It does this by bootstrapping few-shot example candidates, proposing instructions grounded in different dynamics of the task, and finding an optimized combination of these options using Bayesian Optimization. It can be used for optimizing few-shot examples & instructions jointly, or just instructions for 0-shot optimization.
dspy.MIPROv2(metric: Callable, prompt_model: Optional[Any] = None, task_model: Optional[Any] = None, teacher_settings: Optional[dict] = None, max_bootstrapped_demos: int = 4, max_labeled_demos: int = 4, auto: Optional[Literal['light', 'medium', 'heavy']] = 'light', num_candidates: Optional[int] = None, num_threads: Optional[int] = None, max_errors: int = 10, seed: int = 9, init_temperature: float = 0.5, verbose: bool = False, track_stats: bool = True, log_dir: Optional[str] = None, metric_threshold: Optional[float] = None)
Bases: Teleprompter
Source code in dspy/teleprompt/mipro_optimizer_v2.py
Functions
compile(student: Any, *, trainset: List, teacher: Any = None, valset: Optional[List] = None, num_trials: Optional[int] = None, max_bootstrapped_demos: Optional[int] = None, max_labeled_demos: Optional[int] = None, seed: Optional[int] = None, minibatch: bool = True, minibatch_size: int = 35, minibatch_full_eval_steps: int = 5, program_aware_proposer: bool = True, data_aware_proposer: bool = True, view_data_batch_size: int = 10, tip_aware_proposer: bool = True, fewshot_aware_proposer: bool = True, requires_permission_to_run: bool = True, provide_traceback: Optional[bool] = None) -> Any
Source code in dspy/teleprompt/mipro_optimizer_v2.py
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 |
|
get_params() -> dict[str, Any]
Get the parameters of the teleprompter.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
The parameters of the teleprompter. |
Example Usage
The program below shows optimizing a math program with MIPROv2
import dspy
from dspy.datasets.gsm8k import GSM8K, gsm8k_metric
# Import the optimizer
from dspy.teleprompt import MIPROv2
# Initialize the LM
lm = dspy.LM('openai/gpt-4o-mini', api_key='YOUR_OPENAI_API_KEY')
dspy.configure(lm=lm)
# Initialize optimizer
teleprompter = MIPROv2(
metric=gsm8k_metric,
auto="medium", # Can choose between light, medium, and heavy optimization runs
)
# Optimize program
print(f"Optimizing program with MIPROv2...")
optimized_program = teleprompter.compile(
dspy.ChainOfThought("question -> answer"),
trainset=gsm8k.train,
requires_permission_to_run=False,
)
# Save optimize program for future use
optimized_program.save(f"optimized.json")
How MIPROv2
works
At a high level, MIPROv2
works by creating both few-shot examples and new instructions for each predictor in your LM program, and then searching over these using Bayesian Optimization to find the best combination of these variables for your program. If you want a visual explanation check out this twitter thread.
These steps are broken down in more detail below:
1) Bootstrap Few-Shot Examples: Randomly samples examples from your training set, and run them through your LM program. If the output from the program is correct for this example, it is kept as a valid few-shot example candidate. Otherwise, we try another example until we've curated the specified amount of few-shot example candidates. This step creates num_candidates
sets of max_bootstrapped_demos
bootstrapped examples and max_labeled_demos
basic examples sampled from the training set.
2) Propose Instruction Candidates. The instruction proposer includes (1) a generated summary of properties of the training dataset, (2) a generated summary of your LM program's code and the specific predictor that an instruction is being generated for, (3) the previously bootstrapped few-shot examples to show reference inputs / outputs for a given predictor and (4) a randomly sampled tip for generation (i.e. "be creative", "be concise", etc.) to help explore the feature space of potential instructions. This context is provided to a prompt_model
which writes high quality instruction candidates.
3) Find an Optimized Combination of Few-Shot Examples & Instructions. Finally, we use Bayesian Optimization to choose which combinations of instructions and demonstrations work best for each predictor in our program. This works by running a series of num_trials
trials, where a new set of prompts are evaluated over our validation set at each trial. The new set of prompts are only evaluated on a minibatch of size minibatch_size
at each trial (when minibatch
=True
). The best averaging set of prompts is then evalauted on the full validation set every minibatch_full_eval_steps
. At the end of the optimization process, the LM program with the set of prompts that performed best on the full validation set is returned.
For those interested in more details, more information on MIPROv2
along with a study on MIPROv2
compared with other DSPy optimizers can be found in this paper.