Async implementation of run_evolution
(
initial_program: Union[str, Path, List[str]],
evaluator: Union[str, Path, Callable],
config: Union[str, Path, Config, None],
iterations: Optional[int],
output_dir: Optional[str],
cleanup: bool,
)
| 95 | |
| 96 | |
| 97 | async def _run_evolution_async( |
| 98 | initial_program: Union[str, Path, List[str]], |
| 99 | evaluator: Union[str, Path, Callable], |
| 100 | config: Union[str, Path, Config, None], |
| 101 | iterations: Optional[int], |
| 102 | output_dir: Optional[str], |
| 103 | cleanup: bool, |
| 104 | ) -> EvolutionResult: |
| 105 | """Async implementation of run_evolution""" |
| 106 | |
| 107 | temp_dir = None |
| 108 | temp_files = [] |
| 109 | |
| 110 | try: |
| 111 | # Handle configuration |
| 112 | if config is None: |
| 113 | config_obj = Config() |
| 114 | elif isinstance(config, Config): |
| 115 | config_obj = config |
| 116 | else: |
| 117 | config_obj = load_config(str(config)) |
| 118 | |
| 119 | # Validate that LLM models are configured |
| 120 | if not config_obj.llm.models: |
| 121 | raise ValueError( |
| 122 | "No LLM models configured. Please provide a config with LLM models, or set up " |
| 123 | "your configuration with models. For example:\n\n" |
| 124 | "from openevolve.config import Config, LLMModelConfig\n" |
| 125 | "config = Config()\n" |
| 126 | "config.llm.models = [LLMModelConfig(name='gpt-4', api_key='your-key')]\n" |
| 127 | "result = run_evolution(program, evaluator, config=config)" |
| 128 | ) |
| 129 | |
| 130 | # Set up output directory |
| 131 | if output_dir is None and cleanup: |
| 132 | temp_dir = tempfile.mkdtemp(prefix="openevolve_") |
| 133 | actual_output_dir = temp_dir |
| 134 | else: |
| 135 | actual_output_dir = output_dir or "openevolve_output" |
| 136 | os.makedirs(actual_output_dir, exist_ok=True) |
| 137 | |
| 138 | # Process initial program |
| 139 | program_path = _prepare_program(initial_program, temp_dir, temp_files) |
| 140 | |
| 141 | # Process evaluator |
| 142 | evaluator_path = _prepare_evaluator(evaluator, temp_dir, temp_files) |
| 143 | |
| 144 | # Auto-disable cascade evaluation if the evaluator doesn't define stage functions |
| 145 | if config_obj.evaluator.cascade_evaluation: |
| 146 | with open(evaluator_path, "r") as f: |
| 147 | eval_content = f.read() |
| 148 | if "evaluate_stage1" not in eval_content: |
| 149 | config_obj.evaluator.cascade_evaluation = False |
| 150 | |
| 151 | # Create and run controller |
| 152 | controller = OpenEvolve( |
| 153 | initial_program_path=program_path, |
| 154 | evaluation_file=evaluator_path, |
no test coverage detected