Skip to content

Tool

llmfy.llmfy_core.tools.tool

Tool

Decorator class for creating tool definitions.

Source code in llmfy/llmfy_core/tools/tool.py
class Tool:
    """
    Decorator class for creating tool definitions.
    """

    # Register formatter
    _formatters: Dict[ServiceProvider, ModelFormatter] = {
        ServiceProvider.OPENAI: OpenAIFormatter(),
        ServiceProvider.BEDROCK: BedrockFormatter(),
        ServiceProvider.GOOGLE: GoogleAIFormatter(),
    }

    def __init__(self, strict: bool = True):
        self.strict = strict

    def __call__(self, func: Callable) -> Callable:
        func._is_tool = True  # type: ignore # Mark the function as a tool
        func._tool_strict = self.strict  # type: ignore # Store strict setting. to check: getattr(func, '_tool_strict', True)
        return func

    @staticmethod
    def _get_tool_definition(func: Callable, provider: ServiceProvider) -> Dict[str, Any]:
        formatter = Tool._formatters.get(provider)
        if not formatter:
            raise LLMfyException(f"Unsupported model provider: {provider}")

        metadata = FunctionParser.get_function_metadata(func)
        return formatter.format_tool_function(metadata, FUNCTION_TYPE_MAPPING)

strict = strict instance-attribute

__init__(strict=True)

Source code in llmfy/llmfy_core/tools/tool.py
def __init__(self, strict: bool = True):
    self.strict = strict

__call__(func)

Source code in llmfy/llmfy_core/tools/tool.py
def __call__(self, func: Callable) -> Callable:
    func._is_tool = True  # type: ignore # Mark the function as a tool
    func._tool_strict = self.strict  # type: ignore # Store strict setting. to check: getattr(func, '_tool_strict', True)
    return func