[](https://hamel.dev/blog/posts/prompt/#cb20-1)from dspy.teleprompt import BootstrapFewShotWithRandomSearch [](https://hamel.dev/blog/posts/prompt/#cb20-2) [](https://hamel.dev/blog/posts/prompt/#cb20-3)# Set up the optimizer: we want to "bootstrap" (i.e., self-generate) 8-shot examples of our CoT program. [](https://hamel.dev/blog/posts/prompt/#cb20-4)# The optimizer will repeat this 10 times (plus some initial attempts) before selecting its best attempt on the devset. [](https://hamel.dev/blog/posts/prompt/#cb20-5)config = dict(max_bootstrapped_demos=8, max_labeled_demos=8, num_candidate_programs=10, num_threads=4) [](https://hamel.dev/blog/posts/prompt/#cb20-6) [](https://hamel.dev/blog/posts/prompt/#cb20-7)# Optimize! Use the `gms8k_metric` here. In general, the metric is going to tell the optimizer how well it's doing. [](https://hamel.dev/blog/posts/prompt/#cb20-8)teleprompter = BootstrapFewShotWithRandomSearch(metric=gsm8k_metric, **config) [](https://hamel.dev/blog/posts/prompt/#cb20-9)optimized_cot = teleprompter.compile(CoT(), trainset=trainset, valset=devset)