stanfordnlp / dspy

DSPy: The framework for programming—not prompting—foundation models
https://dspy-docs.vercel.app/
MIT License
16.67k stars 1.29k forks source link

No string formatting in Signature docstrings #1429

Closed stantonius closed 2 weeks ago

stantonius commented 2 weeks ago

Hey - thanks for all the work on this library.

Ran into an issue where I tried to re-use text in Signature docstrings, and noticed that these do not get passed to the Modules. For example the following basic setup

class SampleSignature(dspy.Signature):
    """
You are to generate incorrect but funny answers to the question provided.
    """
    question = dspy.InputField(
        desc="A simple question"
    )
    answer = dspy.OutputField(
        description="The answer to the question"
    )

silly_answers = dspy.Predict(SampleSignature)
silly_answers(question="What is the capital of Canada?").answer

>>> 'The answer to the question is: Maple Syrup City, where the trees are always sticky and the pancakes never end!'

generates the following template, visible in inspect_history:

You are to generate incorrect but funny answers to the question provided.

---

Follow the following format.

Question: A simple question
Answer: The answer to the question

However when you try to insert dynamically into the docstring:

st = "You are to generate incorrect but funny answers to the question provided."

class SampleSignature(dspy.Signature):
    f"""
{st}
    """
    question = dspy.InputField(
        desc="A simple question"
    )
    answer = dspy.OutputField(
        description="The answer to the question"
    )

silly_answers = dspy.Predict(SampleSignature)
silly_answers(question="What is the capital of Canada?").answer

>>> 'The capital of Canada is Ottawa.'

then the template doesn't recognize the string and uses the generic Signature docstring:

Given the fields `question`, produce the fields `answer`.

---

Follow the following format.

Question: A simple question
Answer: The answer to the question

This also happens with other forms of string formatting, not just f-strings.

Come to think of it, I've never actually has dynamic text in a class docstring before so I'm not even sure this is a thing in Python. However since I am using dspy for consecutive tasks that share properties (ie. share instructions and context), I thought I would DRY by sharing text amongst signatures, but it doesn't work.

stantonius commented 2 weeks ago

OK so I just checked and confirmed my suspicions that you can't actually do this in Python (I just assumed f-strings worked everywhere...)

However, is it anti-pattern to do something like this in dspy to the Signature class?

MyClass1.__doc__ = f"{shared_text}\n\nAdditional details for MyClass1."

Thanks for any guidance.

okhat commented 2 weeks ago

is it anti-pattern to do something like this

No :D