MIPROv2
MIPROv2 (Multiprompt Instruction PRoposal Optimizer Version 2) is a prompt optimization algorithm within deepeval adapted from the DSPy paper Optimizing Instructions and Demonstrations for Multi-Stage Language Model Programs. It combines intelligent instruction proposal with few-shot demonstration bootstrapping and uses Bayesian Optimization to find the optimal prompt configuration.
The core insight is that both the instruction (what the LLM should do) and the demonstrations (few-shot examples) significantly impact performance—and finding the best combination requires systematic search rather than manual tuning.
MIPROv2 requires the optuna package for Bayesian Optimization. Install it with:
pip install optuna
Optimize Prompts With MIPROv2
To optimize a prompt using MIPROv2, simply provide a MIPROV2 algorithm instance to the optimize() method:
from deepeval.metrics import AnswerRelevancyMetric
from deepeval.prompt import Prompt
from deepeval.optimizer import PromptOptimizer
from deepeval.optimizer.algorithms import MIPROV2
prompt = Prompt(text_template="You are a helpful assistant - now answer this. {input}")
def model_callback(prompt: Prompt, golden) -> str:
prompt_to_llm = prompt.interpolate(input=golden.input)
return your_llm(prompt_to_llm)
optimizer = PromptOptimizer(
algorithm=MIPROV2(), # Provide MIPROv2 here as the algorithm
model_callback=model_callback
)
optimized_prompt = optimizer.optimize(prompt=prompt, goldens=goldens, metrics=[AnswerRelevancyMetric()])
Done ✅. You just used MIPROv2 to run a prompt optimization.
Customize MIPROv2
You can customize MIPROv2's behavior by passing parameters directly to the MIPROV2 constructor:
from deepeval.optimizer.algorithms import MIPROV2
miprov2 = MIPROV2(
num_candidates=10,
num_trials=20,
minibatch_size=25,
max_bootstrapped_demos=4,
max_labeled_demos=4,
num_demo_sets=5
)
There are EIGHT optional parameters when creating a MIPROV2 instance:
- [Optional]
num_candidates: number of diverse instruction candidates to generate in the proposal phase. Defaulted to10. - [Optional]
num_trials: number of Bayesian Optimization trials to run. Each trial evaluates a different (instruction, demo_set) combination. Defaulted to20. - [Optional]
minibatch_size: number of goldens sampled per trial for evaluation. Larger batches give more reliable scores but cost more. Defaulted to25. - [Optional]
minibatch_full_eval_steps: run a full evaluation on all goldens every N trials. This provides accurate score estimates periodically. Defaulted to10. - [Optional]
max_bootstrapped_demos: maximum number of bootstrapped demonstrations (model-generated outputs that passed validation) per demo set. Defaulted to4. - [Optional]
max_labeled_demos: maximum number of labeled demonstrations (fromexpected_outputin your goldens) per demo set. Defaulted to4. - [Optional]
num_demo_sets: number of different demo set configurations to create. More sets provide more variety for the optimizer to explore. Defaulted to5. - [Optional]
random_seed: seed for reproducibility. Controls randomness in candidate generation, demo bootstrapping, and trial sampling. Set a fixed value (e.g.,42) to get identical results across runs. Defaulted totime.time_ns().
How Does MIPROv2 Work?
MIPROv2 works in two phases: a Proposal Phase that generates candidates upfront, followed by an Optimization Phase that uses Bayesian Optimization to find the best combination.
Unlike GEPA which evolves prompts iteratively through mutations, MIPROv2 generates all instruction candidates at once and then intelligently searches the space of (instruction, demonstration) combinations.