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
PARAMETER | DESCRIPTION |
dotenv_file_path | The path to the .env file. Defaults to None. TYPE: str DEFAULT: 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 / dict DEFAULT: 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: dict DEFAULT: None |
RETURNS | DESCRIPTION |
list[dict[str, 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: str | None = None,
model_api_key_map: dict[str, Any] | None = None,
filter_dict: dict[str, list[str | None] | set[str | None]] | None = None,
) -> list[dict[str, 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)
# Ensure the model_api_key_map is not None to prevent TypeErrors during key assignment.
model_api_key_map = model_api_key_map or {}
# Ensure default models are always considered
default_models = ["gpt-4", "gpt-3.5-turbo"]
for model in default_models:
# Only assign default API key if the model is not present in the map.
# If model is present but set to invalid/empty, do not overwrite.
if model not in model_api_key_map:
model_api_key_map[model] = "OPENAI_API_KEY"
env_var = []
# Loop over the models and create configuration dictionaries
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(
"Unsupported configuration type encountered for a model. Please check your model_api_key_map."
)
if not config_dict["api_key"] or config_dict["api_key"].strip() == "":
logging.warning("API key not found or empty for a model. Please ensure path to .env file is correct.")
continue # Skip this configuration and continue with the next
# Add model to the configuration and append to the list
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()
# Assuming config_list_from_json is a valid function from your code
config_list = config_list_from_json(env_or_file=temp_name, filter_dict=filter_dict)
finally:
# The file is deleted after using its name (to prevent windows build from breaking)
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
|