Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Fix save and load state of program with typed predictors

Alex3751c7e57343
diff --git a/dspy/predict/predict.py b/dspy/predict/predict.py
index 37fff10c4..083b6b210 100644
--- a/dspy/predict/predict.py
+++ b/dspy/predict/predict.py
@@ -4,6 +4,7 @@ import dsp
from dspy.predict.parameter import Parameter
from dspy.primitives.prediction import Prediction
from dspy.signatures.signature import ensure_signature, signature_to_template
+from pydantic import BaseModel
class Predict(Parameter):
@@ -20,9 +21,19 @@ class Predict(Parameter):
self.demos = []
def dump_state(self):
- state_keys = ["lm", "traces", "train", "demos"]
+ state_keys = ["lm", "traces", "train"]
state = {k: getattr(self, k) for k in state_keys}
+ state["demos"] = []
+ for demo in self.demos:
+ demo = demo.copy()
+
+ for field in demo:
+ if isinstance(demo[field], BaseModel):
+ demo[field] = demo[field].model_dump_json()
+
+ state["demos"].append(demo)
+
# Cache the signature instructions and the last field's name.
state["signature_instructions"] = self.signature.instructions
diff --git a/tests/predict/test_predict.py b/tests/predict/test_predict.py
index c0407b938..dbdad0d23 100644
--- a/tests/predict/test_predict.py
+++ b/tests/predict/test_predict.py
@@ -1,8 +1,10 @@
import dspy
-from dspy import Predict, Signature
+from dspy import Predict, Signature, TypedPredictor
from dspy.utils.dummies import DummyLM
import copy
import textwrap
+import pydantic
+import ujson
def test_initialization_with_string_signature():
@@ -26,7 +28,7 @@ def test_reset_method():
assert predict_instance.demos == []
-def test_dump_and_load_state():
+def test_lm_after_dump_and_load_state():
predict_instance = Predict("input -> output")
predict_instance.lm = "lm_state"
dumped_state = predict_instance.dump_state()
@@ -53,7 +55,7 @@ def test_call_method():
)
-def test_dump_load_state():
+def test_instructions_after_dump_and_load_state():
predict_instance = Predict(Signature("input -> output", "original instructions"))
dumped_state = predict_instance.dump_state()
new_instance = Predict(Signature("input -> output", "new instructions"))
@@ -61,6 +63,80 @@ def test_dump_load_state():
assert new_instance.signature.instructions == "original instructions"
+class TranslateToEnglish(dspy.Signature):
+ """Translate content from a language to English."""
+
+ content: str = dspy.InputField()
+ language: str = dspy.InputField()
+ translation: str = dspy.OutputField()
+
+
+def test_demos_after_dump_and_load_state():
+ original_instance = Predict(TranslateToEnglish)
+ original_instance.demos = [
+ dspy.Example(
+ content="¿Qué tal?",
+ language="SPANISH",
+ translation="Hello there",
+ ).with_inputs("content", "language"),
+ ]
+
+ dumped_state = original_instance.dump_state()
+ assert len(dumped_state["demos"]) == len(original_instance.demos)
+ assert dumped_state["demos"][0]["content"] == original_instance.demos[0].content
+
+ saved_state = ujson.dumps(dumped_state)
+ loaded_state = ujson.loads(saved_state)
+
+ new_instance = Predict(TranslateToEnglish)
+ new_instance.load_state(loaded_state)
+ assert len(new_instance.demos) == len(original_instance.demos)
+ # Demos don't need to keep the same types after saving and loading the state.
+ assert new_instance.demos[0]["content"] == original_instance.demos[0].content
+
+
+class TypedTranslateToEnglish(dspy.Signature):
+ """Translate content from a language to English."""
+
+ class Input(pydantic.BaseModel):
+ content: str
+ language: str
+
+ class Output(pydantic.BaseModel):
+ translation: str
+
+ input: Input = dspy.InputField()
+ output: Output = dspy.OutputField()
+
+
+def test_typed_demos_after_dump_and_load_state():
+ original_instance = TypedPredictor(TypedTranslateToEnglish).predictor
+ original_instance.demos = [
+ dspy.Example(
+ input=TypedTranslateToEnglish.Input(
+ content="¿Qué tal?",
+ language="SPANISH",
+ ),
+ output=TypedTranslateToEnglish.Output(
+ translation="Hello there",
+ ),
+ ).with_inputs("input"),
+ ]
+
+ dumped_state = original_instance.dump_state()
+ assert len(dumped_state["demos"]) == len(original_instance.demos)
+ assert dumped_state["demos"][0]["input"] == original_instance.demos[0].input.model_dump_json()
+
+ saved_state = ujson.dumps(dumped_state)
+ loaded_state = ujson.loads(saved_state)
+
+ new_instance = TypedPredictor(TypedTranslateToEnglish).predictor
+ new_instance.load_state(loaded_state)
+ assert len(new_instance.demos) == len(original_instance.demos)
+ # 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_forward_method():
program = Predict("question -> answer")
dspy.settings.configure(lm=DummyLM([]))