Skip to content

Commit 74593cb

Browse files
danwatersorangetin
andauthored
Danwaters/eng 21477 ml eng support support google colab secret for together api (#268)
* Support for Google Colab notebook secrets * fix whitespace * fix failing integration tests * fix test * bump tag to 1.4.5 --------- Co-authored-by: orangetin <[email protected]> Co-authored-by: orangetin <[email protected]>
1 parent 639fb48 commit 74593cb

File tree

5 files changed

+52
-4
lines changed

5 files changed

+52
-4
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ build-backend = "poetry.masonry.api"
1212

1313
[tool.poetry]
1414
name = "together"
15-
version = "1.4.4"
15+
version = "1.4.5"
1616
authors = [
1717
"Together AI <[email protected]>"
1818
]

src/together/client.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from __future__ import annotations
22

33
import os
4-
from typing import Dict
4+
import sys
5+
from typing import Dict, TYPE_CHECKING
56

67
from together import resources
78
from together.constants import BASE_URL, MAX_RETRIES, TIMEOUT_SECS
89
from together.error import AuthenticationError
910
from together.types import TogetherClient
1011
from together.utils import enforce_trailing_slash
12+
from together.utils.api_helpers import get_google_colab_secret
1113

1214

1315
class Together:
@@ -44,6 +46,9 @@ def __init__(
4446
if not api_key:
4547
api_key = os.environ.get("TOGETHER_API_KEY")
4648

49+
if not api_key and "google.colab" in sys.modules:
50+
api_key = get_google_colab_secret("TOGETHER_API_KEY")
51+
4752
if not api_key:
4853
raise AuthenticationError(
4954
"The api_key client option must be set either by passing api_key to the client or by setting the "
@@ -117,6 +122,9 @@ def __init__(
117122
if not api_key:
118123
api_key = os.environ.get("TOGETHER_API_KEY")
119124

125+
if not api_key and "google.colab" in sys.modules:
126+
api_key = get_google_colab_secret("TOGETHER_API_KEY")
127+
120128
if not api_key:
121129
raise AuthenticationError(
122130
"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

+40
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

tests/integration/constants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
completion_test_model_list = [
2-
"mistralai/Mistral-7B-v0.1",
2+
"meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
33
]
44
chat_test_model_list = []
55
embedding_test_model_list = []

tests/integration/resources/test_completion.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def test_max_tokens(
213213
product(
214214
completion_test_model_list,
215215
completion_prompt_list,
216-
[35000, 40000, 50000],
216+
[200000, 400000, 500000],
217217
),
218218
)
219219
def test_high_max_tokens(

0 commit comments

Comments
 (0)