Skip to content

Commit 6def3ce

Browse files
committed
fix whitespace
1 parent f1449f7 commit 6def3ce

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

src/together/client.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from together.error import AuthenticationError
1010
from together.types import TogetherClient
1111
from together.utils import enforce_trailing_slash
12+
from together.utils.api_helpers import get_google_colab_secret
1213

1314

1415
class Together:
@@ -45,22 +46,8 @@ def __init__(
4546
if not api_key:
4647
api_key = os.environ.get("TOGETHER_API_KEY")
4748

48-
# If running in Google Colab, check notebook secrets
4949
if not api_key and "google.colab" in sys.modules:
50-
if TYPE_CHECKING:
51-
from google.colab import userdata # type: ignore
52-
else:
53-
from google.colab import userdata
54-
try:
55-
api_key = userdata.get("TOGETHER_API_KEY")
56-
except userdata.NotebookAccessError:
57-
print(
58-
"The TOGETHER_API_KEY Colab secret was found, but notebook access is disabled. Please enable notebook "
59-
"access for the secret."
60-
)
61-
except userdata.SecretNotFoundError:
62-
# warn and carry on
63-
print("Colab: No Google Colab secret named TOGETHER_API_KEY was found.")
50+
api_key = get_google_colab_secret("TOGETHER_API_KEY")
6451

6552
if not api_key:
6653
raise AuthenticationError(
@@ -135,6 +122,9 @@ def __init__(
135122
if not api_key:
136123
api_key = os.environ.get("TOGETHER_API_KEY")
137124

125+
if not api_key and "google.colab" in sys.modules:
126+
api_key = get_google_colab_secret("TOGETHER_API_KEY")
127+
138128
if not api_key:
139129
raise AuthenticationError(
140130
"The api_key client option must be set either by passing api_key to the client or by setting the "

src/together/utils/api_helpers.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import os
5+
import sys
56
import platform
67
from typing import TYPE_CHECKING, Any, Dict
78

@@ -12,6 +13,7 @@
1213
import together
1314
from together import error
1415
from together.utils._log import _console_log_level
16+
from together.utils import log_info
1517

1618

1719
def get_headers(
@@ -82,3 +84,41 @@ def default_api_key(api_key: str | None = None) -> str | None:
8284
return os.environ.get("TOGETHER_API_KEY")
8385

8486
raise error.AuthenticationError(together.constants.MISSING_API_KEY_MESSAGE)
87+
88+
89+
def get_google_colab_secret(secret_name: str = "TOGETHER_API_KEY") -> str | None:
90+
"""
91+
Checks to see if the user is running in Google Colab, and looks for the Together API Key secret.
92+
93+
Args:
94+
secret_name (str, optional). Defaults to TOGETHER_API_KEY
95+
96+
Returns:
97+
str: if the API key is found; None if an error occurred or the secret was not found.
98+
"""
99+
# If running in Google Colab, check for Together in notebook secrets
100+
if "google.colab" in sys.modules:
101+
if TYPE_CHECKING:
102+
from google.colab import userdata # type: ignore
103+
else:
104+
from google.colab import userdata
105+
106+
try:
107+
api_key = userdata.get(secret_name)
108+
if not isinstance(api_key, str):
109+
return None
110+
else:
111+
return str(api_key)
112+
except userdata.NotebookAccessError:
113+
log_info(
114+
"The TOGETHER_API_KEY Colab secret was found, but notebook access is disabled. Please enable notebook "
115+
"access for the secret."
116+
)
117+
except userdata.SecretNotFoundError:
118+
# warn and carry on
119+
log_info("Colab: No Google Colab secret named TOGETHER_API_KEY was found.")
120+
121+
return None
122+
123+
else:
124+
return None

0 commit comments

Comments
 (0)