Yago Bolivar commited on
Commit
1d3883f
·
1 Parent(s): def985a

refactor: convert reverse_string_text function to TextReversalTool class for improved structure and functionality

Browse files
Files changed (1) hide show
  1. src/text_reversal_tool.py +48 -19
src/text_reversal_tool.py CHANGED
@@ -1,27 +1,56 @@
1
- from smolagents import tool
2
-
3
- @tool(
4
- name="reverse_string_text",
5
- description="Reverses the order of characters in a given string.",
6
- inputs={"text": {"type": "str", "description": "String to reverse"}},
7
- outputs={"reversed_text": {"type": "str", "description": "Reversed string"}},
8
- output_type="str"
9
- )
10
- def reverse_string_text(text: str) -> str:
11
- """
12
- Reverses or processes reversed text, useful for decoding or analyzing reversed strings.
13
- """
14
- return text[::-1]
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  if __name__ == '__main__':
17
- """"Main function to demonstrate the text reversal tool.
18
- Example usage:
 
19
  original_text = "hello world"
20
- reversed_text = reverse_text(original_text)
 
 
21
  print(f"Original: {original_text}")
22
  print(f"Reversed: {reversed_text}")
23
- """
24
  reversed_instruction = ".rewsna eht sa \"tfel\" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI"
25
- original_instruction = reverse_text(reversed_instruction)
26
  print(f"Reversed Instruction: {reversed_instruction}")
27
  print(f"Original Instruction: {original_instruction}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents.tools import Tool # Ensure Tool is imported
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+
4
+ class TextReversalTool(Tool):
5
+ name = "reverse_string_text"
6
+ description = "Reverses the order of characters in a given string."
7
+ inputs = {"text": {"type": "string", "description": "String to reverse"}}
8
+ outputs = {"reversed_text": {"type": "string", "description": "Reversed string"}} # Changed "str" to "string"
9
+ output_type = "string" # Changed "str" to "string"
10
+
11
+ def forward(self, text: str) -> str:
12
+ """
13
+ Reverses or processes reversed text, useful for decoding or analyzing reversed strings.
14
+ """
15
+ return text[::-1]
16
+
17
+ def __init__(self, *args, **kwargs):
18
+ # Minimal __init__, can be expanded if needed
19
+ super().__init__(*args, **kwargs)
20
+ # self.is_initialized = False # If you need similar state tracking
21
+
22
+
23
+ # For direct execution/testing of this tool
24
  if __name__ == '__main__':
25
+ """Main function to demonstrate the text reversal tool."""
26
+ tool_instance = TextReversalTool()
27
+
28
  original_text = "hello world"
29
+ # The 'forward' method is what gets called by the agent typically
30
+ # For direct testing, you call it directly.
31
+ reversed_text = tool_instance.forward(original_text)
32
  print(f"Original: {original_text}")
33
  print(f"Reversed: {reversed_text}")
34
+
35
  reversed_instruction = ".rewsna eht sa \"tfel\" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI"
36
+ original_instruction = tool_instance.forward(reversed_instruction)
37
  print(f"Reversed Instruction: {reversed_instruction}")
38
  print(f"Original Instruction: {original_instruction}")
39
+
40
+ # To make the tool discoverable as a standalone function if needed by some frameworks,
41
+ # you can alias the forward method or provide a wrapper.
42
+ # However, if smolagents expects a class instance, this is the primary way.
43
+ reverse_string_text = TextReversalTool().forward
44
+ # Or, if the agent system specifically looks for a function decorated with @tool,
45
+ # and the class-based approach is for a different registration mechanism,
46
+ # then the previous functional approach with direct attribute assignment was more aligned.
47
+ # Given the context of app.py using `reverse_string_text` directly for `TextToolAgent`,
48
+ # we might need to ensure this name is still callable as a function.
49
+
50
+ # If `smolagents` uses the class directly, then `app.py` would need to instantiate `TextReversalTool`
51
+ # and pass its instance or its `forward` method.
52
+ # The current app.py imports `reverse_string_text` as a function.
53
+ # Let's provide a callable instance for that:
54
+ # reverse_string_text_tool_instance = TextReversalTool()
55
+ # reverse_string_text = reverse_string_text_tool_instance.forward
56
+ # This makes `reverse_string_text` callable as before.