File size: 2,717 Bytes
77320e4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import yaml
from typing import Dict, AnyStr, Union, Any
from pathlib import Path
from ..prompt import SimpleReactPrompt, ZeroShotReactPrompt
from .logger import get_logger
logger = get_logger()
class Config:
"""
A class for loading and creating configuration dictionaries from files or dictionaries.
"""
@staticmethod
def _prompt_constructor(loader, node):
value = node.value
if value == "SimpleReactPrompt":
return SimpleReactPrompt()
elif value == "ZeroShotReactPrompt":
return ZeroShotReactPrompt()
else:
logger.warning(f"Unknown prompt name: {value}. use default SimpleReactPrompt")
return SimpleReactPrompt()
@staticmethod
def load(path: Union[Path, AnyStr]) -> Dict[AnyStr, Any]:
"""
Load a configuration dictionary from a YAML file.
:param path: The path to the configuration file.
:type path: Union[Path, AnyStr]
:raises FileNotFoundError: If the file is not found.
:raises yaml.YAMLError: If a YAML error occurred while loading the file.
:raises Exception: If an unexpected error occurred.
:return: A dictionary containing the configuration.
:rtype: Dict[AnyStr, Any]
"""
# logger the start of the loading process
logger.info(f"Starting to load configuration from {path}")
# Register the custom prompt constructor with PyYAML
yaml.add_constructor('!prompt', Config._prompt_constructor)
try:
with open(path, "r") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
logger.info(f"Successfully loaded configuration from {path}")
return config
except FileNotFoundError:
logger.error(f"Config file {path} not found")
raise FileNotFoundError(f"Config file {path} not found")
except yaml.YAMLError as e:
logger.error(f"YAML error occurred while loading the configuration: {str(e)}", exc_info=True)
raise yaml.YAMLError(e)
except Exception as e:
logger.error(f"An unexpected error occurred: {str(e)}", exc_info=True)
raise Exception(e)
@staticmethod
def from_dict(config: Dict[AnyStr, Any]) -> Dict[AnyStr, Any]:
"""
Create a configuration dictionary from a Python dictionary.
:param config: A dictionary containing configuration parameters.
:type config: Dict[AnyStr, Any]
:return: A dictionary containing the configuration.
:rtype: Dict[AnyStr, Any]
"""
logger.info(f"Creating Config from dictionary")
return config
|