Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Add save / load methods for DSpy settings (#9165)

WeichenXua5e068efa457
diff --git a/dspy/dsp/utils/settings.py b/dspy/dsp/utils/settings.py
index 1298f7fda..4debc6ba9 100644
--- a/dspy/dsp/utils/settings.py
+++ b/dspy/dsp/utils/settings.py
@@ -1,11 +1,16 @@
import asyncio
import contextvars
import copy
+import logging
import threading
from contextlib import contextmanager
+import cloudpickle
+
from dspy.dsp.utils.utils import dotdict
+logger = logging.getLogger(__name__)
+
DEFAULT_CONFIG = dotdict(
lm=None,
adapter=None,
@@ -185,5 +190,46 @@ class Settings:
combined_config = {**main_thread_config, **overrides}
return repr(combined_config)
+ def save(self, path, modules_to_serialize=None):
+ """
+ Save the settings to a file using cloudpickle.
+
+ Args:
+ path: The file path to save the settings to.
+ modules_to_serialize (list): A list of modules to serialize with cloudpickle's `register_pickle_by_value`.
+ If None, then no modules will be registered for serialization.
+ """
+ logger.warning(
+ "`dspy.settings` are serialized using cloudpickle. Because cloudpickle allows for the "
+ "execution of arbitrary code during deserialization, you should only load files from "
+ "verified sources within a trusted environment."
+ )
+ try:
+ modules_to_serialize = modules_to_serialize or []
+ for module in modules_to_serialize:
+ cloudpickle.register_pickle_by_value(module)
+
+ with open(path, "wb") as f:
+ cloudpickle.dump(dict(self.config), f)
+ except Exception as e:
+ raise RuntimeError(
+ f"Saving failed with error: {e}. Please remove the non-picklable attributes from the values "
+ "in the `dspy.settings`."
+ )
+
+ def load(self, path):
+ """
+ Load the settings from a file using cloudpickle.
+
+ Note: The `load` method can only be called from the thread that first configured settings
+
+ Args:
+ path: The file path to load the settings from.
+ """
+ with open(path, "rb") as f:
+ configs = cloudpickle.load(f)
+
+ self.configure(**configs)
+
settings = Settings()
diff --git a/tests/utils/test_settings.py b/tests/utils/test_settings.py
index 410309f4a..b1dff242e 100644
--- a/tests/utils/test_settings.py
+++ b/tests/utils/test_settings.py
@@ -1,4 +1,5 @@
import asyncio
+import sys
import time
from concurrent.futures import ThreadPoolExecutor
from unittest import mock
@@ -191,3 +192,56 @@ async def test_dspy_configure_allowance_async():
await asyncio.gather(foo1(), foo2(), foo3())
foo4()
+
+
+def test_dspy_settings_save_load(tmp_path):
+ dspy.configure(lm=dspy.LM("openai/gpt-4o"), adapter=dspy.JSONAdapter(), callbacks=[lambda x: x])
+
+ dspy.settings.save(tmp_path / "settings.pkl")
+ dspy.configure(lm=None, adapter=None, callbacks=None)
+
+ dspy.settings.load(tmp_path / "settings.pkl")
+ assert dspy.settings.lm.model == "openai/gpt-4o"
+ assert isinstance(dspy.settings.adapter, dspy.JSONAdapter)
+ assert len(dspy.settings.callbacks) == 1
+
+
+def test_settings_save_with_extra_modules(tmp_path):
+ # Create a temporary Python file with our custom module
+ custom_module_path = tmp_path / "custom_module.py"
+ with open(custom_module_path, "w") as f:
+ f.write(
+ """
+def callback(x):
+ return x + 1
+"""
+ )
+
+ # Add the tmp_path to Python path so we can import the module
+ sys.path.insert(0, str(tmp_path))
+ try:
+ import custom_module
+
+ dspy.configure(callbacks=[custom_module.callback])
+
+ settings_path = tmp_path / "settings.pkl"
+ sys.path.insert(0, str(tmp_path))
+
+ dspy.configure(callbacks=[custom_module.callback])
+ dspy.settings.save(settings_path, modules_to_serialize=[custom_module])
+
+ # Remove the custom module again to simulate it not being available at load time
+ sys.modules.pop("custom_module", None)
+ sys.path.remove(str(tmp_path))
+ del custom_module
+
+ dspy.configure(callbacks=None)
+ # Loading should now succeed and preserve the adapter instance
+ dspy.settings.load(settings_path)
+
+ assert dspy.settings.callbacks[0](3) == 4
+
+ finally:
+ # Only need to clean up sys.path
+ if str(tmp_path) in sys.path:
+ sys.path.remove(str(tmp_path))