HF_Agents_Final_Project / src /text_reversal_tool.py
Yago Bolivar
refactor: update tool classes to inherit from Tool base class for consistency and improved structure
bffd09a
from smolagents.tools import Tool
class TextReversalTool(Tool):
name = "reverse_string_text"
description = "Reverses the order of characters in a given string."
inputs = {"text": {"type": "string", "description": "String to reverse"}}
outputs = {"reversed_text": {"type": "string", "description": "Reversed string"}} # Changed "str" to "string"
output_type = "string" # Changed "str" to "string"
def forward(self, text: str) -> str:
"""
Reverses or processes reversed text, useful for decoding or analyzing reversed strings.
"""
return text[::-1]
def __init__(self, *args, **kwargs):
# Minimal __init__, can be expanded if needed
super().__init__(*args, **kwargs)
# self.is_initialized = False # If you need similar state tracking
# For direct execution/testing of this tool
if __name__ == '__main__':
"""Main function to demonstrate the text reversal tool."""
tool_instance = TextReversalTool()
original_text = "hello world"
# The 'forward' method is what gets called by the agent typically
# For direct testing, you call it directly.
reversed_text = tool_instance.forward(original_text)
print(f"Original: {original_text}")
print(f"Reversed: {reversed_text}")
reversed_instruction = ".rewsna eht sa \"tfel\" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI"
original_instruction = tool_instance.forward(reversed_instruction)
print(f"Reversed Instruction: {reversed_instruction}")
print(f"Original Instruction: {original_instruction}")
# To make the tool discoverable as a standalone function if needed by some frameworks,
# you can alias the forward method or provide a wrapper.
# However, if smolagents expects a class instance, this is the primary way.
reverse_string_text = TextReversalTool().forward
# Or, if the agent system specifically looks for a function decorated with @tool,
# and the class-based approach is for a different registration mechanism,
# then the previous functional approach with direct attribute assignment was more aligned.
# Given the context of app.py using `reverse_string_text` directly for `TextToolAgent`,
# we might need to ensure this name is still callable as a function.
# If `smolagents` uses the class directly, then `app.py` would need to instantiate `TextReversalTool`
# and pass its instance or its `forward` method.
# The current app.py imports `reverse_string_text` as a function.
# Let's provide a callable instance for that:
# reverse_string_text_tool_instance = TextReversalTool()
# reverse_string_text = reverse_string_text_tool_instance.forward
# This makes `reverse_string_text` callable as before.