Skip to content

Commit

Permalink
feat: handle invalid base_url case
Browse files Browse the repository at this point in the history
  • Loading branch information
csgulati09 committed Oct 17, 2024
1 parent 770e827 commit 3fef14b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 7 additions & 2 deletions portkey_ai/api_resources/global_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
type "ModesLiteral | Modes | None"
"""

LOCALHOST_CONNECTION_ERROR = """Could not make the API request.\
Either enter an API Key, or your base url if self-hosting.
LOCALHOST_CONNECTION_ERROR = """Could not instantiate the Portkey client. \
You can either add a valid `api_key` parameter (from https://app.portkey.ai/api-keys)\
or set the `base_url` parameter to your AI Gateway's instance's URL.
"""

CUSTOM_HOST_CONNECTION_ERROR = """We could not connect to the AI Gateway's instance. \
Please check the `base_url` parameter in the Portkey client.
"""

DEFAULT_MAX_RETRIES = 2
Expand Down
12 changes: 10 additions & 2 deletions portkey_ai/api_resources/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
InternalServerError,
)
from .global_constants import (
CUSTOM_HOST_CONNECTION_ERROR,
LOCAL_BASE_URL,
LOCALHOST_CONNECTION_ERROR,
MISSING_API_KEY_ERROR_MESSAGE,
Expand Down Expand Up @@ -486,13 +487,20 @@ def parse_headers_generic(headers: Optional[httpx.Headers]) -> dict:

return _headers


def get_base_url_from_setup(base_url, api_key):
if not base_url and not api_key:
try:
with urllib.request.urlopen(LOCAL_BASE_URL) as response:
if response.getcode() == 200:
return LOCAL_BASE_URL + "/v1"
except urllib.error.URLError:
raise ValueError(LOCALHOST_CONNECTION_ERROR)
raise ConnectionError(LOCALHOST_CONNECTION_ERROR)
if base_url:
return base_url
base = base_url.rsplit("/v1", 1)[0]
try:
with urllib.request.urlopen(base) as response:
if response.getcode() == 200:
return base_url
except urllib.error.URLError:
raise ConnectionError(CUSTOM_HOST_CONNECTION_ERROR)

0 comments on commit 3fef14b

Please sign in to comment.