|
| 1 | +import logging |
| 2 | +import traceback |
| 3 | +from typing import Any, List |
| 4 | +from ...helpers.code_manager import CodeExecutionContext |
| 5 | +from ...helpers.logger import Logger |
| 6 | +from ..base_logic_unit import BaseLogicUnit |
| 7 | +from ..pipeline_context import PipelineContext |
| 8 | +from ...prompts.correct_error_prompt import CorrectErrorPrompt |
| 9 | + |
| 10 | + |
| 11 | +class CodeExecution(BaseLogicUnit): |
| 12 | + """ |
| 13 | + Code Execution Stage |
| 14 | + """ |
| 15 | + |
| 16 | + pass |
| 17 | + |
| 18 | + def execute(self, input: Any, **kwargs) -> Any: |
| 19 | + """ |
| 20 | + This method will return output according to |
| 21 | + Implementation. |
| 22 | +
|
| 23 | + :param input: Your input data. |
| 24 | + :param kwargs: A dictionary of keyword arguments. |
| 25 | + - 'logger' (any): The logger for logging. |
| 26 | + - 'config' (Config): Global configurations for the test |
| 27 | + - 'context' (any): The execution context. |
| 28 | +
|
| 29 | + :return: The result of the execution. |
| 30 | + """ |
| 31 | + pipeline_context: PipelineContext = kwargs.get("context") |
| 32 | + logger: Logger = kwargs.get("logger") |
| 33 | + |
| 34 | + code = input |
| 35 | + retry_count = 0 |
| 36 | + code_to_run = code |
| 37 | + result = None |
| 38 | + while retry_count < pipeline_context.config.max_retries: |
| 39 | + try: |
| 40 | + # Execute the code |
| 41 | + code_context = CodeExecutionContext( |
| 42 | + pipeline_context.get_intermediate_value("last_prompt_id"), |
| 43 | + pipeline_context.get_intermediate_value("skills"), |
| 44 | + ) |
| 45 | + result = pipeline_context.get_intermediate_value( |
| 46 | + "code_manager" |
| 47 | + ).execute_code( |
| 48 | + code=code_to_run, |
| 49 | + context=code_context, |
| 50 | + ) |
| 51 | + |
| 52 | + break |
| 53 | + |
| 54 | + except Exception as e: |
| 55 | + if ( |
| 56 | + not pipeline_context.config.use_error_correction_framework |
| 57 | + or retry_count >= pipeline_context.config.max_retries - 1 |
| 58 | + ): |
| 59 | + raise e |
| 60 | + |
| 61 | + retry_count += 1 |
| 62 | + |
| 63 | + logger.log( |
| 64 | + f"Failed to execute code with a correction framework " |
| 65 | + f"[retry number: {retry_count}]", |
| 66 | + level=logging.WARNING, |
| 67 | + ) |
| 68 | + |
| 69 | + traceback_error = traceback.format_exc() |
| 70 | + [ |
| 71 | + code_to_run, |
| 72 | + reasoning, |
| 73 | + answer, |
| 74 | + ] = pipeline_context.query_exec_tracker.execute_func( |
| 75 | + self._retry_run_code, |
| 76 | + code, |
| 77 | + pipeline_context, |
| 78 | + logger, |
| 79 | + traceback_error, |
| 80 | + ) |
| 81 | + |
| 82 | + pipeline_context.add_intermediate_value("reasoning", reasoning) |
| 83 | + pipeline_context.add_intermediate_value("answer", answer) |
| 84 | + |
| 85 | + return result |
| 86 | + |
| 87 | + def _retry_run_code( |
| 88 | + self, code: str, context: PipelineContext, logger: Logger, e: Exception |
| 89 | + ) -> List: |
| 90 | + """ |
| 91 | + A method to retry the code execution with error correction framework. |
| 92 | +
|
| 93 | + Args: |
| 94 | + code (str): A python code |
| 95 | + context (PipelineContext) : Pipeline Context |
| 96 | + logger (Logger) : Logger |
| 97 | + e (Exception): An exception |
| 98 | + dataframes |
| 99 | +
|
| 100 | + Returns (str): A python code |
| 101 | + """ |
| 102 | + |
| 103 | + logger.log(f"Failed with error: {e}. Retrying", logging.ERROR) |
| 104 | + |
| 105 | + default_values = { |
| 106 | + "engine": context.dfs[0].engine, |
| 107 | + "code": code, |
| 108 | + "error_returned": e, |
| 109 | + } |
| 110 | + error_correcting_instruction = context.get_intermediate_value("get_prompt")( |
| 111 | + "correct_error", |
| 112 | + default_prompt=CorrectErrorPrompt(), |
| 113 | + default_values=default_values, |
| 114 | + ) |
| 115 | + |
| 116 | + result = context.config.llm.generate_code(error_correcting_instruction) |
| 117 | + if context.config.callback is not None: |
| 118 | + context.config.callback.on_code(result[0]) |
| 119 | + |
| 120 | + return result |
0 commit comments