ahvn.utils.basic.config_utils module

ahvn.utils.basic.config_utils.dmerge(iterable, start=None)[源代码]

Merge multiple dictionaries into a single dictionary, with later dictionaries overwriting earlier ones. Nested dictionaries are merged recursively while all other non-dictionary values are overwritten.

警告

The merging of dictionaries is not order-preserving. The order of keys in the resulting dictionary may not match the order of keys in the input dictionaries.

参数:
  • iterable (Iterable[Dict[str, Any]]) -- An iterable of dictionaries to merge.

  • start (Optional[Dict[str, Any]]) -- An optional starting dictionary to merge into.

返回:

The merged dictionary.

返回类型:

Dict[str, Any]

示例

>>> d1 = {'a': 1, 'b': {'c': 2, 'f': 5}}
>>> d2 = {'b': {'d': 3, 'f': 0}, 'e': 4}
>>> dmerge([d1, d2])
{'a': 1, 'b': {'c': 2, 'f': 0, 'd': 3}, 'e': 4}
>>> dmerge([d2, d1])
{'b': {'d': 3, 'f': 5, 'c': 2}, 'e': 4, 'a': 1}
>>> dmerge([d1, d2], start={'a': 0, 'g': 6})
{'a': 1, 'g': 6, 'b': {'c': 2, 'f': 0, 'd': 3}, 'e': 4}
ahvn.utils.basic.config_utils.dget(d, key_path=None, default=None)[源代码]

Get a value from a dictionary using a dot-separated key path. If the key path does not exist, return the default value.

参数:
  • d (Dict[str, Any]) -- The dictionary to search.

  • key_path (Optional[str]) -- The dot-separated key path to the value.

  • default (Optional[Any]) -- The default value to return if the key path does not exist.

返回:

The value at the specified key path or the default value if not found.

返回类型:

Any

示例

>>> dget({'a': {'b': {'c': 42}}}, 'a.b.c')
42
>>> dget({'a': {'b': {'c': 42}}}, 'a.b.d', default='not found')
'not found'
>>> dget({'a': {'b': {'c': [1, 2, 3]}}}, 'a.b.c[1]')
2
ahvn.utils.basic.config_utils.dset(d, key_path, value=None)[源代码]

Set a value in a dictionary using a dot-separated key path. If the key path does not exist, it will be created.

参数:
  • d (Dict[str, Any]) -- The dictionary to modify.

  • key_path (str) -- The dot-separated key path to the value.

  • value (Optional[Any]) -- The value to set at the specified key path.

返回:

True if the value was set successfully, False if the key path is invalid.

返回类型:

bool

示例

>>> d = {}
>>> dset(d, 'a.b.c', 42)
True
>>> d
{'a': {'b': {'c': 42}}}
ahvn.utils.basic.config_utils.dunset(d, key_path)[源代码]

Unset a value in a dictionary using a dot-separated key path. If the key path does not exist, it will be ignored.

参数:
  • d (Dict[str, Any]) -- The dictionary to modify.

  • key_path (str) -- The dot-separated key path to the value to unset.

返回:

True if the value was unset successfully, False if the key path is invalid.

返回类型:

bool

示例

>>> d = {'a': {'b': {'c': 42}}}
>>> dunset(d, 'a.b.c')
True
>>> d
{'a': {'b': {}}}
ahvn.utils.basic.config_utils.dsetdef(d, key_path, default=None)[源代码]

Set a default value in a dictionary using a dot-separated key path if the key path does not exist.

Notice that if key_path exists but its value is None, the default value will also be set.

参数:
  • d (Dict[str, Any]) -- The dictionary to modify.

  • key_path (str) -- The dot-separated key path to the value.

  • default (Optional[Any]) -- The default value to set at the specified key path if it does not exist.

返回:

True if the default value was set successfully, False if the key path is invalid or already exists.

返回类型:

bool

示例

>>> d = {}
>>> dsetdef(d, 'a.b.c', 42)
True
>>> d
{'a': {'b': {'c': 42}}}
>>> dsetdef(d, 'a.b.c', 100)
False
>>> d
{'a': {'b': {'c': 42}}}
ahvn.utils.basic.config_utils.dflat(d, prefix='', enum=False)[源代码]

Flatten a nested dictionary into a flat dictionary with dot-separated keys.

参数:
  • d (Dict[str, Any]) -- The dictionary to flatten.

  • prefix (str) -- The prefix to prepend to the keys. Defaults to an empty string.

  • enum (bool) -- If True, apart from leaf nodes, also include intermediate nodes in the flattened output. Defaults to False.

生成器:

Generator[str, None, None] -- A generator yielding key-value pairs in the flattened format.

返回类型:

Generator[str, None, None]

示例

>>> dict(dflat({'a': {'b': {'c': 42, 'd': [1, 2, 3]}}, 'e': 5}))
{'a.b.c': 42, 'a.b.d[0]': 1, 'a.b.d[1]': 2, 'a.b.d[2]': 3, 'e': 5}
>>> dict(dflat({'a': {'b': {'c': 42, 'd': [1, 2, 3]}}, 'e': 5}, enum=True))
{'a': {'b': {'c': 42, 'd': [1, 2, 3]}}, 'a.b': {'c': 42, 'd': [1, 2, 3]}, 'a.b.c': 42, 'a.b.d': [1, 2, 3], 'a.b.d[0]': 1, 'a.b.d[1]': 2, 'a.b.d[2]': 3, 'e': 5}
ahvn.utils.basic.config_utils.dunflat(d)[源代码]

Unflatten a flat dictionary with dot-separated keys into a nested dictionary.

参数:

d (Dict[str, Any]) -- The flat dictionary to unflatten.

返回:

The nested dictionary.

返回类型:

Dict[str, Any]

示例

>>> d = {'a.b.c': 42}
>>> dunflat(d)
{'a': {'b': {'c': 42}}}
class ahvn.utils.basic.config_utils.ConfigManager(name=None, package=None, cwd=None)[源代码]

基类:object

参数:
__init__(name=None, package=None, cwd=None)[源代码]
参数:
  • name (str | None)

  • package (str | None)

  • cwd (str | None)

name: str = 'ahvn'
package: str = 'ahvn'
set_cwd(cwd=None)[源代码]

Set the current working directory for the configuration manager.

参数:

cwd (str) -- The new current working directory.

property local_dir: str

Get the local configuration directory.

返回:

The path to the local configuration directory.

返回类型:

str

property local_config_path: str

Get the path to the local configuration file.

返回:

The path to the local configuration file.

返回类型:

str

property global_config_path: str

Get the path to the global configuration file.

返回:

The path to the global configuration file.

返回类型:

str

property system_config_path: str

Get the path to the system configuration file.

返回:

The path to the system configuration file.

返回类型:

str

config_path(level=None)[源代码]

Get the path to the configuration file for a specific level.

参数:

level (Literal[None, 'local', 'global', 'system']) -- The configuration level to get the path for. If None, returns the local configuration path.

返回:

The path to the configuration file for the specified level.

返回类型:

str

resource(*args)[源代码]

Get the path to a resource file in the package resources directory.

参数:

*args (List[str]) -- The path components to the resource file.

返回:

The absolute path to the resource file.

返回类型:

str

property config: Dict[str, Any]
property system_config: Dict[str, Any]
property global_config: Dict[str, Any]
property local_config: Dict[str, Any]
load()[源代码]
save(level=None)[源代码]
参数:

level (Literal[None, 'local', 'global'])

init(reset=False)[源代码]

Initialize the local configuration manager by loading the default configuration and creating the necessary directories.

参数:

reset (bool) -- If True, reset the local configuration to the default values.

返回类型:

bool

setup(reset=False)[源代码]

Setup the global configuration manager by initializing it and loading the configuration and creating the necessary directories.

参数:

reset (bool) -- If True, reset the global configuration to the default values.

返回类型:

bool

get(key_path=None, default=None, level=None)[源代码]

Get a value from the configuration using a dot-separated key path.

参数:
  • key_path (str) -- The dot-separated key path to the value.

  • default (Any) -- The default value to return if the key path does not exist.

  • level (Literal[None,'local','global']) -- The configuration level to use. If None, uses local configuration.

返回:

The value at the specified key path or the default value if not found.

返回类型:

Any

set(key_path, value=None, level=None)[源代码]

Set a value in the configuration using a dot-separated key path.

参数:
  • key_path (str) -- The dot-separated key path to the value.

  • value (Any) -- The value to set at the specified key path.

  • level (Literal['local','global']) -- The configuration level to use. If None, uses local configuration.

返回:

True if the value was set successfully, False if the key path is invalid.

返回类型:

bool

unset(key_path, level=None)[源代码]

Unset a value in the configuration using a dot-separated key path.

参数:
  • key_path (str) -- The dot-separated key path to the value to unset.

  • level (Literal['local','global']) -- The configuration level to use. If None, uses local configuration.

返回:

True if the value was unset successfully, False if the key path is invalid.

返回类型:

bool

ahvn.utils.basic.config_utils.hpj(*args, abs=False, cm=None)[源代码]

Join a list of strings into a path. Platform-agnostic. Spaces and trailing slashes are stripped from each argument. The following characters will be expanded: - '~' to the user's home directory. - '&' the resources directory of AgentHeaven. It is only recommended to use it at the beginning of the path. - '>' the local root folder (without .ahvn/) of the current AgentHeaven repository. It is only recommended to use it at the beginning of the path.

参数:
  • *args (List[str]) -- Components of the path to join. Each argument should be a string.

  • abs (bool, optional) -- If True, returns the absolute path. Defaults to False.

  • cm (Optional[ConfigManager]) -- The configuration manager to use for resource and local directory resolution. Defaults to HEAVEN_CM.

返回:

The joined, normalized path. Expands '~' to the user's home directory.

返回类型:

str

示例

>>> hpj("A", "B/C", " D/ ")
'A/B/C/D'
>>> hpj("A", "B/C", " D/ ", abs=True)
'<path_to_cwd>/A/B/C'
>>> hpj("~", "A", "B/C", " D/ ", abs=True)
'<path_to_user_dir>/A/B/C'
>>> hpj("&", "B", "C")
'<path_to_ahvn>/resources/B/C'
>>> hpj("& B", "C")
'<path_to_ahvn>/resources/B/C'
>>> hpj("&/B", "C")
'<path_to_ahvn>/resources/B/C'
>>> hpj("& /B", "C") # Not recommended
'/B/C'
>>> hpj("> B/C", " D/ ")
'<path_to_repo>/B/C/D'
ahvn.utils.basic.config_utils.encrypt_config(config, encrypt_keys=None)[源代码]

Encrypt sensitive information in the LLM configuration dictionary.

参数:
  • config (Dict[str, Any]) -- The LLM configuration dictionary to encrypt.

  • encrypt_keys (Optional[List[str]]) -- List of keys to encrypt. If None, uses the keys specified in the global config under "core.encrypt_keys".

返回:

The encrypted LLM configuration dictionary.

返回类型:

Dict[str, Any]