Skip to content

FunctionTool

autogen.beta.tools.final.function_tool.FunctionTool #

FunctionTool(model, *, name, description, schema)

Bases: Tool

Source code in autogen/beta/tools/final/function_tool.py
def __init__(
    self,
    model: CallModel,
    *,
    name: str,
    description: str,
    schema: FunctionParameters,
) -> None:
    self.model = model

    self.schema = FunctionToolSchema(
        function=FunctionDefinition(
            name=name,
            description=description,
            parameters=schema,
        )
    )

    self.provider: Provider | None = None

model instance-attribute #

model = model

schema instance-attribute #

schema = FunctionToolSchema(function=FunctionDefinition(name=name, description=description, parameters=schema))

provider instance-attribute #

provider = None

schemas async #

schemas(context)
Source code in autogen/beta/tools/final/function_tool.py
async def schemas(self, context: "Context") -> list[FunctionToolSchema]:
    return [self.schema]

ensure_tool staticmethod #

ensure_tool(func, *, provider=None)
Source code in autogen/beta/tools/final/function_tool.py
@staticmethod
def ensure_tool(
    func: "Tool | Callable[..., Any]",
    *,
    provider: Provider | None = None,
) -> "FunctionTool":
    t = func if isinstance(func, Tool) else tool(func)
    t.provider = provider
    return t

register #

register(stack, context, *, middleware=())
Source code in autogen/beta/tools/final/function_tool.py
def register(
    self,
    stack: "ExitStack",
    context: "Context",
    *,
    middleware: Iterable["BaseMiddleware"] = (),
) -> None:
    execution: ToolExecution = self
    for mw in middleware:
        execution = partial(mw.on_tool_execution, execution)

    async def execute(event: "ToolCall", context: "Context") -> None:
        result = await execution(event, context)
        await context.send(result)

    stack.enter_context(context.stream.where(ToolCall.name == self.schema.function.name).sub_scope(execute))