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
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
18 changes: 18 additions & 0 deletions notebook/coding/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# filename: bubble_sort.py
BeibinLi marked this conversation as resolved.
Show resolved Hide resolved
def bubble_sort(array):
length = len(array)

for i in range(length - 1):
swapped = False
for j in range(0, length - i - 1):
if array[j] > array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]
swapped = True
if not swapped:
break

return array


array = [4, 1, 3, 5, 2]
print(bubble_sort(array))
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
Loading