Skip to content

Commit fecdfe6

Browse files
afourneydavorrunje
andauthored
Change last_n_messages to default to 'auto' (microsoft#1356)
* Change last_n_messages to default to 'auto' * Added last_n_messages validation, as per Eric's comment. --------- Co-authored-by: Davor Runje <[email protected]>
1 parent df3c89d commit fecdfe6

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

autogen/agentchat/conversable_agent.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def __init__(
107107
If False, the code will be executed in the current environment.
108108
We strongly recommend using docker for code execution.
109109
- timeout (Optional, int): The maximum execution time in seconds.
110-
- last_n_messages (Experimental, Optional, int or str): The number of messages to look back for code execution. Default to 1. If set to 'auto', it will scan backwards through all messages arriving since the agent last spoke (typically this is the last time execution was attempted).
110+
- last_n_messages (Experimental, Optional, int or str): The number of messages to look back for code execution. If set to 'auto', it will scan backwards through all messages arriving since the agent last spoke, which is typically the last time execution was attempted. (Default: auto)
111111
llm_config (dict or False): llm inference configuration.
112112
Please refer to [OpenAIWrapper.create](/docs/reference/oai/client#create)
113113
for available options.
@@ -839,7 +839,10 @@ def generate_code_execution_reply(
839839
return False, None
840840
if messages is None:
841841
messages = self._oai_messages[sender]
842-
last_n_messages = code_execution_config.pop("last_n_messages", 1)
842+
last_n_messages = code_execution_config.pop("last_n_messages", "auto")
843+
844+
if not (isinstance(last_n_messages, (int, float)) and last_n_messages >= 0) and last_n_messages != "auto":
845+
raise ValueError("last_n_messages must be either a non-negative integer, or the string 'auto'.")
843846

844847
messages_to_scan = last_n_messages
845848
if last_n_messages == "auto":

test/agentchat/test_conversable_agent.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,15 @@ def test_generate_code_execution_reply():
323323
)
324324
assert agent._code_execution_config["last_n_messages"] == "auto"
325325

326+
# scenario 8: if last_n_messages is misconfigures, we expect to see an error
327+
with pytest.raises(ValueError):
328+
agent._code_execution_config = {"last_n_messages": -1, "use_docker": False}
329+
agent.generate_code_execution_reply([code_message])
330+
331+
with pytest.raises(ValueError):
332+
agent._code_execution_config = {"last_n_messages": "hello world", "use_docker": False}
333+
agent.generate_code_execution_reply([code_message])
334+
326335

327336
def test_max_consecutive_auto_reply():
328337
agent = ConversableAgent("a0", max_consecutive_auto_reply=2, llm_config=False, human_input_mode="NEVER")
@@ -987,6 +996,6 @@ def test_no_llm_config():
987996
# test_trigger()
988997
# test_context()
989998
# test_max_consecutive_auto_reply()
990-
# test_generate_code_execution_reply()
999+
test_generate_code_execution_reply()
9911000
# test_conversable_agent()
992-
test_no_llm_config()
1001+
# test_no_llm_config()

0 commit comments

Comments
 (0)