Skip to content

Commit

Permalink
feat: making api_key optional and setting localhost if api_key not pr…
Browse files Browse the repository at this point in the history
…esent
  • Loading branch information
csgulati09 committed Nov 21, 2024
1 parent 97a1631 commit 817e74a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
6 changes: 5 additions & 1 deletion portkey_ai/_vendor/openai/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ def __init__(self, message: str, *, response: httpx.Response, body: object | Non


class APIConnectionError(APIError):
def __init__(self, *, message: str = "Connection error.", request: httpx.Request) -> None:
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 check the `base_url` parameter in the Portkey client, for your AI Gateway's instance's URL.
"""
def __init__(self, *, message: str = LOCALHOST_CONNECTION_ERROR, request: httpx.Request) -> None:
super().__init__(message, request, body=None)


Expand Down
14 changes: 6 additions & 8 deletions portkey_ai/api_resources/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
from .global_constants import (
PORTKEY_HEADER_PREFIX,
)
from .utils import get_base_url_from_setup, remove_empty_values, Options
from .utils import remove_empty_values, Options, set_base_url
from .exceptions import (
APIStatusError,
APITimeoutError,
APIConnectionError,
)
from portkey_ai.version import VERSION
from .utils import ResponseT, make_status_error, default_api_key, default_base_url
from .utils import ResponseT, make_status_error, default_api_key
from .common_types import StreamT, AsyncStreamT
from .streaming import Stream, AsyncStream

Expand Down Expand Up @@ -84,9 +84,8 @@ def __init__(
mistral_fim_completion: Optional[str] = None,
**kwargs,
) -> None:
base_url = get_base_url_from_setup(base_url, api_key)
self.base_url = base_url or default_base_url()
self.api_key = api_key or default_api_key(self.base_url)
self.base_url = set_base_url(base_url, api_key)
self.api_key = default_api_key(self.base_url, api_key)
self.virtual_key = virtual_key
self.config = config
self.provider = provider
Expand Down Expand Up @@ -726,9 +725,8 @@ def __init__(
mistral_fim_completion: Optional[str] = None,
**kwargs,
) -> None:
base_url = get_base_url_from_setup(base_url, api_key)
self.base_url = base_url or default_base_url()
self.api_key = api_key or default_api_key(self.base_url)
self.base_url = set_base_url(base_url, api_key)
self.api_key = default_api_key(self.base_url, api_key)
self.virtual_key = virtual_key
self.config = config
self.provider = provider
Expand Down
8 changes: 5 additions & 3 deletions portkey_ai/api_resources/global_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
or set it as an environment variable with export PORTKEY_API_KEY=YOUR_API_KEY
"""

# Not being used right now, but can be used in the future if we stop using vendoring
MISSING_BASE_URL = """No Base url provided. Please provide a valid base url.
For example: https://api.portkey.ai
"""
Expand All @@ -25,7 +26,8 @@

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.
or check the `base_url` parameter in the Portkey client, \
for your AI Gateway's instance's URL.
"""

CUSTOM_HOST_CONNECTION_ERROR = """We could not connect to the AI Gateway's instance. \
Expand All @@ -38,7 +40,7 @@
PORTKEY_HEADER_PREFIX = "x-portkey-"
PORTKEY_BASE_URL = "https://api.portkey.ai/v1"
PORTKEY_GATEWAY_URL = PORTKEY_BASE_URL
LOCAL_BASE_URL = "http://localhost:8787"
LOCAL_BASE_URL = "http://localhost:8787/v1"
PORTKEY_API_KEY_ENV = "PORTKEY_API_KEY"
PORTKEY_PROXY_ENV = "PORTKEY_PROXY"
OPEN_AI_API_KEY = "DUMMY-KEY"
OPEN_AI_API_KEY = "OPENAI_API_KEY"
37 changes: 15 additions & 22 deletions portkey_ai/api_resources/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import httpx
import portkey_ai
from pydantic import BaseModel, field_validator
import urllib.request

from portkey_ai.api_resources.types.chat_complete_type import (
ChatCompletionChunk,
Expand Down Expand Up @@ -34,9 +33,7 @@
InternalServerError,
)
from .global_constants import (
CUSTOM_HOST_CONNECTION_ERROR,
LOCAL_BASE_URL,
LOCALHOST_CONNECTION_ERROR,
MISSING_API_KEY_ERROR_MESSAGE,
MISSING_BASE_URL,
MISSING_MODE_MESSAGE,
Expand Down Expand Up @@ -438,17 +435,19 @@ def parse_llms(cls, llms):
return llms


def default_api_key(base_url) -> str:
if portkey_ai.api_key:
return portkey_ai.api_key
def default_api_key(base_url, api_key) -> str:
if api_key:
return api_key
env_api_key = os.environ.get(PORTKEY_API_KEY_ENV, "")
if base_url == PORTKEY_BASE_URL:
if env_api_key:
return env_api_key
raise ValueError(MISSING_API_KEY_ERROR_MESSAGE)
return env_api_key
else:
return env_api_key


# Not being used right now, but can be used in the future if we stop using vendoring
def default_base_url() -> str:
if portkey_ai.base_url:
return portkey_ai.base_url
Expand Down Expand Up @@ -489,19 +488,13 @@ 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 ConnectionError(LOCALHOST_CONNECTION_ERROR)
def set_base_url(base_url, api_key):
if 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)
return base_url

env_base_url = os.environ.get(PORTKEY_BASE_URL) or os.environ.get(PORTKEY_PROXY_ENV)

if env_base_url:
return env_base_url
api_key = api_key or os.environ.get(PORTKEY_API_KEY_ENV)
return PORTKEY_BASE_URL if api_key else LOCAL_BASE_URL

0 comments on commit 817e74a

Please sign in to comment.