Skip to content

Commit

Permalink
Merge pull request stanfordnlp#1321 from jmadden12/typed_opt_fixes
Browse files Browse the repository at this point in the history
fix(dspy): added type check on evaluator result
  • Loading branch information
arnavsinghvi11 authored Jul 29, 2024
2 parents 3b95559 + 902eb3a commit 92bfd7f
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions dspy/teleprompt/signature_opt_typed.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def make_initial_signature(n_prompts: int) -> type[Signature]:

class GenerateInstructionInitial(Signature, Generic[T]):
# TODO: Can we make textwrap default/automatic in all signatures?
__doc__ = textwrap.dedent("""\
__doc__ = textwrap.dedent(
"""\
You are a creative instruction optimizer for large language models.
I will give you a ``signature`` of fields (inputs and outputs) in English.
Expand All @@ -88,7 +89,8 @@ class GenerateInstructionInitial(Signature, Generic[T]):
- This will be fun!
- Take a deep breath and think carefully.
- I really need your help!
""")
"""
)

basic_signature: T = InputField()
proposed_signatures: list[T] = OutputField(
Expand All @@ -102,15 +104,17 @@ class GenerateInstructionInitial(Signature, Generic[T]):

def generate_with_avoidance(signatures_to_avoid: list[BaseModel]) -> type[Signature]:
class GenerateSignature(dspy.Signature, Generic[T]):
__doc__ = textwrap.dedent("""\
__doc__ = textwrap.dedent(
"""\
You are an instruction optimizer for large language models.
I will give some task instructions I've tried, along with their corresponding validation scores.
- The instructions are arranged in order based on their scores, where higher scores indicate better quality.
- Your task is to propose a new instruction that will lead a good language model to perform the task even better.
- Be creative, and think out of the box.
- Don't repeat instructions, descriptions and prefixes that have already been attempted.
""")
"""
)

analysis: str = OutputField(desc="Consider what made the previous instructions good or bad.")
proposed_signature: T = OutputField(desc="A signature that will likely lead to a high score.")
Expand Down Expand Up @@ -230,7 +234,10 @@ def optimize_signature(

# Run evaluator given by user
score = evaluator(module)
scores.append(score)
if isinstance(score, tuple):
scores.append(score[0])
else:
scores.append(score)

# If we are still testing initial prompts, continue
if i + 1 < len(next(iter(candidates.values()))):
Expand Down

0 comments on commit 92bfd7f

Please sign in to comment.