From 5539aa99c27861d9216c994fa568ab3a3085d431 Mon Sep 17 00:00:00 2001 From: Benjamin Ylvisaker Date: Tue, 8 Jul 2025 11:51:43 -0600 Subject: [PATCH] Add the option to use environment variables for api keys in config files with the ${...} syntax --- openevolve/config.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/openevolve/config.py b/openevolve/config.py index c89766d1c..4f6ae6369 100644 --- a/openevolve/config.py +++ b/openevolve/config.py @@ -259,6 +259,11 @@ def from_dict(cls, config_dict: Dict[str, Any]) -> "Config": llm_dict["evaluator_models"] = [ LLMModelConfig(**m) for m in llm_dict["evaluator_models"] ] + api_key = llm_dict.get("api_key", "").strip() + if api_key.startswith("${") and api_key.endswith("}"): + api_key_env = os.environ.get(api_key[2:-1]) + if api_key_env is not None: + llm_dict["api_key"] = api_key_env config.llm = LLMConfig(**llm_dict) if "prompt" in config_dict: config.prompt = PromptConfig(**config_dict["prompt"]) @@ -365,4 +370,4 @@ def load_config(config_path: Optional[Union[str, Path]] = None) -> Config: # Make the system message available to the individual models, in case it is not provided from the prompt sampler config.llm.update_model_params({"system_message": config.prompt.system_message}) - return config \ No newline at end of file + return config