hbllmutils.manage.config

Configuration management module for Language Learning Models (LLM).

This module provides functionality to load and manage LLM configuration from YAML files. It supports model-specific parameters with fallback and default configurations.

The configuration file should contain a ‘models’ section with model definitions, and can include special keys ‘__default__’ and ‘__fallback__’ for default behavior.

Example::
>>> config = LLMConfig.open('config.yaml')
>>> params = config.get_model_params('gpt-4')
>>> # Use params for model initialization

An example configuration file (.llmconfig.yaml):

deepseek: &deepseek
  base_url: https://api.deepseek.com/v1
  api_token: sk-457***af74

aihubmix: &aihubmix
  base_url: https://aihubmix.com/v1
  api_token: sk-6B9***F0Ad

aigcbest: &aigcbest
  base_url: https://api2.aigcbest.top/v1
  api_token: sk-tbK***49kA

openroute: &openroute
  base_url: https://openrouter.ai/api/v1
  api_token: sk-or-v1-9bf***a3d4

models:
  __default__:
    <<: *deepseek
    model_name: deepseek-chat

  deepseek-R1:
    <<: *deepseek
    model_name: deepseek-reasoner

  deepseek-V3:
    <<: *deepseek
    model_name: deepseek-chat

  __fallback__:
    <<: *aihubmix

LLMConfig

class hbllmutils.manage.config.LLMConfig(config: Dict[str, Any])[source]

Configuration manager for Language Learning Models.

This class handles loading and accessing LLM configuration from YAML files, providing methods to retrieve model-specific parameters with support for default and fallback configurations.

__init__(config: Dict[str, Any])[source]

Initialize the LLMConfig with a configuration dictionary.

Parameters:

config (Dict[str, Any]) – The configuration dictionary loaded from YAML.

get_model_params(model_name: str | None = None, **params: Any) Dict[str, Any][source]

Retrieve parameters for a specific model.

This method looks up model parameters in the following order:

  1. If model_name is None, returns ‘__default__’ configuration

  2. If model_name exists in models, returns its configuration

  3. If ‘__fallback__’ exists, returns fallback config with the model_name

  4. Otherwise, raises KeyError

Additional parameters passed as kwargs will override the base configuration.

Parameters:
  • model_name (Optional[str]) – Name of the model to retrieve parameters for. If None, uses ‘__default__’.

  • params (Any) – Additional parameters to override the base configuration.

Returns:

Dictionary containing the merged model parameters.

Return type:

Dict[str, Any]

Raises:

KeyError – If the model is not found and no __fallback__ is provided.

Example::
>>> config = LLMConfig({'models': {'gpt-4': {'api_key': 'xxx'}}})
>>> config.get_model_params('gpt-4', temperature=0.7)
{'api_key': 'xxx', 'temperature': 0.7}
property models: Dict[str, Any]

Get the models configuration dictionary.

Returns:

Dictionary containing model configurations, or empty dict if not found.

Return type:

Dict[str, Any]

classmethod open(file_or_dir: str = '.') LLMConfig[source]

Load LLM configuration from a file or directory.

This method automatically detects whether the provided path is a file or directory:

  • If it’s a directory, looks for ‘.llmconfig.yaml’ inside it

  • If it’s a file, loads it directly as a YAML configuration

Parameters:

file_or_dir (str) – Path to a configuration file or directory. Defaults to current directory.

Returns:

A new LLMConfig instance with the loaded configuration.

Return type:

LLMConfig

Raises:

FileNotFoundError – If no valid configuration file or directory is found.

Example::
>>> config = LLMConfig.open('.')  # Load from current directory
>>> config = LLMConfig.open('config.yaml')  # Load from specific file
classmethod open_from_directory(directory: str) LLMConfig[source]

Load LLM configuration from a directory by looking for ‘.llmconfig.yaml’.

Parameters:

directory (str) – Path to the directory containing ‘.llmconfig.yaml’.

Returns:

A new LLMConfig instance with the loaded configuration.

Return type:

LLMConfig

Raises:

FileNotFoundError – If ‘.llmconfig.yaml’ does not exist in the directory.

Example::
>>> config = LLMConfig.open_from_directory('/path/to/project')
classmethod open_from_yaml(yaml_file: str) LLMConfig[source]

Load LLM configuration from a YAML file.

Parameters:

yaml_file (str) – Path to the YAML configuration file.

Returns:

A new LLMConfig instance with the loaded configuration.

Return type:

LLMConfig

Raises:
  • FileNotFoundError – If the YAML file does not exist.

  • yaml.YAMLError – If the YAML file is malformed.

Example::
>>> config = LLMConfig.open_from_yaml('config.yaml')