Merge branch 'main' into add_azure_engine

This commit is contained in:
rainyrfeng 2023-09-05 11:22:05 +08:00
commit ce0c7b62a0
63 changed files with 645 additions and 333 deletions

View file

@ -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)
@ -85,14 +85,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:
@ -104,11 +104,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()