Skip to content

Allow state management of tools #8190

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

TomeHirata
Copy link
Collaborator

This PR allows tools to maintain states during the lifespan of each ReAct.forward without affecting other executions by making the copies of tool objects. Users can create a tool class that mutates instance variables.

class Counter():
    def __init__(self):
        self.count = 0

    def __call__(self, i: int):
        self.count += i
        return self.count

counter = Counter()

react = dspy.ReAct("max -> sum:int", tools=[counter])

Copy link
Collaborator

@chenmoneygithub chenmoneygithub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor comment

@@ -76,6 +77,7 @@ def _format_trajectory(self, trajectory: dict[str, Any]):
def forward(self, **input_args):
trajectory = {}
max_iters = input_args.pop("max_iters", self.max_iters)
tools = deepcopy(self.tools)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may want to wrap this in a try-except block and fall back to shallow copy in cases that deepcopy fails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants