From 26711c15e19b4d5002836861476d0ca0266e733f Mon Sep 17 00:00:00 2001 From: Mark Sze Date: Thu, 9 May 2024 03:48:31 +0000 Subject: [PATCH 1/4] Update to ensure name on initial messages --- autogen/agentchat/conversable_agent.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index bfd38a54d60..638f0db0bac 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -555,7 +555,7 @@ def _assert_valid_name(name): raise ValueError(f"Invalid name: {name}. Name must be less than 64 characters.") return name - def _append_oai_message(self, message: Union[Dict, str], role, conversation_id: Agent) -> bool: + def _append_oai_message(self, message: Union[Dict, str], role, conversation_id: Agent, is_sending: bool) -> bool: """Append a message to the ChatCompletion conversation. If the message received is a string, it will be put in the "content" field of the new dictionary. @@ -567,6 +567,7 @@ def _append_oai_message(self, message: Union[Dict, str], role, conversation_id: message (dict or str): message to be appended to the ChatCompletion conversation. role (str): role of the message, can be "assistant" or "function". conversation_id (Agent): id of the conversation, should be the recipient or sender. + is_sending (bool): If the agent (aka self) is sending to the conversation_id agent, otherwise receiving. Returns: bool: whether the message is appended to the ChatCompletion conversation. @@ -596,7 +597,15 @@ def _append_oai_message(self, message: Union[Dict, str], role, conversation_id: if oai_message.get("function_call", False) or oai_message.get("tool_calls", False): oai_message["role"] = "assistant" # only messages with role 'assistant' can have a function call. + elif "name" not in oai_message: + # If we don't have a name field, append it + if is_sending: + oai_message["name"] = self.name + else: + oai_message["name"] = conversation_id.name + self._oai_messages[conversation_id].append(oai_message) + return True def _process_message_before_send( @@ -650,7 +659,7 @@ def send( message = self._process_message_before_send(message, recipient, silent) # When the agent composes and sends the message, the role of the message is "assistant" # unless it's "function". - valid = self._append_oai_message(message, "assistant", recipient) + valid = self._append_oai_message(message, "assistant", recipient, is_sending=True) if valid: recipient.receive(message, self, request_reply, silent) else: @@ -700,7 +709,7 @@ async def a_send( message = self._process_message_before_send(message, recipient, silent) # When the agent composes and sends the message, the role of the message is "assistant" # unless it's "function". - valid = self._append_oai_message(message, "assistant", recipient) + valid = self._append_oai_message(message, "assistant", recipient, is_sending=True) if valid: await recipient.a_receive(message, self, request_reply, silent) else: @@ -771,7 +780,7 @@ def _print_received_message(self, message: Union[Dict, str], sender: Agent): def _process_received_message(self, message: Union[Dict, str], sender: Agent, silent: bool): # When the agent receives a message, the role of the message is "user". (If 'role' exists and is 'function', it will remain unchanged.) - valid = self._append_oai_message(message, "user", sender) + valid = self._append_oai_message(message, "user", sender, is_sending=False) if logging_enabled(): log_event(self, "received_message", message=message, sender=sender.name, valid=valid) From 8074f30ea3b4706ce85838d9332fbdfb647b58ce Mon Sep 17 00:00:00 2001 From: Mark Sze Date: Thu, 9 May 2024 19:59:26 +0000 Subject: [PATCH 2/4] Corrected test cases for messages now including names. --- test/agentchat/test_groupchat.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/test/agentchat/test_groupchat.py b/test/agentchat/test_groupchat.py index a4689bd539f..790148bb6ac 100755 --- a/test/agentchat/test_groupchat.py +++ b/test/agentchat/test_groupchat.py @@ -721,7 +721,7 @@ def test_clear_agents_history(): agent1_history = list(agent1._oai_messages.values())[0] agent2_history = list(agent2._oai_messages.values())[0] assert agent1_history == [ - {"content": "hello", "role": "assistant"}, + {"content": "hello", "role": "assistant", "name": "alice"}, {"content": "This is bob speaking.", "name": "bob", "role": "user"}, {"content": "How you doing?", "name": "sam", "role": "user"}, ] @@ -742,7 +742,7 @@ def test_clear_agents_history(): {"content": "How you doing?", "name": "sam", "role": "user"}, ] assert agent2_history == [ - {"content": "This is bob speaking.", "role": "assistant"}, + {"content": "This is bob speaking.", "role": "assistant", "name": "bob"}, {"content": "How you doing?", "name": "sam", "role": "user"}, ] assert groupchat.messages == [ @@ -756,12 +756,12 @@ def test_clear_agents_history(): agent1_history = list(agent1._oai_messages.values())[0] agent2_history = list(agent2._oai_messages.values())[0] assert agent1_history == [ - {"content": "hello", "role": "assistant"}, + {"content": "hello", "role": "assistant", "name": "alice"}, {"content": "This is bob speaking.", "name": "bob", "role": "user"}, {"content": "How you doing?", "name": "sam", "role": "user"}, ] assert agent2_history == [ - {"content": "This is bob speaking.", "role": "assistant"}, + {"content": "This is bob speaking.", "role": "assistant", "name": "bob"}, {"content": "How you doing?", "name": "sam", "role": "user"}, ] assert groupchat.messages == [ @@ -819,6 +819,7 @@ def test_clear_agents_history(): "content": "example tool response", "tool_responses": [{"tool_call_id": "call_emulated", "role": "tool", "content": "example tool response"}], "role": "tool", + "name": "alice", }, ] @@ -1215,7 +1216,7 @@ def test_role_for_select_speaker_messages(): # into a message attribute called 'override_role'. This is evaluated in Conversable Agent's _append_oai_message function # e.g.: message={'content':self.select_speaker_prompt(agents),'override_role':self.role_for_select_speaker_messages}, message = {"content": "A prompt goes here.", "override_role": groupchat.role_for_select_speaker_messages} - checking_agent._append_oai_message(message, "assistant", speaker_selection_agent) + checking_agent._append_oai_message(message, "assistant", speaker_selection_agent, is_sending=True) # Test default is "system" assert len(checking_agent.chat_messages) == 1 @@ -1224,7 +1225,7 @@ def test_role_for_select_speaker_messages(): # Test as "user" groupchat.role_for_select_speaker_messages = "user" message = {"content": "A prompt goes here.", "override_role": groupchat.role_for_select_speaker_messages} - checking_agent._append_oai_message(message, "assistant", speaker_selection_agent) + checking_agent._append_oai_message(message, "assistant", speaker_selection_agent, is_sending=True) assert len(checking_agent.chat_messages) == 1 assert checking_agent.chat_messages[speaker_selection_agent][-1]["role"] == "user" @@ -1232,7 +1233,7 @@ def test_role_for_select_speaker_messages(): # Test as something unusual groupchat.role_for_select_speaker_messages = "SockS" message = {"content": "A prompt goes here.", "override_role": groupchat.role_for_select_speaker_messages} - checking_agent._append_oai_message(message, "assistant", speaker_selection_agent) + checking_agent._append_oai_message(message, "assistant", speaker_selection_agent, is_sending=True) assert len(checking_agent.chat_messages) == 1 assert checking_agent.chat_messages[speaker_selection_agent][-1]["role"] == "SockS" From fa0d56085f719b35000c2e07e850245ffdc29d56 Mon Sep 17 00:00:00 2001 From: Mark Sze Date: Fri, 24 May 2024 07:52:43 +0000 Subject: [PATCH 3/4] Added name to messages within select speaker nested chat --- autogen/agentchat/groupchat.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/autogen/agentchat/groupchat.py b/autogen/agentchat/groupchat.py index 86492455080..a1966ad655b 100644 --- a/autogen/agentchat/groupchat.py +++ b/autogen/agentchat/groupchat.py @@ -636,6 +636,7 @@ def validate_speaker_name(recipient, messages, sender, config) -> Tuple[bool, Un cache=None, # don't use caching for the speaker selection chat message={ "content": self.select_speaker_prompt(agents), + "name": "checking_agent", "override_role": self.role_for_select_speaker_messages, }, max_turns=2 @@ -783,6 +784,7 @@ def _validate_speaker_name( return True, { "content": self.select_speaker_auto_multiple_template.format(agentlist=agentlist), + "name": "checking_agent", "override_role": self.role_for_select_speaker_messages, } else: @@ -812,6 +814,7 @@ def _validate_speaker_name( return True, { "content": self.select_speaker_auto_none_template.format(agentlist=agentlist), + "name": "checking_agent", "override_role": self.role_for_select_speaker_messages, } else: From 7b656385cef152fddfb75de695d19455ac11cf00 Mon Sep 17 00:00:00 2001 From: Mark Sze Date: Thu, 27 Jun 2024 20:36:07 +0000 Subject: [PATCH 4/4] Corrected select speaker group chat tests for name field --- test/agentchat/test_groupchat.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/agentchat/test_groupchat.py b/test/agentchat/test_groupchat.py index efa28a9a1e7..291a94d450f 100755 --- a/test/agentchat/test_groupchat.py +++ b/test/agentchat/test_groupchat.py @@ -1647,6 +1647,7 @@ def test_speaker_selection_validate_speaker_name(): True, { "content": groupchat.select_speaker_auto_multiple_template.format(agentlist=agent_list_string), + "name": "checking_agent", "override_role": groupchat.role_for_select_speaker_messages, }, ) @@ -1693,6 +1694,7 @@ def test_speaker_selection_validate_speaker_name(): True, { "content": groupchat.select_speaker_auto_none_template.format(agentlist=agent_list_string), + "name": "checking_agent", "override_role": groupchat.role_for_select_speaker_messages, }, ) @@ -1762,6 +1764,7 @@ def test_select_speaker_auto_messages(): True, { "content": custom_multiple_names_msg.replace("{agentlist}", "['Alice', 'Bob']"), + "name": "checking_agent", "override_role": groupchat.role_for_select_speaker_messages, }, ) @@ -1771,6 +1774,7 @@ def test_select_speaker_auto_messages(): True, { "content": custom_no_names_msg.replace("{agentlist}", "['Alice', 'Bob']"), + "name": "checking_agent", "override_role": groupchat.role_for_select_speaker_messages, }, )