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

openai_utils.py - functionality for instantiating config_list with a .env file #68

Merged
merged 16 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions autogen/oai/openai_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os
import json
import tempfile
from pathlib import Path
from typing import List, Optional, Dict, Set, Union
import logging
from dotenv import find_dotenv, load_dotenv


NON_CACHE_KEY = ["api_key", "api_base", "api_type", "api_version"]

Expand Down Expand Up @@ -239,3 +243,78 @@ def config_list_from_json(
except FileNotFoundError:
return []
return filter_config(config_list, filter_dict)


def config_list_from_dotenv(
dotenv_file_path: Optional[str] = None, api_key_env_var: str = "OPENAI_API_KEY", filter_dict: Optional[dict] = None
) -> List[Dict[str, Union[str, Set[str]]]]:
"""
Loads configuration details from a .env file or from the environment,
creates a temporary JSON structure, and sets configurations for autogen.

Args:
dotenv_file_path (str, optional): The path to the .env file. If not provided,
it will look for a .env file using the find_dotenv method from the dotenv module.
filter_dict (dict, optional): A dictionary containing the models to be loaded.
If not provided, it defaults to loading 'gpt-4' and 'gpt-3.5-turbo'.
api_key_env_var (str, optional): The name of the environment variable where the API key is stored.
Defaults to 'OPENAI_API_KEY'.

Returns:
config_list: A list of configurations loaded from the .env file or the environment.
Each configuration is a dictionary containing:
- model (str): The model name, e.g., 'gpt-4' or 'gpt-3.5-turbo'.
- api_key (str): The API key for OpenAI.

Raises:
ValueError: If no configurations are loaded or if the API key is not found in the environment variables.

Example:
>>> config_list_from_dotenv(dotenv_file_path='path_to_dotenv_file', api_key_env_var='OPENAI_API_KEY')
[
{'model': 'gpt-4', 'api_key': 'some_api_key'},
{'model': 'gpt-3.5-turbo', 'api_key': 'some_api_key'}
]
AaronWard marked this conversation as resolved.
Show resolved Hide resolved
"""
if dotenv_file_path:
dotenv_path = Path(dotenv_file_path)
if not dotenv_path.exists():
raise FileNotFoundError(f"The specified .env file {dotenv_file_path} does not exist.")
load_dotenv(dotenv_path)
else:
# if the find_dotenv method returns an empty string, it means it didn't find a .env file.
dotenv_path = find_dotenv()
if not dotenv_path:
logging.warning("No .env file found. Loading configurations from environment variables.")
else:
load_dotenv(dotenv_path)

openai_api_key = os.getenv(api_key_env_var)

if openai_api_key is None:
logging.error(f"{api_key_env_var} not found. Please ensure path to .env file is correct.")
return []

if not filter_dict:
filter_dict = {
"model": {
"gpt-4",
"gpt-3.5-turbo",
}
}

env_var = [{"model": model, "api_key": openai_api_key} for model in filter_dict["model"]]

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

config_list = config_list_from_json(env_or_file=temp.name, filter_dict=filter_dict)

if len(config_list) == 0:
logging.error("No configurations loaded.")
return []

logging.info(f"Models available: {[config['model'] for config in config_list]}")
return config_list
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"diskcache",
"termcolor",
"flaml",
"python-dotenv",
]


Expand Down
41 changes: 41 additions & 0 deletions test/oai/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import json
import os
import autogen
import pytest
import tempfile
from test_completion import KEY_LOC, OAI_CONFIG_LIST

from autogen.oai.openai_utils import config_list_from_dotenv


def test_config_list_from_json():
config_list = autogen.config_list_gpt4_gpt35(key_file_path=KEY_LOC)
Expand All @@ -27,5 +31,42 @@ def test_config_list_openai_aoai():
assert all(config.get("api_type") in [None, "open_ai", "azure"] for config in config_list)


@pytest.fixture
def dotenv_file():
with tempfile.NamedTemporaryFile(mode="w+", delete=True) as temp:
temp.write("OPENAI_API_KEY=SomeAPIKey")
temp.flush()
yield temp.name


def test_config_list_from_dotenv(dotenv_file):
# Test valid case
config_list = config_list_from_dotenv(dotenv_file_path=dotenv_file)
assert config_list, "Configuration list is empty in valid case"
assert all(config["api_key"] == "SomeAPIKey" for config in config_list), "API Key mismatch in valid case"

# Test invalid path case
with pytest.raises(FileNotFoundError, match="The specified .env file invalid_path does not exist."):
config_list_from_dotenv(dotenv_file_path="invalid_path")

# Test no API key case
with tempfile.NamedTemporaryFile(mode="w+", delete=True) as temp:
temp.write("DIFFERENT_API_KEY=SomeAPIKey")
temp.flush()
with pytest.raises(
ValueError, match=f"{autogen.api_key_env_var} not found. Please ensure path to .env file is correct."
):
config_list_from_dotenv(dotenv_file_path=temp.name)

# Test empty API key case
with tempfile.NamedTemporaryFile(mode="w+", delete=True) as temp:
temp.write("OPENAI_API_KEY= ")
temp.flush()
with pytest.raises(
ValueError, match=f"{autogen.api_key_env_var} not found. Please ensure path to .env file is correct."
):
config_list_from_dotenv(dotenv_file_path=temp.name)


if __name__ == "__main__":
test_config_list_from_json()