A programming framework for agentic AI
Makes select_speaker more robust by checking for mentions anywhere. (#669)
afourneyf939dda150dc
diff --git a/autogen/agentchat/groupchat.py b/autogen/agentchat/groupchat.pyindex e5a66438d..0f3bfa7a4 100644--- a/autogen/agentchat/groupchat.py+++ b/autogen/agentchat/groupchat.py@@ -2,7 +2,7 @@ import loggingimport sysfrom dataclasses import dataclassfrom typing import Dict, List, Optional, Union-+import refrom .agent import Agentfrom .conversable_agent import ConversableAgent@@ -101,6 +101,13 @@ Then select the next role from {[agent.name for agent in agents]} to play. Onlyif not final:# i = self._random.randint(0, len(self._agent_names) - 1) # randomly pick an idreturn self.next_agent(last_speaker, agents)++ # If exactly one agent is mentioned, use it. Otherwise, leave the OAI response unmodified+ mentions = self._mentioned_agents(name, agents)+ if len(mentions) == 1:+ name = next(iter(mentions))++ # Return the resulttry:return self.agent_by_name(name)except ValueError:@@ -119,6 +126,22 @@ Then select the next role from {[agent.name for agent in agents]} to play. Onlyroles.append(f"{agent.name}: {agent.system_message}")return "\n".join(roles)+ def _mentioned_agents(self, message_content: str, agents: List[Agent]) -> Dict:+ """+ Finds and counts agent mentions in the string message_content, taking word boundaries into account.++ Returns: A dictionary mapping agent names to mention counts (to be included, at least one mention must occur)+ """+ mentions = dict()+ for agent in agents:+ regex = (+ r"(?<=\W)" + re.escape(agent.name) + r"(?=\W)"+ ) # Finds agent mentions, taking word boundaries into account+ count = len(re.findall(regex, " " + message_content + " ")) # Pad the message to help with matching+ if count > 0:+ mentions[agent.name] = count+ return mentions+class GroupChatManager(ConversableAgent):"""(In preview) A chat manager agent that can manage a group chat of multiple agents."""diff --git a/test/agentchat/test_groupchat.py b/test/agentchat/test_groupchat.pyindex c50ef45cd..6f634fd86 100644--- a/test/agentchat/test_groupchat.py+++ b/test/agentchat/test_groupchat.py@@ -1,5 +1,6 @@import pytestimport autogen+import jsondef test_func_call_groupchat():@@ -112,8 +113,80 @@ def test_plugin():assert len(groupchat.messages) == 2+def test_agent_mentions():+ agent1 = autogen.ConversableAgent(+ "alice",+ max_consecutive_auto_reply=2,+ human_input_mode="NEVER",+ llm_config=False,+ default_auto_reply="This is alice sepaking.",+ )+ agent2 = autogen.ConversableAgent(+ "bob",+ max_consecutive_auto_reply=2,+ human_input_mode="NEVER",+ llm_config=False,+ default_auto_reply="This is bob speaking.",+ )+ agent3 = autogen.ConversableAgent(+ "sam",+ max_consecutive_auto_reply=2,+ human_input_mode="NEVER",+ llm_config=False,+ default_auto_reply="This is sam speaking.",+ )+ groupchat = autogen.GroupChat(agents=[agent1, agent2, agent3], messages=[], max_round=2)++ # Basic counting+ assert json.dumps(groupchat._mentioned_agents("", [agent1, agent2, agent3]), sort_keys=True) == "{}"+ assert json.dumps(groupchat._mentioned_agents("alice", [agent1, agent2, agent3]), sort_keys=True) == '{"alice": 1}'+ assert (+ json.dumps(groupchat._mentioned_agents("alice bob alice", [agent1, agent2, agent3]), sort_keys=True)+ == '{"alice": 2, "bob": 1}'+ )+ assert (+ json.dumps(groupchat._mentioned_agents("alice bob alice sam", [agent1, agent2, agent3]), sort_keys=True)+ == '{"alice": 2, "bob": 1, "sam": 1}'+ )+ assert (+ json.dumps(groupchat._mentioned_agents("alice bob alice sam robert", [agent1, agent2, agent3]), sort_keys=True)+ == '{"alice": 2, "bob": 1, "sam": 1}'+ )++ # Substring+ assert (+ json.dumps(groupchat._mentioned_agents("sam samantha basam asami", [agent1, agent2, agent3]), sort_keys=True)+ == '{"sam": 1}'+ )++ # Word boundaries+ assert (+ json.dumps(groupchat._mentioned_agents("alice! .alice. .alice", [agent1, agent2, agent3]), sort_keys=True)+ == '{"alice": 3}'+ )++ # Special characters in agent names+ agent4 = autogen.ConversableAgent(+ ".*",+ max_consecutive_auto_reply=2,+ human_input_mode="NEVER",+ llm_config=False,+ default_auto_reply="Match everything.",+ )++ groupchat = autogen.GroupChat(agents=[agent1, agent2, agent3, agent4], messages=[], max_round=2)+ assert (+ json.dumps(+ groupchat._mentioned_agents("alice bob alice sam robert .*", [agent1, agent2, agent3, agent4]),+ sort_keys=True,+ )+ == '{".*": 1, "alice": 2, "bob": 1, "sam": 1}'+ )++if __name__ == "__main__":test_func_call_groupchat()# test_broadcast()test_chat_manager()# test_plugin()+ # test_agent_mentions()