A programming framework for agentic AI
Allow None for sender field in `CoversableAgent.generate_reply` (#1725)
Rohit Singh Rathaur3c2c251f3fa8
diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.pyindex 7b7ecaa80..a3726678a 100644--- a/autogen/agentchat/conversable_agent.py+++ b/autogen/agentchat/conversable_agent.py@@ -1395,13 +1395,14 @@ class ConversableAgent(LLMAgent):if config is None:config = selfif 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.nameif 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 @@ class ConversableAgent(LLMAgent):# 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 @@ class ConversableAgent(LLMAgent):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 @@ class ConversableAgent(LLMAgent):if config is None:config = selfif 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.nameif 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 @@ class ConversableAgent(LLMAgent):# 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 @@ class ConversableAgent(LLMAgent):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 conversationdiff --git a/test/agentchat/test_conversable_agent.py b/test/agentchat/test_conversable_agent.pyindex 5b0a6c326..b71a6341c 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_aawait 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}")+++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)