Skip to content

Commit

Permalink
Agentchat move termination (microsoft#3992)
Browse files Browse the repository at this point in the history
  • Loading branch information
victordibia authored Oct 30, 2024
1 parent 0f4dd0c commit 75b00e7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,20 @@ 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)):
raise ValueError("The participant names must be unique.")
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(
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,32 @@
"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"
]
}
],
"source": [
"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",
Expand All @@ -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)"
]
Expand All @@ -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. "
]
},
{
Expand All @@ -113,7 +109,7 @@
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "agnext",
"language": "python",
"name": "python3"
},
Expand All @@ -127,7 +123,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.6"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 75b00e7

Please sign in to comment.