From 291af5ad01bef9f6dbaa29305a3b13b29e21763b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sun, 20 Aug 2023 10:19:43 +0800 Subject: [PATCH] feat: + Config.options --- metagpt/config.py | 25 ++++++++++++++++--------- tests/metagpt/utils/test_config.py | 13 +++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/metagpt/config.py b/metagpt/config.py index ac969f2f9..6f3f9732a 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -3,6 +3,8 @@ """ @Desc: Provide configuration, singleton. @Modified By: mashenquan, replace `CONFIG` with `os.environ` to support personal config +@Desc: `os.environ` doesn't support personalization, while `Config` does. + Hence, the parameter reading priority is `Config` first, and if not found, then `os.environ`. """ import os @@ -90,14 +92,6 @@ class Config(metaclass=Singleton): self.model_for_researcher_summary = self._get("MODEL_FOR_RESEARCHER_SUMMARY") self.model_for_researcher_report = self._get("MODEL_FOR_RESEARCHER_REPORT") - # Update environment variables - for k, v in self._configs.items(): - os.environ[k] = str(v) - for attribute, value in vars(self).items(): - if attribute == "_configs": - continue - os.environ[attribute] = str(value) - def _init_with_config_files_and_env(self, configs: dict, yaml_file): """Load in decreasing priority from `config/key.yaml`, `config/config.yaml`, and environment variables.""" configs.update(os.environ) @@ -117,9 +111,22 @@ class Config(metaclass=Singleton): return self._configs.get(*args, **kwargs) def get(self, key, *args, **kwargs): - """从config/key.yaml / config/config.yaml / env三处找值,找不到报错""" + """Retrieve value from `config/key.yaml`, `config/config.yaml`, and environment variables. + Raise an error if not found.""" value = self._get(key, *args, **kwargs) if value is None: raise ValueError(f"Key '{key}' not found in environment variables or in the YAML file") return value + @property + def options(self): + """Return key-value configuration parameters.""" + opts = {} + for k, v in self._configs.items(): + opts[k] = v + for attribute, value in vars(self).items(): + if attribute == "_configs": + continue + opts[attribute] = value + return opts + diff --git a/tests/metagpt/utils/test_config.py b/tests/metagpt/utils/test_config.py index 558a4e5a4..475bac22b 100644 --- a/tests/metagpt/utils/test_config.py +++ b/tests/metagpt/utils/test_config.py @@ -4,7 +4,9 @@ @Time : 2023/5/1 11:19 @Author : alexanderwu @File : test_config.py +@Modified By: mashenquan, 2013/8/20, add `test_options` """ +from pathlib import Path import pytest @@ -29,3 +31,14 @@ def test_config_yaml_file_not_exists(): with pytest.raises(Exception) as exc_info: config.get('OPENAI_BASE_URL') assert str(exc_info.value) == "Key 'OPENAI_BASE_URL' not found in environment variables or in the YAML file" + + +def test_options(): + filename = Path(__file__).resolve().parent.parent.parent.parent / "config/config.yaml" + config = Config(filename) + opts = config.options + assert opts + + +if __name__ == '__main__': + test_options()