config_list_from_dotenv - AG2

config_list_from_dotenv

autogen.config_list_from_dotenv

config_list_from_dotenv(dotenv_file_path=None, model_api_key_map=None, filter_dict=None)

Load API configurations from a specified .env file or environment variables and construct a list of configurations.

This function will:

model_api_key_map will default to { "gpt-4": "OPENAI_API_KEY", "gpt-3.5-turbo": "OPENAI_API_KEY"} if none.

PARAMETER DESCRIPTION
dotenv_file_path The path to the .env file. Defaults to None.
TYPE:strDEFAULT:None
model_api_key_map A dictionary mapping models to their API key configurations. If a string is provided as configuration, it is considered as an environment variable name storing the API key. If a dict is provided, it should contain at least 'api_key_env_var' key, and optionally other API configurations like 'base_url', 'api_type', and 'api_version'. Defaults to a basic map with 'gpt-4' and 'gpt-3.5-turbo' mapped to 'OPENAI_API_KEY'.
TYPE:str / dictDEFAULT:None
filter_dict A dictionary containing the models to be loaded. Containing a 'model' key mapped to a set of model names to be loaded. Defaults to None, which loads all found configurations.
TYPE:dictDEFAULT:None
RETURNS DESCRIPTION
list[dict[str, Union[str, set[str]]]] List[Dict[str, Union[str, Set[str]]]]: A list of configuration dictionaries for each model.
RAISES DESCRIPTION
FileNotFoundError If the specified .env file does not exist.
TypeError If an unsupported type of configuration is provided in model_api_key_map.

Source code in autogen/oai/openai_utils.py

@export_module("autogen")
def config_list_from_dotenv(
    dotenv_file_path: Optional[str] = None,
    model_api_key_map: Optional[dict[str, Any]] = None,
    filter_dict: Optional[dict[str, Union[list[Union[str, None]], set[Union[str, None]]]]] = None,
) -> list[dict[str, Union[str, set[str]]]]:
    """Load API configurations from a specified .env file or environment variables and construct a list of configurations.
    This function will:
    - Load API keys from a provided .env file or from existing environment variables.
    - Create a configuration dictionary for each model using the API keys and additional configurations.
    - Filter and return the configurations based on provided filters.
    model_api_key_map will default to `{"gpt-4": "OPENAI_API_KEY", "gpt-3.5-turbo": "OPENAI_API_KEY"}` if none.
    Args:
        dotenv_file_path (str, optional): The path to the .env file. Defaults to None.
        model_api_key_map (str/dict, optional): A dictionary mapping models to their API key configurations.
                                           If a string is provided as configuration, it is considered as an environment
                                           variable name storing the API key.
                                           If a dict is provided, it should contain at least 'api_key_env_var' key,
                                           and optionally other API configurations like 'base_url', 'api_type', and 'api_version'.
                                           Defaults to a basic map with 'gpt-4' and 'gpt-3.5-turbo' mapped to 'OPENAI_API_KEY'.
        filter_dict (dict, optional): A dictionary containing the models to be loaded.
                                      Containing a 'model' key mapped to a set of model names to be loaded.
                                      Defaults to None, which loads all found configurations.
    Returns:
        List[Dict[str, Union[str, Set[str]]]]: A list of configuration dictionaries for each model.
    Raises:
        FileNotFoundError: If the specified .env file does not exist.
        TypeError: If an unsupported type of configuration is provided in model_api_key_map.
    """
    if dotenv_file_path:
        dotenv_path = Path(dotenv_file_path)
        if dotenv_path.exists():
            load_dotenv(dotenv_path)
        else:
            logging.warning(f"The specified .env file {dotenv_path} does not exist.")
    else:
        dotenv_path_str = find_dotenv()
        if not dotenv_path_str:
            logging.warning("No .env file found. Loading configurations from environment variables.")
        dotenv_path = Path(dotenv_path_str)
        load_dotenv(dotenv_path)
    model_api_key_map = model_api_key_map or {}
    default_models = ["gpt-4", "gpt-3.5-turbo"]
    for model in default_models:
        if model not in model_api_key_map:
            model_api_key_map[model] = "OPENAI_API_KEY" 
    env_var = []
    for model, config in model_api_key_map.items():
        if isinstance(config, str):
            api_key_env_var = config
            config_dict = get_config(api_key=os.getenv(api_key_env_var))
        elif isinstance(config, dict):
            api_key = os.getenv(config.get("api_key_env_var", "OPENAI_API_KEY"))
            config_without_key_var = {k: v for k, v in config.items() if k != "api_key_env_var"}
            config_dict = get_config(api_key=api_key, **config_without_key_var)
        else:
            logging.warning(f"Unsupported type {type(config)} for model {model} configuration")  
        if not config_dict["api_key"] or config_dict["api_key"].strip() == "":
            logging.warning(
                f"API key not found or empty for model {model}. Please ensure path to .env file is correct."
            )
            continue
        config_dict["model"] = model
        env_var.append(config_dict)
    fd, temp_name = tempfile.mkstemp()
    try:
        with os.fdopen(fd, "w+") as temp:
            env_var_str = json.dumps(env_var)
            temp.write(env_var_str)
            temp.flush()
            config_list = config_list_from_json(env_or_file=temp_name, filter_dict=filter_dict)
    finally:
        os.remove(temp_name)
    if len(config_list) == 0:
        logging.error("No configurations loaded.")
        return []
    logging.info(f"Models available: {[config['model'] for config in config_list]}")
    return config_list