mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-05 14:55:18 +02:00
refine config mixin
This commit is contained in:
parent
8bea366f28
commit
b0efa4b6a5
3 changed files with 13 additions and 11 deletions
|
|
@ -156,7 +156,8 @@ def merge_dict(dicts: Iterable[Dict]) -> Dict:
|
|||
class ConfigMixin(BaseModel):
|
||||
"""Mixin class for configurable objects"""
|
||||
|
||||
config: Optional[Config] = None
|
||||
# Env/Role/Action will use this config as private config, or use self.context.config as public config
|
||||
_config: Optional[Config] = None
|
||||
|
||||
def __init__(self, config: Optional[Config] = None, **kwargs):
|
||||
"""Initialize with config"""
|
||||
|
|
@ -164,13 +165,13 @@ class ConfigMixin(BaseModel):
|
|||
self.set_config(config)
|
||||
|
||||
def set(self, k, v, override=False):
|
||||
"""Try to set parent config if not set"""
|
||||
"""Set attribute"""
|
||||
if override or not self.__dict__.get(k):
|
||||
self.__dict__[k] = v
|
||||
|
||||
def set_config(self, config: Config, override=False):
|
||||
"""Set config"""
|
||||
self.set("config", config, override)
|
||||
self.set("_config", config, override)
|
||||
|
||||
|
||||
config = Config.default()
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ from pydantic import BaseModel, ConfigDict, Field, SerializeAsAny, model_validat
|
|||
from metagpt.actions import Action, ActionOutput
|
||||
from metagpt.actions.action_node import ActionNode
|
||||
from metagpt.actions.add_requirement import UserRequirement
|
||||
from metagpt.config2 import ConfigMixin
|
||||
from metagpt.context import Context, context
|
||||
from metagpt.llm import LLM
|
||||
from metagpt.logs import logger
|
||||
|
|
@ -119,7 +120,7 @@ class RoleContext(BaseModel):
|
|||
return self.memory.get()
|
||||
|
||||
|
||||
class Role(SerializationMixin):
|
||||
class Role(SerializationMixin, ConfigMixin, BaseModel):
|
||||
"""Role/Agent"""
|
||||
|
||||
model_config = ConfigDict(arbitrary_types_allowed=True, exclude=["llm"])
|
||||
|
|
|
|||
|
|
@ -53,12 +53,12 @@ def test_config_mixin_2():
|
|||
i = Config(llm={"default": mock_llm_config})
|
||||
j = Config(llm={"new": mock_llm_config})
|
||||
obj = ModelX(config=i)
|
||||
assert obj.config == i
|
||||
assert obj.config.llm["default"] == mock_llm_config
|
||||
assert obj._config == i
|
||||
assert obj._config.llm["default"] == mock_llm_config
|
||||
|
||||
obj.set_config(j)
|
||||
# obj already has a config, so it will not be set
|
||||
assert obj.config == i
|
||||
assert obj._config == i
|
||||
|
||||
|
||||
def test_config_mixin_3():
|
||||
|
|
@ -66,13 +66,13 @@ def test_config_mixin_3():
|
|||
i = Config(llm={"default": mock_llm_config})
|
||||
j = Config(llm={"new": mock_llm_config})
|
||||
obj = ModelY(config=i)
|
||||
assert obj.config == i
|
||||
assert obj.config.llm["default"] == mock_llm_config
|
||||
assert obj._config == i
|
||||
assert obj._config.llm["default"] == mock_llm_config
|
||||
|
||||
obj.set_config(j)
|
||||
# obj already has a config, so it will not be set
|
||||
assert obj.config == i
|
||||
assert obj.config.llm["default"] == mock_llm_config
|
||||
assert obj._config == i
|
||||
assert obj._config.llm["default"] == mock_llm_config
|
||||
|
||||
assert obj.a == "a"
|
||||
assert obj.b == "b"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue