Skip to content

Commit

Permalink
feat: localSetup config if baseUrl and apiKey not found
Browse files Browse the repository at this point in the history
  • Loading branch information
csgulati09 committed Oct 17, 2024
1 parent 8d48d35 commit 770e827
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions portkey_ai/api_resources/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import platform

from portkey_ai.api_resources.apis.create_headers import createHeaders
from .global_constants import PORTKEY_HEADER_PREFIX
from .utils import remove_empty_values, Options
from .global_constants import (
PORTKEY_HEADER_PREFIX,
)
from .utils import get_base_url_from_setup, remove_empty_values, Options
from .exceptions import (
APIStatusError,
APITimeoutError,
Expand Down Expand Up @@ -82,6 +84,7 @@ 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.virtual_key = virtual_key
Expand Down Expand Up @@ -723,6 +726,7 @@ 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.virtual_key = virtual_key
Expand Down
5 changes: 5 additions & 0 deletions portkey_ai/api_resources/global_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@
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.
"""

DEFAULT_MAX_RETRIES = 2
VERSION = "0.1.0"
DEFAULT_TIMEOUT = 60
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"
PORTKEY_API_KEY_ENV = "PORTKEY_API_KEY"
PORTKEY_PROXY_ENV = "PORTKEY_PROXY"
OPEN_AI_API_KEY = "DUMMY-KEY"
14 changes: 14 additions & 0 deletions portkey_ai/api_resources/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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 @@ -33,6 +34,8 @@
InternalServerError,
)
from .global_constants import (
LOCAL_BASE_URL,
LOCALHOST_CONNECTION_ERROR,
MISSING_API_KEY_ERROR_MESSAGE,
MISSING_BASE_URL,
MISSING_MODE_MESSAGE,
Expand Down Expand Up @@ -482,3 +485,14 @@ def parse_headers_generic(headers: Optional[httpx.Headers]) -> dict:
_headers[k] = v

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)
if base_url:
return base_url

0 comments on commit 770e827

Please sign in to comment.