@@ -632,45 +632,43 @@ def filter_employees(min_age, department):
632
632
"""
633
633
634
634
# 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 )} "
642
663
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 )} "
675
673
676
674
return write_and_execute_code_wrapper
0 commit comments