Fix @tool decorator for remote Python executor by tobiasofsn · Pull Request #1334 · huggingface/smolagents (original) (raw)
@aymeric-roucher and @albertvillanova, what do you think about this solution?
I think the problem before was that getting the func, in this case the forward method, from cls.__dict__.items(), doesn't preserve the __source__ field which is set in tools.py#L977. From my testing, using getattr works better in this regard as it preserves the __source__ field. When the __source__ field is preserved it is used by get_source to successfully transform the function into code.
So with the fix the resulting tool source for my example becomes:
class SimpleTool(Tool):
name = "magic_number_tool"
description = "This tool returns the magic number."
inputs = {}
output_type = "integer"
def __init__(self):
self.is_initialized = True
def forward(self) -> int:
"""
This tool returns the magic number.
"""
return 12345
Instead of:
class SimpleTool(Tool):
name = "magic_number_tool"
description = "This tool returns the magic number."
inputs = {}
output_type = "integer"
def __init__(self):
self.is_initialized = True
@tool
def magic_number_tool() -> int:
"""
This tool returns the magic number.
"""
return 12345