Signatures - DSPy
Signatures - DSPy
Signatures
When we assign tasks to LMs in DSPy, we specify the behavior we need as a Signature.
You're probably familiar with function signatures, which specify the input and output arguments
and their types. DSPy signatures are similar, but the differences are that:
While typical function signatures just describe things, DSPy Signatures define and control
the behavior of modules.
The field names matter in DSPy Signatures. You express semantic roles in plain English: a
question is different from an answer , a sql_query is different from python_code .
Long Answer: Most people coerce LMs to do tasks by hacking long, brittle prompts. Or by
collecting/generating data for fine-tuning.
Writing signatures is far more modular, adaptive, and reproducible than hacking at prompts or
finetunes. The DSPy compiler will figure out how to build a highly-optimized prompt for your LM
(or finetune your small LM) for your signature, on your data, and within your pipeline. In many
cases, we found that compiling leads to better prompts than humans write. Not because DSPy
optimizers are more creative than humans, but simply because they can try more things and
tune the metrics directly.
https://dspy-docs.vercel.app/building-blocks/2-signatures/ 1/5
22/10/2024, 15:36 Signatures - DSPy
Tip: For fields, any valid variable names work! Field names should be semantically meaningful,
but start simple and don't prematurely optimize keywords! Leave that kind of hacking to the
DSPy compiler. For example, for summarization, it's probably fine to say "document ->
summary" , "text -> gist" , or "long_context -> tldr" .
sentence = "it's a charming and often affecting journey." # example from the
SST-2 dataset.
Output:
'Positive'
Example B: Summarization
print(response.summary)
Output:
Ask AI
The 21-year-old Lee made seven appearances and scored one goal for West Ham
last season. He had loan spells in League One with Blackpool and Colchester
https://dspy-docs.vercel.app/building-blocks/2-signatures/ 2/5
22/10/2024, 15:36 Signatures - DSPy
United, scoring twice for the latter. He has now signed a contract with
Barnsley, but the length of the contract has not been revealed.
Many DSPy modules (except dspy.Predict ) return auxiliary information by expanding your
signature under the hood.
For example, dspy.ChainOfThought also adds a rationale field that includes the LM's
reasoning before it generates the output summary .
print("Rationale:", response.rationale)
Output:
Rationale: produce the summary. We need to highlight the key points about
Lee's performance for West Ham, his loan spells in League One, and his new
contract with Barnsley. We also need to mention that his contract length has
not been disclosed.
1. Clarify something about the nature of the task (expressed below as a docstring ).
2. Supply hints on the nature of an input field, expressed as a desc keyword argument for
dspy.InputField .
Example C: Classification
Notice how the docstring contains (minimal) instructions, which in this case are necessary to
have a fully-defined task.
Some optimizers in DSPy, like COPRO , can take this simple docstring and then generate more
effective variants if needed.
class Emotion(dspy.Signature):
"""Classify emotion among sadness, joy, love, anger, fear, surprise."""
sentence = dspy.InputField()
sentiment = dspy.OutputField() Ask AI
sentence = "i started feeling a little vulnerable when the giant spotlight
started blinding me" # from dair-ai/emotion
https://dspy-docs.vercel.app/building-blocks/2-signatures/ 3/5
22/10/2024, 15:36 Signatures - DSPy
classify = dspy.Predict(Emotion)
classify(sentence=sentence)
Output:
Prediction(
sentiment='Fear'
)
Tip: There's nothing wrong with specifying your requests to the LM more clearly. Class-based
Signatures help you with that. However, don't prematurely tune the keywords of the your
signature by hand. The DSPy optimizers will likely do a better job (and will transfer better
across LMs).
class CheckCitationFaithfulness(dspy.Signature):
"""Verify that the text is based on the provided context."""
context = "The 21-year-old made seven appearances for the Hammers and netted
his only goal for them in a Europa League qualification round match against
Andorran side FC Lustrains last season. Lee had two loan spells in League One
last term, with Blackpool and then Colchester United. He scored twice for the
U's but was unable to save them from relegation. The length of Lee's contract
with the promoted Tykes has not been revealed. Find all the latest football
transfers on our dedicated page."
faithfulness = dspy.ChainOfThought(CheckCitationFaithfulness)
faithfulness(context=context, text=text)
Output:
Prediction(
rationale="produce the faithfulness. We know that Lee had two loan spells
in League One last term, with Blackpool and then Colchester United. He scored
twice for the U's but was unable to save them from relegation. However, there
is no mention of him scoring three goals for Colchester United.",
faithfulness='False'
)
Ask AI
https://dspy-docs.vercel.app/building-blocks/2-signatures/ 4/5
22/10/2024, 15:36 Signatures - DSPy
While signatures are convenient for prototyping with structured inputs/outputs, that's not the
main reason to use them!
You should compose multiple signatures into bigger DSPy modules and compile these
modules into optimized prompts and finetunes.
Ask AI
https://dspy-docs.vercel.app/building-blocks/2-signatures/ 5/5