Skip to content

Commit b8cb91c

Browse files
committed
refactor(llamabot/components/tools.py)🔧: Refactor write_and_execute_code to add detailed error handling and return code with result
- Add try-except blocks to catch and return specific syntax, name, import, and execution errors during parsing, compilation, and execution phases. - Return a dictionary containing both the executed code and the function result instead of just the result. - Update tests to assert the returned dictionary structure with 'code' and 'result' keys.
1 parent 9392bd1 commit b8cb91c

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

‎llamabot/components/tools.py‎

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -632,45 +632,43 @@ def filter_employees(min_age, department):
632632
"""
633633

634634
# Parse the code to extract the function name
635-
# try:
636-
tree = ast.parse(placeholder_function)
637-
function_name = None
638-
for node in ast.walk(tree):
639-
if isinstance(node, ast.FunctionDef):
640-
function_name = node.name
641-
break
635+
try:
636+
tree = ast.parse(placeholder_function)
637+
function_name = None
638+
for node in ast.walk(tree):
639+
if isinstance(node, ast.FunctionDef):
640+
function_name = node.name
641+
break
642+
643+
if function_name is None:
644+
return "Code validation error: No function definition found in the code"
645+
646+
except SyntaxError as e:
647+
return f"Syntax error in the provided code: {str(e)}"
648+
except Exception as e:
649+
return f"Unexpected error parsing function name: {str(e)}"
650+
651+
try:
652+
ns = globals_dict
653+
compiled = compile(placeholder_function, "<llm>", "exec")
654+
exec(compiled, globals_dict, ns)
655+
except SyntaxError as e:
656+
return f"Syntax error during compilation: {str(e)}"
657+
except NameError as e:
658+
return f"Name error during execution: {str(e)}"
659+
except ImportError as e:
660+
return f"Import error during execution: {str(e)}"
661+
except Exception as e:
662+
return f"Error during code execution: {str(e)}"
642663

643-
if function_name is None:
644-
raise ValueError("No function definition found in the code")
645-
646-
# except SyntaxError as e:
647-
# return f"Syntax error in the provided code: {str(e)}"
648-
# except ValueError as e:
649-
# return f"Code validation error: {str(e)}"
650-
# except Exception as e:
651-
# return f"Unexpected error parsing function name: {str(e)}"
652-
653-
# try:
654-
ns = globals_dict
655-
compiled = compile(placeholder_function, "<llm>", "exec")
656-
exec(compiled, globals_dict, ns)
657-
# except SyntaxError as e:
658-
# return f"Syntax error during compilation: {str(e)}"
659-
# except NameError as e:
660-
# return f"Name error during execution: {str(e)}"
661-
# except ImportError as e:
662-
# return f"Import error during execution: {str(e)}"
663-
# except Exception as e:
664-
# return f"Error during code execution: {str(e)}"
665-
666-
# try:
667-
result = ns[function_name](**keyword_args)
668-
return {"code": placeholder_function, "result": result}
669-
# except KeyError:
670-
# return f"Function '{function_name}' not found in compiled namespace"
671-
# except TypeError as e:
672-
# return f"Type error calling function '{function_name}': {str(e)}"
673-
# except Exception as e:
674-
# return f"Error executing function '{function_name}': {str(e)}"
664+
try:
665+
result = ns[function_name](**keyword_args)
666+
return {"code": placeholder_function, "result": result}
667+
except KeyError:
668+
return f"Function '{function_name}' not found in compiled namespace"
669+
except TypeError as e:
670+
return f"Type error calling function '{function_name}': {str(e)}"
671+
except Exception as e:
672+
return f"Error executing function '{function_name}': {str(e)}"
675673

676674
return write_and_execute_code_wrapper

‎tests/bot/test_toolbot.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def test_function():
4747
wrapper = write_and_execute_code(globals_dict)
4848
result = wrapper(valid_code, {})
4949

50-
assert result == "Hello, World!"
50+
assert result["result"] == "Hello, World!"
51+
assert result["code"] == valid_code
5152

5253

5354
def test_write_and_execute_code_accesses_globals():
@@ -63,7 +64,8 @@ def test_function():
6364
wrapper = write_and_execute_code(globals_dict)
6465
result = wrapper(code_with_global, {})
6566

66-
assert result == 3
67+
assert result["result"] == 3
68+
assert result["code"] == code_with_global
6769

6870

6971
def test_toolbot_initialization():

0 commit comments

Comments
 (0)