@@ -140,7 +140,17 @@ def execute(self, state: dict) -> dict:
140
140
141
141
def overall_reasoning_loop (self , state : dict ) -> dict :
142
142
"""
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.
144
154
"""
145
155
self .logger .info (f"--- (Generating Code) ---" )
146
156
state ["generated_code" ] = self .generate_initial_code (state )
@@ -166,7 +176,8 @@ def overall_reasoning_loop(self, state: dict) -> dict:
166
176
if state ["errors" ]["validation" ]:
167
177
continue
168
178
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) ---""" )
170
181
state = self .semantic_comparison_loop (state )
171
182
if state ["errors" ]["semantic" ]:
172
183
continue
@@ -183,7 +194,13 @@ def overall_reasoning_loop(self, state: dict) -> dict:
183
194
184
195
def syntax_reasoning_loop (self , state : dict ) -> dict :
185
196
"""
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.
187
204
"""
188
205
for _ in range (self .max_iterations ["syntax" ]):
189
206
syntax_valid , syntax_message = self .syntax_check (state ["generated_code" ])
@@ -203,10 +220,17 @@ def syntax_reasoning_loop(self, state: dict) -> dict:
203
220
204
221
def execution_reasoning_loop (self , state : dict ) -> dict :
205
222
"""
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.
207
230
"""
208
231
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" ])
210
234
if execution_success :
211
235
state ["execution_result" ] = execution_result
212
236
state ["errors" ]["execution" ] = []
@@ -222,6 +246,16 @@ def execution_reasoning_loop(self, state: dict) -> dict:
222
246
return state
223
247
224
248
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
+ """
225
259
for _ in range (self .max_iterations ["validation" ]):
226
260
validation , errors = self .validate_dict (state ["execution_result" ],
227
261
self .output_schema .schema ())
@@ -232,12 +266,24 @@ def validation_reasoning_loop(self, state: dict) -> dict:
232
266
state ["errors" ]["validation" ] = errors
233
267
self .logger .info (f"--- (Code Output not compliant to the deisred Output Schema) ---" )
234
268
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 )
237
273
state ["generated_code" ] = extract_code (state ["generated_code" ])
238
274
return state
239
275
240
276
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
+ """
241
287
for _ in range (self .max_iterations ["semantic" ]):
242
288
comparison_result = self .semantic_comparison (state ["execution_result" ],
243
289
state ["reference_answer" ])
@@ -246,16 +292,25 @@ def semantic_comparison_loop(self, state: dict) -> dict:
246
292
return state
247
293
248
294
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) ---""" )
250
297
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 )
253
302
state ["generated_code" ] = extract_code (state ["generated_code" ])
254
303
return state
255
304
256
305
def generate_initial_code (self , state : dict ) -> str :
257
306
"""
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.
259
314
"""
260
315
prompt = PromptTemplate (
261
316
template = TEMPLATE_INIT_CODE_GENERATION ,
@@ -275,7 +330,15 @@ def generate_initial_code(self, state: dict) -> str:
275
330
276
331
def semantic_comparison (self , generated_result : Any , reference_result : Any ) -> Dict [str , Any ]:
277
332
"""
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.
279
342
"""
280
343
reference_result_dict = self .output_schema (** reference_result ).dict ()
281
344
if are_content_equal (generated_result , reference_result_dict ):
@@ -312,7 +375,13 @@ def semantic_comparison(self, generated_result: Any, reference_result: Any) -> D
312
375
313
376
def syntax_check (self , code ):
314
377
"""
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.
316
385
"""
317
386
try :
318
387
ast .parse (code )
@@ -322,7 +391,14 @@ def syntax_check(self, code):
322
391
323
392
def create_sandbox_and_execute (self , function_code ):
324
393
"""
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.
326
402
"""
327
403
sandbox_globals = {
328
404
'BeautifulSoup' : BeautifulSoup ,
@@ -350,7 +426,15 @@ def create_sandbox_and_execute(self, function_code):
350
426
351
427
def validate_dict (self , data : dict , schema ):
352
428
"""
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.
354
438
"""
355
439
try :
356
440
validate (instance = data , schema = schema )
0 commit comments