Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Improve config_list_from_json #1026

Merged
merged 11 commits into from
Jan 2, 2024
28 changes: 20 additions & 8 deletions autogen/oai/openai_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
import json
import logging
import os
import tempfile
from pathlib import Path
from typing import List, Optional, Dict, Set, Union
import logging
from dotenv import find_dotenv, load_dotenv
from typing import Dict, List, Optional, Set, Union

from dotenv import find_dotenv, load_dotenv
from genericpath import isdir

NON_CACHE_KEY = ["api_key", "base_url", "api_type", "api_version"]

Expand Down Expand Up @@ -248,8 +249,9 @@ def config_list_from_json(
"""Get a list of configs from a json parsed from an env variable or a file.

Args:
env_or_file (str): The env variable name or file name.
file_location (str, optional): The file location.
env_or_file (str): The env variable name, or file name, or the env
variable for the file name.
file_location (str, optional): The folder where the config file sits in.
filter_dict (dict, optional): The filter dict with keys corresponding to a field in each config,
and values corresponding to lists of acceptable values for each key.
e.g.,
Expand All @@ -263,10 +265,20 @@ def config_list_from_json(
Returns:
list: A list of configs for openai api calls.
"""
json_str = os.environ.get(env_or_file)
if json_str:
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.
json_str = open(env_str).read()
else:
BeibinLi marked this conversation as resolved.
Show resolved Hide resolved
# 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)
try:
with open(config_list_path) as json_file:
Expand Down
22 changes: 19 additions & 3 deletions test/oai/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os
import sys
import json
import pytest
import logging
import os
import sys
import tempfile
BeibinLi marked this conversation as resolved.
Show resolved Hide resolved
from unittest import mock

import pytest

import autogen # noqa: E402

KEY_LOC = "notebook"
Expand Down Expand Up @@ -64,8 +66,22 @@ def test_config_list_from_json():
config_list_3 = autogen.config_list_from_json(
OAI_CONFIG_LIST, file_location=KEY_LOC, filter_dict={"model": ["gpt4", "gpt-4-32k"]}
)

assert all(config.get("model") in ["gpt4", "gpt-4-32k"] for config in config_list_3)

# Test: the env variable is set to a file path with folder name inside.
config_list_4 = autogen.config_list_from_json(
os.path.join(KEY_LOC, OAI_CONFIG_LIST), filter_dict={"model": ["gpt4", "gpt-4-32k"]}
)
assert config_list_3 == config_list_4

# Test: the env variable is set to a file path.
fd, temp_name = tempfile.mkstemp()
json.dump(config_list, os.fdopen(fd, "w+"), indent=4)
os.environ["config_list_test"] = temp_name
config_list_5 = autogen.config_list_from_json("config_list_test")
assert config_list_5 == config_list_2

del os.environ["config_list_test"]
os.remove(json_file)

Expand Down
Loading