autogen_core.tools#

class BaseTool(args_type: Type[ArgsT], return_type: Type[ReturnT], name: str, description: str)[source]#

Bases: ABC, Tool, Generic[ArgsT, ReturnT], ComponentBase[BaseModel]

args_type() Type[BaseModel][source]#
component_type: ClassVar[ComponentType] = 'tool'#

The logical type of the component.

property description: str#
load_state_json(state: Mapping[str, Any]) None[source]#
property name: str#
return_type() Type[Any][source]#
return_value_as_string(value: Any) str[source]#
abstract async run(args: ArgsT, cancellation_token: CancellationToken) ReturnT[source]#
async run_json(args: Mapping[str, Any], cancellation_token: CancellationToken) Any[source]#
save_state_json() Mapping[str, Any][source]#
property schema: ToolSchema#
state_type() Type[BaseModel] | None[source]#
class BaseToolWithState(args_type: Type[ArgsT], return_type: Type[ReturnT], state_type: Type[StateT], name: str, description: str)[source]#

Bases: BaseTool[ArgsT, ReturnT], ABC, Generic[ArgsT, ReturnT, StateT], ComponentBase[BaseModel]

component_type: ClassVar[ComponentType] = 'tool'#

The logical type of the component.

abstract load_state(state: StateT) None[source]#
load_state_json(state: Mapping[str, Any]) None[source]#
abstract save_state() StateT[source]#
save_state_json() Mapping[str, Any][source]#
class FunctionTool(func: Callable[[...], Any], description: str, name: str | None = None, global_imports: Sequence[str | ImportFromModule | Alias] = [])[source]#

Bases: BaseTool[BaseModel, BaseModel], Component[FunctionToolConfig]

Create custom tools by wrapping standard Python functions.

FunctionTool offers an interface for executing Python functions either asynchronously or synchronously. Each function must include type annotations for all parameters and its return type. These annotations enable FunctionTool to generate a schema necessary for input validation, serialization, and for informing the LLM about expected parameters. When the LLM prepares a function call, it leverages this schema to generate arguments that align with the function’s specifications.

Note

It is the user’s responsibility to verify that the tool’s output type matches the expected type.

Parameters:
  • func (Callable[..., ReturnT | Awaitable[ReturnT]]) – The function to wrap and expose as a tool.

  • description (str) – A description to inform the model of the function’s purpose, specifying what it does and the context in which it should be called.

  • name (str, optional) – An optional custom name for the tool. Defaults to the function’s original name if not provided.

Example

import random
from autogen_core import CancellationToken
from autogen_core.tools import FunctionTool
from typing_extensions import Annotated
import asyncio


async def get_stock_price(ticker: str, date: Annotated[str, "Date in YYYY/MM/DD"]) -> float:
    # Simulates a stock price retrieval by returning a random float within a specified range.
    return random.uniform(10, 200)


async def example():
    # Initialize a FunctionTool instance for retrieving stock prices.
    stock_price_tool = FunctionTool(get_stock_price, description="Fetch the stock price for a given ticker.")

    # Execute the tool with cancellation support.
    cancellation_token = CancellationToken()
    result = await stock_price_tool.run_json({"ticker": "AAPL", "date": "2021/01/01"}, cancellation_token)

    # Output the result as a formatted string.
    print(stock_price_tool.return_value_as_string(result))


asyncio.run(example())
classmethod _from_config(config: FunctionToolConfig) Self[source]#

Create a new instance of the component from a configuration object.

Parameters:

config (T) – The configuration object.

Returns:

Self – The new instance of the component.

_to_config() FunctionToolConfig[source]#

Dump the configuration that would be requite to create a new instance of a component matching the configuration of this instance.

Returns:

T – The configuration of the component.

component_config_schema#

alias of FunctionToolConfig

component_provider_override: ClassVar[str | None] = 'autogen_core.tools.FunctionTool'#

Override the provider string for the component. This should be used to prevent internal module names being a part of the module name.

async run(args: BaseModel, cancellation_token: CancellationToken) Any[source]#
class ParametersSchema[source]#

Bases: TypedDict

properties: Dict[str, Any]#
required: NotRequired[Sequence[str]]#
type: str#
class Tool(*args, **kwargs)[source]#

Bases: Protocol

args_type() Type[BaseModel][source]#
property description: str#
load_state_json(state: Mapping[str, Any]) None[source]#
property name: str#
return_type() Type[Any][source]#
return_value_as_string(value: Any) str[source]#
async run_json(args: Mapping[str, Any], cancellation_token: CancellationToken) Any[source]#
save_state_json() Mapping[str, Any][source]#
property schema: ToolSchema#
state_type() Type[BaseModel] | None[source]#
class ToolSchema[source]#

Bases: TypedDict

description: NotRequired[str]#
name: str#
parameters: NotRequired[ParametersSchema]#