Skip to content

Agent

autogen.beta.agent.Agent #

Agent(name, prompt=(), *, config=None, hitl_hook=None, tools=(), middleware=(), dependencies=None, variables=None)

Bases: Askable

Source code in autogen/beta/agent.py
def __init__(
    self,
    name: str,
    prompt: PromptType | Iterable[PromptType] = (),
    *,
    config: ModelConfig | None = None,
    hitl_hook: HumanHook | None = None,
    tools: Iterable[Callable[..., Any] | Tool] = (),
    middleware: Iterable["MiddlewareFactory"] = (),
    dependencies: dict[Any, Any] | None = None,
    variables: dict[Any, Any] | None = None,
):
    self.name = name
    self.config = config

    self._agent_dependencies = dependencies or {}
    self._agent_variables = variables or {}

    self._middleware = middleware
    self.dependency_provider = Provider()
    self.tools = [FunctionTool.ensure_tool(t, provider=self.dependency_provider) for t in tools]

    self.__hitl_hook = wrap_hitl(hitl_hook) if hitl_hook else default_hitl_hook
    self.__tool_executor = ToolExecutor()

    self._system_prompt: list[str] = []
    self._dynamic_prompt: list[Callable[[ModelRequest, Context], Awaitable[str]]] = []

    if isinstance(prompt, str) or callable(prompt):
        prompt = [prompt]

    for p in prompt:
        if isinstance(p, str):
            self._system_prompt.append(p)
        else:
            self._dynamic_prompt.append(_wrap_prompt_hook(p))

name instance-attribute #

name = name

config instance-attribute #

config = config

dependency_provider instance-attribute #

dependency_provider = Provider()

tools instance-attribute #

tools = [(ensure_tool(t, provider=dependency_provider)) for t in tools]

hitl_hook #

hitl_hook(func)
Source code in autogen/beta/agent.py
def hitl_hook(self, func: HumanHook) -> HumanHook:
    if self.__hitl_hook is not default_hitl_hook:
        warnings.warn(
            "You already set HITL hook, provided value overrides it",
            category=RuntimeWarning,
            stacklevel=2,
        )

    self.__hitl_hook = wrap_hitl(func)
    return func

prompt #

prompt(func: None = None) -> Callable[[PromptHook], PromptHook]
prompt(func: PromptHook) -> PromptHook
prompt(func=None)
Source code in autogen/beta/agent.py
def prompt(
    self,
    func: PromptHook | None = None,
) -> PromptHook | Callable[[PromptHook], PromptHook]:
    def wrapper(f: PromptHook) -> PromptHook:
        self._dynamic_prompt.append(f)
        return f

    if func:
        return wrapper(func)
    return wrapper

tool #

tool(function: Callable[..., Any], *, name: str | None = None, description: str | None = None, schema: FunctionParameters | None = None, sync_to_thread: bool = True) -> Tool
tool(function: None = None, *, name: str | None = None, description: str | None = None, schema: FunctionParameters | None = None, sync_to_thread: bool = True) -> Callable[[Callable[..., Any]], Tool]
tool(function=None, *, name=None, description=None, schema=None, sync_to_thread=True)
Source code in autogen/beta/agent.py
def tool(
    self,
    function: Callable[..., Any] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    schema: FunctionParameters | None = None,
    sync_to_thread: bool = True,
) -> Tool | Callable[[Callable[..., Any]], Tool]:
    def make_tool(f: Callable[..., Any]) -> Tool:
        t = tool(
            f,
            name=name,
            description=description,
            schema=schema,
            sync_to_thread=sync_to_thread,
        )
        t = FunctionTool.ensure_tool(t, provider=self.dependency_provider)
        self.tools.append(t)
        return t

    if function:
        return make_tool(function)

    return make_tool

ask async #

ask(msg, *, stream=None, dependencies=None, variables=None, prompt=(), config=None, tools=(), middleware=())
Source code in autogen/beta/agent.py
async def ask(
    self,
    msg: str,
    *,
    stream: Stream | None = None,
    dependencies: dict[Any, Any] | None = None,
    variables: dict[Any, Any] | None = None,
    prompt: Iterable[str] = (),
    config: ModelConfig | None = None,
    tools: Iterable[Tool] = (),
    middleware: Iterable["MiddlewareFactory"] = (),
) -> "AgentReply":
    config = config or self.config
    if not config:
        raise ConfigNotProvidedError()
    client = config.create()

    stream = stream or MemoryStream()

    initial_event = ModelRequest(content=msg)

    context = Context(
        stream,
        prompt=list(prompt),
        dependencies=self._agent_dependencies | (dependencies or {}),
        variables=self._agent_variables | (variables or {}),
        dependency_provider=self.dependency_provider,
    )

    if not context.prompt:
        context.prompt.extend(self._system_prompt)

        for dp in self._dynamic_prompt:
            p = await dp(initial_event, context)
            context.prompt.append(p)

    return await self._execute(
        initial_event,
        context=context,
        client=client,
        additional_tools=tools,
        additional_middleware=middleware,
    )

as_conversable #

as_conversable()
Source code in autogen/beta/agent.py
def as_conversable(self) -> "ConversableAdapter":
    from .conversable import ConversableAdapter

    return ConversableAdapter(self)