A programming framework for agentic AI
auto reset termination when a run stops (#4126)
Eric Zhu5547a6716e78
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.pyindex 2c0d16760..989c6f62c 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@@ -106,16 +106,21 @@ class AssistantAgent(BaseChatAgent):.. code-block:: pythonimport asyncio+ from autogen_core.base import CancellationTokenfrom autogen_ext.models import OpenAIChatCompletionClientfrom autogen_agentchat.agents import AssistantAgent- from autogen_agentchat.task import MaxMessageTermination+ from autogen_agentchat.messages import TextMessage+async def main() -> None:model_client = OpenAIChatCompletionClient(model="gpt-4o")agent = AssistantAgent(name="assistant", model_client=model_client)- result await agent.run("What is the capital of France?", termination_condition=MaxMessageTermination(2))- print(result)+ response = await agent.on_messages(+ [TextMessage(content="What is the capital of France?", source="user")], CancellationToken()+ )+ print(response)+asyncio.run(main())@@ -128,7 +133,8 @@ class AssistantAgent(BaseChatAgent):import asynciofrom autogen_ext.models import OpenAIChatCompletionClientfrom autogen_agentchat.agents import AssistantAgent- from autogen_agentchat.task import MaxMessageTermination+ from autogen_agentchat.messages import TextMessage+ from autogen_core.base import CancellationTokenasync def get_current_time() -> str:@@ -139,7 +145,9 @@ class AssistantAgent(BaseChatAgent):model_client = OpenAIChatCompletionClient(model="gpt-4o")agent = AssistantAgent(name="assistant", model_client=model_client, tools=[get_current_time])- stream = agent.run_stream("What is the current time?", termination_condition=MaxMessageTermination(3))+ stream = agent.on_messages_stream(+ [TextMessage(content="What is the current time?", source="user")], CancellationToken()+ )async for message in stream:print(message)diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat.py b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat.pyindex 46a579292..6eb659d7c 100644--- a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat.py+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat.py@@ -165,6 +165,7 @@ class BaseGroupChat(Team, ABC):) -> TaskResult:"""Run the team and return the result. The base implementation uses:meth:`run_stream` to run the team and then returns the final result.+ Once the team is stopped, the termination condition is reset.Example using the :class:`~autogen_agentchat.teams.RoundRobinGroupChat` team:@@ -189,10 +190,7 @@ class BaseGroupChat(Team, ABC):result = await team.run(task="Count from 1 to 10, respond one at a time.")print(result)- # Reset the termination condition.- await termination.reset()-- # Run the team again without a task.+ # Run the team again without a task to continue the previous task.result = await team.run()print(result)@@ -217,7 +215,8 @@ class BaseGroupChat(Team, ABC):cancellation_token: CancellationToken | None = None,) -> AsyncGenerator[AgentMessage | TaskResult, None]:"""Run the team and produces a stream of messages and the final result- of the type :class:`TaskResult` as the last item in the stream.+ of the type :class:`TaskResult` as the last item in the stream. Once the+ team is stopped, the termination condition is reset.Example using the :class:`~autogen_agentchat.teams.RoundRobinGroupChat` team:@@ -242,10 +241,7 @@ class BaseGroupChat(Team, ABC):async for message in stream:print(message)- # Reset the termination condition.- await termination.reset()-- # Run the team again without a task.+ # Run the team again without a task to continue the previous task.stream = team.run_stream()async for message in stream:print(message)@@ -305,7 +301,7 @@ class BaseGroupChat(Team, ABC):async def reset(self) -> None:"""Reset the team and its participants to their initial state.- This includes the termination condition. The team must be stopped before it can be reset.+ The team must be stopped before it can be reset.Raises:RuntimeError: If the team has not been initialized or is currently running.diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat_manager.py b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat_manager.pyindex ebbd9d5c8..43bda969c 100644--- a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat_manager.py+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat_manager.py@@ -80,7 +80,8 @@ class BaseGroupChatManager(SequentialRoutedAgent, ABC):GroupChatTermination(message=stop_message),topic_id=DefaultTopicId(type=self._output_topic_type),)- # Stop the group chat.+ # Stop the group chat and reset the termination condition.+ await self._termination_condition.reset()returnspeaker_topic_type = await self.select_speaker(self._message_thread)@@ -104,7 +105,8 @@ class BaseGroupChatManager(SequentialRoutedAgent, ABC):await self.publish_message(GroupChatTermination(message=stop_message), topic_id=DefaultTopicId(type=self._output_topic_type))- # Stop the group chat.+ # Stop the group chat and reset the termination condition.+ await self._termination_condition.reset()return# Select a speaker to continue the conversation.diff --git a/python/packages/autogen-agentchat/tests/test_group_chat.py b/python/packages/autogen-agentchat/tests/test_group_chat.pyindex fc6da212b..cd792f73d 100644--- a/python/packages/autogen-agentchat/tests/test_group_chat.py+++ b/python/packages/autogen-agentchat/tests/test_group_chat.py@@ -333,7 +333,6 @@ async def test_round_robin_group_chat_with_resume_and_reset() -> None:assert result.stop_reason is not None# Resume.- await termination.reset()result = await team.run()assert len(result.messages) == 3assert result.messages[0].source == "agent_3"