Upload tools.py
Browse files- tests/fixtures/tools.py +103 -0
tests/fixtures/tools.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
from smolagents.tools import Tool, tool
|
4 |
+
|
5 |
+
|
6 |
+
@pytest.fixture
|
7 |
+
def test_tool():
|
8 |
+
class TestTool(Tool):
|
9 |
+
name = "test_tool"
|
10 |
+
description = "A test tool"
|
11 |
+
inputs = {"input": {"type": "string", "description": "Input value"}}
|
12 |
+
output_type = "string"
|
13 |
+
|
14 |
+
def forward(self, input):
|
15 |
+
if input == "error":
|
16 |
+
raise ValueError("Tool execution error")
|
17 |
+
return f"Processed: {input}"
|
18 |
+
|
19 |
+
return TestTool()
|
20 |
+
|
21 |
+
|
22 |
+
@pytest.fixture
|
23 |
+
def example_tool():
|
24 |
+
@tool
|
25 |
+
def valid_tool_function(input: str) -> str:
|
26 |
+
"""A valid tool function.
|
27 |
+
|
28 |
+
Args:
|
29 |
+
input (str): Input string.
|
30 |
+
"""
|
31 |
+
return input.upper()
|
32 |
+
|
33 |
+
return valid_tool_function
|
34 |
+
|
35 |
+
|
36 |
+
@pytest.fixture
|
37 |
+
def boolean_default_tool_class():
|
38 |
+
class BooleanDefaultTool(Tool):
|
39 |
+
name = "boolean_default_tool"
|
40 |
+
description = "A tool with a boolean default parameter"
|
41 |
+
inputs = {
|
42 |
+
"text": {"type": "string", "description": "Input text"},
|
43 |
+
"flag": {"type": "boolean", "description": "Boolean flag with default value", "nullable": True},
|
44 |
+
}
|
45 |
+
output_type = "string"
|
46 |
+
|
47 |
+
def forward(self, text: str, flag: bool = False) -> str:
|
48 |
+
return f"Text: {text}, Flag: {flag}"
|
49 |
+
|
50 |
+
return BooleanDefaultTool()
|
51 |
+
|
52 |
+
|
53 |
+
@pytest.fixture
|
54 |
+
def boolean_default_tool_function():
|
55 |
+
@tool
|
56 |
+
def boolean_default_tool(text: str, flag: bool = False) -> str:
|
57 |
+
"""
|
58 |
+
A tool with a boolean default parameter.
|
59 |
+
|
60 |
+
Args:
|
61 |
+
text: Input text
|
62 |
+
flag: Boolean flag with default value
|
63 |
+
"""
|
64 |
+
return f"Text: {text}, Flag: {flag}"
|
65 |
+
|
66 |
+
return boolean_default_tool
|
67 |
+
|
68 |
+
|
69 |
+
@pytest.fixture
|
70 |
+
def optional_input_tool_class():
|
71 |
+
class OptionalInputTool(Tool):
|
72 |
+
name = "optional_input_tool"
|
73 |
+
description = "A tool with an optional input parameter"
|
74 |
+
inputs = {
|
75 |
+
"required_text": {"type": "string", "description": "Required input text"},
|
76 |
+
"optional_text": {"type": "string", "description": "Optional input text", "nullable": True},
|
77 |
+
}
|
78 |
+
output_type = "string"
|
79 |
+
|
80 |
+
def forward(self, required_text: str, optional_text: str | None = None) -> str:
|
81 |
+
if optional_text:
|
82 |
+
return f"{required_text} + {optional_text}"
|
83 |
+
return required_text
|
84 |
+
|
85 |
+
return OptionalInputTool()
|
86 |
+
|
87 |
+
|
88 |
+
@pytest.fixture
|
89 |
+
def optional_input_tool_function():
|
90 |
+
@tool
|
91 |
+
def optional_input_tool(required_text: str, optional_text: str | None = None) -> str:
|
92 |
+
"""
|
93 |
+
A tool with an optional input parameter.
|
94 |
+
|
95 |
+
Args:
|
96 |
+
required_text: Required input text
|
97 |
+
optional_text: Optional input text
|
98 |
+
"""
|
99 |
+
if optional_text:
|
100 |
+
return f"{required_text} + {optional_text}"
|
101 |
+
return required_text
|
102 |
+
|
103 |
+
return optional_input_tool
|