File size: 6,882 Bytes
5e4510c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""
Tests for api_key ${VAR} environment variable substitution in configuration.
"""

import os
import tempfile
import unittest

from openevolve.config import Config, LLMModelConfig, _resolve_env_var


class TestEnvVarSubstitution(unittest.TestCase):
    """Tests for ${VAR} environment variable substitution in api_key fields"""

    def setUp(self):
        """Set up test environment variables"""
        self.test_env_var = "TEST_OPENEVOLVE_API_KEY"
        self.test_api_key = "test-secret-key-12345"
        os.environ[self.test_env_var] = self.test_api_key

    def tearDown(self):
        """Clean up test environment variables"""
        if self.test_env_var in os.environ:
            del os.environ[self.test_env_var]

    def test_resolve_env_var_with_match(self):
        """Test that _resolve_env_var resolves ${VAR} syntax"""
        result = _resolve_env_var(f"${{{self.test_env_var}}}")
        self.assertEqual(result, self.test_api_key)

    def test_resolve_env_var_no_match(self):
        """Test that strings without ${VAR} are returned unchanged"""
        result = _resolve_env_var("regular-api-key")
        self.assertEqual(result, "regular-api-key")

    def test_resolve_env_var_none(self):
        """Test that None is returned unchanged"""
        result = _resolve_env_var(None)
        self.assertIsNone(result)

    def test_resolve_env_var_missing_var(self):
        """Test that missing environment variable raises ValueError"""
        with self.assertRaises(ValueError) as context:
            _resolve_env_var("${NONEXISTENT_ENV_VAR_12345}")

        self.assertIn("NONEXISTENT_ENV_VAR_12345", str(context.exception))
        self.assertIn("is not set", str(context.exception))

    def test_api_key_env_var_in_model_config(self):
        """Test that api_key ${VAR} works in LLMModelConfig"""
        model_config = LLMModelConfig(name="test-model", api_key=f"${{{self.test_env_var}}}")

        self.assertEqual(model_config.api_key, self.test_api_key)

    def test_api_key_direct_value(self):
        """Test that direct api_key value still works"""
        direct_key = "direct-api-key-value"
        model_config = LLMModelConfig(name="test-model", api_key=direct_key)

        self.assertEqual(model_config.api_key, direct_key)

    def test_api_key_none(self):
        """Test that api_key can be None"""
        model_config = LLMModelConfig(name="test-model", api_key=None)

        self.assertIsNone(model_config.api_key)

    def test_api_key_env_var_in_llm_config(self):
        """Test that api_key ${VAR} works at LLM config level"""
        yaml_config = {
            "log_level": "INFO",
            "llm": {
                "api_base": "https://api.openai.com/v1",
                "api_key": f"${{{self.test_env_var}}}",
                "models": [{"name": "test-model", "weight": 1.0}],
            },
        }

        config = Config.from_dict(yaml_config)

        self.assertEqual(config.llm.api_key, self.test_api_key)
        # Models should inherit the resolved api_key
        self.assertEqual(config.llm.models[0].api_key, self.test_api_key)

    def test_api_key_env_var_per_model(self):
        """Test that api_key ${VAR} can be specified per model"""
        # Set up a second env var for testing
        second_env_var = "TEST_OPENEVOLVE_API_KEY_2"
        second_api_key = "second-secret-key-67890"
        os.environ[second_env_var] = second_api_key

        try:
            yaml_config = {
                "log_level": "INFO",
                "llm": {
                    "api_base": "https://api.openai.com/v1",
                    "models": [
                        {
                            "name": "model-1",
                            "weight": 1.0,
                            "api_key": f"${{{self.test_env_var}}}",
                        },
                        {
                            "name": "model-2",
                            "weight": 0.5,
                            "api_key": f"${{{second_env_var}}}",
                        },
                    ],
                },
            }

            config = Config.from_dict(yaml_config)

            self.assertEqual(config.llm.models[0].api_key, self.test_api_key)
            self.assertEqual(config.llm.models[1].api_key, second_api_key)
        finally:
            if second_env_var in os.environ:
                del os.environ[second_env_var]

    def test_api_key_env_var_in_evaluator_models(self):
        """Test that api_key ${VAR} works in evaluator_models"""
        yaml_config = {
            "log_level": "INFO",
            "llm": {
                "api_base": "https://api.openai.com/v1",
                "models": [{"name": "evolution-model", "weight": 1.0, "api_key": "direct-key"}],
                "evaluator_models": [
                    {
                        "name": "evaluator-model",
                        "weight": 1.0,
                        "api_key": f"${{{self.test_env_var}}}",
                    }
                ],
            },
        }

        config = Config.from_dict(yaml_config)

        self.assertEqual(config.llm.evaluator_models[0].api_key, self.test_api_key)

    def test_yaml_file_loading_with_env_var(self):
        """Test loading api_key ${VAR} from actual YAML file"""
        yaml_content = f"""
log_level: INFO
llm:
  api_base: https://api.openai.com/v1
  api_key: ${{{self.test_env_var}}}
  models:
  - name: test-model
    weight: 1.0
"""

        with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
            f.write(yaml_content)
            f.flush()

            try:
                config = Config.from_yaml(f.name)
                self.assertEqual(config.llm.api_key, self.test_api_key)
            finally:
                os.unlink(f.name)

    def test_mixed_api_key_sources(self):
        """Test mixing direct api_key and ${VAR} in same config"""
        yaml_config = {
            "log_level": "INFO",
            "llm": {
                "api_base": "https://api.openai.com/v1",
                "api_key": "llm-level-direct-key",
                "models": [
                    {
                        "name": "model-with-env",
                        "weight": 1.0,
                        "api_key": f"${{{self.test_env_var}}}",
                    },
                    {
                        "name": "model-with-direct",
                        "weight": 0.5,
                        "api_key": "model-direct-key",
                    },
                ],
            },
        }

        config = Config.from_dict(yaml_config)

        self.assertEqual(config.llm.api_key, "llm-level-direct-key")
        self.assertEqual(config.llm.models[0].api_key, self.test_api_key)
        self.assertEqual(config.llm.models[1].api_key, "model-direct-key")


if __name__ == "__main__":
    unittest.main()