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

chore(ai_services): raise exception if api_key None or empty #1056

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vipyne
Copy link
Member

@vipyne vipyne commented Jan 21, 2025

Please describe the changes in your PR. If it is addressing an issue, please reference that as well.

closes #941

Raises exception if the value of an API key is None or empty.

@@ -48,6 +48,8 @@
class AIService(FrameProcessor):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._require_api_key: bool = True
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this could go in the constructor:

def __init__(self, *, require_api_key: bool = True, **kwargs):
    super().__init__(**kwargs)
     self._require_api_key: bool = require_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.

+1, we can also add and use self._api_key at all places, currently we are using self._api_key just for validation

    super().__init__(**kwargs)
    self._api_key = api_key
     self._require_api_key: bool = require_api_key```

@@ -44,6 +44,7 @@ def __init__(
self, *, model="vikhyatk/moondream2", revision="2024-08-26", use_cpu=False, **kwargs
):
super().__init__(**kwargs)
self._require_api_key = False
Copy link
Contributor

Choose a reason for hiding this comment

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

super().__init__(require_api_key = False, **kwargs)

@@ -49,6 +49,7 @@ def __init__(
**kwargs,
):
super().__init__(**kwargs)
self._require_api_key = False
Copy link
Contributor

Choose a reason for hiding this comment

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

super().__init__(require_api_key = False, **kwargs)

@@ -80,6 +80,7 @@ def __init__(
**kwargs,
):
super().__init__(sample_rate=sample_rate, **kwargs)
self._require_api_key = False
Copy link
Contributor

@aconchillo aconchillo Jan 22, 2025

Choose a reason for hiding this comment

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

super().__init__(require_api_key = False, sample_rate=sample_rate, **kwargs)

@@ -61,7 +63,10 @@ def set_model_name(self, model: str):
self.set_core_metrics_data(MetricsData(processor=self.name, model=self._model_name))

async def start(self, frame: StartFrame):
pass
if self._require_api_key and not self._api_key:
raise Exception(f"{self}: Error: No api_key value found.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we could make it more explicit: No api_key provided or You need to pass an api_key.

@@ -61,7 +63,10 @@ def set_model_name(self, model: str):
self.set_core_metrics_data(MetricsData(processor=self.name, model=self._model_name))

async def start(self, frame: StartFrame):
pass
if self._require_api_key and not self._api_key:
raise Exception(f"{self}: Error: No api_key value found.")
Copy link
Contributor

Choose a reason for hiding this comment

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

can we create a custom expection for this, like APIKeyMissing / APIKeyError, this is save us from cases where we would need to catch it but can only do it via expect Exception

@@ -48,6 +48,8 @@
class AIService(FrameProcessor):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._require_api_key: bool = True
Copy link
Contributor

Choose a reason for hiding this comment

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

+1, we can also add and use self._api_key at all places, currently we are using self._api_key just for validation

    super().__init__(**kwargs)
    self._api_key = api_key
     self._require_api_key: bool = require_api_key```

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.

Raising Exception when API Key in any service is None or Blank
3 participants