MCPcopy Create free account
hub / github.com/SqueezeAILab/LLMCompiler / tool

Function tool

src/tools/base.py:378–464  ·  view source on GitHub ↗

Make tools out of functions, can be used with or without arguments. Args: *args: The arguments to the tool. return_direct: Whether to return directly from the tool rather than continuing the agent loop. args_schema: optional argument schema for user to specif

(
    *args: Union[str, Callable],
    return_direct: bool = False,
    args_schema: Optional[Type[BaseModel]] = None,
    infer_schema: bool = True,
)

Source from the content-addressed store, hash-verified

376
377
378def tool(
379 *args: Union[str, Callable],
380 return_direct: bool = False,
381 args_schema: Optional[Type[BaseModel]] = None,
382 infer_schema: bool = True,
383) -> Callable:
384 """Make tools out of functions, can be used with or without arguments.
385
386 Args:
387 *args: The arguments to the tool.
388 return_direct: Whether to return directly from the tool rather
389 than continuing the agent loop.
390 args_schema: optional argument schema for user to specify
391 infer_schema: Whether to infer the schema of the arguments from
392 the function's signature. This also makes the resultant tool
393 accept a dictionary input to its `run()` function.
394
395 Requires:
396 - Function must be of type (str) -> str
397 - Function must have a docstring
398
399 Examples:
400 .. code-block:: python
401
402 @tool
403 def search_api(query: str) -> str:
404 # Searches the API for the query.
405 return
406
407 @tool("search", return_direct=True)
408 def search_api(query: str) -> str:
409 # Searches the API for the query.
410 return
411 """
412
413 def _make_with_name(tool_name: str) -> Callable:
414 def _make_tool(dec_func: Callable) -> BaseTool:
415 if inspect.iscoroutinefunction(dec_func):
416 coroutine = dec_func
417 func = None
418 else:
419 coroutine = None
420 func = dec_func
421
422 if infer_schema or args_schema is not None:
423 return StructuredTool.from_function(
424 func,
425 coroutine,
426 name=tool_name,
427 return_direct=return_direct,
428 args_schema=args_schema,
429 infer_schema=infer_schema,
430 )
431 # If someone doesn't want a schema applied, we must treat it as
432 # a simple string->string function
433 if func.__doc__ is None:
434 raise ValueError(
435 "Function must have a docstring if "

Callers

nothing calls this directly

Calls 1

_make_with_nameFunction · 0.85

Tested by

no test coverage detected