File size: 2,709 Bytes
bffd09a
aff539c
1d3883f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aff539c
1d3883f
 
 
aff539c
1d3883f
 
 
aff539c
 
1d3883f
aff539c
1d3883f
aff539c
 
1d3883f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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.