Fix issues with execute_python_code responses (#4738)

Co-authored-by: merwanehamadi <merwanehamadi@gmail.com>
pull/4746/head
Erik Peterson 2023-06-18 20:30:08 -07:00 committed by GitHub
parent ee7d04775e
commit 9f737274b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 6 deletions

View File

@ -32,13 +32,13 @@ DENYLIST_CONTROL = "denylist"
},
},
)
def execute_python_code(code: str, basename: str, agent: Agent) -> str:
def execute_python_code(code: str, name: str, agent: Agent) -> str:
"""Create and execute a Python file in a Docker container and return the STDOUT of the
executed code. If there is any data that needs to be captured use a print statement
Args:
code (str): The Python code to run
basename (str): A name to be given to the Python file
name (str): A name to be given to the Python file
Returns:
str: The STDOUT captured from the code when it ran
@ -47,10 +47,10 @@ def execute_python_code(code: str, basename: str, agent: Agent) -> str:
directory = os.path.join(agent.config.workspace_path, ai_name, "executed_code")
os.makedirs(directory, exist_ok=True)
if not basename.endswith(".py"):
basename = basename + ".py"
if not name.endswith(".py"):
name = name + ".py"
path = os.path.join(directory, basename)
path = os.path.join(directory, name)
try:
with open(path, "w+", encoding="utf-8") as f:

View File

@ -22,7 +22,8 @@ def extract_json_from_response(response_content: str) -> dict:
try:
return ast.literal_eval(response_content)
except BaseException as e:
logger.error(f"Error parsing JSON response with literal_eval {e}")
logger.info(f"Error parsing JSON response with literal_eval {e}")
logger.debug(f"Invalid JSON received in response: {response_content}")
# TODO: How to raise an error here without causing the program to exit?
return {}