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

fix: validate provider urls before use #147

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

codefromthecrypt
Copy link
Collaborator

This fixes validation, like #133, except for all providers that accept a hostname/base URL variable.

key = os.environ.get("OPENAI_API_KEY")

client = httpx.Client(
base_url=url + "v1/",
base_url=url.join("v1/"),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

Copy link
Collaborator

Choose a reason for hiding this comment

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

interesting, i always remembered // would resolve? just curious what bug you can into since i haven't seen this before!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

@michaelneale
Copy link
Collaborator

@lamchau can you cast eagle eyes over this?


val = os.environ.get(key, default)
if val == "":
raise ValueError(f"{key} was empty")
Copy link
Collaborator

Choose a reason for hiding this comment

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

:nit: should use KeyError which is consistent with the non-default value getter os.environ[key].

[nav] In [1]: import os
         ...:
         ...: os.environ["SHELL"]
         ...: os.environ["SHELL_MISSING"]
         ...:
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[1], line 4
      1 import os
      3 os.environ["SHELL"]
----> 4 os.environ["SHELL_MISSING"]

File <frozen os>:714, in __getitem__(self, key)

KeyError: 'SHELL_MISSING'

Copy link
Collaborator

Choose a reason for hiding this comment

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

forgot what package i was in – it just occurred to me that we recently wrote up a way to do this on the base provider. could we add the check in there?

https://github.com/block-open-source/goose/blob/06c12b627c4b2da94c05b021a6b2e31539557798/packages/exchange/src/exchange/providers/base.py#L26-L29

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

so you mean add a parameter to check_env_vars() like base_url_key?

Copy link
Collaborator

Choose a reason for hiding this comment

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

exactly! imo we might want to tweak it slightly so it'll dump all the env vars in a single pass rather than one at a time (especially with how much setup is needed for AZURE_*).

not sure if @elenazherdeva is working on that atm, but it was from this #116 (comment)

Copy link
Collaborator

Choose a reason for hiding this comment

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

@lamchau thank you for pinging! I’m not working on it at the moment, but I should fix it. Let me create a follow-up PR based on your comments!

@codefromthecrypt
Copy link
Collaborator Author

thanks for the advice @lamchau will revise impl after clarification on #147 (comment)

@codefromthecrypt
Copy link
Collaborator Author

sorry was awol on medical leave. sporadic this week, but will adjust this and others soon

@lamchau
Copy link
Collaborator

lamchau commented Oct 28, 2024

@codefromthecrypt no worries! hopefully you're doing well/on the mend!

Signed-off-by: Adrian Cole <adrian.cole@elastic.co>
Signed-off-by: Adrian Cole <adrian.cole@elastic.co>
@codefromthecrypt
Copy link
Collaborator Author

@lamchau PTAL I have consolidated what I could into the same check, and refactored things around it.

@@ -164,5 +164,5 @@ def recommended_models() -> tuple[str, str]:

@retry_procedure
def _post(self, payload: dict) -> httpx.Response:
response = self.client.post(ANTHROPIC_HOST, json=payload)
response = self.client.post(self.BASE_URL_DEFAULT, json=payload)
Copy link
Collaborator Author

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...

REQUIRED_ENV_VARS = [
"AZURE_CHAT_COMPLETIONS_HOST_NAME",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

@@ -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"
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants