Chapter 7. Customizing DSPy Programs
In Chapter 6, you mastered DSPy’s optimizers: the algorithms that tune your prompts, select few-shot examples, and even fine-tune model weights automatically. But as we noted at the end of that chapter, if the task isn’t succeeding after a few optimization runs, it’s most likely a module problem. The optimizer can only work with the strategy you give it. A bad module with a great optimizer still produces bad results.
Modules are just inference strategies, for spending more tokens in a way that guides the model towards better results. Because DSPy defines your task with Signatures rather than string-based templates, it’s easy to swap in a different strategy without rewriting your program. Your Signature stays the same. Your dataset stays the same. Your metrics stay the same. Only the inference strategy changes.
This is one of Omar Khattab’s core bets for DSPy: inference strategies should be polymorphic modules. That sounds academic, but the practical implication is powerful. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access