Skip to content

A programming framework for agentic AI

Make it clear the on_messages method is stateful. (#4751)

Eric Zhu44b27ad81a7a
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_base_chat_agent.py b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_base_chat_agent.py
index c06fb8d6d..c6a4fad1a 100644
--- a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_base_chat_agent.py
+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_base_chat_agent.py
@@ -42,7 +42,18 @@ class BaseChatAgent(ChatAgent, ABC):
@abstractmethod
async def on_messages(self, messages: Sequence[ChatMessage], cancellation_token: CancellationToken) -> Response:
- """Handles incoming messages and returns a response."""
+ """Handles incoming messages and returns a response.
+
+ .. note::
+
+ Agents are stateful and the messages passed to this method should
+ be the new messages since the last call to this method. The agent
+ should maintain its state between calls to this method. For example,
+ if the agent needs to remember the previous messages to respond to
+ the current message, it should store the previous messages in the
+ agent state.
+
+ """
...
async def on_messages_stream(
@@ -51,7 +62,18 @@ class BaseChatAgent(ChatAgent, ABC):
"""Handles incoming messages and returns a stream of messages and
and the final item is the response. The base implementation in
:class:`BaseChatAgent` simply calls :meth:`on_messages` and yields
- the messages in the response."""
+ the messages in the response.
+
+ .. note::
+
+ Agents are stateful and the messages passed to this method should
+ be the new messages since the last call to this method. The agent
+ should maintain its state between calls to this method. For example,
+ if the agent needs to remember the previous messages to respond to
+ the current message, it should store the previous messages in the
+ agent state.
+
+ """
response = await self.on_messages(messages, cancellation_token)
for inner_message in response.inner_messages or []:
yield inner_message
diff --git a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/agents.ipynb b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/agents.ipynb
index 90f3f169c..04a51ae43 100644
--- a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/agents.ipynb
+++ b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/agents.ipynb
@@ -103,6 +103,13 @@
"as well as a list of inner messages in the {py:attr}`~autogen_agentchat.base.Response.inner_messages` attribute,\n",
"which stores the agent's \"thought process\" that led to the final response.\n",
"\n",
+ "```{note}\n",
+ "It is important to note that {py:meth}`~autogen_agentchat.agents.AssistantAgent.on_messages`\n",
+ "will update the internal state of the agent -- it will add the messages to the agent's\n",
+ "history. So you should not repeatedly call this method with the same messages if you want to\n",
+ "carry on a conversation with the agent.\n",
+ "```\n",
+ "\n",
"## User Proxy Agent\n",
"\n",
"{py:class}`~autogen_agentchat.agents.UserProxyAgent` is a built-in agent that\n",
diff --git a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/state.ipynb b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/state.ipynb
index c023dd640..dad2dd559 100644
--- a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/state.ipynb
+++ b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/state.ipynb
@@ -316,6 +316,7 @@
],
"source": [
"import json\n",
+ "\n",
"## save state to disk\n",
"\n",
"with open(\"coding/team_state.json\", \"w\") as f:\n",