[Core] Throw an error when the OAI_CONFIG_LIST is missing. (#1082)

* Throw an explicit and proper error when someone asks to load the OAI_CONFIG_LIST, and it is missing.

* Updated to use pytest.raises. Added docstring. Updated some tests to not try to load the config_list when skipping OAI tests.

* Fixed wrong indentation in config_list_from_json, and updated test_utils to work with non-empty lists.

* Read key location from global constants.

* Added missingpath.

* Moved config_list_from_json to inside a skip check.
This commit is contained in:
afourney
2024-01-03 08:58:58 -08:00
committed by GitHub
parent fed9384414
commit 4fcc45f140
4 changed files with 26 additions and 20 deletions

View File

@@ -436,6 +436,9 @@ def config_list_from_json(
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)
@@ -453,12 +456,8 @@ def config_list_from_json(
# 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:
config_list = json.load(json_file)
except FileNotFoundError:
logging.warning(f"The specified config_list file '{config_list_path}' does not exist.")
return []
with open(config_list_path) as json_file:
config_list = json.load(json_file)
return filter_config(config_list, filter_dict)

View File

@@ -6,25 +6,27 @@ from conftest import skip_openai
from autogen.agentchat.contrib.compressible_agent import CompressibleAgent
here = os.path.abspath(os.path.dirname(__file__))
KEY_LOC = "notebook"
OAI_CONFIG_LIST = "OAI_CONFIG_LIST"
config_list = autogen.config_list_from_json(
OAI_CONFIG_LIST,
file_location=KEY_LOC,
filter_dict={
"model": ["gpt-3.5-turbo", "gpt-35-turbo", "gpt-3.5-turbo-16k", "gpt-35-turbo-16k"],
},
)
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from test_assistant_agent import OAI_CONFIG_LIST, KEY_LOC # noqa: E402
try:
import openai
except ImportError:
skip = True
else:
skip = False or skip_openai
if not skip:
config_list = autogen.config_list_from_json(
OAI_CONFIG_LIST,
file_location=KEY_LOC,
filter_dict={
"model": ["gpt-3.5-turbo", "gpt-35-turbo", "gpt-3.5-turbo-16k", "gpt-35-turbo-16k"],
},
)
@pytest.mark.skipif(
sys.platform in ["darwin", "win32"] or skip,

View File

@@ -12,14 +12,16 @@ try:
import openai
from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent
from autogen.oai.openai_utils import retrieve_assistants_by_name
except ImportError:
skip = True
else:
skip = False or skip_openai
config_list = autogen.config_list_from_json(
OAI_CONFIG_LIST, file_location=KEY_LOC, filter_dict={"api_type": ["openai"]}
)
if not skip:
config_list = autogen.config_list_from_json(
OAI_CONFIG_LIST, file_location=KEY_LOC, filter_dict={"api_type": ["openai"]}
)
def ask_ossinsight(question):

View File

@@ -73,7 +73,6 @@ def test_config_list_from_json():
json_data = json.loads(JSON_SAMPLE)
tmp_file.write(JSON_SAMPLE)
tmp_file.flush()
config_list = autogen.config_list_from_json(tmp_file.name)
assert len(config_list) == len(json_data)
@@ -115,6 +114,10 @@ def test_config_list_from_json():
del os.environ["config_list_test"]
# Test that an error is thrown when the config list is missing
with pytest.raises(FileNotFoundError):
autogen.config_list_from_json("OAI_CONFIG_LIST.missing")
def test_config_list_openai_aoai():
# Testing the functionality for loading configurations for different API types