From 3f67e19143433c3bf773a1feb81e735c904d99f0 Mon Sep 17 00:00:00 2001 From: Li Jiang Date: Wed, 7 Aug 2024 08:18:38 +0000 Subject: [PATCH 1/3] Add last_speaker to GroupChatManager's property --- autogen/agentchat/groupchat.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/autogen/agentchat/groupchat.py b/autogen/agentchat/groupchat.py index 48f11d526cc6..52c417de39e2 100644 --- a/autogen/agentchat/groupchat.py +++ b/autogen/agentchat/groupchat.py @@ -964,6 +964,7 @@ def __init__( log_new_agent(self, locals()) # Store groupchat self._groupchat = groupchat + self.last_speaker = None self._silent = silent @@ -1034,6 +1035,7 @@ def run_chat( a.previous_cache = a.client_cache a.client_cache = self.client_cache for i in range(groupchat.max_round): + self.last_speaker = speaker groupchat.append(message, speaker) # broadcast the message to all agents except the speaker for agent in groupchat.agents: From d81f12f8be0d7af04ad1805ade83be4c982659fd Mon Sep 17 00:00:00 2001 From: Li Jiang Date: Fri, 9 Aug 2024 02:11:54 +0000 Subject: [PATCH 2/3] Add docstring for last_speaker --- autogen/agentchat/groupchat.py | 35 ++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/autogen/agentchat/groupchat.py b/autogen/agentchat/groupchat.py index 52c417de39e2..4ad8798d5c21 100644 --- a/autogen/agentchat/groupchat.py +++ b/autogen/agentchat/groupchat.py @@ -964,8 +964,8 @@ def __init__( log_new_agent(self, locals()) # Store groupchat self._groupchat = groupchat - self.last_speaker = None + self._last_speaker = None self._silent = silent # Order of register_reply is important. @@ -1007,6 +1007,37 @@ def _prepare_chat( if (recipient != agent or prepare_recipient) and isinstance(agent, ConversableAgent): agent._prepare_chat(self, clear_history, False, reply_at_receive) + @property + def last_speaker(self) -> Agent: + """Return the agent who sent the last message to group chat manager. + + In a group chat, an agent will always send a message to the group chat manager, and the group chat manager will send the message to + all other agents in the group chat. So, when an agent receives a message, it will always be from the group chat manager. With this + property, the agent receiving the message can know who actually sent the message. + + Example: + ```python + from autogen import ConversableAgent + from autogen import GroupChat, GroupChatManager + + def print_messages(recipient, messages, sender, config): + # Print the message immediately + print(f"Sender: {sender.name} | Recipient: {recipient.name} | Message: {messages[-1].get('content')}") + print(f"Real Sender: {sender.last_speaker.name}") + assert sender.last_speaker.name in messages[-1].get('content') + return False, None # Required to ensure the agent communication flow continues + + agent_a = ConversableAgent("agent A", default_auto_reply="I'm agent A.") + agent_b = ConversableAgent("agent B", default_auto_reply="I'm agent B.") + agent_c = ConversableAgent("agent C", default_auto_reply="I'm agent C.") + for agent in [agent_a, agent_b, agent_c]: + agent.register_reply([ConversableAgent, None], reply_func=print_messages, config=None) + group_chat = GroupChat([agent_a, agent_b, agent_c], messages=[], max_round=6, speaker_selection_method="random", allow_repeat_speaker=True) + chat_manager = GroupChatManager(group_chat) + groupchat_result = agent_a.initiate_chat(chat_manager, message="Hi, I'm agent A.") + """ + return self._last_speaker + def run_chat( self, messages: Optional[List[Dict]] = None, @@ -1035,7 +1066,7 @@ def run_chat( a.previous_cache = a.client_cache a.client_cache = self.client_cache for i in range(groupchat.max_round): - self.last_speaker = speaker + self._last_speaker = speaker groupchat.append(message, speaker) # broadcast the message to all agents except the speaker for agent in groupchat.agents: From 3618f2a178867c784ef8459a6c8f5f980fd4adf9 Mon Sep 17 00:00:00 2001 From: Li Jiang Date: Fri, 9 Aug 2024 02:15:09 +0000 Subject: [PATCH 3/3] Format docstring --- autogen/agentchat/groupchat.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/autogen/agentchat/groupchat.py b/autogen/agentchat/groupchat.py index 4ad8798d5c21..bcb8c30e2b6a 100644 --- a/autogen/agentchat/groupchat.py +++ b/autogen/agentchat/groupchat.py @@ -1011,30 +1011,45 @@ def _prepare_chat( def last_speaker(self) -> Agent: """Return the agent who sent the last message to group chat manager. - In a group chat, an agent will always send a message to the group chat manager, and the group chat manager will send the message to - all other agents in the group chat. So, when an agent receives a message, it will always be from the group chat manager. With this - property, the agent receiving the message can know who actually sent the message. + In a group chat, an agent will always send a message to the group chat manager, and the group chat manager will + send the message to all other agents in the group chat. So, when an agent receives a message, it will always be + from the group chat manager. With this property, the agent receiving the message can know who actually sent the + message. Example: ```python from autogen import ConversableAgent from autogen import GroupChat, GroupChatManager + def print_messages(recipient, messages, sender, config): # Print the message immediately - print(f"Sender: {sender.name} | Recipient: {recipient.name} | Message: {messages[-1].get('content')}") + print( + f"Sender: {sender.name} | Recipient: {recipient.name} | Message: {messages[-1].get('content')}" + ) print(f"Real Sender: {sender.last_speaker.name}") - assert sender.last_speaker.name in messages[-1].get('content') + assert sender.last_speaker.name in messages[-1].get("content") return False, None # Required to ensure the agent communication flow continues + agent_a = ConversableAgent("agent A", default_auto_reply="I'm agent A.") agent_b = ConversableAgent("agent B", default_auto_reply="I'm agent B.") agent_c = ConversableAgent("agent C", default_auto_reply="I'm agent C.") for agent in [agent_a, agent_b, agent_c]: - agent.register_reply([ConversableAgent, None], reply_func=print_messages, config=None) - group_chat = GroupChat([agent_a, agent_b, agent_c], messages=[], max_round=6, speaker_selection_method="random", allow_repeat_speaker=True) + agent.register_reply( + [ConversableAgent, None], reply_func=print_messages, config=None + ) + group_chat = GroupChat( + [agent_a, agent_b, agent_c], + messages=[], + max_round=6, + speaker_selection_method="random", + allow_repeat_speaker=True, + ) chat_manager = GroupChatManager(group_chat) - groupchat_result = agent_a.initiate_chat(chat_manager, message="Hi, I'm agent A.") + groupchat_result = agent_a.initiate_chat( + chat_manager, message="Hi, there, I'm agent A." + ) """ return self._last_speaker