Skip to content

Commit

Permalink
feat: Wire up API keys via env var for Phoenix clients and experiments (
Browse files Browse the repository at this point in the history
#4617)

* feat: Wire up API keys via env var for Phoenix clients and experiments

* Unify header management

* Capitalize `authorization` header key

* Capitalization shouldn't matter

* Recapitalize b/c REST
  • Loading branch information
anticorrelator authored Sep 17, 2024
1 parent 80106f0 commit c0b917a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
5 changes: 1 addition & 4 deletions src/phoenix/experiments/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from opentelemetry.trace import Status, StatusCode, Tracer
from typing_extensions import TypeAlias

from phoenix.config import get_base_url, get_env_client_headers
from phoenix.config import get_base_url
from phoenix.evals.executors import get_executor_on_sync_context
from phoenix.evals.models.rate_limiters import RateLimiter
from phoenix.evals.utils import get_tqdm_progress_bar_formatter
Expand Down Expand Up @@ -77,13 +77,10 @@


def _phoenix_clients() -> Tuple[httpx.Client, httpx.AsyncClient]:
headers = get_env_client_headers()
return VersionedClient(
base_url=get_base_url(),
headers=headers,
), VersionedAsyncClient(
base_url=get_base_url(),
headers=headers,
)


Expand Down
7 changes: 1 addition & 6 deletions src/phoenix/session/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@
from typing_extensions import TypeAlias, assert_never

from phoenix.config import (
get_env_client_headers,
get_env_collector_endpoint,
get_env_host,
get_env_phoenix_api_key,
get_env_port,
get_env_project_name,
)
Expand Down Expand Up @@ -88,15 +86,12 @@ def __init__(
)
if kwargs:
raise TypeError(f"Unexpected keyword arguments: {', '.join(kwargs)}")
headers = dict((headers or get_env_client_headers() or {}))
headers = dict(headers or {})
if api_key:
headers = {
**{k: v for k, v in (headers or {}).items() if k.lower() != "authorization"},
"Authorization": f"Bearer {api_key}",
}
elif api_key := get_env_phoenix_api_key():
if not headers or ("authorization" not in [k.lower() for k in headers]):
headers = {**(headers or {}), "Authorization": f"Bearer {api_key}"}
host = get_env_host()
if host == "0.0.0.0":
host = "127.0.0.1"
Expand Down
16 changes: 16 additions & 0 deletions src/phoenix/utilities/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import httpx

from phoenix.config import get_env_client_headers, get_env_phoenix_api_key

PHOENIX_SERVER_VERSION_HEADER = "x-phoenix-server-version"


Expand All @@ -15,6 +17,13 @@ def __init__(self, *args: Any, **kwargs: Any):
from phoenix import __version__ as phoenix_version

super().__init__(*args, **kwargs)

if env_headers := get_env_client_headers():
self.headers.update(env_headers)
if "authorization" not in [k.lower() for k in self.headers]:
if api_key := get_env_phoenix_api_key():
self.headers["Authorization"] = f"Bearer {api_key}"

self._client_phoenix_version = phoenix_version
self._warned_on_minor_version_mismatch = False

Expand Down Expand Up @@ -72,6 +81,13 @@ def __init__(self, *args: Any, **kwargs: Any):
from phoenix import __version__ as phoenix_version

super().__init__(*args, **kwargs)

if env_headers := get_env_client_headers():
self.headers.update(env_headers)
if "authorization" not in [k.lower() for k in self.headers]:
if api_key := get_env_phoenix_api_key():
self.headers["Authorization"] = f"Bearer {api_key}"

self._client_phoenix_version = phoenix_version
self._warned_on_minor_version_mismatch = False

Expand Down

0 comments on commit c0b917a

Please sign in to comment.