chapter seven

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.

7.1 Types of DSPy modules

7.2 Coding a custom module

7.3 DSPy’s built-in modules

7.3.1 ChainOfThought (CoT)

7.3.2 BestOfN

7.3.3 MultiChainComparison

7.3.4 Refine

7.4 Examples of custom modules

7.4.1 Adding a closer section

7.4.2 Rewording the inputs

7.4.3 Separating out toxic comments

7.4.4 Module to check for security

7.4.5 Chain of Draft

7.4.6 Creating a critic-based module

7.5 Parallelism in custom modules

7.5.1 The asyncio library

7.5.2 Calling modules in parallel

7.5.3 Parallel execution within modules

7.5.4 Asynchronous Skeleton-of-Thought

7.6 Optimizing custom modules

7.6.1 Using BootstrapFewShot

7.6.2 Optimizing using MIRPOv2

7.6.3 Examining the set of sub-modules

7.7 Creating complex vs simple modules

7.8 Using DSPy to create custom modules

7.9 Summary