Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make register_factory a user facing API #4854

Merged
merged 5 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions python/packages/autogen-core/src/autogen_core/_agent_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,54 @@ async def register(

async def register_factory(
self,
*,
type: AgentType,
type: str | AgentType,
agent_factory: Callable[[], T | Awaitable[T]],
expected_class: type[T],
*,
expected_class: type[T] | None = None,
) -> AgentType:
"""Register an agent factory with the runtime associated with a specific type. The type must be unique.
"""Register an agent factory with the runtime associated with a specific type. The type must be unique. This API does not add any subscriptions.

.. note::

This is a low level API and usually the agent class's `register` method should be used instead, as this also handles subscriptions automatically.

Example:

.. code-block:: python

from dataclasses import dataclass

from autogen_core import AgentRuntime, MessageContext, RoutedAgent, event
from autogen_core.models import UserMessage


@dataclass
class MyMessage:
content: str


class MyAgent(RoutedAgent):
def __init__(self) -> None:
super().__init__("My core agent")

@event
async def handler(self, message: UserMessage, context: MessageContext) -> None:
print("Event received: ", message.content)


async def my_agent_factory():
return MyAgent()


async def main() -> None:
runtime: AgentRuntime = ... # type: ignore
await runtime.register_factory("my_agent", lambda: MyAgent())
jackgerrits marked this conversation as resolved.
Show resolved Hide resolved


Args:
type (str): The type of agent this factory creates. It is not the same as agent class name. The `type` parameter is used to differentiate between different factory functions rather than agent classes.
agent_factory (Callable[[], T]): The factory that creates the agent, where T is a concrete Agent type. Inside the factory, use `autogen_core.AgentInstantiationContext` to access variables like the current runtime and agent ID.
expected_class (type[T] | None, optional): The expected class of the agent, used for runtime validation of the factory. Defaults to None.
"""
...

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,11 +592,14 @@ async def register(

async def register_factory(
self,
*,
type: AgentType,
type: str | AgentType,
agent_factory: Callable[[], T | Awaitable[T]],
expected_class: type[T],
*,
expected_class: type[T] | None = None,
) -> AgentType:
if isinstance(type, str):
type = AgentType(type)

if type.type in self._agent_factories:
raise ValueError(f"Agent with type {type} already exists.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,11 +720,14 @@ async def register(

async def register_factory(
self,
*,
type: AgentType,
type: str | AgentType,
agent_factory: Callable[[], T | Awaitable[T]],
expected_class: type[T],
*,
expected_class: type[T] | None = None,
) -> AgentType:
if isinstance(type, str):
type = AgentType(type)

if type.type in self._agent_factories:
raise ValueError(f"Agent with type {type} already exists.")
if self._host_connection is None:
Expand Down
Loading