加载已保存的 DSPy 模型。
此方法用于加载使用 save_program=True
保存的 DSPy 模型,即模型使用 cloudpickle 保存。
参数
返回值
源代码位于 dspy/utils/saving.py
| def load(path):
"""Load saved DSPy model.
This method is used to load a saved DSPy model with `save_program=True`, i.e., the model is saved with cloudpickle.
Args:
path (str): Path to the saved model.
Returns:
The loaded model, a `dspy.Module` instance.
"""
path = Path(path)
if not path.exists():
raise FileNotFoundError(f"The path '{path}' does not exist.")
with open(path / "metadata.json") as f:
metadata = ujson.load(f)
dependency_versions = get_dependency_versions()
saved_dependency_versions = metadata["dependency_versions"]
for key, saved_version in saved_dependency_versions.items():
if dependency_versions[key] != saved_version:
logger.warning(
f"There is a mismatch of {key} version between saved model and current environment. You saved with "
f"`{key}=={saved_version}`, but now you have `{key}=={dependency_versions[key]}`. This might cause "
"errors or performance downgrade on the loaded model, please consider loading the model in the same "
"environment as the saving environment."
)
with open(path / "program.pkl", "rb") as f:
return cloudpickle.load(f)
|