smolagents/tests/fixtures/tools.py
Albert Villanova del Moral 68cdf52213
Fix deprecated typing (#1249)
2025-04-25 10:58:38 +02:00

87 lines
2.4 KiB
Python

import pytest
from smolagents.tools import Tool, tool
@pytest.fixture
def example_tool():
@tool
def valid_tool_function(input: str) -> str:
"""A valid tool function.
Args:
input (str): Input string.
"""
return input.upper()
return valid_tool_function
@pytest.fixture
def boolean_default_tool_class():
class BooleanDefaultTool(Tool):
name = "boolean_default_tool"
description = "A tool with a boolean default parameter"
inputs = {
"text": {"type": "string", "description": "Input text"},
"flag": {"type": "boolean", "description": "Boolean flag with default value", "nullable": True},
}
output_type = "string"
def forward(self, text: str, flag: bool = False) -> str:
return f"Text: {text}, Flag: {flag}"
return BooleanDefaultTool()
@pytest.fixture
def boolean_default_tool_function():
@tool
def boolean_default_tool(text: str, flag: bool = False) -> str:
"""
A tool with a boolean default parameter.
Args:
text: Input text
flag: Boolean flag with default value
"""
return f"Text: {text}, Flag: {flag}"
return boolean_default_tool
@pytest.fixture
def optional_input_tool_class():
class OptionalInputTool(Tool):
name = "optional_input_tool"
description = "A tool with an optional input parameter"
inputs = {
"required_text": {"type": "string", "description": "Required input text"},
"optional_text": {"type": "string", "description": "Optional input text", "nullable": True},
}
output_type = "string"
def forward(self, required_text: str, optional_text: str | None = None) -> str:
if optional_text:
return f"{required_text} + {optional_text}"
return required_text
return OptionalInputTool()
@pytest.fixture
def optional_input_tool_function():
@tool
def optional_input_tool(required_text: str, optional_text: str | None = None) -> str:
"""
A tool with an optional input parameter.
Args:
required_text: Required input text
optional_text: Optional input text
"""
if optional_text:
return f"{required_text} + {optional_text}"
return required_text
return optional_input_tool