Skip to content

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.py
index e5a66438d..0f3bfa7a4 100644
--- a/autogen/agentchat/groupchat.py
+++ b/autogen/agentchat/groupchat.py
@@ -2,7 +2,7 @@ import logging
import sys
from dataclasses import dataclass
from typing import Dict, List, Optional, Union
-
+import re
from .agent import Agent
from .conversable_agent import ConversableAgent
@@ -101,6 +101,13 @@ Then select the next role from {[agent.name for agent in agents]} to play. Only
if not final:
# i = self._random.randint(0, len(self._agent_names) - 1) # randomly pick an id
return 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 result
try:
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. Only
roles.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.py
index c50ef45cd..6f634fd86 100644
--- a/test/agentchat/test_groupchat.py
+++ b/test/agentchat/test_groupchat.py
@@ -1,5 +1,6 @@
import pytest
import autogen
+import json
def 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()