5 Optimizing prompt examples
As we’ve discussed throughout the book, optimizing prompts is one of the most important parts of prompt programming, so let’s now look at how this is done in DSPy. DSPy optimization works in much the same way as training machine learning models. In particular, it’s also a data-driven process. When training a machine learning model, we may evaluate many different ways to create a model and then select the one that appears to work best: the model that maximizes (or minimizes) the relevant metric on the validation set. This general approach has strongly influenced prompt programming and is the basis of DSPy’s optimization methods.
With prompt programming, we seek to find the prompt that maximizes the specified metric function for our validation dataset. As with machine learning, this is an automated and data-driven approach, in which DSPy generates many candidate prompts and methodically tests them with the validation set against the language model (LM). It’s impossible to evaluate the infinite number of possible prompts, so we need to generate a reasonable number and carefully evaluate each one. We go over how this is done with DSPy in this chapter and the next.