File size: 4,147 Bytes
5374a2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import evoagentx.workflow.operators as operator
import examples.aflow.mbpp.optimized.round_2.prompt as prompt_custom
from evoagentx.models.model_configs import LLMConfig
from evoagentx.benchmark.benchmark import Benchmark
from evoagentx.models.model_utils import create_llm_instance

class Workflow:

    def __init__(
        self,
        name: str,
        llm_config: LLMConfig,
        benchmark: Benchmark
    ):
        self.name = name
        self.llm = create_llm_instance(llm_config)
        self.benchmark = benchmark 
        self.custom = operator.Custom(self.llm)
        self.custom_code_generate = operator.CustomCodeGenerate(self.llm)
        self.test = operator.Test(self.llm)  # Initialize the Test operator
        self.ensemble = operator.ScEnsemble(self.llm)  # Initialize the self-consistency operator

    async def __call__(self, problem: str, entry_point: str):
        """
        Implementation of the workflow
        Custom operator to generate anything you want.
        But when you want to get standard code, you should use custom_code_generate operator.
        """
        # Generate the first solution considering feedback
        solution = await self.custom_code_generate(problem=problem, entry_point=entry_point, instruction=prompt_custom.GENERATE_PYTHON_CODE_PROMPT) 
        # Testing the generated solution
        test_result = await self.test(problem=problem, solution=solution['response'], entry_point=entry_point, benchmark=self.benchmark)  
        
        if not test_result['result']:
            unique_solutions = set()  # Use a set to ensure uniqueness
            while len(unique_solutions) < 3:  # Attempt to generate three unique fallback solutions
                feedback = f"Last solution failed: {test_result['solution']}.\nPrevious errors: {test_result['error_logs']}."
                fallback_solution = await self.custom_code_generate(problem=problem, entry_point=entry_point, instruction=prompt_custom.GENERATE_PYTHON_CODE_WITH_FEEDBACK_PROMPT + feedback)
                
                # Introduced a check to avoid duplicates
                if fallback_solution['response'] not in unique_solutions:
                    unique_solutions.add(fallback_solution['response'])
            
            # Test all unique fallback solutions simultaneously
            fallback_results = await asyncio.gather(*(self.test(problem=problem, solution=fallback, entry_point=entry_point, benchmark=self.benchmark) for fallback in unique_solutions))
            valid_fallbacks = [res['solution'] for res in fallback_results if res['result']]
            
            if valid_fallbacks:
                # Adding custom validation for fallback solutions with integrated feedback mechanism
                final_fallback = await self.custom(input=problem + f" Verify this solution: {valid_fallbacks[0]}.", instruction=prompt_custom.VERIFY_SOLUTION_PROMPT)
                return final_fallback['response']  # Return the validated fallback solution

            # Secondary fallback generation for enhanced robustness
            secondary_fallback_solution = await self.custom_code_generate(problem=problem, entry_point=entry_point, instruction=prompt_custom.GENERATE_PYTHON_CODE_PROMPT)
            secondary_test_result = await self.test(problem=problem, solution=secondary_fallback_solution['response'], entry_point=entry_point, benchmark=self.benchmark)
            if secondary_test_result['result']:
                return secondary_test_result['solution']  # Return the alternative validated fallback solution

            # Generate an additional unique solution without document validation before ensemble
            additional_solution = await self.custom_code_generate(problem=problem, entry_point=entry_point, instruction=prompt_custom.GENERATE_PYTHON_CODE_PROMPT)
            ensemble_result = await self.ensemble(solutions=[solution['response']] + list(unique_solutions) + [additional_solution['response']], problem=problem)
            return ensemble_result['response']  # Return the ensemble decision

        return test_result['solution']  # Return the verified solution