Skip to content

Commit e31a6fa

Browse files
BeibinLiekzhusonichi
authored
[Core] Improve config_list_from_json (microsoft#1026)
* Improve config_list_from_json The `env_or_file` variabe can point to an environment variable of file path. * Update autogen/oai/openai_utils.py Co-authored-by: Chi Wang <[email protected]> * Use "with" to open config file * Remove unused. * Remove accidental added file --------- Co-authored-by: Eric Zhu <[email protected]> Co-authored-by: Chi Wang <[email protected]>
1 parent 06e2121 commit e31a6fa

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

autogen/oai/openai_utils.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import os
21
import json
2+
import logging
3+
import os
34
import tempfile
45
from pathlib import Path
5-
from typing import List, Optional, Dict, Set, Union
6-
import logging
6+
from typing import Dict, List, Optional, Set, Union
7+
78
from dotenv import find_dotenv, load_dotenv
89

910
try:
@@ -416,7 +417,8 @@ def config_list_from_json(
416417
of acceptable values for that field, the configuration will still be considered a match.
417418
418419
Args:
419-
env_or_file (str): The name of the environment variable or the filename containing the JSON data.
420+
env_or_file (str): The name of the environment variable, the filename, or the environment variable of the filename
421+
that containing the JSON data.
420422
file_location (str, optional): The directory path where the file is located, if `env_or_file` is a filename.
421423
filter_dict (dict, optional): A dictionary specifying the filtering criteria for the configurations, with
422424
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(
435437
Returns:
436438
List[Dict]: A list of configuration dictionaries that match the filtering criteria specified in `filter_dict`.
437439
"""
438-
json_str = os.environ.get(env_or_file)
439-
if json_str:
440+
env_str = os.environ.get(env_or_file)
441+
442+
if env_str:
443+
# The environment variable exists. We should use information from it.
444+
if os.path.exists(env_str):
445+
# It is a file location, and we need to load the json from the file.
446+
with open(env_str, "r") as file:
447+
json_str = file.read()
448+
else:
449+
# Else, it should be a JSON string by itself.
450+
json_str = env_str
440451
config_list = json.loads(json_str)
441452
else:
453+
# The environment variable does not exist.
454+
# So, `env_or_file` is a filename. We should use the file location.
442455
config_list_path = os.path.join(file_location, env_or_file)
443456
try:
444457
with open(config_list_path) as json_file:

test/oai/test_utils.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import os
21
import json
3-
import pytest
42
import logging
3+
import os
54
import tempfile
65
from unittest import mock
76
from unittest.mock import patch
7+
8+
import pytest
9+
810
import autogen # noqa: E402
911
from autogen.oai.openai_utils import DEFAULT_AZURE_API_VERSION
1012

@@ -87,13 +89,32 @@ def test_config_list_from_json():
8789
config_list_2 = autogen.config_list_from_json("config_list_test")
8890
assert config_list == config_list_2
8991

92+
# Test: the env variable is set to a file path with folder name inside.
9093
config_list_3 = autogen.config_list_from_json(
9194
tmp_file.name, filter_dict={"model": ["gpt", "gpt-4", "gpt-4-32k"]}
9295
)
9396
assert all(config.get("model") in ["gpt-4", "gpt"] for config in config_list_3)
9497

9598
del os.environ["config_list_test"]
9699

100+
# Test: using the `file_location` parameter.
101+
config_list_4 = autogen.config_list_from_json(
102+
os.path.basename(tmp_file.name),
103+
file_location=os.path.dirname(tmp_file.name),
104+
filter_dict={"model": ["gpt4", "gpt-4-32k"]},
105+
)
106+
107+
assert all(config.get("model") in ["gpt4", "gpt-4-32k"] for config in config_list_4)
108+
109+
# Test: the env variable is set to a file path.
110+
fd, temp_name = tempfile.mkstemp()
111+
json.dump(config_list, os.fdopen(fd, "w+"), indent=4)
112+
os.environ["config_list_test"] = temp_name
113+
config_list_5 = autogen.config_list_from_json("config_list_test")
114+
assert config_list_5 == config_list_2
115+
116+
del os.environ["config_list_test"]
117+
97118

98119
def test_config_list_openai_aoai():
99120
# Testing the functionality for loading configurations for different API types

0 commit comments

Comments
 (0)