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