Update agent and agent runtime doc with routed agent (#4935)

* add back removed note

Signed-off-by: Mohammad Mazraeh <mazraeh.mohammad@gmail.com>

* fix formatting issues

Signed-off-by: Mohammad Mazraeh <mazraeh.mohammad@gmail.com>

---------

Signed-off-by: Mohammad Mazraeh <mazraeh.mohammad@gmail.com>
This commit is contained in:
Mohammad Mazraeh 2025-01-08 06:23:31 -08:00 committed by GitHub
parent d610d481cd
commit ad123641da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -28,8 +28,8 @@
"An agent in AutoGen is an entity defined by the base class {py:class}`autogen_core.Agent`.\n",
"It has a unique identifier of the type {py:class}`autogen_core.AgentId`,\n",
"a metadata dictionary of the type {py:class}`autogen_core.AgentMetadata`,\n",
"and method for handling messages {py:meth}`autogen_core.BaseAgent.on_message_impl`.\n",
"\n",
"and method for handling messages {py:meth}`autogen_core.BaseAgent.on_message_impl`. In most cases, you can subclass your agents from higher level class {py:class}`autogen_core.RoutedAgent` which enables you to route messages to corresponding message handler specified with {py:meth}`autogen_core.message_handler` decorator and proper type hint for the `message` variable.\n",
"An agent runtime is the execution environment for agents in AutoGen.\n",
"Similar to the runtime environment of a programming language,\n",
"an agent runtime provides the necessary infrastructure to facilitate communication\n",
@ -50,21 +50,21 @@
"source": [
"## Implementing an Agent\n",
"\n",
"To implement an agent, the developer must subclass the {py:class}`~autogen_core.BaseAgent` class\n",
"and implement the {py:meth}`~autogen_core.BaseAgent.on_message_impl` method.\n",
"To implement an agent, the developer must subclass the {py:class}`~autogen_core.RoutedAgent` class\n",
"and implement the {py:meth}`~autogen_core.RoutedAgent.on_message_impl` method.\n",
"This method is invoked when the agent receives a message. For example,\n",
"the following agent handles a simple message type and prints the message it receives:"
"the following agent handles a simple message type `MyMessageType` and prints the message it receives:"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from dataclasses import dataclass\n",
"\n",
"from autogen_core import AgentId, BaseAgent, MessageContext\n",
"from autogen_core import AgentId, MessageContext, RoutedAgent, message_handler\n",
"\n",
"\n",
"@dataclass\n",
@ -72,11 +72,12 @@
" content: str\n",
"\n",
"\n",
"class MyAgent(BaseAgent):\n",
"class MyAgent(RoutedAgent):\n",
" def __init__(self) -> None:\n",
" super().__init__(\"MyAgent\")\n",
"\n",
" async def on_message_impl(self, message: MyMessageType, ctx: MessageContext) -> None:\n",
" @message_handler\n",
" async def handle_my_message_type(self, message: MyMessageType, ctx: MessageContext) -> None:\n",
" print(f\"Received message: {message.content}\") # type: ignore"
]
},
@ -84,9 +85,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This agent only handles `MyMessageType` messages. \n",
"To handle multiple message types, developers can subclass the {py:class}`~autogen_core.RoutedAgent` class\n",
"which provides an easy-to use API to implement different message handlers for different message types.\n",
"This agent only handles `MyMessageType` and messages will be delivered to `handle_my_message_type` method. Developers can have multiple message handlers for different message types by using `@message_handler` decorator and setting the type hint for the `message` variable in the handler function. You can also leverage [python typing union](https://docs.python.org/3/library/typing.html#typing.Union) for the `message` variable in one message handler function if it better suits agent's logic.\n",
"See the next section on [message and communication](./message-and-communication.ipynb)."
]
},
@ -126,7 +125,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [
{
@ -203,7 +202,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
@ -228,7 +227,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
@ -246,7 +245,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -277,7 +276,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.6"
"version": "3.11.11"
}
},
"nbformat": 4,