-
Notifications
You must be signed in to change notification settings - Fork 65
Move from legacy proxy to gateway #191
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,16 @@ | |
| from dataclasses import dataclass | ||
| from typing import Any, Optional, Protocol, TypedDict, TypeVar, Union, cast, runtime_checkable | ||
|
|
||
| PROXY_URL = "https://api.braintrust.dev/v1/proxy" | ||
| GATEWAY_URL = "https://gateway.braintrust.dev" | ||
|
|
||
|
|
||
| def _gateway_url() -> str: | ||
| return os.environ.get("BRAINTRUST_AI_GATEWAY_URL", "").strip() or GATEWAY_URL | ||
|
|
||
|
|
||
| def _is_gateway_url(base_url: str) -> bool: | ||
| normalized_base_url = base_url.rstrip("/") | ||
| return normalized_base_url == GATEWAY_URL or normalized_base_url == _gateway_url().rstrip("/") | ||
|
|
||
|
|
||
| class DefaultModelConfig(TypedDict, total=False): | ||
|
|
@@ -195,7 +204,9 @@ def __post_init__(self): | |
| if not has_customization and not isinstance(self.openai, NamedWrapper): | ||
| self.openai = wrap_openai(self.openai) | ||
|
|
||
| self._is_wrapped = isinstance(self.openai, NamedWrapper) | ||
| self._is_wrapped = isinstance(self.openai, NamedWrapper) or ( | ||
| not has_customization and wrap_openai.__module__.startswith("braintrust.") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. drive-by, the test was broken |
||
| ) | ||
|
|
||
| openai_module = get_openai_module() | ||
|
|
||
|
|
@@ -431,7 +442,7 @@ def init( | |
| models for different evaluation types. Only the specified models are updated; | ||
| others remain unchanged. | ||
|
|
||
| When using non-OpenAI providers via the Braintrust proxy, set this to the | ||
| When using non-OpenAI providers via the Braintrust Gateway, set this to the | ||
| appropriate model string (e.g., "claude-3-5-sonnet-20241022"). | ||
|
|
||
| Example: | ||
|
|
@@ -448,7 +459,7 @@ def init( | |
| init( | ||
| client=OpenAI( | ||
| api_key=os.environ["BRAINTRUST_API_KEY"], | ||
| base_url="https://api.braintrust.dev/v1/proxy", | ||
| base_url=os.getenv("BRAINTRUST_AI_GATEWAY_URL") or "https://gateway.braintrust.dev", | ||
| ), | ||
| default_model={ | ||
| "completion": "claude-3-5-sonnet-20241022", | ||
|
|
@@ -520,7 +531,8 @@ def prepare_openai( | |
| Deprecated: Use the `client` argument and set the `openai`. | ||
|
|
||
| base_url (str, optional): Base URL for API requests. If not provided, will | ||
| use OPENAI_BASE_URL from environment or fall back to PROXY_URL. | ||
| use OPENAI_BASE_URL from environment or fall back to BRAINTRUST_AI_GATEWAY_URL | ||
| or GATEWAY_URL. | ||
| Deprecated: Use the `client` argument and set the `openai`. | ||
|
|
||
| Returns: | ||
|
|
@@ -559,11 +571,14 @@ def prepare_openai( | |
| ) | ||
| warned_deprecated_api_key_base_url = True | ||
|
|
||
| if base_url is None: | ||
| base_url = os.environ.get("OPENAI_BASE_URL") or _gateway_url() | ||
| # prepare the default openai sdk, if not provided | ||
| if api_key is None: | ||
| api_key = os.environ.get("OPENAI_API_KEY") or os.environ.get("BRAINTRUST_API_KEY") | ||
| if base_url is None: | ||
| base_url = os.environ.get("OPENAI_BASE_URL", PROXY_URL) | ||
| if _is_gateway_url(base_url): | ||
| api_key = os.environ.get("BRAINTRUST_API_KEY") or os.environ.get("OPENAI_API_KEY") | ||
| else: | ||
| api_key = os.environ.get("OPENAI_API_KEY") or os.environ.get("BRAINTRUST_API_KEY") | ||
|
|
||
| if hasattr(openai_module, "OpenAI"): | ||
| openai_module = cast(OpenAIV1Module, openai_module) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
|
|
||
| from autoevals import init # type: ignore[import] | ||
| from autoevals.oai import ( # type: ignore[import] | ||
| GATEWAY_URL, | ||
| LLMClient, | ||
| OpenAIV0Module, | ||
| OpenAIV1Module, | ||
|
|
@@ -28,6 +29,7 @@ def reset_env_and_client(monkeypatch: pytest.MonkeyPatch): | |
| monkeypatch.delenv("OPENAI_API_KEY", raising=False) | ||
| monkeypatch.setenv("OPENAI_API_KEY", "test-key") | ||
| monkeypatch.setenv("OPENAI_BASE_URL", "http://test-url") | ||
| monkeypatch.delenv("BRAINTRUST_AI_GATEWAY_URL", raising=False) | ||
| monkeypatch.setattr("autoevals.oai._named_wrapper", None) | ||
| monkeypatch.setattr("autoevals.oai._wrap_openai", None) | ||
| monkeypatch.setattr("autoevals.oai._openai_module", None) | ||
|
|
@@ -89,6 +91,32 @@ def test_prepare_openai_defaults(): | |
| assert openai_obj.base_url == "http://test-url" | ||
|
|
||
|
|
||
| def test_prepare_openai_defaults_to_gateway(monkeypatch: pytest.MonkeyPatch): | ||
| monkeypatch.delenv("OPENAI_BASE_URL", raising=False) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (fyi) i like to use getEnv() and similar helpers to avoid monkey patching. you can then just set/unset env vars more easily.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense! |
||
| monkeypatch.delenv("BRAINTRUST_AI_GATEWAY_URL", raising=False) | ||
| monkeypatch.setenv("OPENAI_API_KEY", "openai-key") | ||
| monkeypatch.setenv("BRAINTRUST_API_KEY", "braintrust-key") | ||
|
|
||
| prepared_client = prepare_openai() | ||
|
|
||
| openai_obj = unwrap_named_wrapper(prepared_client.openai) | ||
| assert openai_obj.api_key == "braintrust-key" | ||
| assert str(openai_obj.base_url).rstrip("/") == GATEWAY_URL | ||
|
|
||
|
|
||
| def test_prepare_openai_uses_configured_gateway_url(monkeypatch: pytest.MonkeyPatch): | ||
| monkeypatch.delenv("OPENAI_BASE_URL", raising=False) | ||
| monkeypatch.setenv("BRAINTRUST_AI_GATEWAY_URL", " https://gateway.example.com ") | ||
| monkeypatch.setenv("OPENAI_API_KEY", "openai-key") | ||
| monkeypatch.setenv("BRAINTRUST_API_KEY", "braintrust-key") | ||
|
|
||
| prepared_client = prepare_openai() | ||
|
|
||
| openai_obj = unwrap_named_wrapper(prepared_client.openai) | ||
| assert openai_obj.api_key == "braintrust-key" | ||
| assert str(openai_obj.base_url).rstrip("/") == "https://gateway.example.com" | ||
|
|
||
|
|
||
| def test_prepare_openai_with_plain_openai(): | ||
| client = openai.OpenAI(api_key="api-key", base_url="http://test") | ||
| prepared_client = prepare_openai(client=client) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/braintrustdata/autoevals/pull/191/changes#r3337093260