MCPcopy Create free account
hub / github.com/SqueezeAILab/TinyAgent / from_function

Method from_function

src/tools/base.py:307–374  ·  view source on GitHub ↗

Create tool from a given function. A classmethod that helps to create a tool from a function. Args: func: The function from which to create a tool coroutine: The async function from which to create a tool name: The name of the tool. Defaults to t

(
        cls,
        func: Optional[Callable] = None,
        coroutine: Optional[Callable[..., Awaitable[Any]]] = None,
        name: Optional[str] = None,
        description: Optional[str] = None,
        return_direct: bool = False,
        args_schema: Optional[Type[BaseModel]] = None,
        infer_schema: bool = True,
        **kwargs: Any,
    )

Source from the content-addressed store, hash-verified

305
306 @classmethod
307 def from_function(
308 cls,
309 func: Optional[Callable] = None,
310 coroutine: Optional[Callable[..., Awaitable[Any]]] = None,
311 name: Optional[str] = None,
312 description: Optional[str] = None,
313 return_direct: bool = False,
314 args_schema: Optional[Type[BaseModel]] = None,
315 infer_schema: bool = True,
316 **kwargs: Any,
317 ) -> StructuredTool:
318 """Create tool from a given function.
319
320 A classmethod that helps to create a tool from a function.
321
322 Args:
323 func: The function from which to create a tool
324 coroutine: The async function from which to create a tool
325 name: The name of the tool. Defaults to the function name
326 description: The description of the tool. Defaults to the function docstring
327 return_direct: Whether to return the result directly or as a callback
328 args_schema: The schema of the tool's input arguments
329 infer_schema: Whether to infer the schema from the function's signature
330 **kwargs: Additional arguments to pass to the tool
331
332 Returns:
333 The tool
334
335 Examples:
336
337 .. code-block:: python
338
339 def add(a: int, b: int) -> int:
340 \"\"\"Add two numbers\"\"\"
341 return a + b
342 tool = StructuredTool.from_function(add)
343 tool.run(1, 2) # 3
344 """
345
346 if func is not None:
347 source_function = func
348 elif coroutine is not None:
349 source_function = coroutine
350 else:
351 raise ValueError("Function and/or coroutine must be provided")
352 name = name or source_function.__name__
353 description = description or source_function.__doc__
354 if description is None:
355 raise ValueError(
356 "Function must have a docstring if description not provided."
357 )
358
359 # Description example:
360 # search_api(query: str) - Searches the API for the query.
361 sig = signature(source_function)
362 description = f"{name}{sig} - {description.strip()}"
363 _args_schema = args_schema
364 if _args_schema is None and infer_schema:

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected