Skip to content

Commit

Permalink
[Core] Improve config_list_from_json (microsoft#1026)
Browse files Browse the repository at this point in the history
* 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 <wang.chi@microsoft.com>

* Use "with" to open config file

* Remove unused.

* Remove accidental added file

---------

Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>
Co-authored-by: Chi Wang <wang.chi@microsoft.com>
  • Loading branch information
3 people authored Jan 2, 2024
1 parent d20bc09 commit b26e659
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
25 changes: 19 additions & 6 deletions autogen/oai/openai_utils.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down
25 changes: 23 additions & 2 deletions test/oai/test_utils.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -87,13 +89,32 @@ 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"]}
)
assert all(config.get("model") in ["gpt-4", "gpt"] for config in config_list_3)

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
Expand Down

0 comments on commit b26e659

Please sign in to comment.