Modules - DSPy
Modules - DSPy
Modules - DSPy
Modules
Each built-in module abstracts a prompting technique (like chain of thought or ReAct).
Crucially, they are generalized to handle any DSPy 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.
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.
Output:
Ask AI
Positive
https://dspy-docs.vercel.app/building-blocks/3-modules/ 1/3
22/10/2024, 15:36 Modules - DSPy
Below, we'll pass n=5 to request five completions. We can also pass temperature or
max_len , etc.
Output:
['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.']
The dspy.ChainOfThought module will generally inject a rationale before the output field(s)
of your signature.
print(f"Rationale: {response.rationale}")
print(f"Answer: {response.answer}")
Output:
Rationale: produce the answer. 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. Ask AI
Answer: One great thing about the ColBERT retrieval model is its superior
efficiency and effectiveness compared to other models.
https://dspy-docs.vercel.app/building-blocks/3-modules/ 2/3
22/10/2024, 15:36 Modules - DSPy
We can also access the different completions as a list of Prediction s or as several lists, one
for each field.
response.completions[3].rationale == response.completions.rationale[3]
Output:
True
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!
1. 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).
4. dspy.ReAct : An agent that can use tools to implement the given signature.
1. dspy.majority : Can do basic voting to return the most popular response from a set of
predictions.
What this means is that, you can just call the modules freely. No weird abstractions for
chaining calls. Ask AI
This is basically PyTorch's design approach for define-by-run / dynamic computation graphs.
Refer to the intro tutorials for examples.
https://dspy-docs.vercel.app/building-blocks/3-modules/ 3/3