From 647f7b57a0622700bddc8d54b40585f5fe232b69 Mon Sep 17 00:00:00 2001 From: Beibin Li Date: Tue, 19 Dec 2023 10:10:32 -0800 Subject: [PATCH 1/5] Improve config_list_from_json The `env_or_file` variabe can point to an environment variable of file path. --- autogen/oai/openai_utils.py | 28 ++++++++++++++++++++-------- test/oai/test_utils.py | 22 +++++++++++++++++++--- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/autogen/oai/openai_utils.py b/autogen/oai/openai_utils.py index 966646cdb802..c27ec65f7880 100644 --- a/autogen/oai/openai_utils.py +++ b/autogen/oai/openai_utils.py @@ -1,11 +1,12 @@ -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 dotenv import find_dotenv, load_dotenv +from typing import Dict, List, Optional, Set, Union +from dotenv import find_dotenv, load_dotenv +from genericpath import isdir NON_CACHE_KEY = ["api_key", "base_url", "api_type", "api_version"] @@ -248,8 +249,9 @@ def config_list_from_json( """Get a list of configs from a json parsed from an env variable or a file. Args: - env_or_file (str): The env variable name or file name. - file_location (str, optional): The file location. + env_or_file (str): The env variable name, or file name, or the env + variable for the file name. + file_location (str, optional): The folder where the config file sits in. filter_dict (dict, optional): The filter dict with keys corresponding to a field in each config, and values corresponding to lists of acceptable values for each key. e.g., @@ -263,10 +265,20 @@ def config_list_from_json( Returns: list: A list of configs for openai api calls. """ - 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. + json_str = open(env_str).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 579fc6f9d8a2..90d159224e1c 100644 --- a/test/oai/test_utils.py +++ b/test/oai/test_utils.py @@ -1,10 +1,12 @@ -import os -import sys import json -import pytest import logging +import os +import sys import tempfile from unittest import mock + +import pytest + import autogen # noqa: E402 KEY_LOC = "notebook" @@ -64,8 +66,22 @@ def test_config_list_from_json(): config_list_3 = autogen.config_list_from_json( OAI_CONFIG_LIST, file_location=KEY_LOC, filter_dict={"model": ["gpt4", "gpt-4-32k"]} ) + assert all(config.get("model") in ["gpt4", "gpt-4-32k"] for config in config_list_3) + # Test: the env variable is set to a file path with folder name inside. + config_list_4 = autogen.config_list_from_json( + os.path.join(KEY_LOC, OAI_CONFIG_LIST), filter_dict={"model": ["gpt4", "gpt-4-32k"]} + ) + assert config_list_3 == 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"] os.remove(json_file) From 935bf5f2d2b4393a60071d2c249ce335b9dea2d0 Mon Sep 17 00:00:00 2001 From: Beibin Li Date: Sat, 30 Dec 2023 14:00:49 -0800 Subject: [PATCH 2/5] Update autogen/oai/openai_utils.py Co-authored-by: Chi Wang --- autogen/oai/openai_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogen/oai/openai_utils.py b/autogen/oai/openai_utils.py index 5e6800f0c77c..2ef32bc8fdda 100644 --- a/autogen/oai/openai_utils.py +++ b/autogen/oai/openai_utils.py @@ -405,7 +405,7 @@ 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, the filename, or the environment variable of the filename + 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 From 023a3dc2df28acd4355eefb03f042d3eb3f85ada Mon Sep 17 00:00:00 2001 From: Beibin Li Date: Sat, 30 Dec 2023 14:03:17 -0800 Subject: [PATCH 3/5] Use "with" to open config file --- autogen/oai/openai_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/autogen/oai/openai_utils.py b/autogen/oai/openai_utils.py index 5e6800f0c77c..948433fc7b66 100644 --- a/autogen/oai/openai_utils.py +++ b/autogen/oai/openai_utils.py @@ -431,7 +431,8 @@ def config_list_from_json( # 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. - json_str = open(env_str).read() + 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 From 13ce2c4c39d73d06f574d6e11de79fea867309af Mon Sep 17 00:00:00 2001 From: Beibin Li Date: Mon, 1 Jan 2024 16:32:11 -0800 Subject: [PATCH 4/5] Remove unused. --- notebook/coding/bubble_sort.py | 18 ++++++++++++++++++ test/oai/test_utils.py | 1 - 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 notebook/coding/bubble_sort.py diff --git a/notebook/coding/bubble_sort.py b/notebook/coding/bubble_sort.py new file mode 100644 index 000000000000..72076eea17c0 --- /dev/null +++ b/notebook/coding/bubble_sort.py @@ -0,0 +1,18 @@ +# filename: bubble_sort.py +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)) diff --git a/test/oai/test_utils.py b/test/oai/test_utils.py index ac94f1157526..7de8dc3227e1 100644 --- a/test/oai/test_utils.py +++ b/test/oai/test_utils.py @@ -1,7 +1,6 @@ import json import logging import os -import sys import tempfile from unittest import mock from unittest.mock import patch From 361e90d97cf3d0254fd9f98d6719a02cfbe0a16e Mon Sep 17 00:00:00 2001 From: Beibin Li Date: Tue, 2 Jan 2024 09:54:59 -0800 Subject: [PATCH 5/5] Remove accidental added file --- notebook/coding/bubble_sort.py | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 notebook/coding/bubble_sort.py diff --git a/notebook/coding/bubble_sort.py b/notebook/coding/bubble_sort.py deleted file mode 100644 index 72076eea17c0..000000000000 --- a/notebook/coding/bubble_sort.py +++ /dev/null @@ -1,18 +0,0 @@ -# filename: bubble_sort.py -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))