diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/task/_terminations.py b/python/packages/autogen-agentchat/src/autogen_agentchat/task/_terminations.py index 191e14f8566c..ade11d759b36 100644 --- a/python/packages/autogen-agentchat/src/autogen_agentchat/task/_terminations.py +++ b/python/packages/autogen-agentchat/src/autogen_agentchat/task/_terminations.py @@ -48,7 +48,7 @@ async def __call__(self, messages: Sequence[ChatMessage]) -> StopMessage | None: self._message_count += len(messages) if self._message_count >= self._max_messages: return StopMessage( - content=f"Maximal number of messages {self._max_messages} reached, current message count: {self._message_count}", + content=f"Maximum number of messages {self._max_messages} reached, current message count: {self._message_count}", source="MaxMessageTermination", ) return None 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.py index 6ee79d4ded0a..f5268a3a9afa 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 @@ -28,7 +28,12 @@ class BaseGroupChat(Team, ABC): create a subclass of :class:`BaseGroupChat` that uses the group chat manager. """ - def __init__(self, participants: List[ChatAgent], group_chat_manager_class: type[BaseGroupChatManager]): + def __init__( + self, + participants: List[ChatAgent], + group_chat_manager_class: type[BaseGroupChatManager], + termination_condition: TerminationCondition | None = None, + ): if len(participants) == 0: raise ValueError("At least one participant is required.") if len(participants) != len(set(participant.name for participant in participants)): @@ -36,6 +41,7 @@ def __init__(self, participants: List[ChatAgent], group_chat_manager_class: type self._participants = participants self._team_id = str(uuid.uuid4()) self._base_group_chat_manager_class = group_chat_manager_class + self._termination_condition = termination_condition @abstractmethod def _create_group_chat_manager_factory( @@ -109,7 +115,7 @@ async def run( group_topic_type=group_topic_type, participant_topic_types=participant_topic_types, participant_descriptions=participant_descriptions, - termination_condition=termination_condition, + termination_condition=termination_condition or self._termination_condition, ), ) # Add subscriptions for the group chat manager. diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_round_robin_group_chat.py b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_round_robin_group_chat.py index fff872dd84b6..e8f5f66533f2 100644 --- a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_round_robin_group_chat.py +++ b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_round_robin_group_chat.py @@ -82,8 +82,12 @@ class RoundRobinGroupChat(BaseGroupChat): """ - def __init__(self, participants: List[ChatAgent]): - super().__init__(participants, group_chat_manager_class=RoundRobinGroupChatManager) + def __init__(self, participants: List[ChatAgent], termination_condition: TerminationCondition | None = None): + super().__init__( + participants, + termination_condition=termination_condition, + group_chat_manager_class=RoundRobinGroupChatManager, + ) def _create_group_chat_manager_factory( self, 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 79f8b60decf4..3cc489daa6b7 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 @@ -140,7 +140,8 @@ def _mentioned_agents(self, message_content: str, agent_names: List[str]) -> Dic + re.escape(name.replace("_", r"\_")) + r")(?=\W)" ) - count = len(re.findall(regex, f" {message_content} ")) # Pad the message to help with matching + # Pad the message to help with matching + count = len(re.findall(regex, f" {message_content} ")) if count > 0: mentions[name] = count return mentions @@ -184,6 +185,7 @@ def __init__( participants: List[ChatAgent], model_client: ChatCompletionClient, *, + termination_condition: TerminationCondition | None = None, selector_prompt: str = """You are in a role play game. The following roles are available: {roles}. Read the following conversation. Then select the next role from {participants} to play. Only return the role. @@ -194,7 +196,9 @@ def __init__( """, allow_repeated_speaker: bool = False, ): - super().__init__(participants, group_chat_manager_class=SelectorGroupChatManager) + super().__init__( + participants, termination_condition=termination_condition, group_chat_manager_class=SelectorGroupChatManager + ) # Validate the participants. if len(participants) < 2: raise ValueError("At least two participants are required for SelectorGroupChat.") diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_swarm_group_chat.py b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_swarm_group_chat.py index 7c24ac4c197c..694584706d01 100644 --- a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_swarm_group_chat.py +++ b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_swarm_group_chat.py @@ -82,8 +82,10 @@ class Swarm(BaseGroupChat): await team.run("What is bob's birthday?", termination_condition=MaxMessageTermination(3)) """ - def __init__(self, participants: List[ChatAgent]): - super().__init__(participants, group_chat_manager_class=SwarmGroupChatManager) + def __init__(self, participants: List[ChatAgent], termination_condition: TerminationCondition | None = None): + super().__init__( + participants, termination_condition=termination_condition, group_chat_manager_class=SwarmGroupChatManager + ) def _create_group_chat_manager_factory( self, diff --git a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/quickstart.ipynb b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/quickstart.ipynb index be69005d7a05..52f4b1baa914 100644 --- a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/quickstart.ipynb +++ b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/quickstart.ipynb @@ -37,18 +37,18 @@ "text": [ "\n", "--------------------------------------------------------------------------- \n", - "\u001b[91m[2024-10-23T12:15:51.582079]:\u001b[0m\n", + "\u001b[91m[2024-10-29T15:48:06.329810]:\u001b[0m\n", "\n", "What is the weather in New York?\n", "--------------------------------------------------------------------------- \n", - "\u001b[91m[2024-10-23T12:15:52.745820], writing_agent:\u001b[0m\n", + "\u001b[91m[2024-10-29T15:48:08.085839], weather_agent:\u001b[0m\n", "\n", - "The weather in New York is currently 73 degrees and sunny. TERMINATE\n", + "The weather in New York is 73 degrees and sunny.\n", "--------------------------------------------------------------------------- \n", - "\u001b[91m[2024-10-23T12:15:52.746210], Termination:\u001b[0m\n", + "\u001b[91m[2024-10-29T15:48:08.086180], Termination:\u001b[0m\n", "\n", - "Maximal number of messages 1 reached, current message count: 1\n", - " TaskResult(messages=[TextMessage(source='user', content='What is the weather in New York?'), StopMessage(source='writing_agent', content='The weather in New York is currently 73 degrees and sunny. TERMINATE')])\n" + "Maximum number of messages 2 reached, current message count: 2\n", + " TaskResult(messages=[TextMessage(source='user', content='What is the weather in New York?'), TextMessage(source='weather_agent', content='The weather in New York is 73 degrees and sunny.')])\n" ] } ], @@ -56,13 +56,13 @@ "import logging\n", "\n", "from autogen_agentchat import EVENT_LOGGER_NAME\n", - "from autogen_agentchat.agents import ToolUseAssistantAgent\n", + "from autogen_agentchat.agents import AssistantAgent\n", "from autogen_agentchat.logging import ConsoleLogHandler\n", "from autogen_agentchat.task import MaxMessageTermination\n", "from autogen_agentchat.teams import RoundRobinGroupChat\n", - "from autogen_core.components.tools import FunctionTool\n", "from autogen_ext.models import OpenAIChatCompletionClient\n", "\n", + "# set up logging. You can define your own logger\n", "logger = logging.getLogger(EVENT_LOGGER_NAME)\n", "logger.addHandler(ConsoleLogHandler())\n", "logger.setLevel(logging.INFO)\n", @@ -73,22 +73,18 @@ " return f\"The weather in {city} is 73 degrees and Sunny.\"\n", "\n", "\n", - "# wrap the tool for use with the agent\n", - "get_weather_tool = FunctionTool(get_weather, description=\"Get the weather for a city\")\n", - "\n", "# define an agent\n", - "weather_agent = ToolUseAssistantAgent(\n", - " name=\"writing_agent\",\n", + "weather_agent = AssistantAgent(\n", + " name=\"weather_agent\",\n", " model_client=OpenAIChatCompletionClient(model=\"gpt-4o-2024-08-06\"),\n", - " registered_tools=[get_weather_tool],\n", + " tools=[get_weather],\n", ")\n", "\n", "# add the agent to a team\n", - "agent_team = RoundRobinGroupChat([weather_agent])\n", + "agent_team = RoundRobinGroupChat([weather_agent], termination_condition=MaxMessageTermination(max_messages=2))\n", "# Note: if running in a Python file directly you'll need to use asyncio.run(agent_team.run(...)) instead of await agent_team.run(...)\n", "result = await agent_team.run(\n", " task=\"What is the weather in New York?\",\n", - " termination_condition=MaxMessageTermination(max_messages=1),\n", ")\n", "print(\"\\n\", result)" ] @@ -97,7 +93,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The code snippet above introduces two high level concepts in AgentChat: `Agent` and `Team`. An Agent helps us define what actions are taken when a message is received. Specifically, we use the `ToolUseAssistantAgent` preset - an agent that can be given a function that it can then use to address tasks. A Team helps us define the rules for how agents interact with each other. In the `RoundRobinGroupChat` team, agents receive messages in a sequential round-robin fashion. " + "The code snippet above introduces two high level concepts in AgentChat: `Agent` and `Team`. An Agent helps us define what actions are taken when a message is received. Specifically, we use the `AssistantAgent` preset - an agent that can be given tools (functions) that it can then use to address tasks. A Team helps us define the rules for how agents interact with each other. In the `RoundRobinGroupChat` team, agents receive messages in a sequential round-robin fashion. " ] }, { @@ -113,7 +109,7 @@ ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "agnext", "language": "python", "name": "python3" }, @@ -127,7 +123,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.6" + "version": "3.11.9" } }, "nbformat": 4,