Modules
A DSPy module is a building block for programs that use LMs.
-
Each built-in module abstracts a prompting technique (like chain of thought or ReAct). Crucially, they are generalized to handle any signature.
-
A DSPy module has learnable parameters (i.e., the little pieces comprising the prompt and the LM weights) and can be invoked (called) to process inputs and return outputs.
-
Multiple modules can be composed into bigger modules (programs). DSPy modules are inspired directly by NN modules in PyTorch, but applied to LM programs.
How do I use a built-in module, like dspy.Predict
or dspy.ChainOfThought
?
Let's start with the most fundamental module, dspy.Predict
. Internally, all other DSPy modules are built using dspy.Predict
. We'll assume you are already at least a little familiar with DSPy signatures, which are declarative specs for defining the behavior of any module we use in DSPy.
To use a module, we first declare it by giving it a signature. Then we call the module with the input arguments, and extract the output fields!
sentence = "it's a charming and often affecting journey." # example from the SST-2 dataset.
# 1) Declare with a signature.
classify = dspy.Predict('sentence -> sentiment: bool')
# 2) Call with input argument(s).
response = classify(sentence=sentence)
# 3) Access the output.
print(response.sentiment)
When we declare a module, we can pass configuration keys to it.
Below, we'll pass n=5
to request five completions. We can also pass temperature
or max_len
, etc.
Let's use dspy.ChainOfThought
. In many cases, simply swapping dspy.ChainOfThought
in place of dspy.Predict
improves quality.
question = "What's something great about the ColBERT retrieval model?"
# 1) Declare with a signature, and pass some config.
classify = dspy.ChainOfThought('question -> answer', n=5)
# 2) Call with input argument.
response = classify(question=question)
# 3) Access the outputs.
response.completions.answer
['One great thing about the ColBERT retrieval model is its superior efficiency and effectiveness compared to other models.',
'Its ability to efficiently retrieve relevant information from large document collections.',
'One great thing about the ColBERT retrieval model is its superior performance compared to other models and its efficient use of pre-trained language models.',
'One great thing about the ColBERT retrieval model is its superior efficiency and accuracy compared to other models.',
'One great thing about the ColBERT retrieval model is its ability to incorporate user feedback and support complex queries.']
Let's discuss the output object here. The dspy.ChainOfThought
module will generally inject a reasoning
before the output field(s) of your signature.
Let's inspect the (first) reasoning and answer!
Possible Output:Reasoning: We can consider the fact that ColBERT has shown to outperform other state-of-the-art retrieval models in terms of efficiency and effectiveness. It uses contextualized embeddings and performs document retrieval in a way that is both accurate and scalable.
Answer: One great thing about the ColBERT retrieval model is its superior efficiency and effectiveness compared to other models.
This is accessible whether we request one or many completions.
We can also access the different completions as a list of Prediction
s or as several lists, one for each field.
What other DSPy modules are there? How can I use them?
The others are very similar. They mainly change the internal behavior with which your signature is implemented!
-
dspy.Predict
: Basic predictor. Does not modify the signature. Handles the key forms of learning (i.e., storing the instructions and demonstrations and updates to the LM). -
dspy.ChainOfThought
: Teaches the LM to think step-by-step before committing to the signature's response. -
dspy.ProgramOfThought
: Teaches the LM to output code, whose execution results will dictate the response. -
dspy.ReAct
: An agent that can use tools to implement the given signature. -
dspy.MultiChainComparison
: Can compare multiple outputs fromChainOfThought
to produce a final prediction.
We also have some function-style modules:
dspy.majority
: Can do basic voting to return the most popular response from a set of predictions.
A few examples of DSPy modules on simple tasks.
Try the examples below after configuring your lm
. Adjust the fields to explore what tasks your LM can do well out of the box.
Possible Output:
Prediction(
reasoning='When two dice are tossed, each die has 6 faces, resulting in a total of 6 x 6 = 36 possible outcomes. The sum of the numbers on the two dice equals two only when both dice show a 1. This is just one specific outcome: (1, 1). Therefore, there is only 1 favorable outcome. The probability of the sum being two is the number of favorable outcomes divided by the total number of possible outcomes, which is 1/36.',
answer=0.0277776
)
Possible Output:
Prediction(
reasoning='The context provides information about David Gregory, a Scottish physician and inventor. It specifically mentions that he inherited Kinnairdy Castle in 1664. This detail directly answers the question about the name of the castle that David Gregory inherited.',
response='Kinnairdy Castle'
)
Possible Output:
Possible Output:
Possible Output:
How do I compose multiple modules into a bigger program?
DSPy is just Python code that uses modules in any control flow you like, with a little magic internally at compile
time to trace your LM calls. What this means is that, you can just call the modules freely. No weird abstractions for chaining calls. This is basically PyTorch's design approach for define-by-run / dynamic computation graphs. Refer to the intro tutorials for examples.