diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index 7b7ecaa80204..a3726678a452 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -1395,13 +1395,14 @@ def check_termination_and_human_reply( if config is None: config = self if messages is None: - messages = self._oai_messages[sender] + messages = self._oai_messages[sender] if sender else [] message = messages[-1] reply = "" no_human_input_msg = "" + sender_name = "the sender" if sender is None else sender.name if self.human_input_mode == "ALWAYS": reply = self.get_human_input( - f"Provide feedback to {sender.name}. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: " + f"Provide feedback to {sender_name}. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: " ) no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else "" # if the human input is empty, and the message is a termination message, then we will terminate the conversation @@ -1414,9 +1415,9 @@ def check_termination_and_human_reply( # self.human_input_mode == "TERMINATE": terminate = self._is_termination_msg(message) reply = self.get_human_input( - f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: " + f"Please give feedback to {sender_name}. Press enter or type 'exit' to stop the conversation: " if terminate - else f"Please give feedback to {sender.name}. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: " + else f"Please give feedback to {sender_name}. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: " ) no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else "" # if the human input is empty, and the message is a termination message, then we will terminate the conversation @@ -1427,7 +1428,7 @@ def check_termination_and_human_reply( else: # self.human_input_mode == "TERMINATE": reply = self.get_human_input( - f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: " + f"Please give feedback to {sender_name}. Press enter or type 'exit' to stop the conversation: " ) no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else "" # if the human input is empty, and the message is a termination message, then we will terminate the conversation @@ -1505,13 +1506,14 @@ async def a_check_termination_and_human_reply( if config is None: config = self if messages is None: - messages = self._oai_messages[sender] - message = messages[-1] + messages = self._oai_messages[sender] if sender else [] + message = messages[-1] if messages else {} reply = "" no_human_input_msg = "" + sender_name = "the sender" if sender is None else sender.name if self.human_input_mode == "ALWAYS": reply = await self.a_get_human_input( - f"Provide feedback to {sender.name}. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: " + f"Provide feedback to {sender_name}. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: " ) no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else "" # if the human input is empty, and the message is a termination message, then we will terminate the conversation @@ -1524,9 +1526,9 @@ async def a_check_termination_and_human_reply( # self.human_input_mode == "TERMINATE": terminate = self._is_termination_msg(message) reply = await self.a_get_human_input( - f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: " + f"Please give feedback to {sender_name}. Press enter or type 'exit' to stop the conversation: " if terminate - else f"Please give feedback to {sender.name}. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: " + else f"Please give feedback to {sender_name}. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: " ) no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else "" # if the human input is empty, and the message is a termination message, then we will terminate the conversation @@ -1537,7 +1539,7 @@ async def a_check_termination_and_human_reply( else: # self.human_input_mode == "TERMINATE": reply = await self.a_get_human_input( - f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: " + f"Please give feedback to {sender_name}. Press enter or type 'exit' to stop the conversation: " ) no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else "" # if the human input is empty, and the message is a termination message, then we will terminate the conversation diff --git a/test/agentchat/test_conversable_agent.py b/test/agentchat/test_conversable_agent.py index 5b0a6c326b43..b71a6341c877 100644 --- a/test/agentchat/test_conversable_agent.py +++ b/test/agentchat/test_conversable_agent.py @@ -472,6 +472,29 @@ async def test_a_generate_reply_raises_on_messages_and_sender_none(conversable_a await conversable_agent.a_generate_reply(messages=None, sender=None) +def test_generate_reply_with_messages_and_sender_none(conversable_agent): + messages = [{"role": "user", "content": "hello"}] + try: + response = conversable_agent.generate_reply(messages=messages, sender=None) + assert response is not None, "Response should not be None" + except AssertionError as e: + pytest.fail(f"Unexpected AssertionError: {e}") + except Exception as e: + pytest.fail(f"Unexpected exception: {e}") + + +@pytest.mark.asyncio +async def test_a_generate_reply_with_messages_and_sender_none(conversable_agent): + messages = [{"role": "user", "content": "hello"}] + try: + response = await conversable_agent.a_generate_reply(messages=messages, sender=None) + assert response is not None, "Response should not be None" + except AssertionError as e: + pytest.fail(f"Unexpected AssertionError: {e}") + except Exception as e: + pytest.fail(f"Unexpected exception: {e}") + + def test_update_function_signature_and_register_functions() -> None: with pytest.MonkeyPatch.context() as mp: mp.setenv("OPENAI_API_KEY", MOCK_OPEN_AI_API_KEY)