π§© Building with Modules
From simple predictors to complex multi-step pipelines
What You'll Learn
By the end of this chapter, you will:
Chapter Overview
This chapter takes you from basic module usage to building sophisticated LM applications:
What Are Modules?
Understand modules as the "how" to signatures' "what"βthe executable components.
Built-in Modules
Master Predict, ChainOfThought, ProgramOfThought, ReAct, and more.
Creating Custom Modules
Build your own modules with __init__ and forward methods.
Module Composition
Combine modules into pipelines and handle complex workflows.
Exercises
Build real-world modules and pipelines hands-on.
Modules vs. Signatures
Understanding the relationship between signatures and modules is key:
π Signatures
# WHAT to do (specification)
class Summarize(dspy.Signature):
"""Summarize the text."""
text: str = dspy.InputField()
summary: str = dspy.OutputField()
π§© Modules
# HOW to do it (implementation)
summarize = dspy.Predict(Summarize)
# Or with reasoning
summarize = dspy.ChainOfThought(Summarize)
result = summarize(text="...")
Key Concepts Preview
dspy.Predict
Basic module that executes a signature with a single LM call.
dspy.ChainOfThought
Adds step-by-step reasoning before generating the final answer.
dspy.ReAct
Enables tool use with an action-observation loop.
dspy.Module
Base class for creating custom modules with complex logic.
Prerequisites
Before starting this chapter, ensure you have:
Note: This chapter builds heavily on Signatures from Chapter 2. Make sure you're comfortable creating class-based signatures.
Let's Build!
Ready to turn your signatures into powerful, composable modules? Let's start!