@@ -140,7 +140,17 @@ def execute(self, state: dict) -> dict:
140140
141141 def overall_reasoning_loop (self , state : dict ) -> dict :
142142 """
143- overrall_reasoning_loop
143+ Executes the overall reasoning loop to generate and validate the code.
144+
145+ Args:
146+ state (dict): The current state of the reasoning process.
147+
148+ Returns:
149+ dict: The final state after the reasoning loop.
150+
151+ Raises:
152+ RuntimeError: If the maximum number of iterations
153+ is reached without obtaining the desired code.
144154 """
145155 self .logger .info (f"--- (Generating Code) ---" )
146156 state ["generated_code" ] = self .generate_initial_code (state )
@@ -166,7 +176,8 @@ def overall_reasoning_loop(self, state: dict) -> dict:
166176 if state ["errors" ]["validation" ]:
167177 continue
168178
169- self .logger .info (f"--- (Checking if the informations exctrcated are the ones Requested) ---" )
179+ self .logger .info (f"""--- (Checking if the informations
180+ exctrcated are the ones Requested) ---""" )
170181 state = self .semantic_comparison_loop (state )
171182 if state ["errors" ]["semantic" ]:
172183 continue
@@ -183,7 +194,13 @@ def overall_reasoning_loop(self, state: dict) -> dict:
183194
184195 def syntax_reasoning_loop (self , state : dict ) -> dict :
185196 """
186- syntax reasoning loop
197+ Executes the syntax reasoning loop to ensure the generated code has correct syntax.
198+
199+ Args:
200+ state (dict): The current state of the reasoning process.
201+
202+ Returns:
203+ dict: The updated state after the syntax reasoning loop.
187204 """
188205 for _ in range (self .max_iterations ["syntax" ]):
189206 syntax_valid , syntax_message = self .syntax_check (state ["generated_code" ])
@@ -203,10 +220,17 @@ def syntax_reasoning_loop(self, state: dict) -> dict:
203220
204221 def execution_reasoning_loop (self , state : dict ) -> dict :
205222 """
206- execution of the reasoning loop
223+ Executes the execution reasoning loop to ensure the generated code runs without errors.
224+
225+ Args:
226+ state (dict): The current state of the reasoning process.
227+
228+ Returns:
229+ dict: The updated state after the execution reasoning loop.
207230 """
208231 for _ in range (self .max_iterations ["execution" ]):
209- execution_success , execution_result = self .create_sandbox_and_execute (state ["generated_code" ])
232+ execution_success , execution_result = self .create_sandbox_and_execute (
233+ state ["generated_code" ])
210234 if execution_success :
211235 state ["execution_result" ] = execution_result
212236 state ["errors" ]["execution" ] = []
@@ -222,6 +246,16 @@ def execution_reasoning_loop(self, state: dict) -> dict:
222246 return state
223247
224248 def validation_reasoning_loop (self , state : dict ) -> dict :
249+ """
250+ Executes the validation reasoning loop to ensure the
251+ generated code's output matches the desired schema.
252+
253+ Args:
254+ state (dict): The current state of the reasoning process.
255+
256+ Returns:
257+ dict: The updated state after the validation reasoning loop.
258+ """
225259 for _ in range (self .max_iterations ["validation" ]):
226260 validation , errors = self .validate_dict (state ["execution_result" ],
227261 self .output_schema .schema ())
@@ -232,12 +266,24 @@ def validation_reasoning_loop(self, state: dict) -> dict:
232266 state ["errors" ]["validation" ] = errors
233267 self .logger .info (f"--- (Code Output not compliant to the deisred Output Schema) ---" )
234268 analysis = validation_focused_analysis (state , self .llm_model )
235- self .logger .info (f"--- (Regenerating Code to make the Output compliant to the deisred Output Schema) ---" )
236- state ["generated_code" ] = validation_focused_code_generation (state , analysis , self .llm_model )
269+ self .logger .info (f"""--- (Regenerating Code to make the
270+ Output compliant to the deisred Output Schema) ---""" )
271+ state ["generated_code" ] = validation_focused_code_generation (state ,
272+ analysis , self .llm_model )
237273 state ["generated_code" ] = extract_code (state ["generated_code" ])
238274 return state
239275
240276 def semantic_comparison_loop (self , state : dict ) -> dict :
277+ """
278+ Executes the semantic comparison loop to ensure the generated code's
279+ output is semantically equivalent to the reference answer.
280+
281+ Args:
282+ state (dict): The current state of the reasoning process.
283+
284+ Returns:
285+ dict: The updated state after the semantic comparison loop.
286+ """
241287 for _ in range (self .max_iterations ["semantic" ]):
242288 comparison_result = self .semantic_comparison (state ["execution_result" ],
243289 state ["reference_answer" ])
@@ -246,16 +292,25 @@ def semantic_comparison_loop(self, state: dict) -> dict:
246292 return state
247293
248294 state ["errors" ]["semantic" ] = comparison_result ["differences" ]
249- self .logger .info (f"--- (The informations exctrcated are not the all ones requested) ---" )
295+ self .logger .info (f"""--- (The informations exctrcated
296+ are not the all ones requested) ---""" )
250297 analysis = semantic_focused_analysis (state , comparison_result , self .llm_model )
251- self .logger .info (f"--- (Regenerating Code to obtain all the infromation requested) ---" )
252- state ["generated_code" ] = semantic_focused_code_generation (state , analysis , self .llm_model )
298+ self .logger .info (f"""--- (Regenerating Code to
299+ obtain all the infromation requested) ---""" )
300+ state ["generated_code" ] = semantic_focused_code_generation (state ,
301+ analysis , self .llm_model )
253302 state ["generated_code" ] = extract_code (state ["generated_code" ])
254303 return state
255304
256305 def generate_initial_code (self , state : dict ) -> str :
257306 """
258- function for generating the initial code
307+ Generates the initial code based on the provided state.
308+
309+ Args:
310+ state (dict): The current state of the reasoning process.
311+
312+ Returns:
313+ str: The initially generated code.
259314 """
260315 prompt = PromptTemplate (
261316 template = TEMPLATE_INIT_CODE_GENERATION ,
@@ -275,7 +330,15 @@ def generate_initial_code(self, state: dict) -> str:
275330
276331 def semantic_comparison (self , generated_result : Any , reference_result : Any ) -> Dict [str , Any ]:
277332 """
278- semtantic comparison formula
333+ Performs a semantic comparison between the generated result and the reference result.
334+
335+ Args:
336+ generated_result (Any): The result generated by the code.
337+ reference_result (Any): The reference result for comparison.
338+
339+ Returns:
340+ Dict[str, Any]: A dictionary containing the comparison result,
341+ differences, and explanation.
279342 """
280343 reference_result_dict = self .output_schema (** reference_result ).dict ()
281344 if are_content_equal (generated_result , reference_result_dict ):
@@ -312,7 +375,13 @@ def semantic_comparison(self, generated_result: Any, reference_result: Any) -> D
312375
313376 def syntax_check (self , code ):
314377 """
315- syntax checker
378+ Checks the syntax of the provided code.
379+
380+ Args:
381+ code (str): The code to be checked for syntax errors.
382+
383+ Returns:
384+ tuple: A tuple containing a boolean indicating if the syntax is correct and a message.
316385 """
317386 try :
318387 ast .parse (code )
@@ -322,7 +391,14 @@ def syntax_check(self, code):
322391
323392 def create_sandbox_and_execute (self , function_code ):
324393 """
325- Create a sandbox environment
394+ Creates a sandbox environment and executes the provided function code.
395+
396+ Args:
397+ function_code (str): The code to be executed in the sandbox.
398+
399+ Returns:
400+ tuple: A tuple containing a boolean indicating if
401+ the execution was successful and the result or error message.
326402 """
327403 sandbox_globals = {
328404 'BeautifulSoup' : BeautifulSoup ,
@@ -350,7 +426,15 @@ def create_sandbox_and_execute(self, function_code):
350426
351427 def validate_dict (self , data : dict , schema ):
352428 """
353- validate_dict method
429+ Validates the provided data against the given schema.
430+
431+ Args:
432+ data (dict): The data to be validated.
433+ schema (dict): The schema against which the data is validated.
434+
435+ Returns:
436+ tuple: A tuple containing a boolean indicating
437+ if the validation was successful and a list of errors if any.
354438 """
355439 try :
356440 validate (instance = data , schema = schema )
0 commit comments