mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-08 15:05:17 +02:00
refine code
This commit is contained in:
parent
f4ae3bbfd9
commit
2ff2877536
2 changed files with 25 additions and 2 deletions
|
|
@ -153,12 +153,13 @@ def merge_dict(dicts: Iterable[Dict]) -> Dict:
|
|||
return result
|
||||
|
||||
|
||||
class ConfigMixin:
|
||||
class ConfigMixin(BaseModel):
|
||||
"""Mixin class for configurable objects"""
|
||||
|
||||
_config: Optional[Config] = None
|
||||
|
||||
def __init__(self, config: Optional[Config] = None):
|
||||
super().__init__()
|
||||
self._config = config
|
||||
|
||||
def try_set_parent_config(self, parent_config):
|
||||
|
|
|
|||
|
|
@ -5,8 +5,9 @@
|
|||
@Author : alexanderwu
|
||||
@File : test_config.py
|
||||
"""
|
||||
from pydantic import BaseModel
|
||||
|
||||
from metagpt.config2 import Config, config
|
||||
from metagpt.config2 import Config, ConfigMixin, config
|
||||
from metagpt.configs.llm_config import LLMType
|
||||
from tests.metagpt.provider.mock_llm_config import mock_llm_config
|
||||
|
||||
|
|
@ -26,3 +27,24 @@ def test_config_from_dict():
|
|||
cfg = Config(llm={"default": mock_llm_config})
|
||||
assert cfg
|
||||
assert cfg.llm["default"].api_key == "mock_api_key"
|
||||
|
||||
|
||||
class NewModel(ConfigMixin, BaseModel):
|
||||
a: str = "a"
|
||||
b: str = "b"
|
||||
|
||||
|
||||
def test_config_mixin():
|
||||
new_model = NewModel()
|
||||
assert new_model.a == "a"
|
||||
assert new_model.b == "b"
|
||||
assert new_model._config == new_model.config
|
||||
assert new_model._config is None
|
||||
|
||||
|
||||
def test_config_mixin_2():
|
||||
i = Config(llm={"default": mock_llm_config})
|
||||
new_model = NewModel(config=i)
|
||||
assert new_model.config == i
|
||||
assert new_model._config == i
|
||||
assert new_model.config.llm["default"] == mock_llm_config
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue