stanfordnlp / dspy

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

DSPY 2.5 + Avatar agent: max_iters ignored #1588

Open tikoehle opened 2 weeks ago

tikoehle commented 2 weeks ago

In the avatar example notebook there is a comment about max_iters "is used to control the number of iterations in multi step execution". With my agent instance (max_iters=10)

self.avatar = Avatar(
            tools=self.tools,
            signature=TestSignature,
            verbose=verbose,
            max_iters=10,
        )

the process was terminated at action_73 with querying on a new tool_name (not exist in my list of Tools!), which consequently resulted in a validation error because tool_output was None. Creating the FINISH action somehow adaptive to the problem resolution could be an interesting feature because I observed my result_72 was very detailed and perfectly solved the problem.

Action 73: VERIFICATION_TOOL (...)
1 validation error for ActionOutput
tool_output
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.9/v/string_type

In dspy/predict/avatar/avatar.py debugging shows the max_iters is None and the while loop never terminates.

        max_iters = None if "max_iters" not in kwargs else kwargs["max_iters"]
        while tool_name != "Finish" and (max_iters > 0 if max_iters else True):

This simple fix solved the max_iters problem:

        # max_iters = None if "max_iters" not in kwargs else kwargs["max_iters"]                                                                                            
        max_iters = self.max_iters
okhat commented 2 weeks ago

@krypticmouse

tikoehle commented 2 weeks ago

Attaching a diff for avatar.py which solved the following problems for me. Feel free to use or object.