add context tests

This commit is contained in:
geekan 2024-01-09 14:16:32 +08:00
parent b6508d1054
commit b4f049294b
3 changed files with 105 additions and 22 deletions

View file

@ -3,7 +3,7 @@
"""
@Time : 2024/1/4 01:25
@Author : alexanderwu
@File : llm_factory.py
@File : config2.py
"""
import os
from pathlib import Path
@ -23,6 +23,8 @@ from metagpt.utils.yaml_model import YamlModel
class 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 = True
self.project_name = self.project_name or Path(self.project_path).name
class Config(CLIParams, YamlModel):
"""Configurations for MetaGPT"""
# Key Parameters
llm: 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()

View file

@ -9,6 +9,8 @@ import os
from pathlib import Path
from typing import Optional
from pydantic import BaseModel, ConfigDict
from metagpt.config2 import Config
from metagpt.configs.llm_config import LLMType
from metagpt.const import OPTIONS
@ -18,28 +20,33 @@ from metagpt.utils.cost_manager import CostManager
from 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."""
def __init__(self, d=None):
if d is None:
d = {}
self.__dict__["_dict"] = d
model_config = ConfigDict(extra="allow")
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] = value
def __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 Env
context = 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不存在)

View file

@ -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"