Skip to content

get_config_list

autogen.get_config_list #

get_config_list(api_keys, base_urls=None, api_type=None, api_version=None)

Get a list of configs for OpenAI API client.

PARAMETER DESCRIPTION
api_keys

The api keys for openai api calls.

TYPE: list

base_urls

The api bases for openai api calls. If provided, should match the length of api_keys.

TYPE: list DEFAULT: None

api_type

The api type for openai api calls.

TYPE: str DEFAULT: None

api_version

The api version for openai api calls.

TYPE: str DEFAULT: None

RETURNS DESCRIPTION
list

A list of configs for OepnAI API calls.

TYPE: list[dict[str, Any]]

Example:

# Define a list of API keys
api_keys = ["key1", "key2", "key3"]

# Optionally, define a list of base URLs corresponding to each API key
base_urls = ["https://api.service1.com", "https://api.service2.com", "https://api.service3.com"]

# Optionally, define the API type and version if they are common for all keys
api_type = "azure"
api_version = "2024-02-01"

# Call the get_config_list function to get a list of configuration dictionaries
config_list = get_config_list(api_keys, base_urls, api_type, api_version)

Source code in autogen/oai/openai_utils.py
@export_module("autogen")
def get_config_list(
    api_keys: list[str],
    base_urls: Optional[list[str]] = None,
    api_type: Optional[str] = None,
    api_version: Optional[str] = None,
) -> list[dict[str, Any]]:
    """Get a list of configs for OpenAI API client.

    Args:
        api_keys (list): The api keys for openai api calls.
        base_urls (list, optional): The api bases for openai api calls. If provided, should match the length of api_keys.
        api_type (str, optional): The api type for openai api calls.
        api_version (str, optional): The api version for openai api calls.

    Returns:
        list: A list of configs for OepnAI API calls.

    Example:
    ```python
    # Define a list of API keys
    api_keys = ["key1", "key2", "key3"]

    # Optionally, define a list of base URLs corresponding to each API key
    base_urls = ["https://api.service1.com", "https://api.service2.com", "https://api.service3.com"]

    # Optionally, define the API type and version if they are common for all keys
    api_type = "azure"
    api_version = "2024-02-01"

    # Call the get_config_list function to get a list of configuration dictionaries
    config_list = get_config_list(api_keys, base_urls, api_type, api_version)
    ```

    """
    if base_urls is not None:
        assert len(api_keys) == len(base_urls), "The length of api_keys must match the length of base_urls"
    config_list = []
    for i, api_key in enumerate(api_keys):
        if not api_key.strip():
            continue
        config = {"api_key": api_key}
        if base_urls:
            config["base_url"] = base_urls[i]
        if api_type:
            config["api_type"] = api_type
        if api_version:
            config["api_version"] = api_version
        config_list.append(config)
    return config_list