Skip to content

Commit aae7e08

Browse files
Allow None for sender field in CoversableAgent.generate_reply (microsoft#1725)
* 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]>
1 parent 41fefbb commit aae7e08

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

autogen/agentchat/conversable_agent.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -1395,13 +1395,14 @@ def check_termination_and_human_reply(
13951395
if config is None:
13961396
config = self
13971397
if messages is None:
1398-
messages = self._oai_messages[sender]
1398+
messages = self._oai_messages[sender] if sender else []
13991399
message = messages[-1]
14001400
reply = ""
14011401
no_human_input_msg = ""
1402+
sender_name = "the sender" if sender is None else sender.name
14021403
if self.human_input_mode == "ALWAYS":
14031404
reply = self.get_human_input(
1404-
f"Provide feedback to {sender.name}. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: "
1405+
f"Provide feedback to {sender_name}. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: "
14051406
)
14061407
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else ""
14071408
# 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(
14141415
# self.human_input_mode == "TERMINATE":
14151416
terminate = self._is_termination_msg(message)
14161417
reply = self.get_human_input(
1417-
f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: "
1418+
f"Please give feedback to {sender_name}. Press enter or type 'exit' to stop the conversation: "
14181419
if terminate
1419-
else f"Please give feedback to {sender.name}. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: "
1420+
else f"Please give feedback to {sender_name}. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: "
14201421
)
14211422
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else ""
14221423
# 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(
14271428
else:
14281429
# self.human_input_mode == "TERMINATE":
14291430
reply = self.get_human_input(
1430-
f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: "
1431+
f"Please give feedback to {sender_name}. Press enter or type 'exit' to stop the conversation: "
14311432
)
14321433
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else ""
14331434
# 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(
15051506
if config is None:
15061507
config = self
15071508
if messages is None:
1508-
messages = self._oai_messages[sender]
1509-
message = messages[-1]
1509+
messages = self._oai_messages[sender] if sender else []
1510+
message = messages[-1] if messages else {}
15101511
reply = ""
15111512
no_human_input_msg = ""
1513+
sender_name = "the sender" if sender is None else sender.name
15121514
if self.human_input_mode == "ALWAYS":
15131515
reply = await self.a_get_human_input(
1514-
f"Provide feedback to {sender.name}. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: "
1516+
f"Provide feedback to {sender_name}. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: "
15151517
)
15161518
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else ""
15171519
# 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(
15241526
# self.human_input_mode == "TERMINATE":
15251527
terminate = self._is_termination_msg(message)
15261528
reply = await self.a_get_human_input(
1527-
f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: "
1529+
f"Please give feedback to {sender_name}. Press enter or type 'exit' to stop the conversation: "
15281530
if terminate
1529-
else f"Please give feedback to {sender.name}. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: "
1531+
else f"Please give feedback to {sender_name}. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: "
15301532
)
15311533
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else ""
15321534
# 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(
15371539
else:
15381540
# self.human_input_mode == "TERMINATE":
15391541
reply = await self.a_get_human_input(
1540-
f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: "
1542+
f"Please give feedback to {sender_name}. Press enter or type 'exit' to stop the conversation: "
15411543
)
15421544
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else ""
15431545
# if the human input is empty, and the message is a termination message, then we will terminate the conversation

test/agentchat/test_conversable_agent.py

+23
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,29 @@ async def test_a_generate_reply_raises_on_messages_and_sender_none(conversable_a
472472
await conversable_agent.a_generate_reply(messages=None, sender=None)
473473

474474

475+
def test_generate_reply_with_messages_and_sender_none(conversable_agent):
476+
messages = [{"role": "user", "content": "hello"}]
477+
try:
478+
response = conversable_agent.generate_reply(messages=messages, sender=None)
479+
assert response is not None, "Response should not be None"
480+
except AssertionError as e:
481+
pytest.fail(f"Unexpected AssertionError: {e}")
482+
except Exception as e:
483+
pytest.fail(f"Unexpected exception: {e}")
484+
485+
486+
@pytest.mark.asyncio
487+
async def test_a_generate_reply_with_messages_and_sender_none(conversable_agent):
488+
messages = [{"role": "user", "content": "hello"}]
489+
try:
490+
response = await conversable_agent.a_generate_reply(messages=messages, sender=None)
491+
assert response is not None, "Response should not be None"
492+
except AssertionError as e:
493+
pytest.fail(f"Unexpected AssertionError: {e}")
494+
except Exception as e:
495+
pytest.fail(f"Unexpected exception: {e}")
496+
497+
475498
def test_update_function_signature_and_register_functions() -> None:
476499
with pytest.MonkeyPatch.context() as mp:
477500
mp.setenv("OPENAI_API_KEY", MOCK_OPEN_AI_API_KEY)

0 commit comments

Comments
 (0)