Skip to content

Commit

Permalink
UPDATE - code and tests to potentially get around the window build pe…
Browse files Browse the repository at this point in the history
…rmission error, used different method of producing temporary files
  • Loading branch information
Ward authored and Ward committed Oct 4, 2023
1 parent 3b0adf6 commit d0cfdaf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
19 changes: 12 additions & 7 deletions autogen/oai/openai_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,18 @@ def config_list_from_dotenv(
config_dict["model"] = model
env_var.append(config_dict)

with tempfile.NamedTemporaryFile(mode="w+", delete=True) as temp:
env_var_str = json.dumps(env_var)
temp.write(env_var_str)
temp.flush()

# Assuming config_list_from_json is a valid function from your code
config_list = config_list_from_json(env_or_file=temp.name, filter_dict=filter_dict)
fd, temp_name = tempfile.mkstemp()
try:
with os.fdopen(fd, "w+") as temp:
env_var_str = json.dumps(env_var)
temp.write(env_var_str)
temp.flush()

# Assuming config_list_from_json is a valid function from your code
config_list = config_list_from_json(env_or_file=temp_name, filter_dict=filter_dict)
finally:
# The file is deleted after using its name (to prevent windows build from breaking)
os.remove(temp_name)

if len(config_list) == 0:
logging.error("No configurations loaded.")
Expand Down
51 changes: 27 additions & 24 deletions test/oai/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,33 @@ def test_config_list_openai_aoai():

def test_config_list_from_dotenv(mock_os_environ, caplog):
# Test with valid .env file
with tempfile.NamedTemporaryFile(mode="w+", delete=True) as temp:
temp.write("\n".join([f"{k}={v}" for k, v in ENV_VARS.items()]))
temp.flush()

# Use the updated config_list_from_dotenv function
config_list = autogen.config_list_from_dotenv(dotenv_file_path=temp.name)

# Ensure configurations are loaded and API keys match expected values
assert config_list, "Config list is empty with default API keys"

# Check that configurations only include models specified in the filter
for config in config_list:
assert config["model"] in FILTER_DICT["model"], f"Model {config['model']} not in filter"

# Check the default API key for gpt-4 and gpt-3.5-turbo when model_api_key_map is None
config_list = autogen.config_list_from_dotenv(dotenv_file_path=temp.name, model_api_key_map=None)

expected_api_key = os.getenv("OPENAI_API_KEY")
assert any(
config["model"] == "gpt-4" and config["api_key"] == expected_api_key for config in config_list
), "Default gpt-4 configuration not found or incorrect"
assert any(
config["model"] == "gpt-3.5-turbo" and config["api_key"] == expected_api_key for config in config_list
), "Default gpt-3.5-turbo configuration not found or incorrect"
fd, temp_name = tempfile.mkstemp()
try:
with os.fdopen(fd, "w+") as temp:
temp.write("\n".join([f"{k}={v}" for k, v in ENV_VARS.items()]))
temp.flush()
# Use the updated config_list_from_dotenv function
config_list = autogen.config_list_from_dotenv(dotenv_file_path=temp_name)

# Ensure configurations are loaded and API keys match expected values
assert config_list, "Config list is empty with default API keys"

# Check that configurations only include models specified in the filter
for config in config_list:
assert config["model"] in FILTER_DICT["model"], f"Model {config['model']} not in filter"

# Check the default API key for gpt-4 and gpt-3.5-turbo when model_api_key_map is None
config_list = autogen.config_list_from_dotenv(dotenv_file_path=temp_name, model_api_key_map=None)

expected_api_key = os.getenv("OPENAI_API_KEY")
assert any(
config["model"] == "gpt-4" and config["api_key"] == expected_api_key for config in config_list
), "Default gpt-4 configuration not found or incorrect"
assert any(
config["model"] == "gpt-3.5-turbo" and config["api_key"] == expected_api_key for config in config_list
), "Default gpt-3.5-turbo configuration not found or incorrect"
finally:
os.remove(temp_name) # The file is deleted after using its name (to prevent windows build from breaking)

# Test with missing dotenv file
with caplog.at_level(logging.WARNING):
Expand Down

0 comments on commit d0cfdaf

Please sign in to comment.