DSPy: The framework for programming—not prompting—language models
Python
Fix the "no inputs" case
Thomas D Ahlea2fb536eb0eb
diff --git a/dsp/templates/template_v2.py b/dsp/templates/template_v2.pyindex d6e61642e..9ca90779f 100644--- a/dsp/templates/template_v2.py+++ b/dsp/templates/template_v2.py@@ -70,6 +70,8 @@ class TemplateV2:"""Retrieves the input variables from the example and formats them into a query string."""result: list[str] = []+ # If not a demo, find the last field that doesn't have a value set in `example` and set it to ""+ # This creates the "Output:" prefix at the end of the prompt.if not is_demo:has_value = [field.input_variable in example@@ -78,40 +80,40 @@ class TemplateV2:for field in self.fields]- for i in range(1, len(has_value)):- if has_value[i - 1] and not any(has_value[i:]):- example[self.fields[i].input_variable] = ""- break+ # If there are no inputs, set the first field to ""+ if not any(has_value):+ example[self.fields[0].input_variable] = ""+ # Otherwise find the first field without a value.+ else:+ for i in range(1, len(has_value)):+ if has_value[i - 1] and not any(has_value[i:]):+ example[self.fields[i].input_variable] = ""+ breakfor field in self.fields:- if (- field.input_variable in example- and example[field.input_variable] is not None- ):+ if field.input_variable in example and example[field.input_variable] is not None:if field.input_variable in self.format_handlers:format_handler = self.format_handlers[field.input_variable]else:+def format_handler(x):assert type(x) == str, f"Need format_handler for {field.input_variable} of type {type(x)}"return " ".join(x.split())formatted_value = format_handler(example[field.input_variable])- separator = '\n' if field.separator == ' ' and '\n' in formatted_value else field.separator+ separator = "\n" if field.separator == " " and "\n" in formatted_value else field.separatorresult.append(f"{field.name}{separator}{formatted_value}",)- if self._has_augmented_guidelines() and (example.get('augmented', False)):+ if self._has_augmented_guidelines() and (example.get("augmented", False)):return "\n\n".join([r for r in result if r])return "\n".join([r for r in result if r])def guidelines(self, show_guidelines=True) -> str:"""Returns the task guidelines as described in the lm prompt"""- if (not show_guidelines) or (- hasattr(dsp.settings, "show_guidelines")- and not dsp.settings.show_guidelines- ):+ if (not show_guidelines) or (hasattr(dsp.settings, "show_guidelines") and not dsp.settings.show_guidelines):return ""result = "Follow the following format.\n\n"@@ -126,11 +128,13 @@ class TemplateV2:def _has_augmented_guidelines(self):return len(self.fields) > 3 or any(- ("\n" in field.separator) or ('\n' in field.description) for field in self.fields+ ("\n" in field.separator) or ("\n" in field.description) for field in self.fields)def extract(- self, example: Union[Example, dict[str, Any]], raw_pred: str,+ self,+ example: Union[Example, dict[str, Any]],+ raw_pred: str,) -> Example:"""Extracts the answer from the LM raw prediction using the template structure@@ -147,10 +151,7 @@ class TemplateV2:idx = 0while idx < len(self.fields):- if (- self.fields[idx].input_variable not in example- or example[self.fields[idx].input_variable] is None- ):+ if self.fields[idx].input_variable not in example or example[self.fields[idx].input_variable] is None:breakidx += 1@@ -164,8 +165,8 @@ class TemplateV2:if offset >= 0:if dspy.settings.release >= 20231003:- example[self.fields[idx].output_variable] = raw_pred[:offset].strip().rstrip('---').strip()- raw_pred = raw_pred[offset + len(next_field_name) :].strip().rstrip('---').strip()+ example[self.fields[idx].output_variable] = raw_pred[:offset].strip().rstrip("---").strip()+ raw_pred = raw_pred[offset + len(next_field_name) :].strip().rstrip("---").strip()else:example[self.fields[idx].output_variable] = raw_pred[:offset].strip()raw_pred = raw_pred[offset + len(next_field_name) :].strip()@@ -173,7 +174,7 @@ class TemplateV2:idx += 1else:if dspy.settings.release >= 20231003:- example[self.fields[idx].output_variable] = raw_pred.strip().rstrip('---').strip()+ example[self.fields[idx].output_variable] = raw_pred.strip().rstrip("---").strip()else:example[self.fields[idx].output_variable] = raw_pred.strip()@@ -185,7 +186,7 @@ class TemplateV2:assert idx == len(self.fields) - 1, (idx, len(self.fields))if dspy.settings.release >= 20231003:- example[self.fields[idx].output_variable] = raw_pred.strip().rstrip('---').strip()+ example[self.fields[idx].output_variable] = raw_pred.strip().rstrip("---").strip()else:example[self.fields[idx].output_variable] = raw_pred.strip()@@ -196,7 +197,7 @@ class TemplateV2:def __call__(self, example, show_guidelines=True) -> str:example = dsp.Example(example)- if hasattr(dsp.settings, 'query_only') and dsp.settings.query_only:+ if hasattr(dsp.settings, "query_only") and dsp.settings.query_only:return self.query(example)# The training data should not contain the output variable@@ -207,29 +208,20 @@ class TemplateV2:self.query(demo, is_demo=True)for demo in example.demosif (- (not demo.get('augmented', False))+ (not demo.get("augmented", False))and ( # validate that the training example has the same primitive input var as the template- self.fields[-1].input_variable in demo- and demo[self.fields[-1].input_variable] is not None+ self.fields[-1].input_variable in demo and demo[self.fields[-1].input_variable] is not None))]- ademos = [- self.query(demo, is_demo=True)- for demo in example.demos- if demo.get('augmented', False)- ]+ ademos = [self.query(demo, is_demo=True) for demo in example.demos if demo.get("augmented", False)]# Move the rdemos to ademos if rdemo has all the fields filled inrdemos_ = []new_ademos = []for rdemo in rdemos:- if all(- (field.name in rdemo)- for field in self.fields- if field.input_variable in example- ):+ if all((field.name in rdemo) for field in self.fields if field.input_variable in example):import dspyif dspy.settings.release >= 20230928:@@ -242,7 +234,6 @@ class TemplateV2:ademos = new_ademos + ademosrdemos = rdemos_-long_query = self._has_augmented_guidelines()if long_query:@@ -251,10 +242,10 @@ class TemplateV2:query = self.query(example)# if it has more lines than fields- if len(query.split('\n')) > len(self.fields):+ if len(query.split("\n")) > len(self.fields):long_query = True- if not example.get('augmented', False):+ if not example.get("augmented", False):example["augmented"] = Truequery = self.query(example)diff --git a/dspy/predict/predict.py b/dspy/predict/predict.pyindex d0a5f6010..1243e8b88 100644--- a/dspy/predict/predict.py+++ b/dspy/predict/predict.py@@ -73,7 +73,8 @@ class Predict(Parameter):# print(f"#> Setting temperature to 0.7 since n={num_generations} and prior temperature={temperature}.")# All of the other kwargs are presumed to fit a prefix of the signature.-+ # That is, they are input variables for the bottom most generation, so+ # we place them inside the input - x - together with the demos.x = dsp.Example(demos=demos, **kwargs)if new_signature is not None:@@ -86,8 +87,6 @@ class Predict(Parameter):# Switch to legacy format for dsp.generatetemplate = signature_to_template(signature)- # print("Created template", template)- # print("From Signature", signature)if self.lm is None:x, C = dsp.generate(template, **config)(x, stage=self.stage)diff --git a/tests/predict/test_predict.py b/tests/predict/test_predict.pyindex 2ded1e9f1..c0407b938 100644--- a/tests/predict/test_predict.py+++ b/tests/predict/test_predict.py@@ -2,6 +2,7 @@ import dspyfrom dspy import Predict, Signaturefrom dspy.utils.dummies import DummyLMimport copy+import textwrapdef test_initialization_with_string_signature():@@ -119,3 +120,27 @@ def test_named_predictors():# Check that it also works the second time.program2 = copy.deepcopy(program)assert program2.named_predictors() == [("inner", program2.inner)]+++def test_output_only():+ class OutputOnlySignature(dspy.Signature):+ output = dspy.OutputField()++ predictor = Predict(OutputOnlySignature)++ lm = DummyLM(["short answer"])+ dspy.settings.configure(lm=lm)+ assert predictor().output == "short answer"++ assert lm.get_convo(-1) == textwrap.dedent("""\+ Given the fields , produce the fields `output`.++ ---++ Follow the following format.++ Output: ${output}++ ---++ Output: short answer""")