🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
add context tests
geekan62677c37b7e6
diff --git a/metagpt/config2.py b/metagpt/config2.pyindex a6aa62f6b..9c809e559 100644--- a/metagpt/config2.py+++ b/metagpt/config2.py@@ -3,7 +3,7 @@"""@Time : 2024/1/4 01:25@Author : alexanderwu-@File : llm_factory.py+@File : config2.py"""import osfrom pathlib import Path@@ -23,6 +23,8 @@ from metagpt.utils.yaml_model import YamlModelclass CLIParams(BaseModel):+ """CLI parameters"""+project_path: str = ""project_name: str = ""inc: bool = False@@ -32,12 +34,15 @@ class CLIParams(BaseModel):@model_validator(mode="after")def check_project_path(self):+ """Check project_path and project_name"""if self.project_path:self.inc = Trueself.project_name = self.project_name or Path(self.project_path).nameclass Config(CLIParams, YamlModel):+ """Configurations for MetaGPT"""+# Key Parametersllm: Dict[str, LLMConfig] = Field(default_factory=Dict)@@ -133,4 +138,21 @@ def merge_dict(dicts: Iterable[Dict]) -> Dict:return result+class ConfigurableMixin:+ """Mixin class for configurable objects"""++ def __init__(self, config=None):+ self._config = config++ def try_set_parent_config(self, parent_config):+ """Try to set parent config if not set"""+ if self._config is None:+ self._config = parent_config++ @property+ def config(self):+ """Get config"""+ return self._config++config = Config.default()diff --git a/metagpt/context.py b/metagpt/context.pyindex 0ea5d6046..e396de7e1 100644--- a/metagpt/context.py+++ b/metagpt/context.py@@ -9,6 +9,8 @@ import osfrom pathlib import Pathfrom typing import Optional+from pydantic import BaseModel, ConfigDict+from metagpt.config2 import Configfrom metagpt.configs.llm_config import LLMTypefrom metagpt.const import OPTIONS@@ -18,28 +20,33 @@ from metagpt.utils.cost_manager import CostManagerfrom metagpt.utils.git_repository import GitRepository-class AttrDict:- """A dict-like object that allows access to keys as attributes."""+class AttrDict(BaseModel):+ """A dict-like object that allows access to keys as attributes, compatible with Pydantic."""++ model_config = ConfigDict(extra="allow")- def __init__(self, d=None):- if d is None:- d = {}- self.__dict__["_dict"] = d+ def __init__(self, **kwargs):+ super().__init__(**kwargs)+ self.__dict__.update(kwargs)def __getattr__(self, key):- return self._dict.get(key, None)+ return self.__dict__.get(key, None)def __setattr__(self, key, value):- self._dict[key] = value+ self.__dict__[key] = valuedef __delattr__(self, key):- if key in self._dict:- del self._dict[key]+ if key in self.__dict__:+ del self.__dict__[key]else:raise AttributeError(f"No such attribute: {key}")-class Context:+class Context(BaseModel):+ """Env context for MetaGPT"""++ model_config = ConfigDict(arbitrary_types_allowed=True)+kwargs: AttrDict = AttrDict()config: Config = Config.default()git_repo: Optional[GitRepository] = None@@ -82,14 +89,5 @@ class Context:return llm-# Global context+# Global context, not in Envcontext = Context()---if __name__ == "__main__":- # print(context.model_dump_json(indent=4))- # print(context.config.get_openai_llm())- ad = AttrDict({"name": "John", "age": 30})-- print(ad.name) # Output: John- print(ad.height) # Output: None (因为height不存在)diff --git a/tests/metagpt/test_context.py b/tests/metagpt/test_context.pynew file mode 100644index 000000000..d4f29e352--- /dev/null+++ b/tests/metagpt/test_context.py@@ -0,0 +1,63 @@+#!/usr/bin/env python+# -*- coding: utf-8 -*-+"""+@Time : 2024/1/9 13:52+@Author : alexanderwu+@File : test_context.py+"""+from metagpt.configs.llm_config import LLMType+from metagpt.context import AttrDict, Context, context+++def test_attr_dict_1():+ ad = AttrDict(name="John", age=30)+ assert ad.name == "John"+ assert ad.age == 30+ assert ad.height is None+++def test_attr_dict_2():+ ad = AttrDict(name="John", age=30)+ ad.height = 180+ assert ad.height == 180+++def test_attr_dict_3():+ ad = AttrDict(name="John", age=30)+ del ad.age+ assert ad.age is None+++def test_attr_dict_4():+ ad = AttrDict(name="John", age=30)+ try:+ del ad.weight+ except AttributeError as e:+ assert str(e) == "No such attribute: weight"+++def test_attr_dict_5():+ ad = AttrDict.model_validate({"name": "John", "age": 30})+ assert ad.name == "John"+ assert ad.age == 30+++def test_context_1():+ ctx = Context()+ assert ctx.config is not None+ assert ctx.git_repo is None+ assert ctx.src_workspace is None+ assert ctx.cost_manager is not None+ assert ctx.options is not None+++def test_context_2():+ llm = context.config.get_openai_llm()+ assert llm is not None+ assert llm.api_type == LLMType.OPENAI++ kwargs = context.kwargs+ assert kwargs is not None++ kwargs.test_key = "test_value"+ assert kwargs.test_key == "test_value"