Skip to content

config_list_from_json

autogen.config_list_from_json #

config_list_from_json(env_or_file, file_location='', filter_dict=None)

Retrieves a list of API configurations from a JSON stored in an environment variable or a file.

This function attempts to parse JSON data from the given env_or_file parameter. If env_or_file is an environment variable containing JSON data, it will be used directly. Otherwise, it is assumed to be a filename, and the function will attempt to read the file from the specified file_location.

The filter_dict parameter allows for filtering the configurations based on specified criteria. Each key in the filter_dict corresponds to a field in the configuration dictionaries, and the associated value is a list or set of acceptable values for that field. If a field is missing in a configuration and None is included in the list of acceptable values for that field, the configuration will still be considered a match.

PARAMETER DESCRIPTION
env_or_file

The name of the environment variable, the filename, or the environment variable of the filename that containing the JSON data.

TYPE: str

file_location

The directory path where the file is located, if env_or_file is a filename.

TYPE: str DEFAULT: ''

filter_dict

A dictionary specifying the filtering criteria for the configurations, with keys representing field names and values being lists or sets of acceptable values for those fields.

TYPE: dict DEFAULT: None

Example:

# Suppose we have an environment variable 'CONFIG_JSON' with the following content:
# '[{"model": "gpt-3.5-turbo", "api_type": "azure"}, {"model": "gpt-4"}]'

# We can retrieve a filtered list of configurations like this:
filter_criteria = {"model": ["gpt-3.5-turbo"]}
configs = config_list_from_json("CONFIG_JSON", filter_dict=filter_criteria)
# The 'configs' variable will now contain only the configurations that match the filter criteria.

RETURNS DESCRIPTION
list[dict[str, Any]]

List[Dict]: A list of configuration dictionaries that match the filtering criteria specified in filter_dict.

RAISES DESCRIPTION
FileNotFoundError

if env_or_file is neither found as an environment variable nor a file

Source code in autogen/oai/openai_utils.py
@export_module("autogen")
def config_list_from_json(
    env_or_file: str,
    file_location: Optional[str] = "",
    filter_dict: Optional[dict[str, Union[list[Union[str, None]], set[Union[str, None]]]]] = None,
) -> list[dict[str, Any]]:
    """Retrieves a list of API configurations from a JSON stored in an environment variable or a file.

    This function attempts to parse JSON data from the given `env_or_file` parameter. If `env_or_file` is an
    environment variable containing JSON data, it will be used directly. Otherwise, it is assumed to be a filename,
    and the function will attempt to read the file from the specified `file_location`.

    The `filter_dict` parameter allows for filtering the configurations based on specified criteria. Each key in the
    `filter_dict` corresponds to a field in the configuration dictionaries, and the associated value is a list or set
    of acceptable values for that field. If a field is missing in a configuration and `None` is included in the list
    of acceptable values for that field, the configuration will still be considered a match.

    Args:
        env_or_file (str): The name of the environment variable, the filename, or the environment variable of the filename
            that containing the JSON data.
        file_location (str, optional): The directory path where the file is located, if `env_or_file` is a filename.
        filter_dict (dict, optional): A dictionary specifying the filtering criteria for the configurations, with
            keys representing field names and values being lists or sets of acceptable values for those fields.

    Example:
    ```python
    # Suppose we have an environment variable 'CONFIG_JSON' with the following content:
    # '[{"model": "gpt-3.5-turbo", "api_type": "azure"}, {"model": "gpt-4"}]'

    # We can retrieve a filtered list of configurations like this:
    filter_criteria = {"model": ["gpt-3.5-turbo"]}
    configs = config_list_from_json("CONFIG_JSON", filter_dict=filter_criteria)
    # The 'configs' variable will now contain only the configurations that match the filter criteria.
    ```

    Returns:
        List[Dict]: A list of configuration dictionaries that match the filtering criteria specified in `filter_dict`.

    Raises:
        FileNotFoundError: if env_or_file is neither found as an environment variable nor a file
    """
    env_str = os.environ.get(env_or_file)

    if env_str:
        # The environment variable exists. We should use information from it.
        if os.path.exists(env_str):
            # It is a file location, and we need to load the json from the file.
            with open(env_str) as file:
                json_str = file.read()
        else:
            # Else, it should be a JSON string by itself.
            json_str = env_str
        config_list = json.loads(json_str)
    else:
        # The environment variable does not exist.
        # So, `env_or_file` is a filename. We should use the file location.
        config_list_path = os.path.join(file_location, env_or_file) if file_location is not None else env_or_file

        with open(config_list_path) as json_file:
            config_list = json.load(json_file)

    config_list = filter_config(config_list, filter_dict)

    return filter_config(config_list, filter_dict)