File size: 4,836 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
"""
Test CLI model override functionality (GitHub issue #245)
"""
import unittest
import tempfile
import os

from openevolve.config import Config, load_config


class TestCLIModelOverride(unittest.TestCase):
    """Test that CLI model overrides work correctly"""
    
    def test_rebuild_models_with_both_models(self):
        """Test rebuilding models with both primary and secondary models"""
        config = Config()
        
        # Initially no models
        self.assertEqual(len(config.llm.models), 0)
        
        # Set CLI overrides
        config.llm.primary_model = "gpt-4"
        config.llm.secondary_model = "gpt-3.5-turbo"
        
        # Models list should still be empty before rebuild
        self.assertEqual(len(config.llm.models), 0)
        
        # Rebuild models
        config.llm.rebuild_models()
        
        # Now should have both models
        self.assertEqual(len(config.llm.models), 2)
        self.assertEqual(config.llm.models[0].name, "gpt-4")
        self.assertEqual(config.llm.models[0].weight, 1.0)
        self.assertEqual(config.llm.models[1].name, "gpt-3.5-turbo")
        self.assertEqual(config.llm.models[1].weight, 0.2)
    
    def test_rebuild_models_primary_only(self):
        """Test rebuilding with only primary model"""
        config = Config()
        config.llm.primary_model = "claude-3-opus"
        
        config.llm.rebuild_models()
        
        self.assertEqual(len(config.llm.models), 1)
        self.assertEqual(config.llm.models[0].name, "claude-3-opus")
        self.assertEqual(config.llm.models[0].weight, 1.0)
    
    def test_rebuild_models_with_weights(self):
        """Test rebuilding with custom weights"""
        config = Config()
        config.llm.primary_model = "gpt-4"
        config.llm.primary_model_weight = 0.8
        config.llm.secondary_model = "gpt-3.5-turbo"
        config.llm.secondary_model_weight = 0.5
        
        config.llm.rebuild_models()
        
        self.assertEqual(len(config.llm.models), 2)
        self.assertEqual(config.llm.models[0].weight, 0.8)
        self.assertEqual(config.llm.models[1].weight, 0.5)
    
    def test_rebuild_models_zero_weight_secondary(self):
        """Test that secondary model with zero weight is excluded"""
        config = Config()
        config.llm.primary_model = "gpt-4"
        config.llm.secondary_model = "gpt-3.5-turbo"
        config.llm.secondary_model_weight = 0.0
        
        config.llm.rebuild_models()
        
        # Should only have primary model
        self.assertEqual(len(config.llm.models), 1)
        self.assertEqual(config.llm.models[0].name, "gpt-4")
    
    def test_rebuild_preserves_shared_config(self):
        """Test that rebuilding preserves shared configuration"""
        config = Config()
        config.llm.api_base = "https://custom-api.com/v1"
        config.llm.temperature = 0.8
        config.llm.primary_model = "custom-model"
        
        config.llm.rebuild_models()
        
        # Model should inherit shared configuration
        self.assertEqual(config.llm.models[0].api_base, "https://custom-api.com/v1")
        self.assertEqual(config.llm.models[0].temperature, 0.8)
    
    def test_rebuild_models_with_config_file_override(self):
        """Test CLI override of config file models"""
        config_content = """
llm:
  primary_model: "original-model"
  temperature: 0.5
"""
        
        with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
            f.write(config_content)
            config_path = f.name
        
        try:
            # Load config from file
            config = load_config(config_path)
            
            # Verify original model is loaded
            self.assertEqual(config.llm.models[0].name, "original-model")
            
            # Apply CLI override
            config.llm.primary_model = "overridden-model"
            config.llm.rebuild_models()
            
            # Should now use overridden model
            self.assertEqual(len(config.llm.models), 1)
            self.assertEqual(config.llm.models[0].name, "overridden-model")
            # Should preserve other settings
            self.assertEqual(config.llm.temperature, 0.5)
            
        finally:
            os.unlink(config_path)
    
    def test_evaluator_models_updated_after_rebuild(self):
        """Test that evaluator_models list is also updated after rebuild"""
        config = Config()
        config.llm.primary_model = "test-model"
        
        config.llm.rebuild_models()
        
        # Evaluator models should be populated from main models
        self.assertEqual(len(config.llm.evaluator_models), 1)
        self.assertEqual(config.llm.evaluator_models[0].name, "test-model")


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