Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Convert Predict and ChainOfThought into Modules, while retaining backward compatible .load()

Omar Khattab730fd1363f9d
diff --git a/dspy/predict/chain_of_thought.py b/dspy/predict/chain_of_thought.py
index 70bab0a7a..050d97760 100644
--- a/dspy/predict/chain_of_thought.py
+++ b/dspy/predict/chain_of_thought.py
@@ -1,87 +1,41 @@
-import dsp
import dspy
+from dspy.primitives.program import Module
from dspy.signatures.signature import ensure_signature
-from .predict import Predict
-
-# TODO: FIXME: Insert this right before the *first* output field. Also rewrite this to use the new signature system.
# TODO: This shouldn't inherit from Predict. It should be a module that has one or two predictors.
# Let's focus on the activated case. It's a predictor with the expanded signature.
# Now, when deactivated, it's a predictor with the original signature.
# When activate is None, though, we need the expanded one but during forward we need to pass the right signature.
-"""
-class ChainOfThought(dspy.Module):
- def __init__(self, signature):
-
- input_fields, output_fields = dspy.process_signature(signature)
- output_fields = dict(rationale=dspy.OutputField(prefix="Reasoning: Let's think step by step."), **output_fields)
- self.signature = dspy.Signature(input_fields, output_fields)
-
- self.predict = dspy.Predict(self.signature)
-
- def forward(self, **kwargs):
- return self.predict(**kwargs)
-
-# How this should look like. But with also passing signature=simpler_signature to the predict module *if* deactivated.
-"""
-
-class ChainOfThought(Predict):
+class ChainOfThought(Module):
def __init__(self, signature, rationale_type=None, activated=True, **config):
- super().__init__(signature, **config)
+ super().__init__()
self.activated = activated
- signature = ensure_signature(self.signature)
+ self.signature = signature = ensure_signature(signature)
*_keys, last_key = signature.output_fields.keys()
- rationale_type = rationale_type or dspy.OutputField(
- prefix="Reasoning: Let's think step by step in order to",
- desc="${produce the " + last_key + "}. We ...",
- )
+ prefix = "Reasoning: Let's think step by step in order to"
+ desc = "${produce the " + last_key + "}. We ..."
- self.extended_signature = signature.prepend("rationale", rationale_type, type_=str)
+ if dspy.settings.experimental:
+ desc = "${produce the output fields}. We ..."
- def forward(self, **kwargs):
- new_signature = kwargs.pop("new_signature", None)
- if new_signature is None:
- if self.activated is True or (
- self.activated is None and isinstance(dsp.settings.lm, dsp.GPT3)
- ):
- signature = self.extended_signature
- else:
- signature = self.signature
- else:
- signature = new_signature
- # template = dsp.Template(self.signature.instructions, **new_signature)
- return super().forward(signature=signature, **kwargs)
-
-
- def dump_state(self):
- state = super().dump_state()
-
- # Cache the signature instructions and the last field's name.
- state["extended_signature_instructions"] = self.extended_signature.instructions
+ rationale_type = rationale_type or dspy.OutputField(prefix=prefix, desc=desc)
- *_, last_key = self.signature.fields.keys()
- state["extended_signature_prefix"] = self.extended_signature.fields[last_key].json_schema_extra['prefix']
-
- return state
+ self.extended_signature = signature.prepend("rationale", rationale_type, type_=str)
+ self._predict = dspy.Predict(self.extended_signature, **config)
+ self._predict.extended_signature = self.extended_signature
- def load_state(self, state):
- super().load_state(state)
-
- # Reconstruct the signature.
- if "extended_signature_instructions" in state:
- instructions = state["extended_signature_instructions"]
- self.extended_signature = self.extended_signature.with_instructions(instructions)
+ def forward(self, **kwargs):
+ assert self.activated in [True, False]
- if "extended_signature_prefix" in state:
- prefix = state["extended_signature_prefix"]
- *_, last_key = self.extended_signature.fields.keys()
- self.extended_signature = self.extended_signature.with_updated_fields(last_key, prefix=prefix)
+ signature = kwargs.pop("new_signature", self.extended_signature if self.activated else self.signature)
+ return self._predict(signature=signature, **kwargs)
+ # return super().forward(signature=signature, **kwargs)
"""
TODO: In principle, we can update the field's prefix during forward too to fill any thing based on the input args.
diff --git a/dspy/predict/predict.py b/dspy/predict/predict.py
index 086009e5d..b9827c446 100644
--- a/dspy/predict/predict.py
+++ b/dspy/predict/predict.py
@@ -4,11 +4,13 @@ from pydantic import BaseModel
import dsp
from dspy.predict.parameter import Parameter
+from dspy.primitives.program import Module
+
from dspy.primitives.prediction import Prediction
from dspy.signatures.signature import ensure_signature, signature_to_template
-class Predict(Parameter):
+class Predict(Module, Parameter):
def __init__(self, signature, **config):
self.stage = random.randbytes(8).hex()
self.signature = ensure_signature(signature)
@@ -36,11 +38,16 @@ class Predict(Parameter):
state["demos"].append(demo)
# Cache the signature instructions and the last field's name.
- state["signature_instructions"] = self.signature.instructions
-
*_, last_key = self.signature.fields.keys()
+ state["signature_instructions"] = self.signature.instructions
state["signature_prefix"] = self.signature.fields[last_key].json_schema_extra["prefix"]
+ # Some special stuff for CoT.
+ if hasattr(self, "extended_signature"):
+ # Cache the signature instructions and the last field's name.
+ state["extended_signature_instructions"] = self.extended_signature.instructions
+ state["extended_signature_prefix"] = self.extended_signature.fields[last_key].json_schema_extra['prefix']
+
return state
def load_state(self, state):
@@ -56,6 +63,16 @@ class Predict(Parameter):
prefix = state["signature_prefix"]
*_, last_key = self.signature.fields.keys()
self.signature = self.signature.with_updated_fields(last_key, prefix=prefix)
+
+ # Some special stuff for CoT.
+ if "extended_signature_instructions" in state:
+ instructions = state["extended_signature_instructions"]
+ self.extended_signature = self.extended_signature.with_instructions(instructions)
+
+ if "extended_signature_prefix" in state:
+ prefix = state["extended_signature_prefix"]
+ *_, last_key = self.extended_signature.fields.keys()
+ self.extended_signature = self.extended_signature.with_updated_fields(last_key, prefix=prefix)
def __call__(self, **kwargs):
return self.forward(**kwargs)
@@ -137,10 +154,7 @@ def old_generate(demos, signature, kwargs, config, lm, stage):
completions.append({})
for field in template.fields:
if field.output_variable not in kwargs.keys():
- completions[-1][field.output_variable] = getattr(
- c,
- field.output_variable,
- )
+ completions[-1][field.output_variable] = getattr(c, field.output_variable)
return completions
diff --git a/dspy/primitives/module.py b/dspy/primitives/module.py
index 5cf0b2231..8b72bffd6 100644
--- a/dspy/primitives/module.py
+++ b/dspy/primitives/module.py
@@ -23,10 +23,15 @@ class BaseModule:
named_parameters = []
def add_parameter(param_name, param_value):
+ param_name = postprocess_parameter_name(param_name, param_value)
+
if isinstance(param_value, Parameter) and id(param_value) not in visited:
visited.add(id(param_value))
named_parameters.append((param_name, param_value))
+ if isinstance(self, Parameter):
+ add_parameter("self", self)
+
for name, value in self.__dict__.items():
if isinstance(value, Parameter):
add_parameter(name, value)
@@ -61,12 +66,15 @@ class BaseModule:
seen = {id(self)}
def add_to_queue(name, item):
+ name = postprocess_parameter_name(name, item)
+
if id(item) not in seen:
seen.add(id(item))
queue.append((name, item))
while queue:
name, item = queue.popleft()
+
if isinstance(item, type_):
yield name, item
@@ -99,11 +107,19 @@ class BaseModule:
return obj
def dump_state(self):
+ print(self.named_parameters())
return {name: param.dump_state() for name, param in self.named_parameters()}
def load_state(self, state):
for name, param in self.named_parameters():
param.load_state(state[name])
+ # try:
+ # param.load_state(state[name])
+ # except KeyError:
+ # if name.endswith("._predict"):
+ # param.load_state(state[name[:-9]])
+ # else:
+ # raise
def save(self, path):
with open(path, "w") as f:
@@ -112,3 +128,17 @@ class BaseModule:
def load(self, path):
with open(path) as f:
self.load_state(ujson.loads(f.read()))
+
+
+def postprocess_parameter_name(name, value):
+ # For ChainOfThought backward compatibility, remove ending ._predict if it's there
+ if name.endswith("._predict"):
+ name = name[:-9]
+
+ if name.endswith(".self"):
+ name = name[:-5]
+
+ if name == "_predict":
+ return "self"
+
+ return name
diff --git a/dspy/teleprompt/bootstrap.py b/dspy/teleprompt/bootstrap.py
index 69e13d4b3..0513502d4 100644
--- a/dspy/teleprompt/bootstrap.py
+++ b/dspy/teleprompt/bootstrap.py
@@ -213,12 +213,7 @@ class BootstrapFewShot(Teleprompter):
if success:
for step in trace:
predictor, inputs, outputs = step
-
- if "dspy_uuid" in example:
- demo = Example(augmented=True, dspy_uuid=example.dspy_uuid, **inputs, **outputs)
- else:
- # TODO: FIXME: This is a hack. RandomSearch will complain for now in this edge case.
- demo = Example(augmented=True, **inputs, **outputs)
+ demo = Example(augmented=True, **inputs, **outputs)
try:
predictor_name = self.predictor2name[id(predictor)]