Skip to content

config_list_from_models

autogen.config_list_from_models #

config_list_from_models(key_file_path='.', openai_api_key_file='key_openai.txt', aoai_api_key_file='key_aoai.txt', aoai_api_base_file='base_aoai.txt', exclude=None, model_list=None)

Get a list of configs for API calls with models specified in the model list.

This function extends config_list_openai_aoai by allowing to clone its' out for each of the models provided. Each configuration will have a 'model' key with the model name as its value. This is particularly useful when all endpoints have same set of models.

PARAMETER DESCRIPTION
key_file_path

The path to the key files.

TYPE: str DEFAULT: '.'

openai_api_key_file

The file name of the OpenAI API key.

TYPE: str DEFAULT: 'key_openai.txt'

aoai_api_key_file

The file name of the Azure OpenAI API key.

TYPE: str DEFAULT: 'key_aoai.txt'

aoai_api_base_file

The file name of the Azure OpenAI API base.

TYPE: str DEFAULT: 'base_aoai.txt'

exclude

The API type to exclude, "openai" or "aoai".

TYPE: str DEFAULT: None

model_list

The list of model names to include in the configs.

TYPE: list DEFAULT: None

RETURNS DESCRIPTION
list

A list of configs for OpenAI API calls, each including model information.

TYPE: list[dict[str, Any]]

Example:

# Define the path where the API key files are located
key_file_path = "/path/to/key/files"

# Define the file names for the OpenAI and Azure OpenAI API keys and bases
openai_api_key_file = "key_openai.txt"
aoai_api_key_file = "key_aoai.txt"
aoai_api_base_file = "base_aoai.txt"

# Define the list of models for which to create configurations
model_list = ["gpt-4", "gpt-3.5-turbo"]

# Call the function to get a list of configuration dictionaries
config_list = config_list_from_models(
    key_file_path=key_file_path,
    openai_api_key_file=openai_api_key_file,
    aoai_api_key_file=aoai_api_key_file,
    aoai_api_base_file=aoai_api_base_file,
    model_list=model_list,
)

# The `config_list` will contain configurations for the specified models, for example:
# [
#     {'api_key': '...', 'base_url': 'https://api.openai.com', 'model': 'gpt-4'},
#     {'api_key': '...', 'base_url': 'https://api.openai.com', 'model': 'gpt-3.5-turbo'}
# ]

Source code in autogen/oai/openai_utils.py
@export_module("autogen")
def config_list_from_models(
    key_file_path: Optional[str] = ".",
    openai_api_key_file: Optional[str] = "key_openai.txt",
    aoai_api_key_file: Optional[str] = "key_aoai.txt",
    aoai_api_base_file: Optional[str] = "base_aoai.txt",
    exclude: Optional[str] = None,
    model_list: Optional[list[str]] = None,
) -> list[dict[str, Any]]:
    """Get a list of configs for API calls with models specified in the model list.

    This function extends `config_list_openai_aoai` by allowing to clone its' out for each of the models provided.
    Each configuration will have a 'model' key with the model name as its value. This is particularly useful when
    all endpoints have same set of models.

    Args:
        key_file_path (str, optional): The path to the key files.
        openai_api_key_file (str, optional): The file name of the OpenAI API key.
        aoai_api_key_file (str, optional): The file name of the Azure OpenAI API key.
        aoai_api_base_file (str, optional): The file name of the Azure OpenAI API base.
        exclude (str, optional): The API type to exclude, "openai" or "aoai".
        model_list (list, optional): The list of model names to include in the configs.

    Returns:
        list: A list of configs for OpenAI API calls, each including model information.

    Example:
    ```python
    # Define the path where the API key files are located
    key_file_path = "/path/to/key/files"

    # Define the file names for the OpenAI and Azure OpenAI API keys and bases
    openai_api_key_file = "key_openai.txt"
    aoai_api_key_file = "key_aoai.txt"
    aoai_api_base_file = "base_aoai.txt"

    # Define the list of models for which to create configurations
    model_list = ["gpt-4", "gpt-3.5-turbo"]

    # Call the function to get a list of configuration dictionaries
    config_list = config_list_from_models(
        key_file_path=key_file_path,
        openai_api_key_file=openai_api_key_file,
        aoai_api_key_file=aoai_api_key_file,
        aoai_api_base_file=aoai_api_base_file,
        model_list=model_list,
    )

    # The `config_list` will contain configurations for the specified models, for example:
    # [
    #     {'api_key': '...', 'base_url': 'https://api.openai.com', 'model': 'gpt-4'},
    #     {'api_key': '...', 'base_url': 'https://api.openai.com', 'model': 'gpt-3.5-turbo'}
    # ]
    ```
    """
    config_list = config_list_openai_aoai(
        key_file_path=key_file_path,
        openai_api_key_file=openai_api_key_file,
        aoai_api_key_file=aoai_api_key_file,
        aoai_api_base_file=aoai_api_base_file,
        exclude=exclude,
    )
    if model_list:
        config_list = [{**config, "model": model} for model in model_list for config in config_list]
    return config_list