DSPy: The framework for programming—not prompting—language models
Python
Convert a method on the Predict class to a function
dilaraf30d572b862c
diff --git a/dspy/predict/predict.py b/dspy/predict/predict.pyindex c17a60be0..78dc0c20d 100644--- a/dspy/predict/predict.py+++ b/dspy/predict/predict.py@@ -10,15 +10,30 @@ from dspy.primitives.program import Modulefrom dspy.primitives.prediction import Predictionfrom dspy.signatures.signature import ensure_signature, signature_to_template+#--------------------------------------------------------------------------------# Templates for the user-facing strings used by this module+# Helper functions#--------------------------------------------------------------------------------_ERR_MSG_CLASS = """The class of this object does not match the class of the \-other object: '{sname}' != '{oname}'"""+def assert_structural_equivalency_for_predictors(+ predictor1: object,+ predictor2: object,+ ) -> Optional[AssertionError]:+ """Assert that the two predictors are structurally equivalent to each other.++ Args:+ predictor1: The predictor to compare with.+ predictor2: The predictor to compare with.-_ERR_MSG_SIGNATURE = """Structurally equivalent predictors must have the same \-signature: '{sname}' != '{oname}'"""+ Raises:+ AssertionError: If the predictors are not structurally equivalent.+ """+ # Assert that the objects are predictors+ assert isinstance(predictor1, Predict)+ assert isinstance(predictor2, Predict)++ # Assert that the predictors have equivalent signatures+ assert predictor1.signature.equals(predictor2.signature)#-------------------------------------------------------------------------------@@ -32,30 +47,6 @@ class Predict(Module, Parameter):self.config = configself.reset()- def _assert_structural_equivalency(- self,- other: Predict- ) -> Optional[AssertionError]:- """Assert that the predictor is structurally equivalent to another.-- Args:- other: The predictor to compare with.-- Raises:- AssertionError: If the predictors are not structurally equivalent.- """- # Assert that the other object is an instance of the same class- sname = self.__class__.__name__- oname = other.__class__.__name__- err_msg = _ERR_MSG_CLASS.format(sname=sname, oname=oname)- assert isinstance(other, self.__class__), err_msg-- # Assert that the predictors have equivalent signatures- sname = self.signature.signature- oname = other.signature.signature- err_msg = _ERR_MSG_SIGNATURE.format(sname=sname, oname=oname)- assert self.signature.equals(other.signature), err_msg-def reset(self):self.lm = Noneself.traces = []@@ -252,4 +243,4 @@ def new_generate(lm, signature, example, max_depth=6, **kwargs):# # TODO: FIXME: Hmm, I guess expected behavior is that contexts can# affect execution. Well, we need to determine whether context dominates, __init__ demoninates, or forward dominates.# Generally, unless overwritten, we'd see n=None, temperature=None.-# That will eventually mean we have to learn them.\ No newline at end of file+# That will eventually mean we have to learn them.