This chapter covers
- Defining custom modules
- Coding prompting techniques
- Coding complex, multihop workflows
- Optimizing custom modules
So far, we’ve been using DSPy’s built-in modules, particularly Predict and ChainOfThought. But one of the more powerful aspects of DSPy is that it allows us to create our own. We often learn about prompting techniques that appear powerful and potentially advantageous for our work. To use these techniques with DSPy, we can implement them as custom modules. As discussed in chapter 1, DSPy allows us to develop complex systems such as agents or retrieval-augmented generation (RAG) applications by creating custom modules. Creating them is usually relatively easy; DSPy allows them to be arbitrarily complex if necessary, but they are usually quite simple. Custom modules also allow us to create more effective language model (LM)–based applications.
We’ll go over some of the built-in DSPy modules, which provide good examples of how we can code our own. We also discuss several examples of fully working custom modules. This will cover a range of prompting techniques and multistep workflows, including adding instructions to prompts, verifying and cleaning inputs before executing queries, optimizing prompts at runtime, deconstructing complex tasks into many simpler tasks, and executing LM queries in parallel.