liveBook · Manning

Modularity

As explained in Building LLM Applications with DSPy

Modular organization is a structural pattern that separates an application into composable components with distinct responsibilities.

Modularity organizes an application as small components with clear inputs, outputs, and purposes. This makes components easier to test, debug, review, and monitor, while allowing one component to be replaced without rewriting the rest. section 1.2.1

In DSPy, modularity supports practical substitutions:

  • Swap one language model for another without changing the surrounding code. section 1.2.1
  • Replace a prompting technique, such as Predict, with another while keeping the rest of the application intact. section 1.2.1
Listing 1.1 DSPy example
import dspy
OPENAI_API_KEY = [indicate your API key]
lm = dspy.LM("openai/gpt-4o-mini", api_key=OPENAI_API_KEY)
dspy.configure(lm=lm) 
predictor = dspy.Predict("question, context -> answer, confidence") 
prediction = predictor(question="What is the capital of France?", context="")
print(prediction.answer, prediction.confidence)

The example configures a language model and a Predict component separately, then calls the predictor with a question and context. section 1.2.1

How Building LLM Applications with DSPy covers it

How deep it goes
Explains component boundaries and substitution, but does not detail implementation internals.
Examples
Uses interchangeable language models and predictors, with scikit-learn model swaps and PyTorch architecture changes as comparisons.
What you can do after
Apply modular organization by swapping DSPy language models or prompting techniques without changing other code.
How it connects
Connects modularity to DSPy, language models, prompting techniques, scikit-learn, and PyTorch.

Also explained in

Compare how 10 books explain modularity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.