Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

immutable signature

chenmoneygithub6779776480f2
diff --git a/dspy/signatures/signature.py b/dspy/signatures/signature.py
index c84182a9f..38f546f63 100644
--- a/dspy/signatures/signature.py
+++ b/dspy/signatures/signature.py
@@ -164,25 +164,27 @@ class SignatureMeta(type(BaseModel)):
return Signature(new_fields, cls.instructions)
def dump_state(cls):
- state = {
- "instructions": cls.instructions,
- "fields": []
- }
+ state = {"instructions": cls.instructions, "fields": []}
for field in cls.fields:
- state["fields"].append({
- "prefix": cls.fields[field].json_schema_extra["prefix"],
- "description": cls.fields[field].json_schema_extra["desc"],
- })
+ state["fields"].append(
+ {
+ "prefix": cls.fields[field].json_schema_extra["prefix"],
+ "description": cls.fields[field].json_schema_extra["desc"],
+ }
+ )
return state
def load_state(cls, state):
- cls.instructions = state["instructions"]
+ signature_copy = Signature(deepcopy(cls.fields), cls.instructions)
- for field, saved_field in zip(cls.fields.values(), state["fields"]):
+ signature_copy.instructions = state["instructions"]
+ for field, saved_field in zip(signature_copy.fields.values(), state["fields"]):
field.json_schema_extra["prefix"] = saved_field["prefix"]
field.json_schema_extra["desc"] = saved_field["description"]
+ return signature_copy
+
def equals(cls, other) -> bool:
"""Compare the JSON schema of two Pydantic models."""
if not isinstance(other, type) or not issubclass(other, BaseModel):
@@ -212,7 +214,6 @@ class SignatureMeta(type(BaseModel)):
return f"{cls.__name__}({cls.signature}\n instructions={repr(cls.instructions)}\n {field_repr}\n)"
-
# A signature for a predictor.
#
# You typically subclass it, like this:
@@ -448,4 +449,4 @@ def infer_prefix(attribute_name: str) -> str:
else:
title_cased_words.append(word.capitalize())
- return " ".join(title_cased_words)
\ No newline at end of file
+ return " ".join(title_cased_words)
diff --git a/tests/signatures/test_signature.py b/tests/signatures/test_signature.py
index 4e2b56a03..bc63db1ac 100644
--- a/tests/signatures/test_signature.py
+++ b/tests/signatures/test_signature.py
@@ -239,9 +239,11 @@ def test_multiple_replaced_by_update_signatures():
assert "input1" in SignatureOne.input_fields
assert "input2" in SignatureTwo.input_fields
+
def test_dump_and_load_state():
class CustomSignature(dspy.Signature):
"""I am just an instruction."""
+
sentence = dspy.InputField(desc="I am an innocent input!")
sentiment = dspy.OutputField()
@@ -263,13 +265,17 @@ def test_dump_and_load_state():
class CustomSignature2(dspy.Signature):
"""I am a malicious instruction."""
+
sentence = dspy.InputField(desc="I am an malicious input!")
sentiment = dspy.OutputField()
assert CustomSignature2.dump_state() != expected
# Overwrite the state with the state of CustomSignature.
- CustomSignature2.load_state(state)
- assert CustomSignature2.instructions == "I am just an instruction."
+ loaded_signature = CustomSignature2.load_state(state)
+ assert loaded_signature.instructions == "I am just an instruction."
# After `load_state`, the state should be the same as CustomSignature.
- assert CustomSignature2.dump_state() == expected
-
+ assert loaded_signature.dump_state() == expected
+ # CustomSignature2 should not have been modified.
+ assert CustomSignature2.instructions == "I am a malicious instruction."
+ assert CustomSignature2.fields["sentence"].json_schema_extra["desc"] == "I am an malicious input!"
+ assert CustomSignature2.fields["sentiment"].json_schema_extra["prefix"] == "Sentiment:"