26
26
from .base_node import BaseNode
27
27
from jsonschema import validate , ValidationError
28
28
29
-
30
29
class GenerateCodeNode (BaseNode ):
31
30
"""
32
31
A node that generates Python code for a function that extracts data
@@ -96,7 +95,7 @@ def execute(self, state: dict) -> dict:
96
95
Raises:
97
96
KeyError: If the input keys are not found in the state, indicating
98
97
that the necessary information for generating an answer is missing.
99
- RuntimeError: If the maximum number of iterations is
98
+ RuntimeError: If the maximum number of iterations is
100
99
reached without obtaining the desired code.
101
100
"""
102
101
@@ -170,7 +169,7 @@ def overall_reasoning_loop(self, state: dict) -> dict:
170
169
self .logger .info (f"--- (Checking if the informations exctrcated are the ones Requested) ---" )
171
170
state = self .semantic_comparison_loop (state )
172
171
if state ["errors" ]["semantic" ]:
173
- continue
172
+ continue
174
173
break
175
174
176
175
if state ["iteration" ] == self .max_iterations ["overall" ] and \
@@ -195,9 +194,9 @@ def syntax_reasoning_loop(self, state: dict) -> dict:
195
194
state ["errors" ]["syntax" ] = [syntax_message ]
196
195
self .logger .info (f"--- (Synax Error Found: { syntax_message } ) ---" )
197
196
analysis = syntax_focused_analysis (state , self .llm_model )
198
- self .logger .info (f"""--- (Regenerating Code
197
+ self .logger .info (f"""--- (Regenerating Code
199
198
to fix the Error) ---""" )
200
- state ["generated_code" ] = syntax_focused_code_generation (state ,
199
+ state ["generated_code" ] = syntax_focused_code_generation (state ,
201
200
analysis , self .llm_model )
202
201
state ["generated_code" ] = extract_code (state ["generated_code" ])
203
202
return state
@@ -217,14 +216,14 @@ def execution_reasoning_loop(self, state: dict) -> dict:
217
216
self .logger .info (f"--- (Code Execution Error: { execution_result } ) ---" )
218
217
analysis = execution_focused_analysis (state , self .llm_model )
219
218
self .logger .info (f"--- (Regenerating Code to fix the Error) ---" )
220
- state ["generated_code" ] = execution_focused_code_generation (state ,
219
+ state ["generated_code" ] = execution_focused_code_generation (state ,
221
220
analysis , self .llm_model )
222
221
state ["generated_code" ] = extract_code (state ["generated_code" ])
223
222
return state
224
223
225
224
def validation_reasoning_loop (self , state : dict ) -> dict :
226
225
for _ in range (self .max_iterations ["validation" ]):
227
- validation , errors = self .validate_dict (state ["execution_result" ],
226
+ validation , errors = self .validate_dict (state ["execution_result" ],
228
227
self .output_schema .schema ())
229
228
if validation :
230
229
state ["errors" ]["validation" ] = []
@@ -240,7 +239,7 @@ def validation_reasoning_loop(self, state: dict) -> dict:
240
239
241
240
def semantic_comparison_loop (self , state : dict ) -> dict :
242
241
for _ in range (self .max_iterations ["semantic" ]):
243
- comparison_result = self .semantic_comparison (state ["execution_result" ],
242
+ comparison_result = self .semantic_comparison (state ["execution_result" ],
244
243
state ["reference_answer" ])
245
244
if comparison_result ["are_semantically_equivalent" ]:
246
245
state ["errors" ]["semantic" ] = []
@@ -342,7 +341,7 @@ def create_sandbox_and_execute(self, function_code):
342
341
if not extract_data :
343
342
raise NameError ("Function 'extract_data' not found in the generated code." )
344
343
345
- result = extract_data (self .raw_html )
344
+ result = extract_data (self .raw_html )
346
345
return True , result
347
346
except Exception as e :
348
347
return False , f"Error during execution: { str (e )} "
@@ -357,5 +356,5 @@ def validate_dict(self, data: dict, schema):
357
356
validate (instance = data , schema = schema )
358
357
return True , None
359
358
except ValidationError as e :
360
- errors = e . errors ()
359
+ errors = [ e . message ]
361
360
return False , errors
0 commit comments