Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

allow passing of kwargs to typed predictor decorators

Nick Johnsonb63b7af3561c
diff --git a/dspy/functional/functional.py b/dspy/functional/functional.py
index 92f7c5a15..c30acd37d 100644
--- a/dspy/functional/functional.py
+++ b/dspy/functional/functional.py
@@ -14,18 +14,32 @@ from dspy.primitives.prediction import Prediction
from dspy.signatures.signature import ensure_signature, make_signature
-def predictor(func) -> dspy.Module:
- """Decorator that creates a predictor module based on the provided function."""
- signature = _func_to_signature(func)
- *_, output_key = signature.output_fields.keys()
- return _StripOutput(TypedPredictor(signature), output_key)
-
-
-def cot(func) -> dspy.Module:
- """Decorator that creates a chain of thought module based on the provided function."""
- signature = _func_to_signature(func)
- *_, output_key = signature.output_fields.keys()
- return _StripOutput(TypedChainOfThought(signature), output_key)
+def predictor(*args, **kwargs):
+ def _predictor(func) -> dspy.Module:
+ """Decorator that creates a predictor module based on the provided function."""
+ signature = _func_to_signature(func)
+ *_, output_key = signature.output_fields.keys()
+ return _StripOutput(TypedPredictor(signature, **kwargs), output_key)
+
+ # if we have only a single callable argument, the decorator was invoked with no key word arguments
+ # so we just return the wrapped function
+ if len(args) == 1 and callable(args[0]) and len(kwargs) == 0:
+ return _predictor(args[0])
+ return _predictor
+
+
+def cot(*args, **kwargs):
+ def _cot(func) -> dspy.Module:
+ """Decorator that creates a chain of thought module based on the provided function."""
+ signature = _func_to_signature(func)
+ *_, output_key = signature.output_fields.keys()
+ return _StripOutput(TypedChainOfThought(signature, **kwargs), output_key)
+
+ # if we have only a single callable argument, the decorator was invoked with no key word arguments
+ # so we just return the wrapped function
+ if len(args) == 1 and callable(args[0]) and len(kwargs) == 0:
+ return _cot(args[0])
+ return _cot
class _StripOutput(dspy.Module):