Skip to content

Commit

Permalink
Make get_api_key_from_environment return nullable
Browse files Browse the repository at this point in the history
Sometimes key is not defined or set, we need to do this to unblock #769

The functionality is still unchanged because the optional param `required` defaults to true

## Test Plan
Pass all automated tests (which it didn't before)
  • Loading branch information
Rossdan Craig [email protected] committed Jan 5, 2024
1 parent c6a00d4 commit 1458985
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 22 deletions.
4 changes: 2 additions & 2 deletions cookbooks/HuggingFace/hf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ParameterizedModelParser,
Prompt,
PromptMetadata,
get_api_key_from_environment,
maybe_get_api_key_from_environment,
resolve_prompt,
)

Expand Down Expand Up @@ -153,7 +153,7 @@ def __init__(self, model_id: str = None, use_api_token=False):
token = None

if use_api_token:
token = get_api_key_from_environment("HUGGING_FACE_API_TOKEN")
token = maybe_get_api_key_from_environment("HUGGING_FACE_API_TOKEN")

self.client = InferenceClient(model_id, token=token)

Expand Down
4 changes: 2 additions & 2 deletions cookbooks/HuggingFace/python/hf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ParameterizedModelParser,
Prompt,
PromptMetadata,
get_api_key_from_environment,
maybe_get_api_key_from_environment,
resolve_prompt,
)

Expand Down Expand Up @@ -153,7 +153,7 @@ def __init__(self, model_id: str = None, use_api_token=False):
token = None

if use_api_token:
token = get_api_key_from_environment("HUGGING_FACE_API_TOKEN")
token = maybe_get_api_key_from_environment("HUGGING_FACE_API_TOKEN")

self.client = InferenceClient(model_id, token=token)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from aiconfig import (
AIConfigRuntime,
CallbackEvent,
get_api_key_from_environment,
maybe_get_api_key_from_environment,
)
from aiconfig.default_parsers.parameterized_model_parser import ParameterizedModelParser
from aiconfig.model_parser import InferenceOptions
Expand Down Expand Up @@ -326,7 +326,7 @@ async def run_inference(
)

# Auth check. don't need to explicitly set the key as long as this is set as an env var. genai.configure() will pick it up
get_api_key_from_environment("GOOGLE_API_KEY")
maybe_get_api_key_from_environment("GOOGLE_API_KEY")
genai.configure()

# TODO: check and handle api key here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Prompt,
PromptMetadata,
)
from aiconfig.util.config_utils import get_api_key_from_environment
from aiconfig.util.config_utils import maybe_get_api_key_from_environment
from aiconfig.util.params import resolve_prompt


Expand Down Expand Up @@ -154,7 +154,7 @@ def __init__(self, model_id: str = None, use_api_token=False):
token = None

if use_api_token:
token = get_api_key_from_environment("HUGGING_FACE_API_TOKEN")
token = maybe_get_api_key_from_environment("HUGGING_FACE_API_TOKEN")

self.client = InferenceClient(model_id, token=token)

Expand Down
2 changes: 1 addition & 1 deletion python/src/aiconfig/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
PromptMetadata,
SchemaVersion,
)
from .util.config_utils import get_api_key_from_environment
from .util.config_utils import maybe_get_api_key_from_environment
from .util.params import resolve_prompt
4 changes: 2 additions & 2 deletions python/src/aiconfig/default_parsers/dalle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import openai
from aiconfig.default_parsers.parameterized_model_parser import ParameterizedModelParser
from aiconfig.util.config_utils import get_api_key_from_environment
from aiconfig.util.config_utils import maybe_get_api_key_from_environment
from aiconfig.util.params import resolve_prompt
from openai import OpenAI

Expand Down Expand Up @@ -163,7 +163,7 @@ async def run_inference(self, prompt: Prompt, aiconfig, _options, parameters) ->
"""
# If needed, certify the API key and initialize the OpenAI client
if not openai.api_key:
openai.api_key = get_api_key_from_environment("OPENAI_API_KEY")
openai.api_key = maybe_get_api_key_from_environment("OPENAI_API_KEY")
if not self.client:
self.client = OpenAI(api_key=openai.api_key)

Expand Down
4 changes: 2 additions & 2 deletions python/src/aiconfig/default_parsers/hf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from aiconfig.default_parsers.parameterized_model_parser import ParameterizedModelParser
from aiconfig.model_parser import InferenceOptions
from aiconfig.util.config_utils import get_api_key_from_environment
from aiconfig.util.config_utils import maybe_get_api_key_from_environment
from aiconfig.util.params import resolve_prompt

# HuggingFace API imports
Expand Down Expand Up @@ -145,7 +145,7 @@ def __init__(self, model_id: str = None, use_api_token=False):
token = None

if use_api_token:
token = get_api_key_from_environment("HUGGING_FACE_API_TOKEN")
token = maybe_get_api_key_from_environment("HUGGING_FACE_API_TOKEN")

self.client = InferenceClient(model_id, token=token)

Expand Down
4 changes: 2 additions & 2 deletions python/src/aiconfig/default_parsers/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from aiconfig.callback import CallbackEvent
from aiconfig.default_parsers.parameterized_model_parser import ParameterizedModelParser
from aiconfig.model_parser import InferenceOptions
from aiconfig.util.config_utils import get_api_key_from_environment
from aiconfig.util.config_utils import maybe_get_api_key_from_environment
from aiconfig.util.params import resolve_prompt, resolve_prompt_string, resolve_system_prompt
from openai.types.chat import ChatCompletionMessage

Expand Down Expand Up @@ -235,7 +235,7 @@ async def run_inference(
)

if not openai.api_key:
openai.api_key = get_api_key_from_environment("OPENAI_API_KEY")
openai.api_key = maybe_get_api_key_from_environment("OPENAI_API_KEY")

completion_data = await self.deserialize(prompt, aiconfig, parameters)
# if stream enabled in runtime options and config, then stream. Otherwise don't stream.
Expand Down
26 changes: 22 additions & 4 deletions python/src/aiconfig/util/config_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
import dotenv
import os
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Union

if TYPE_CHECKING:
pass
Expand All @@ -10,10 +11,27 @@
from ..schema import AIConfig


def get_api_key_from_environment(api_key_name: str):
if api_key_name not in os.environ:
raise Exception("Missing API key '{}' in environment".format(api_key_name))
def maybe_get_api_key_from_environment(
api_key_name: str,
required: bool = True) -> Union[str, None]:
"""Get the API key if it exists, return None or error if it doesn't
Args:
api_key_name (str): The keyname that we're trying to import from env variable
required (bool, optional): If this is true, we raise an error if the
key is not found
Returns:
Union[str, None]: the value of the key. If `required` is false, this can be None
"""
dotenv.load_dotenv()
if required:
_get_api_key_from_environment(api_key_name)
return os.getenv(api_key_name)

def _get_api_key_from_environment(api_key_name: str) -> str:
if api_key_name not in os.environ:
raise KeyError(f"Missing API key '{api_key_name}' in environment")
return os.environ[api_key_name]


Expand Down
6 changes: 3 additions & 3 deletions python/tests/test_util/test_config_util.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from aiconfig.util.config_utils import get_api_key_from_environment
from aiconfig.util.config_utils import maybe_get_api_key_from_environment


def test_get_api_key_from_environment():
def test_maybe_get_api_key_from_environment():
key = "TEST_API_KEY"
try:
get_api_key_from_environment(key)
maybe_get_api_key_from_environment(key)
except Exception:
pass # The expected exception was raised, so do nothing
else:
Expand Down

0 comments on commit 1458985

Please sign in to comment.