Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an 'auto' mode to last_n_messages #693

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def __init__(
When set to True, a default list will be used.
We strongly recommend using docker for code execution.
- timeout (Optional, int): The maximum execution time in seconds.
- last_n_messages (Experimental, Optional, int): The number of messages to look back for code execution. Default to 1.
- 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).
llm_config (dict or False): llm inference configuration.
Please refer to [OpenAIWrapper.create](/docs/reference/oai/client#create)
for available options.
Expand Down Expand Up @@ -635,10 +635,23 @@ def generate_code_execution_reply(
messages = self._oai_messages[sender]
last_n_messages = code_execution_config.pop("last_n_messages", 1)

messages_to_scan = last_n_messages
if last_n_messages == "auto":
# Find when the agent last spoke
messages_to_scan = 0
for i in range(len(messages)):
message = messages[-(i + 1)]
if "role" not in message:
break
elif message["role"] != "user":
break
else:
messages_to_scan += 1

# iterate through the last n messages reversly
# if code blocks are found, execute the code blocks and return the output
# if no code blocks are found, continue
for i in range(min(len(messages), last_n_messages)):
for i in range(min(len(messages), messages_to_scan)):
message = messages[-(i + 1)]
if not message["content"]:
continue
Expand Down
Loading