Skip to content

A programming framework for agentic AI

Move reset from a message to a command (#4073)

Eric Zhu5be7ac7b12c7
diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml
index 667317870..73e48edb1 100644
--- a/.github/workflows/checks.yml
+++ b/.github/workflows/checks.yml
@@ -177,11 +177,9 @@ jobs:
source ${{ github.workspace }}/python/.venv/bin/activate
poe gen-proto
working-directory: ./python
- - name: Evaluate if there are changes
- run: |
- if [[ `git status --porcelain` ]]; then
- echo "There are changes that need to be generated and commit for the proto files"
- git --no-pager diff
- exit 1
- fi
- shell: bash
+ - name: Check if there are uncommited changes
+ id: changes
+ uses: UnicornGlobal/[email protected]
+ - name: Process changes
+ if: steps.changes.outputs.changed == 1
+ run: echo "There are changes in the proto files. Please commit them."
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py
index 862b15e07..348b4f104 100644
--- a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py
+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py
@@ -23,7 +23,6 @@ from ..messages import (
ChatMessage,
HandoffMessage,
InnerMessage,
- ResetMessage,
TextMessage,
ToolCallMessage,
ToolCallResultMessage,
@@ -221,10 +220,7 @@ class AssistantAgent(BaseChatAgent):
) -> AsyncGenerator[InnerMessage | Response, None]:
# Add messages to the model context.
for msg in messages:
- if isinstance(msg, ResetMessage):
- self._model_context.clear()
- else:
- self._model_context.append(UserMessage(content=msg.content, source=msg.source))
+ self._model_context.append(UserMessage(content=msg.content, source=msg.source))
# Inner messages.
inner_messages: List[InnerMessage] = []
@@ -301,3 +297,7 @@ class AssistantAgent(BaseChatAgent):
return FunctionExecutionResult(content=result_as_str, call_id=tool_call.id)
except Exception as e:
return FunctionExecutionResult(content=f"Error: {e}", call_id=tool_call.id)
+
+ async def reset(self, cancellation_token: CancellationToken) -> None:
+ """Reset the assistant agent to its initialization state."""
+ self._model_context.clear()
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 bbfc1ce1e..811430564 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
@@ -89,3 +89,8 @@ class BaseChatAgent(ChatAgent, ABC):
else:
messages.append(message)
yield message
+
+ @abstractmethod
+ async def reset(self, cancellation_token: CancellationToken) -> None:
+ """Resets the agent to its initialization state."""
+ ...
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py
index 8c21d53fb..4b7848bbd 100644
--- a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py
+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py
@@ -38,3 +38,7 @@ class CodeExecutorAgent(BaseChatAgent):
return Response(chat_message=TextMessage(content=result.output, source=self.name))
else:
return Response(chat_message=TextMessage(content="No code blocks found in the thread.", source=self.name))
+
+ async def reset(self, cancellation_token: CancellationToken) -> None:
+ """It it's a no-op as the code executor agent has no mutable state."""
+ pass
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/base/_chat_agent.py b/python/packages/autogen-agentchat/src/autogen_agentchat/base/_chat_agent.py
index ce73352da..2db6d5023 100644
--- a/python/packages/autogen-agentchat/src/autogen_agentchat/base/_chat_agent.py
+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/base/_chat_agent.py
@@ -50,3 +50,7 @@ class ChatAgent(TaskRunner, Protocol):
"""Handles incoming messages and returns a stream of inner messages and
and the final item is the response."""
...
+
+ async def reset(self, cancellation_token: CancellationToken) -> None:
+ """Resets the agent to its initialization state."""
+ ...
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/messages.py b/python/packages/autogen-agentchat/src/autogen_agentchat/messages.py
index c2d2b7abf..280b53224 100644
--- a/python/packages/autogen-agentchat/src/autogen_agentchat/messages.py
+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/messages.py
@@ -48,13 +48,6 @@ class HandoffMessage(BaseMessage):
"""The handoff message to the target agent."""
-class ResetMessage(BaseMessage):
- """A message requesting reset of the recipient's state in the current conversation."""
-
- content: str
- """The content for the reset message."""
-
-
class ToolCallMessage(BaseMessage):
"""A message signaling the use of tools."""
@@ -73,7 +66,7 @@ InnerMessage = ToolCallMessage | ToolCallResultMessage
"""Messages for intra-agent monologues."""
-ChatMessage = TextMessage | MultiModalMessage | StopMessage | HandoffMessage | ResetMessage
+ChatMessage = TextMessage | MultiModalMessage | StopMessage | HandoffMessage
"""Messages for agent-to-agent communication."""
@@ -87,7 +80,6 @@ __all__ = [
"MultiModalMessage",
"StopMessage",
"HandoffMessage",
- "ResetMessage",
"ToolCallMessage",
"ToolCallResultMessage",
"ChatMessage",
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_chat_agent_container.py b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_chat_agent_container.py
index 49325c39f..689748338 100644
--- a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_chat_agent_container.py
+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_chat_agent_container.py
@@ -5,7 +5,7 @@ from autogen_core.components import DefaultTopicId, event
from ...base import ChatAgent, Response
from ...messages import ChatMessage
-from ._events import GroupChatAgentResponse, GroupChatMessage, GroupChatRequestPublish, GroupChatStart
+from ._events import GroupChatAgentResponse, GroupChatMessage, GroupChatRequestPublish, GroupChatReset, GroupChatStart
from ._sequential_routed_agent import SequentialRoutedAgent
@@ -37,6 +37,12 @@ class ChatAgentContainer(SequentialRoutedAgent):
"""Handle an agent response event by appending the content to the buffer."""
self._message_buffer.append(message.agent_response.chat_message)
+ @event
+ async def handle_reset(self, message: GroupChatReset, ctx: MessageContext) -> None:
+ """Handle a reset event by resetting the agent."""
+ self._message_buffer.clear()
+ await self._agent.reset(ctx.cancellation_token)
+
@event
async def handle_request(self, message: GroupChatRequestPublish, ctx: MessageContext) -> None:
"""Handle a content request event by passing the messages in the buffer
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_events.py b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_events.py
index 0b146c886..5e1635bb8 100644
--- a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_events.py
+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_events.py
@@ -36,3 +36,9 @@ class GroupChatTermination(BaseModel):
message: StopMessage
"""The stop message that indicates the reason of termination."""
+
+
+class GroupChatReset(BaseModel):
+ """A request to reset the agents in the group chat."""
+
+ ...
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_selector_group_chat.py b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_selector_group_chat.py
index a70f84946..39c763c48 100644
--- a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_selector_group_chat.py
+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_selector_group_chat.py
@@ -10,7 +10,6 @@ from ...messages import (
AgentMessage,
HandoffMessage,
MultiModalMessage,
- ResetMessage,
StopMessage,
TextMessage,
ToolCallMessage,
@@ -71,8 +70,8 @@ class SelectorGroupChatManager(BaseGroupChatManager):
# Construct the history of the conversation.
history_messages: List[str] = []
for msg in thread:
- if isinstance(msg, ToolCallMessage | ToolCallResultMessage | ResetMessage):
- # Ignore tool call messages and reset messages.
+ if isinstance(msg, ToolCallMessage | ToolCallResultMessage):
+ # Ignore tool call messages.
continue
# The agent type must be the same as the topic type, which we use as the agent name.
message = f"{msg.source}:"
diff --git a/python/packages/autogen-agentchat/tests/test_group_chat.py b/python/packages/autogen-agentchat/tests/test_group_chat.py
index 8f05c3f49..5b37d166b 100644
--- a/python/packages/autogen-agentchat/tests/test_group_chat.py
+++ b/python/packages/autogen-agentchat/tests/test_group_chat.py
@@ -82,6 +82,9 @@ class _EchoAgent(BaseChatAgent):
assert self._last_message is not None
return Response(chat_message=TextMessage(content=self._last_message, source=self.name))
+ async def reset(self, cancellation_token: CancellationToken) -> None:
+ self._last_message = None
+
class _StopAgent(_EchoAgent):
def __init__(self, name: str, description: str, *, stop_at: int = 1) -> None:
@@ -575,6 +578,9 @@ class _HandOffAgent(BaseChatAgent):
)
)
+ async def reset(self, cancellation_token: CancellationToken) -> None:
+ pass
+
@pytest.mark.asyncio
async def test_swarm_handoff() -> None:
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 d367ba29b..207559136 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
@@ -235,7 +235,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": null,
"metadata": {},
"outputs": [
{
@@ -257,6 +257,7 @@
" StopMessage,\n",
" TextMessage,\n",
")\n",
+ "from autogen_core.base import CancellationToken\n",
"\n",
"\n",
"class UserProxyAgent(BaseChatAgent):\n",
@@ -273,6 +274,9 @@
" return Response(chat_message=StopMessage(content=\"User has terminated the conversation.\", source=self.name))\n",
" return Response(chat_message=TextMessage(content=user_input, source=self.name))\n",
"\n",
+ " async def reset(self, cancellation_token: CancellationToken) -> None:\n",
+ " pass\n",
+ "\n",
"\n",
"user_proxy_agent = UserProxyAgent(name=\"user_proxy_agent\")\n",
"\n",
@@ -317,7 +321,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.11.5"
+ "version": "3.12.6"
}
},
"nbformat": 4,
diff --git a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/selector-group-chat.ipynb b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/selector-group-chat.ipynb
index bcf8c6afc..5ca7a0dc0 100644
--- a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/selector-group-chat.ipynb
+++ b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/selector-group-chat.ipynb
@@ -64,7 +64,7 @@
},
{
"cell_type": "code",
- "execution_count": 2,
+ "execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -80,7 +80,10 @@
" user_input = await asyncio.get_event_loop().run_in_executor(None, input, \"Enter your response: \")\n",
" if \"TERMINATE\" in user_input:\n",
" return Response(chat_message=StopMessage(content=\"User has terminated the conversation.\", source=self.name))\n",
- " return Response(chat_message=TextMessage(content=user_input, source=self.name))"
+ " return Response(chat_message=TextMessage(content=user_input, source=self.name))\n",
+ "\n",
+ " async def reset(self, cancellation_token: CancellationToken) -> None:\n",
+ " pass"
]
},
{
@@ -278,7 +281,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.11.5"
+ "version": "3.12.6"
}
},
"nbformat": 4,