-
Notifications
You must be signed in to change notification settings - Fork 36
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
fix: validate provider urls before use #147
base: main
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -8,8 +8,8 @@ class AzureProvider(OpenAiProvider): | |
"""Provides chat completions for models hosted by the Azure OpenAI Service.""" | ||
|
||
PROVIDER_NAME = "azure" | ||
BASE_URL_ENV_VAR = "AZURE_CHAT_COMPLETIONS_HOST_NAME" | ||
REQUIRED_ENV_VARS = [ | ||
"AZURE_CHAT_COMPLETIONS_HOST_NAME", | ||
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. I separated out the base URL enforcement from the other ENV vars as there is special handling |
||
"AZURE_CHAT_COMPLETIONS_DEPLOYMENT_NAME", | ||
"AZURE_CHAT_COMPLETIONS_DEPLOYMENT_API_VERSION", | ||
"AZURE_CHAT_COMPLETIONS_KEY", | ||
|
@@ -21,13 +21,13 @@ def __init__(self, client: httpx.Client) -> None: | |
@classmethod | ||
def from_env(cls: type["AzureProvider"]) -> "AzureProvider": | ||
cls.check_env_vars() | ||
url = os.environ.get("AZURE_CHAT_COMPLETIONS_HOST_NAME") | ||
url = httpx.URL(os.environ.get(cls.BASE_URL_ENV_VAR, cls.BASE_URL_DEFAULT)) | ||
deployment_name = os.environ.get("AZURE_CHAT_COMPLETIONS_DEPLOYMENT_NAME") | ||
api_version = os.environ.get("AZURE_CHAT_COMPLETIONS_DEPLOYMENT_API_VERSION") | ||
key = os.environ.get("AZURE_CHAT_COMPLETIONS_KEY") | ||
|
||
# format the url host/"openai/deployments/" + deployment_name + "/?api-version=" + api_version | ||
url = f"{url}/openai/deployments/{deployment_name}/" | ||
url = url.join(f"/openai/deployments/{deployment_name}/") | ||
client = httpx.Client( | ||
base_url=url, | ||
headers={"api-key": key, "Content-Type": "application/json"}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,6 @@ | |
from exchange.providers.utils import raise_for_status, retry_if_status, encode_image | ||
from exchange.langfuse_wrapper import observe_wrapper | ||
|
||
|
||
GOOGLE_HOST = "https://generativelanguage.googleapis.com/v1beta" | ||
|
||
retry_procedure = retry( | ||
wait=wait_fixed(2), | ||
stop=stop_after_attempt(2), | ||
|
@@ -24,6 +21,8 @@ class GoogleProvider(Provider): | |
"""Provides chat completions for models hosted by Google, including Gemini and other experimental models.""" | ||
|
||
PROVIDER_NAME = "google" | ||
BASE_URL_ENV_VAR = "GOOGLE_HOST" | ||
BASE_URL_DEFAULT = "https://generativelanguage.googleapis.com/v1beta" | ||
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. does it make sense to have the constant at the top of the file just as people may want to quickly scan/change? 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. So, these constants are accessed as class variables, so if we move to the top they'd no longer be that. I'm not sure a hack to work around this.. |
||
REQUIRED_ENV_VARS = ["GOOGLE_API_KEY"] | ||
instructions_url = "https://ai.google.dev/gemini-api/docs/api-key" | ||
|
||
|
@@ -33,7 +32,7 @@ def __init__(self, client: httpx.Client) -> None: | |
@classmethod | ||
def from_env(cls: type["GoogleProvider"]) -> "GoogleProvider": | ||
cls.check_env_vars(cls.instructions_url) | ||
url = os.environ.get("GOOGLE_HOST", GOOGLE_HOST) | ||
url = httpx.URL(os.environ.get(cls.BASE_URL_ENV_VAR, cls.BASE_URL_DEFAULT)) | ||
key = os.environ.get("GOOGLE_API_KEY") | ||
client = httpx.Client( | ||
base_url=url, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ | |
from exchange.providers.utils import retry_if_status | ||
from exchange.langfuse_wrapper import observe_wrapper | ||
|
||
OPENAI_HOST = "https://api.openai.com/" | ||
|
||
retry_procedure = retry( | ||
wait=wait_fixed(2), | ||
|
@@ -30,6 +29,8 @@ class OpenAiProvider(Provider): | |
"""Provides chat completions for models hosted directly by OpenAI.""" | ||
|
||
PROVIDER_NAME = "openai" | ||
BASE_URL_ENV_VAR = "OPENAI_HOST" | ||
BASE_URL_DEFAULT = "https://api.openai.com/" | ||
REQUIRED_ENV_VARS = ["OPENAI_API_KEY"] | ||
instructions_url = "https://platform.openai.com/docs/api-reference/api-keys" | ||
|
||
|
@@ -39,11 +40,11 @@ def __init__(self, client: httpx.Client) -> None: | |
@classmethod | ||
def from_env(cls: type["OpenAiProvider"]) -> "OpenAiProvider": | ||
cls.check_env_vars(cls.instructions_url) | ||
url = os.environ.get("OPENAI_HOST", OPENAI_HOST) | ||
url = httpx.URL(os.environ.get(cls.BASE_URL_ENV_VAR, cls.BASE_URL_DEFAULT)) | ||
key = os.environ.get("OPENAI_API_KEY") | ||
|
||
client = httpx.Client( | ||
base_url=url + "v1/", | ||
base_url=url.join("v1/"), | ||
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 using httpx.URL as it isn't so sensitive about trailing slash etc when joining 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. interesting, i always remembered 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. I think it was more about not having a slash at all. e.g if you set a base URL ending in the port |
||
auth=("Bearer", key), | ||
timeout=httpx.Timeout(60 * 10), | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import pytest | ||
|
||
from exchange.providers.base import MissingProviderEnvVariableError, Provider | ||
|
||
|
||
def test_missing_provider_env_variable_error_without_instructions_url(): | ||
env_variable = "API_KEY" | ||
provider = "TestProvider" | ||
error = MissingProviderEnvVariableError(env_variable, provider) | ||
|
||
assert error.env_variable == env_variable | ||
assert error.provider == provider | ||
assert error.instructions_url is None | ||
assert error.message == "Missing environment variables: API_KEY for provider TestProvider." | ||
|
||
|
||
def test_missing_provider_env_variable_error_with_instructions_url(): | ||
env_variable = "API_KEY" | ||
provider = "TestProvider" | ||
instructions_url = "http://example.com/instructions" | ||
error = MissingProviderEnvVariableError(env_variable, provider, instructions_url) | ||
|
||
assert error.env_variable == env_variable | ||
assert error.provider == provider | ||
assert error.instructions_url == instructions_url | ||
assert error.message == ( | ||
"Missing environment variables: API_KEY for provider TestProvider.\n" | ||
"Please see http://example.com/instructions for instructions" | ||
) | ||
|
||
|
||
class TestProvider(Provider): | ||
PROVIDER_NAME = "test_provider" | ||
REQUIRED_ENV_VARS = [] | ||
|
||
def complete(self, model, system, messages, tools, **kwargs): | ||
pass | ||
|
||
|
||
class TestProviderBaseURL(Provider): | ||
PROVIDER_NAME = "test_provider_base_url" | ||
BASE_URL_ENV_VAR = "TEST_PROVIDER_BASE_URL" | ||
REQUIRED_ENV_VARS = [] | ||
|
||
def complete(self, model, system, messages, tools, **kwargs): | ||
pass | ||
|
||
|
||
class TestProviderBaseURLDefault(Provider): | ||
PROVIDER_NAME = "test_provider_base_url_default" | ||
BASE_URL_ENV_VAR = "TEST_PROVIDER_BASE_URL_DEFAULT" | ||
BASE_URL_DEFAULT = "http://localhost:11434/" | ||
REQUIRED_ENV_VARS = [] | ||
|
||
def complete(self, model, system, messages, tools, **kwargs): | ||
pass | ||
|
||
|
||
def test_check_env_vars_no_base_url(): | ||
TestProvider.check_env_vars() | ||
|
||
|
||
def test_check_env_vars_base_url_valid_http(monkeypatch): | ||
monkeypatch.setenv(TestProviderBaseURL.BASE_URL_ENV_VAR, "http://localhost:11434/") | ||
|
||
TestProviderBaseURL.check_env_vars() | ||
|
||
|
||
def test_check_env_vars_base_url_valid_https(monkeypatch): | ||
monkeypatch.setenv(TestProviderBaseURL.BASE_URL_ENV_VAR, "https://localhost:11434/v1") | ||
|
||
TestProviderBaseURL.check_env_vars() | ||
|
||
|
||
def test_check_env_vars_base_url_default(): | ||
TestProviderBaseURLDefault.check_env_vars() | ||
|
||
|
||
def test_check_env_vars_base_url_throw_error_when_empty(monkeypatch): | ||
monkeypatch.setenv(TestProviderBaseURL.BASE_URL_ENV_VAR, "") | ||
|
||
with pytest.raises(KeyError, match="TEST_PROVIDER_BASE_URL"): | ||
TestProviderBaseURL.check_env_vars() | ||
|
||
|
||
def test_check_env_vars_base_url_throw_error_when_missing_schemes(monkeypatch): | ||
monkeypatch.setenv(TestProviderBaseURL.BASE_URL_ENV_VAR, "localhost:11434") | ||
|
||
with pytest.raises( | ||
ValueError, match="Expected TEST_PROVIDER_BASE_URL to be a 'http' or 'https' url: localhost:11434" | ||
): | ||
TestProviderBaseURL.check_env_vars() | ||
|
||
|
||
def test_check_env_vars_base_url_throw_error_when_invalid_scheme(monkeypatch): | ||
monkeypatch.setenv(TestProviderBaseURL.BASE_URL_ENV_VAR, "ftp://localhost:11434/v1") | ||
|
||
with pytest.raises( | ||
ValueError, match="Expected TEST_PROVIDER_BASE_URL to be a 'http' or 'https' url: ftp://localhost:11434/v1" | ||
): | ||
TestProviderBaseURL.check_env_vars() |
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.
I think this is incorrect as it defers to the default value without considering an override. OTOH, if I make this empty, it fails tests...