Skip to content

Warning instead of error on auth verification failure in REST models #218

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

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 13 additions & 3 deletions spacy_llm/models/rest/anthropic/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import warnings
from enum import Enum
from typing import Any, Dict, Iterable, List, Sized, Tuple

Expand Down Expand Up @@ -33,17 +34,26 @@ def credentials(self) -> Dict[str, str]:
# Fetch and check the key, set up headers
api_key = os.getenv("ANTHROPIC_API_KEY")
if api_key is None:
raise ValueError(
warnings.warn(
"Could not find the API key to access the Anthropic Claude API. Ensure you have an API key "
"set up via the Anthropic console (https://console.anthropic.com/), then make it available as "
"an environment variable 'ANTHROPIC_API_KEY."
)

return {"X-API-Key": api_key}
return {"X-API-Key": api_key if api_key else ""}

def _verify_auth(self) -> None:
# Execute a dummy prompt. If the API setup is incorrect, we should fail at initialization time.
self(["test"])
try:
self(["test"])
except ValueError as err:
if "authentication_error" in str(err):
warnings.warn(
"Authentication with provided API key failed. Please double-check you provided the correct "
"credentials."
)
else:
raise err

def __call__(self, prompts: Iterable[str]) -> Iterable[str]:
headers = {
Expand Down
14 changes: 12 additions & 2 deletions spacy_llm/models/rest/cohere/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import warnings
from enum import Enum
from typing import Any, Dict, Iterable, List, Sized, Tuple

Expand All @@ -18,7 +19,7 @@ class Cohere(REST):
def credentials(self) -> Dict[str, str]:
api_key = os.getenv("CO_API_KEY")
if api_key is None:
raise ValueError(
warnings.warn(
"Could not find the API key to access the Cohere API. Ensure you have an API key "
"set up via https://dashboard.cohere.ai/api-keys, then make it available as "
"an environment variable 'CO_API_KEY'."
Expand All @@ -27,7 +28,16 @@ def credentials(self) -> Dict[str, str]:
return {"Authorization": f"Bearer {api_key}"}

def _verify_auth(self) -> None:
self(["test"])
try:
self(["test"])
except ValueError as err:
if "invalid api token" in str(err):
warnings.warn(
"Authentication with provided API key failed. Please double-check you provided the correct "
"credentials."
)
else:
raise err

def __call__(self, prompts: Iterable[str]) -> Iterable[str]:
headers = {
Expand Down
17 changes: 12 additions & 5 deletions spacy_llm/models/rest/openai/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import warnings
from enum import Enum
from typing import Any, Dict, Iterable, List, Sized, Tuple

Expand All @@ -21,7 +22,7 @@ def credentials(self) -> Dict[str, str]:
api_key = os.getenv("OPENAI_API_KEY")
api_org = os.getenv("OPENAI_API_ORG")
if api_key is None:
raise ValueError(
warnings.warn(
"Could not find the API key to access the OpenAI API. Ensure you have an API key "
"set up via https://platform.openai.com/account/api-keys, then make it available as "
"an environment variable 'OPENAI_API_KEY."
Expand Down Expand Up @@ -51,14 +52,20 @@ def _verify_auth(self) -> None:
timeout=self._max_request_time,
)
if r.status_code == 422:
raise ValueError(
warnings.warn(
"Could not access api.openai.com -- 422 permission denied."
"Visit https://platform.openai.com/account/api-keys to check your API keys."
)
elif r.status_code != 200:
raise ValueError(
f"Error accessing api.openai.com ({r.status_code}): {r.text}"
)
if "Incorrect API key" in r.text:
warnings.warn(
"Authentication with provided API key failed. Please double-check you provided the correct "
"credentials."
)
else:
warnings.warn(
f"Error accessing api.openai.com ({r.status_code}): {r.text}"
)

response = r.json()["data"]
models = [response[i]["id"] for i in range(len(response))]
Expand Down