Skip to main content

internal-signatures

DSPy's Internal Signatures

DSPy makes extensive use of signatures internally to define tasks and their requirements. Signatures are used to define the inputs and outputs of a task, and optionally, a small description about them and the task too.

Sometimes, you may want to update the signature of a task to include instructions or additional fields. You can access and replace any signature in DSPy using the dspy.Signature.replace context manager.

Updating Signatures

To update a signature, you can use the dspy.Signature.replace context manager. This context manager allows you to replace the signature of a task with a new one. Here's an example of updating one of the signatures used in the Copro optimizer to add additional instructions:

from dspy.teleprompt import copro_optimizer
import dspy

class MyBasicGenerateInstruction(mipro_optimizer.BasicGenerateInstruction):
"""
<PERSONA>
You are an instruction optimizer for large language models.
</PERSONA>
<TASK>
I will give you a ``signature`` of fields (inputs and outputs) in English. Your task is to propose an instruction that will lead a good language model to perform the task well. Don't be afraid to be creative, but the new instruction you propose should be clear and concise.
</TASK>
"""
basic_instruction = dspy.InputField(desc="The unoptimized instruction. New instructions should achieve the same goals.")

with mipro_optimizer.BasicGenerateInstruction.replace(MyBasicGenerateInstruction):
teleprompter = COPRO(prompt_model=prompt_model, metric=metric, breadth=BREADTH, depth=DEPTH, init_temperature=INIT_TEMPERATURE)
kwargs = dict(num_threads=NUM_THREADS, display_progress=True, display_table=0)
compiled_prompt_opt = teleprompter.compile(program.deepcopy(), trainset=trainset[:DEV_NUM], eval_kwargs=kwargs)
eval_score = evaluate(compiled_prompt_opt, devset=evalset[:EVAL_NUM], **kwargs)

This will now update the signature of the BasicGenerateInstruction task in the Copro optimizer to include the new basic_instruction field.

You can replace any signature in DSPy using this method, which will be used as long as the context manager is active.

There is also a helper function dspy.update_signatures that can update multiple signatures at once. This function takes a dictionary of signatures to update, with the key being the signature to update and the value being the new signature class.


from dspy.teleprompt import copro_optimizer
import dspy

class MyBasicGenerateInstruction(mipro_optimizer.BasicGenerateInstruction):
"""
<PERSONA>
You are an instruction optimizer for large language models.
</PERSONA>
<TASK>
I will give you a ``signature`` of fields (inputs and outputs) in English. Your task is to propose an instruction that will lead a good language model to perform the task well. Don't be afraid to be creative, but the new instruction you propose should be clear and concise.
</TASK>
"""
basic_instruction = dspy.InputField(desc="The unoptimized instruction. New instructions should achieve the same goals.")

class GenerateInstructionGivenAttempts(mipro_optimizer.GenerateInstructionGivenAttempts):
"""
<PERSONA>
You are an instruction optimizer for large language models.
</PERSONA>
<TASK>
I will give some task instructions I've tried, along with their corresponding validation scores. The instructions are arranged in increasing order based on their scores, where higher scores indicate better quality.

Your task is to propose a new instruction that will lead a good language model to perform the task even better. Don't be afraid to be creative
</TASK>
"""
pass

with dspy.update_signatures({
copro_optimizer.BasicGenerateInstruction: MyBasicGenerateInstruction,
copro_optimizer.GenerateInstructionGivenAttempts: GenerateInstructionGivenAttempts
}):
...


Written By: Michael Jones