agent: python tool: return errors

This commit is contained in:
ochafik 2024-04-10 09:27:25 +01:00
parent f9afb041e2
commit a98f48315c

View file

@ -22,18 +22,23 @@ def execute_python(source: str) -> Union[Dict, str]:
Returns: Returns:
dict | str: A dictionary containing variables declared, or an error message if an exception occurred. dict | str: A dictionary containing variables declared, or an error message if an exception occurred.
""" """
namespace = {} try:
sys.stderr.write(f"Executing Python program:\n{source}\n") namespace = {}
exec(source, namespace) sys.stderr.write(f"Executing Python program:\n{source}\n")
results = { exec(source, namespace)
k: v results = {
for k, v in namespace.items() k: v
if not k.startswith('_') \ for k, v in namespace.items()
and not isinstance(v, type) \ if not k.startswith('_') \
and not isinstance(v, types.ModuleType) \ and not isinstance(v, type) \
and not callable(v) \ and not isinstance(v, types.ModuleType) \
and _is_serializable(v) and not callable(v) \
} and _is_serializable(v)
sys.stderr.write(f"Results: {json.dumps(results, indent=2)}\n") }
sys.stderr.write(f"Results: {json.dumps(results, indent=2)}\n")
return results
except Exception as e:
msg = f"Error: {sys.exc_info()[1]}"
sys.stderr.write(f"{msg}\n")
return msg
return results