From 817e74a514a68ae2524312343836de54c16a4b2e Mon Sep 17 00:00:00 2001 From: csgulati09 Date: Thu, 21 Nov 2024 14:55:23 +0530 Subject: [PATCH] feat: making api_key optional and setting localhost if api_key not present --- portkey_ai/_vendor/openai/_exceptions.py | 6 +++- portkey_ai/api_resources/base_client.py | 14 ++++---- portkey_ai/api_resources/global_constants.py | 8 +++-- portkey_ai/api_resources/utils.py | 37 ++++++++------------ 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/portkey_ai/_vendor/openai/_exceptions.py b/portkey_ai/_vendor/openai/_exceptions.py index f44f90b..3a87c92 100644 --- a/portkey_ai/_vendor/openai/_exceptions.py +++ b/portkey_ai/_vendor/openai/_exceptions.py @@ -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) diff --git a/portkey_ai/api_resources/base_client.py b/portkey_ai/api_resources/base_client.py index edad73a..a5171d8 100644 --- a/portkey_ai/api_resources/base_client.py +++ b/portkey_ai/api_resources/base_client.py @@ -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 @@ -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 @@ -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 diff --git a/portkey_ai/api_resources/global_constants.py b/portkey_ai/api_resources/global_constants.py index f9ce110..a238b1a 100644 --- a/portkey_ai/api_resources/global_constants.py +++ b/portkey_ai/api_resources/global_constants.py @@ -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 """ @@ -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. \ @@ -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" diff --git a/portkey_ai/api_resources/utils.py b/portkey_ai/api_resources/utils.py index ce97a9d..3c0bc0f 100644 --- a/portkey_ai/api_resources/utils.py +++ b/portkey_ai/api_resources/utils.py @@ -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, @@ -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, @@ -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 @@ -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