-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_llm_ensemble.py
More file actions
69 lines (58 loc) · 2.57 KB
/
Copy pathtest_llm_ensemble.py
File metadata and controls
69 lines (58 loc) · 2.57 KB
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
"""
Tests for LLMEnsemble in openevolve.llm.ensemble
"""
from typing import Any, Dict, List
import unittest
from openevolve.llm.ensemble import LLMEnsemble
from openevolve.config import LLMModelConfig
from openevolve.llm.base import LLMInterface
class TestLLMEnsemble(unittest.TestCase):
def test_weighted_sampling(self):
models = [
LLMModelConfig(name="a", weight=0.0, api_key="test", api_base="http://test"),
LLMModelConfig(name="b", weight=1.0, api_key="test", api_base="http://test"),
]
ensemble = LLMEnsemble(models)
# Should always sample model 'b'
for _ in range(10):
model, model_id = ensemble._sample_model()
self.assertEqual(model.model, "b")
self.assertEqual(model_id, 1)
models = [
LLMModelConfig(name="a", weight=0.3, api_key="test", api_base="http://test"),
LLMModelConfig(name="b", weight=0.3, api_key="test", api_base="http://test"),
LLMModelConfig(name="c", weight=0.3, api_key="test", api_base="http://test"),
]
ensemble = LLMEnsemble(models)
# Should sample all models. Track sampled models in a set
sampled_models = set()
for _ in range(1000):
model, model_id = ensemble._sample_model()
sampled_models.add(model.model)
# Cancel once we have all models
if len(sampled_models) == len(models):
break
self.assertEqual(len(sampled_models), len(models))
class TestEnsembleInit(unittest.TestCase):
class MyCustomLLM(LLMInterface):
def __init__(self, model, some_field):
self.model = model
self.some_field = some_field
async def generate(self, prompt: str, **kwargs) -> str:
return "custom-generate"
async def generate_with_context(self, system_message: str, messages: List[Dict[str, str]], **kwargs) -> str:
return "custom-generate-with-context"
def init_custom_llm(self, model_cfg):
return self.MyCustomLLM(model=model_cfg.name, some_field="value")
def test_ensemble_initialization(self):
models = [
LLMModelConfig(name="a"),
LLMModelConfig(name="b", init_client=self.init_custom_llm),
]
ensemble = LLMEnsemble(models)
self.assertEqual(len(ensemble.models), len(models))
self.assertEqual(ensemble.models[0].model, "a")
self.assertEqual(ensemble.models[1].model, "b")
self.assertEqual(ensemble.models[1].some_field, "value")
if __name__ == "__main__":
unittest.main()