Skip to content
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

Refactor from_defaults to Use tool_metadata_from_defaults for Cleaner and More Efficient Metadata Handling #17852

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

Conversation

johnmalek312
Copy link

I added a separate helper method, tool_metadata_from_defaults, to handle metadata extraction instead of doing it inline in FunctionTool.from_defaults(). This makes the code cleaner and keeps metadata handling separate from tool initialization. Now, from_defaults() calls this method when no tool_metadata is provided.

This Helps When Wrapping Tools

import functools

def my_decorator(fn):
    original_metadata = FunctionTool.tool_metadata_from_defaults(fn)

    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        print("Wrapper: Pre-processing")
        result = fn(*args, **kwargs)
        print("Wrapper: Post-processing")
        return result

    return FunctionTool.from_defaults(fn=wrapper, tool_metadata=original_metadata)

This makes from_defaults() more readable while ensuring wrapped functions retain their original metadata without having to create a seperate tool and extracting metadata from.

I refactored the code by splitting the original `FunctionTool.from_defaults()` method into two methods. The metadata creation logic has been moved into a new helper method called `FunctionTool.tool_metadata_from_defaults()`. Now, `from_defaults()` focuses solely on instantiating the `FunctionTool`, while `tool_metadata_from_defaults()` is responsible for constructing the metadata based on the provided defaults. This separation of concerns makes the code cleaner and easier to maintain.
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant