dspy icon indicating copy to clipboard operation
dspy copied to clipboard

feat(core): add gradle plugin

Open bjsi opened this issue 1 year ago • 2 comments

Here's my simple example program.

import dspy
from dspy.predict.retry import Retry
from dspy.primitives.assertions import (
    assert_transform_module,
    suggest_backtrack_handler,
)

turbo = dspy.OpenAI(model="gpt-3.5-turbo", max_tokens=1000)
dspy.settings.configure(lm=turbo, trace=[])


class GenerateQuestions(dspy.Signature):
    concept = dspy.InputField(desc="a concept to generate questions for")
    questions_and_answers = dspy.OutputField(
        desc="five questions and answers about the concept in the form 'question = answer'",
        format=lambda x: x,
    )


class Program(dspy.Module):
    def __init__(self):
        super().__init__()
        self.generate_questions = dspy.Predict(GenerateQuestions)

    def forward(self, concept):
        qas = self.generate_questions(
            concept=concept
        ).questions_and_answers.splitlines()
        dspy.Suggest(
            len(qas) == 5,
            f"Generate five questions about that concept. Found {len(qas)} questions.",
        )
        dspy.Suggest(
            all("?" not in qa for qa in qas),
            f"Questions should not end with a question mark.",
        )
        dspy.Suggest(
            all(len(qa.split(" ")) <= "3" for qa in qas),
            f"Questions should be heavily abbreviated and less than or equal to 3 words long.",
        )
        return dspy.Prediction(questions_and_answers=qas)


program = assert_transform_module(
    Program().map_named_predictors(Retry), suggest_backtrack_handler
)
program(concept="the moon")
turbo.inspect_history(n=1)

When the program backtracks when one of the assertions fails, calling self.generate_questions in Program's forward method fails with the following error message:

Exception has occurred: TypeError
'dict' object is not callable
  File "/Users/james/Projects/Python/dspy-experiments/test.py", line 26, in forward
    qas = self.generate_questions(
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/james/Projects/Python/dspy-experiments/test.py", line 47, in <module>
    program(concept="the moon")
TypeError: 'dict' object is not callable

bjsi avatar Jan 31 '24 09:01 bjsi

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nx-dev ✅ Ready (Inspect) Visit Preview Feb 29, 2024 5:06pm

vercel[bot] avatar Jan 09 '24 03:01 vercel[bot]

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

github-actions[bot] avatar Mar 06 '24 00:03 github-actions[bot]