Skip to content

A programming framework for agentic AI

[Core] Improve config_list_from_json (#1026)

Beibin Lib26e659362da
diff --git a/autogen/oai/openai_utils.py b/autogen/oai/openai_utils.py
index 251397cda..70662668d 100644
--- a/autogen/oai/openai_utils.py
+++ b/autogen/oai/openai_utils.py
@@ -1,9 +1,10 @@
-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 typing import Dict, List, Optional, Set, Union
+
from dotenv import find_dotenv, load_dotenv
try:
@@ -416,7 +417,8 @@ def config_list_from_json(
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 or the filename containing the JSON data.
+ 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.
@@ -435,10 +437,21 @@ def config_list_from_json(
Returns:
List[Dict]: A list of configuration dictionaries that match the filtering criteria specified in `filter_dict`.
"""
- 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.
+ with open(env_str, "r") 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)
try:
with open(config_list_path) as json_file:
diff --git a/test/oai/test_utils.py b/test/oai/test_utils.py
index 921cf1360..7de8dc322 100644
--- a/test/oai/test_utils.py
+++ b/test/oai/test_utils.py
@@ -1,10 +1,12 @@
-import os
import json
-import pytest
import logging
+import os
import tempfile
from unittest import mock
from unittest.mock import patch
+
+import pytest
+
import autogen # noqa: E402
from autogen.oai.openai_utils import DEFAULT_AZURE_API_VERSION
@@ -87,6 +89,7 @@ def test_config_list_from_json():
config_list_2 = autogen.config_list_from_json("config_list_test")
assert config_list == config_list_2
+ # Test: the env variable is set to a file path with folder name inside.
config_list_3 = autogen.config_list_from_json(
tmp_file.name, filter_dict={"model": ["gpt", "gpt-4", "gpt-4-32k"]}
)
@@ -94,6 +97,24 @@ def test_config_list_from_json():
del os.environ["config_list_test"]
+ # Test: using the `file_location` parameter.
+ config_list_4 = autogen.config_list_from_json(
+ os.path.basename(tmp_file.name),
+ file_location=os.path.dirname(tmp_file.name),
+ filter_dict={"model": ["gpt4", "gpt-4-32k"]},
+ )
+
+ assert all(config.get("model") in ["gpt4", "gpt-4-32k"] for config in 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"]
+
def test_config_list_openai_aoai():
# Testing the functionality for loading configurations for different API types