diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index fdf42f67b6a3..e086762a419d 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -611,25 +611,26 @@ def generate_code_execution_reply( if messages is None: messages = self._oai_messages[sender] last_n_messages = code_execution_config.pop("last_n_messages", 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)): message = messages[-(i + 1)] code_blocks = extract_code(message["content"]) if len(code_blocks) == 1 and code_blocks[0][0] == UNKNOWN: - # no code block is found, lang should be `UNKNOWN` - - if i == last_n_messages - 1: - code_execution_config["last_n_messages"] = last_n_messages - return False, None continue - # code_blocks, _ = find_code(messages, sys_msg=self._oai_system_message, **self.llm_config) - # if len(code_blocks) == 1 and code_blocks[0][0] == UNKNOWN: - # return code_blocks[0][1] - # try to execute the code + + # found code blocks, push last_n_messages back and try to execute the code + code_execution_config["last_n_messages"] = last_n_messages exitcode, logs = self.execute_code_blocks(code_blocks) exitcode2str = "execution succeeded" if exitcode == 0 else "execution failed" - break + return True, f"exitcode: {exitcode} ({exitcode2str})\nCode output: {logs}" + + # no code blocks are found, push last_n_messages back and return. code_execution_config["last_n_messages"] = last_n_messages - return True, f"exitcode: {exitcode} ({exitcode2str})\nCode output: {logs}" + + return False, None def generate_function_call_reply( self, diff --git a/autogen/code_utils.py b/autogen/code_utils.py index 1a6b970c6427..98a0f44a7966 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -31,7 +31,14 @@ def infer_lang(code): """ if code.startswith("python ") or code.startswith("pip") or code.startswith("python3 "): return "sh" - return "python" + + # check if code is a valid python code + try: + compile(code, "test", "exec") + return "python" + except SyntaxError: + # not a valid python code + return UNKNOWN def extract_code( @@ -252,7 +259,7 @@ def execute_code( file_dir = os.path.dirname(filepath) os.makedirs(file_dir, exist_ok=True) if code is not None: - with open(filepath, "w") as fout: + with open(filepath, "w", encoding="utf-8") as fout: fout.write(code) # check if already running in a docker container in_docker_container = os.path.exists("/.dockerenv")