Skip to content

Commit 5649a5f

Browse files
committed
feat: implementation
1 parent a7428ef commit 5649a5f

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

openevolve/config.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ class LLMModelConfig:
1919

2020
# API configuration
2121
api_base: str = None
22-
api_key: Optional[str] = None
22+
api_key: Optional[Union[str, Dict[str, str]]] = (
23+
None # either api_key or from_env: api_key_var_name
24+
)
2325
name: str = None
2426

2527
# Custom LLM client
@@ -45,6 +47,20 @@ class LLMModelConfig:
4547
# Reasoning parameters
4648
reasoning_effort: Optional[str] = None
4749

50+
def __post_init__(self):
51+
"""Post-initialization to set up API key"""
52+
if self.api_key is not None:
53+
if isinstance(self.api_key, dict):
54+
env_var_name = self.api_key.get("from_env")
55+
if env_var_name is None:
56+
raise ValueError(
57+
f"api_key is a dict but 'from_env' key is not set: {self.api_key}"
58+
)
59+
key = os.environ.get(env_var_name)
60+
if key is None:
61+
raise ValueError(f"Environment variable {env_var_name} is not set")
62+
self.api_key = key
63+
4864

4965
@dataclass
5066
class LLMConfig(LLMModelConfig):
@@ -81,6 +97,8 @@ class LLMConfig(LLMModelConfig):
8197

8298
def __post_init__(self):
8399
"""Post-initialization to set up model configurations"""
100+
super().__post_init__() # resolve from_env for api_key at LLMConfig level
101+
84102
# Handle backward compatibility for primary_model(_weight) and secondary_model(_weight).
85103
if self.primary_model:
86104
# Create primary model

0 commit comments

Comments
 (0)