Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
anticorrelator marked this conversation as resolved.
Show resolved Hide resolved
), VersionedAsyncClient(
base_url=get_base_url(),
headers=headers,
)


Expand Down
9 changes: 2 additions & 7 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}",
"authorization": f"Bearer {api_key}",
anticorrelator marked this conversation as resolved.
Show resolved Hide resolved
}
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():
anticorrelator marked this conversation as resolved.
Show resolved Hide resolved
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}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.headers["authorization"] = f"Bearer {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
Loading