mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-05-05 05:42:37 +02:00
添加一个代码
This commit is contained in:
commit
552547e662
58 changed files with 632 additions and 327 deletions
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
提供配置,单例
|
||||
Provide configuration, singleton
|
||||
"""
|
||||
import os
|
||||
|
||||
|
|
@ -28,7 +28,7 @@ class NotConfiguredException(Exception):
|
|||
|
||||
class Config(metaclass=Singleton):
|
||||
"""
|
||||
常规使用方法:
|
||||
Regular usage method:
|
||||
config = Config("config.yaml")
|
||||
secret_key = config.get_key("MY_SECRET_KEY")
|
||||
print("Secret key:", secret_key)
|
||||
|
|
@ -94,14 +94,14 @@ class Config(metaclass=Singleton):
|
|||
self.model_for_researcher_report = self._get("MODEL_FOR_RESEARCHER_REPORT")
|
||||
|
||||
def _init_with_config_files_and_env(self, configs: dict, yaml_file):
|
||||
"""从config/key.yaml / config/config.yaml / env三处按优先级递减加载"""
|
||||
"""Load from config/key.yaml, config/config.yaml, and env in decreasing order of priority"""
|
||||
configs.update(os.environ)
|
||||
|
||||
for _yaml_file in [yaml_file, self.key_yaml_file]:
|
||||
if not _yaml_file.exists():
|
||||
continue
|
||||
|
||||
# 加载本地 YAML 文件
|
||||
# Load local YAML file
|
||||
with open(_yaml_file, "r", encoding="utf-8") as file:
|
||||
yaml_data = yaml.safe_load(file)
|
||||
if not yaml_data:
|
||||
|
|
@ -113,11 +113,11 @@ class Config(metaclass=Singleton):
|
|||
return self._configs.get(*args, **kwargs)
|
||||
|
||||
def get(self, key, *args, **kwargs):
|
||||
"""从config/key.yaml / config/config.yaml / env三处找值,找不到报错"""
|
||||
"""Search for a value in config/key.yaml, config/config.yaml, and env; 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
|
||||
|
||||
|
||||
CONFIG = Config()
|
||||
CONFIG = Config()
|
||||
Loading…
Add table
Add a link
Reference in a new issue