Skip to content

Commit

Permalink
Allow None for sender field in CoversableAgent.generate_reply (#1725)
Browse files Browse the repository at this point in the history
* Allow None for sender field

* Apply formatting fixes

* Add test for ConversableAgent generate_reply with message is provided and sender is None

---------

Co-authored-by: Chi Wang <[email protected]>
  • Loading branch information
RohitRathore1 and sonichi authored Feb 21, 2024
1 parent a4ab4cc commit 3c2c251
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
24 changes: 13 additions & 11 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/agentchat/test_conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3c2c251

Please sign in to comment.