Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dspy): Fix template.extract to be more robust #1097

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions dsp/templates/template_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,19 @@ def extract(

import dspy

# This should point at the first output field.
# So we can start truncating raw_pred from this point.
idx = min(idx, len(self.fields) - 1)

# Before that, truncate raw_pred to the output prefix - sometimes
# we see the input fields was also included in the completion.
# see test_predict.py:test_single_output_with_noise tests
next_field_regex = "^" + re.escape(self.fields[idx].name)
match = re.search(next_field_regex, raw_pred, flags=re.MULTILINE)
if match:
end_offset = match.end()
raw_pred = raw_pred[end_offset:].strip()

while raw_pred != "" and idx < len(self.fields):
if idx < len(self.fields) - 1:
next_field_name = "\n" + self.fields[idx + 1].name
Expand Down
25 changes: 25 additions & 0 deletions tests/predict/test_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,31 @@ def test_config_management():
assert "new_key" in config and config["new_key"] == "value"


def test_single_output():
program = Predict("question -> answer")
dspy.settings.configure(lm=DummyLM(["my answer"]))
results = program(question="What is 1+1?")
assert results.completions.answer[0] == "my answer"


def test_single_output_with_prefix():
"""Text for when prefix for answer is included (e.g Gemini)."""
program = Predict("question -> answer")
dspy.settings.configure(lm=DummyLM(["Answer: my answer"]))
results = program(question="What is 1+1?")
assert results.completions.answer[0] == "my answer"


def test_single_output_with_noise():
"""Sometimes we see question was also listed as an output."""
program = Predict("question -> answer")
dspy.settings.configure(lm=DummyLM([
"Question: What is 1+1?\nAnswer: my answer"
]))
results = program(question="What is 1+1?")
assert results.completions.answer[0] == "my answer"


def test_multi_output():
program = Predict("question -> answer", n=2)
dspy.settings.configure(lm=DummyLM(["my first answer", "my second answer"]))
Expand Down
Loading