Asynchronous implementation of the run_evolution function.
(
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,
)
| 62 | |
| 63 | |
| 64 | async def _run_evolution_async( |
| 65 | initial_program: Union[str, Path, List[str]], |
| 66 | evaluator: Union[str, Path, Callable], |
| 67 | config: Union[str, Path, Config, None], |
| 68 | iterations: Optional[int], |
| 69 | output_dir: Optional[str], |
| 70 | cleanup: bool, |
| 71 | ) -> EvolutionResult: |
| 72 | """Asynchronous implementation of the run_evolution function.""" |
| 73 | |
| 74 | temp_dir = None |
| 75 | temp_files = [] |
| 76 | |
| 77 | try: |
| 78 | # --- Handle Configuration --- |
| 79 | if config is None: |
| 80 | # If no config is provided, create a default one. |
| 81 | config_obj = Config() |
| 82 | elif isinstance(config, Config): |
| 83 | # If a Config object is provided, use it directly. |
| 84 | config_obj = config |
| 85 | else: |
| 86 | # If a path is provided, load the config from the YAML file. |
| 87 | config_obj = load_config(str(config)) |
| 88 | |
| 89 | # Validate that at least one LLM model is configured. |
| 90 | if not config_obj.llm.models: |
| 91 | raise ValueError( |
| 92 | "No LLM models configured. Please provide a config with LLM models, or set up " |
| 93 | "your configuration with models. For example:\n\n" |
| 94 | "from longhorizon.config import Config, LLMModelConfig\n" |
| 95 | "config = Config()\n" |
| 96 | "config.llm.models = [LLMModelConfig(name='gpt-4', api_key='your-key')]\n" |
| 97 | "result = run_evolution(program, evaluator, config=config)" |
| 98 | ) |
| 99 | |
| 100 | # --- Set up Output Directory --- |
| 101 | if output_dir is None and cleanup: |
| 102 | # If no output directory is specified and cleanup is enabled, create a temporary directory. |
| 103 | temp_dir = tempfile.mkdtemp(prefix="longhorizon_") |
| 104 | actual_output_dir = temp_dir |
| 105 | else: |
| 106 | # Otherwise, use the specified directory or a default one. |
| 107 | actual_output_dir = output_dir or "longhorizon_output" |
| 108 | os.makedirs(actual_output_dir, exist_ok=True) |
| 109 | |
| 110 | # --- Process Inputs --- |
| 111 | # Convert the initial_program input into a file path, creating a temp file if necessary. |
| 112 | program_path = _prepare_program(initial_program, temp_dir, temp_files) |
| 113 | # Convert the evaluator input into a file path, creating a temp file if necessary. |
| 114 | evaluator_path = _prepare_evaluator(evaluator, temp_dir, temp_files) |
| 115 | |
| 116 | # --- Run Evolution --- |
| 117 | # Create the main LongHorizon controller. |
| 118 | controller = LongHorizon( |
| 119 | initial_program_path=program_path, |
| 120 | evaluation_file=evaluator_path, |
| 121 | config=config_obj, |
no test coverage detected