Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

fix signature saving

chenmoneygithub8c929d3ec08d
diff --git a/dspy/predict/predict.py b/dspy/predict/predict.py
index 8bd42699d..c90419ad0 100644
--- a/dspy/predict/predict.py
+++ b/dspy/predict/predict.py
@@ -1,16 +1,15 @@
+import logging
import random
+from functools import lru_cache
from pydantic import BaseModel
import dsp
from dspy.predict.parameter import Parameter
-from dspy.primitives.program import Module
-
from dspy.primitives.prediction import Prediction
+from dspy.primitives.program import Module
from dspy.signatures.signature import ensure_signature, signature_to_template
-import logging
-from functools import lru_cache
@lru_cache(maxsize=None)
def warn_once(msg: str):
@@ -31,7 +30,13 @@ class Predict(Module, Parameter):
self.train = []
self.demos = []
- def dump_state(self, save_verbose=False):
+ def dump_state(self, save_verbose=None):
+ if save_verbose:
+ logging.warning(
+ "`save_verbose` is deprecated and will be removed in DSPy 2.6.0 release. Currently `save_verbose` "
+ "does nothing."
+ )
+
state_keys = ["lm", "traces", "train"]
state = {k: getattr(self, k) for k in state_keys}
@@ -45,55 +50,25 @@ class Predict(Module, Parameter):
state["demos"].append(demo)
- # If `save_verbose` save all field metadata as well.
- if save_verbose:
- fields = []
- for field_key in self.signature.fields.keys():
- field_metadata = self.signature.fields[field_key]
- fields.append({
- "name": field_key,
- "field_type": field_metadata.json_schema_extra["__dspy_field_type"],
- "description": field_metadata.json_schema_extra["desc"],
- "prefix": field_metadata.json_schema_extra["prefix"]
- })
- state["fields"] = fields
-
- # Cache the signature instructions and the last field's name.
- *_, last_key = self.signature.fields.keys()
- state["signature_instructions"] = self.signature.instructions
- state["signature_prefix"] = self.signature.fields[last_key].json_schema_extra["prefix"]
-
- # Some special stuff for CoT.
+ state["signature"] = self.signature.dump_state()
+ # `extended_signature` is a special field for `Predict`s like CoT.
if hasattr(self, "extended_signature"):
- # Cache the signature instructions and the last field's name.
- state["extended_signature_instructions"] = self.extended_signature.instructions
- state["extended_signature_prefix"] = self.extended_signature.fields[last_key].json_schema_extra['prefix']
+ state["extended_signature"] = self.extended_signature.dump_state()
return state
def load_state(self, state):
+ excluded_keys = ["signature", "extended_signature"]
for name, value in state.items():
- setattr(self, name, value)
-
- # Reconstruct the signature.
- if "signature_instructions" in state:
- instructions = state["signature_instructions"]
- self.signature = self.signature.with_instructions(instructions)
-
- if "signature_prefix" in state:
- prefix = state["signature_prefix"]
- *_, last_key = self.signature.fields.keys()
- self.signature = self.signature.with_updated_fields(last_key, prefix=prefix)
-
- # Some special stuff for CoT.
- if "extended_signature_instructions" in state:
- instructions = state["extended_signature_instructions"]
- self.extended_signature = self.extended_signature.with_instructions(instructions)
-
- if "extended_signature_prefix" in state:
- prefix = state["extended_signature_prefix"]
- *_, last_key = self.extended_signature.fields.keys()
- self.extended_signature = self.extended_signature.with_updated_fields(last_key, prefix=prefix)
+ # `excluded_keys` are fields that go through special handling.
+ if name not in excluded_keys:
+ setattr(self, name, value)
+
+ self.signature.load_state(state["signature"])
+
+ if "extended_signature" in state:
+ self.extended_signature.load_state(state["extended_signature"])
+
def __call__(self, **kwargs):
return self.forward(**kwargs)
diff --git a/dspy/signatures/signature.py b/dspy/signatures/signature.py
index 16c2dc66d..c84182a9f 100644
--- a/dspy/signatures/signature.py
+++ b/dspy/signatures/signature.py
@@ -96,6 +96,10 @@ class SignatureMeta(type(BaseModel)):
def instructions(cls) -> str:
return inspect.cleandoc(getattr(cls, "__doc__", ""))
+ @instructions.setter
+ def instructions(cls, instructions: str) -> None:
+ setattr(cls, "__doc__", instructions)
+
def with_instructions(cls, instructions: str) -> Type["Signature"]:
return Signature(cls.fields, instructions)
@@ -159,6 +163,26 @@ class SignatureMeta(type(BaseModel)):
new_fields = dict(input_fields + output_fields)
return Signature(new_fields, cls.instructions)
+ def dump_state(cls):
+ 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"],
+ })
+
+ return state
+
+ def load_state(cls, state):
+ cls.instructions = state["instructions"]
+
+ for field, saved_field in zip(cls.fields.values(), state["fields"]):
+ field.json_schema_extra["prefix"] = saved_field["prefix"]
+ field.json_schema_extra["desc"] = saved_field["description"]
+
def equals(cls, other) -> bool:
"""Compare the JSON schema of two Pydantic models."""
if not isinstance(other, type) or not issubclass(other, BaseModel):
@@ -188,6 +212,7 @@ 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:
diff --git a/tests/predict/test_predict.py b/tests/predict/test_predict.py
index b04e087e8..8d0121eed 100644
--- a/tests/predict/test_predict.py
+++ b/tests/predict/test_predict.py
@@ -126,6 +126,27 @@ def test_typed_demos_after_dump_and_load_state():
# Demos don't need to keep the same types after saving and loading the state.
assert new_instance.demos[0]["input"] == original_instance.demos[0].input.model_dump_json()
+def test_signature_fields_after_dump_and_load_state(tmp_path):
+ class CustomSignature(dspy.Signature):
+ """I am just an instruction."""
+ sentence = dspy.InputField(desc="I am an innocent input!")
+ sentiment = dspy.OutputField()
+
+ file_path = tmp_path / "tmp.json"
+ original_instance = Predict(CustomSignature)
+ original_instance.save(file_path)
+
+ class CustomSignature2(dspy.Signature):
+ """I am not a pure instruction."""
+ sentence = dspy.InputField(desc="I am a malicious input!")
+ sentiment = dspy.OutputField(desc="I am a malicious output!", prefix="I am a prefix!")
+
+ new_instance = Predict(CustomSignature2)
+ assert new_instance.signature.dump_state() != original_instance.signature.dump_state()
+ # After loading, the fields should be the same.
+ new_instance.load(file_path)
+ assert new_instance.signature.dump_state() == original_instance.signature.dump_state()
+
def test_forward_method():
program = Predict("question -> answer")
diff --git a/tests/signatures/test_signature.py b/tests/signatures/test_signature.py
index 8fb9c5ff2..4e2b56a03 100644
--- a/tests/signatures/test_signature.py
+++ b/tests/signatures/test_signature.py
@@ -238,3 +238,38 @@ def test_multiple_replaced_by_update_signatures():
assert "input4" in SignatureTwo.input_fields
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()
+
+ state = CustomSignature.dump_state()
+ expected = {
+ "instructions": "I am just an instruction.",
+ "fields": [
+ {
+ "prefix": "Sentence:",
+ "description": "I am an innocent input!",
+ },
+ {
+ "prefix": "Sentiment:",
+ "description": "${sentiment}",
+ },
+ ],
+ }
+ assert state == expected
+
+ 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."
+ # After `load_state`, the state should be the same as CustomSignature.
+ assert CustomSignature2.dump_state() == expected
+