DSPy: The framework for programming—not prompting—language models
Python
Fix save predict model with LM (#7861)
TomuHirata08478cc84723
diff --git a/dspy/clients/lm.py b/dspy/clients/lm.pyindex 6f5468b84..47e69f95f 100644--- a/dspy/clients/lm.py+++ b/dspy/clients/lm.py@@ -246,6 +246,10 @@ class LM(BaseLM):new_instance.kwargs[key] = valuereturn new_instance++ def dump_state(self):+ state_keys = ["model", "model_type", "cache", "cache_in_memory", "num_retries", "finetuning_model", "launch_kwargs", "train_kwargs"]+ return { key: getattr(self, key) for key in state_keys } | self.kwargsdef request_cache(maxsize: Optional[int] = None):diff --git a/dspy/predict/predict.py b/dspy/predict/predict.pyindex c0e13ec62..e064a966e 100644--- a/dspy/predict/predict.py+++ b/dspy/predict/predict.py@@ -7,6 +7,7 @@ from dspy.primitives.prediction import Predictionfrom dspy.primitives.program import Modulefrom dspy.signatures.signature import ensure_signaturefrom dspy.utils.callback import with_callbacks+from dspy.clients.lm import LMclass Predict(Module, Parameter):@@ -24,7 +25,7 @@ class Predict(Module, Parameter):self.demos = []def dump_state(self):- state_keys = ["lm", "traces", "train"]+ state_keys = ["traces", "train"]state = {k: getattr(self, k) for k in state_keys}state["demos"] = []@@ -38,6 +39,7 @@ class Predict(Module, Parameter):state["demos"].append(demo)state["signature"] = self.signature.dump_state()+ state["lm"] = self.lm.dump_state() if self.lm else Nonereturn statedef load_state(self, state):@@ -49,13 +51,14 @@ class Predict(Module, Parameter):Returns:self: Returns self to allow method chaining"""- excluded_keys = ["signature", "extended_signature"]+ excluded_keys = ["signature", "extended_signature", "lm"]for name, value in state.items():# `excluded_keys` are fields that go through special handling.if name not in excluded_keys:setattr(self, name, value)self.signature = self.signature.load_state(state["signature"])+ self.lm = LM(**state["lm"]) if state["lm"] else Noneif "extended_signature" in state: # legacy, up to and including 2.5, for CoT.raise NotImplementedError("Loading extended_signature is no longer supported in DSPy 2.6+")diff --git a/tests/clients/test_lm.py b/tests/clients/test_lm.pyindex 46572c8c3..9d218ff90 100644--- a/tests/clients/test_lm.py+++ b/tests/clients/test_lm.py@@ -226,3 +226,27 @@ def test_reasoning_model_requirements():max_tokens=5000,)assert lm.kwargs["max_completion_tokens"] == 5000++def test_dump_state():+ lm = dspy.LM(+ model="openai/gpt-4o-mini",+ model_type="chat",+ temperature=1,+ max_tokens=100,+ num_retries=10,+ launch_kwargs={ "temperature": 1 },+ train_kwargs={ "temperature": 5 },+ )++ assert lm.dump_state() == {+ "model": "openai/gpt-4o-mini",+ "model_type": "chat",+ "temperature": 1,+ "max_tokens": 100,+ "num_retries": 10,+ "cache": True,+ "cache_in_memory": True,+ "finetuning_model": None,+ "launch_kwargs": { "temperature": 1 },+ "train_kwargs": { "temperature": 5 },+ }diff --git a/tests/predict/test_predict.py b/tests/predict/test_predict.pyindex 037a49404..c734f6219 100644--- a/tests/predict/test_predict.py+++ b/tests/predict/test_predict.py@@ -35,11 +35,31 @@ def test_reset_method():def test_lm_after_dump_and_load_state():predict_instance = Predict("input -> output")- predict_instance.lm = "lm_state"+ lm = dspy.LM(+ model="openai/gpt-4o-mini",+ model_type="chat",+ temperature=1,+ max_tokens=100,+ num_retries=10,+ )+ predict_instance.lm = lm+ expected_lm_state = {+ "model": "openai/gpt-4o-mini",+ "model_type": "chat",+ "temperature": 1,+ "max_tokens": 100,+ "num_retries": 10,+ "cache": True,+ "cache_in_memory": True,+ "finetuning_model": None,+ "launch_kwargs": {},+ "train_kwargs": {},+ }+ assert lm.dump_state() == expected_lm_statedumped_state = predict_instance.dump_state()new_instance = Predict("input -> output")new_instance.load_state(dumped_state)- assert new_instance.lm == "lm_state"+ assert new_instance.lm.dump_state() == expected_lm_statedef test_call_method():@@ -209,6 +229,28 @@ def test_signature_fields_after_dump_and_load_state(tmp_path):new_instance.load(file_path)assert new_instance.signature.dump_state() == original_instance.signature.dump_state()[email protected]("filename", ["model.json", "model.pkl"])+def test_lm_field_after_dump_and_load_state(tmp_path, filename):+ file_path = tmp_path / filename+ lm = dspy.LM(+ model="openai/gpt-4o-mini",+ model_type="chat",+ temperature=1,+ max_tokens=100,+ num_retries=10,+ )+ original_predict = dspy.Predict("q->a")+ original_predict.lm = lm++ original_predict.save(file_path)++ assert file_path.exists()++ loaded_predict = dspy.Predict("q->a")+ loaded_predict.load(file_path)++ assert original_predict.dump_state() == loaded_predict.dump_state()+def test_forward_method():program = Predict("question -> answer")