Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

pick up completion from first missing field

JONEMI19991d713d22b9
diff --git a/dsp/primitives/predict.py b/dsp/primitives/predict.py
index f8db9c47e..029865571 100644
--- a/dsp/primitives/predict.py
+++ b/dsp/primitives/predict.py
@@ -63,9 +63,9 @@ def _generate(template: Template, **kwargs) -> Callable:
def extend_generation(completion: Example, field_names: list[str], stage:str, max_depth: int, original_example:Example):
"""If the required fields are not present in the completion, extend the generation."""
-
# remove content of last field to avoid half-completed content
- completion[get_last_field(completion, field_names)] = ""
+ for field_name in get_all_fields_following_missing_field(completion, field_names):
+ completion[field_name] = ""
# Recurse with greedy decoding and a shorter length.
max_tokens = (kwargs.get("max_tokens") or
@@ -117,7 +117,11 @@ def _generate(template: Template, **kwargs) -> Callable:
finished_completions = []
for completion in completions:
- if all(key in completion for key in field_names):
+ all_keys_valid = all(
+ (completion.get(key, None) is not None) and
+ (completion.get(key, None) !="") for key in field_names
+ )
+ if all_keys_valid:
finished_completions.append(completion)
continue
finished_completions.append(
@@ -161,12 +165,15 @@ def _generate(template: Template, **kwargs) -> Callable:
return do_generate
-def get_last_field(completion: Example, field_names: list[str]):
- for key in field_names:
- if key not in completion:
- return key
- if completion[key] is None or completion[key] == "":
- return key
+def get_all_fields_following_missing_field(completion: Example, field_names: list[str]) -> list[str]:
+ """Returns every field following the first missing field"""
+ for i, field_name in enumerate(field_names):
+ if field_name not in completion:
+ return field_names[i:]
+ if completion[field_name] == "":
+ return field_names[i:]
+ return []
+
def generate_sc(
example, prompt, normalize=True, extract=None, prediction_field=None, **kwargs,