func_tool¶
Tool is LLM’s extended capability which is one of the core design pattern of Agent. All tools can be wrapped in a FunctionTool class. This helps to standardize the tool interface and metadata to communicate with the Agent.
Functions
|
Attempt to find the variable name of the instance in the calling context. |
Classes
|
Describing and Parsing(to LLM) and executing a function. |
|
Enumeration of the 4 core function types supported by FunctionTool. |
- find_instance_name_from_self(instance)[source]¶
Attempt to find the variable name of the instance in the calling context.
- Parameters:
instance – The instance to find the name for.
- Returns:
The variable name of the instance, if found; otherwise, None.
- class FunctionType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Bases:
Enum
Enumeration of the 4 core function types supported by FunctionTool.
- SYNC = 1¶
- ASYNC = 2¶
- SYNC_GENERATOR = 3¶
- ASYNC_GENERATOR = 4¶
- class FunctionTool(fn: Callable | FunGradComponent, definition: FunctionDefinition | None = None, require_approval: bool = False, pre_execute_callback: Callable | None = None)[source]¶
Bases:
Component
Describing and Parsing(to LLM) and executing a function.
Supports both normal callable functions and class methods. When component is used, we support both the training and eval mode.
Note
When the eval mode, it outputs FunctionOutput, and when the training mode, it outputs Parameter with data as FunctionOutput.
- Parameters:
fn (Callable) – The function to be executed.
definition (FunctionDefinition, optional) – The definition of the function. Defaults to None.
Function be used by LLM as a tool to achieve a specific task.
What function can you pass as a tool? 1. Any unbound function you wrote outside of a class. 2. Any class method you wrote in your component. It can call self and other methods inside of your component. 3. When the function is using a trainable component, and you can directly use the component’s method as a tool or wrap it in a function. But you need to make sure to pass the component to the tool.
Here are some examples:
from adalflow.core.func_tool import FunctionTool class AgenticRAG(Component): def __init__(self, ...): super().__init__() self.retriever = Retriever() self.llm = Generator() def retriever_as_tool(input: str) -> str: r"Used as a retriever tool." return self.retriever(input) tools = [FunctionTool(retriever_as_tool, component=self.retriever), FunctionTool(self.llm.__call__, component=self.llm)] # if you have trainable component, this will ensure it can be trained together with your whole task pipeline # if you dont want to train them and simply treating them as a tool, you can call like this # tools = [FunctionTool(retriever_as_tool), FunctionTool(self.llm.__call__, component=self.llm)]
Features:
Supports both synchronous and asynchronous functions via call and acall.
Creates a FunctionDefinition from the function using get_fun_schema.
- Executes the function with arguments.
Parses the function call expression (FunctionExpression) into Function (name, args, kwargs).
- Executes the function using one of the following methods:
Via call with args and kwargs.
Via eval, without any context or sandboxing.
Via sandboxed execution directly using sandbox_exec.
A FunctionTool allows other GradComponent(as a tool) to pass through correctly.
- fn: Callable¶
- function_type: FunctionType¶
- definition: FunctionDefinition¶
- classmethod detect_function_type(fn: Callable) FunctionType [source]¶
Detect the function type of a given callable.
- Parameters:
fn – The callable to analyze
- Returns:
The detected function type
- Return type:
- Raises:
ValueError – If the function type cannot be determined or is not supported
- call(*args: Any, **kwargs: Any) FunctionOutput [source]¶
Execute the function synchronously, supporting all function types.
This method provides a unified sync interface for all function types: - SYNC: Calls the function directly - ASYNC: Runs the coroutine in a new event loop (blocks until complete) - SYNC_GENERATOR: Returns the generator object - ASYNC_GENERATOR: Runs the async generator and collects all values into a list
Warning: For async functions, this will block the current thread until completion. For better performance with async functions, consider using acall() instead.
Example
import asyncio
- async def async_func():
await asyncio.sleep(1) return “async result”
- def sync_func():
return “sync result”
tool1 = FunctionTool(async_func) tool2 = FunctionTool(sync_func)
# Both work synchronously result1 = tool1.call() # Blocks for 1 second result2 = tool2.call() # Returns immediately
- bicall(*args: Any, **kwargs: Any) FunctionOutput | Parameter [source]¶
This should only be used in training, where a fun is required to be a FunGradComponent where the output from function execution is a Parameter.
- async acall(*args, **kwargs) FunctionOutput | Parameter [source]¶
Async call the function. Handles all function types appropriately.
For different function types: - SYNC: Returns FunctionOutput with the result - ASYNC: Awaits the coroutine and returns FunctionOutput with the result - SYNC_GENERATOR: Returns FunctionOutput with the generator object - ASYNC_GENERATOR: Returns FunctionOutput with the async generator object
Note: For generators, users need to iterate over the generator themselves.