-
Notifications
You must be signed in to change notification settings - Fork 21
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
Refactor ApiAssistant
to AuthenticatedApiAssistant
and UnauthenticatedApiAssistant
#381
Changes from all commits
85680e2
0cd6174
7395131
917e958
aa1f985
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 |
---|---|---|
|
@@ -11,12 +11,10 @@ | |
from ragna.core import Assistant, EnvVarRequirement, RagnaException, Requirement, Source | ||
|
||
|
||
class ApiAssistant(Assistant): | ||
_API_KEY_ENV_VAR: str | ||
|
||
class _ApiAssistant(Assistant): | ||
@classmethod | ||
def requirements(cls) -> list[Requirement]: | ||
return [EnvVarRequirement(cls._API_KEY_ENV_VAR), *cls._extra_requirements()] | ||
return [] | ||
|
||
@classmethod | ||
def _extra_requirements(cls) -> list[Requirement]: | ||
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. Not sure if it makes sense to have this on the superclass if for now only the authenticated subclass needs it. |
||
|
@@ -27,7 +25,6 @@ def __init__(self) -> None: | |
headers={"User-Agent": f"{ragna.__version__}/{self}"}, | ||
timeout=60, | ||
) | ||
self._api_key = os.environ[self._API_KEY_ENV_VAR] | ||
|
||
async def answer( | ||
self, prompt: str, sources: list[Source], *, max_new_tokens: int = 256 | ||
|
@@ -58,3 +55,20 @@ async def _assert_api_call_is_success(self, response: Response) -> None: | |
response_status_code=response.status_code, | ||
response_content=content, | ||
) | ||
|
||
|
||
class AuthenticatedApiAssistant(_ApiAssistant): | ||
_API_KEY_ENV_VAR: str | ||
|
||
@classmethod | ||
def requirements(cls) -> list[Requirement]: | ||
return [EnvVarRequirement(cls._API_KEY_ENV_VAR), *cls._extra_requirements()] | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
self._api_key = os.environ[self._API_KEY_ENV_VAR] | ||
|
||
|
||
class UnauthenticatedApiAssistant(_ApiAssistant): | ||
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. This class is not needed. We can simply use the superclass for this. |
||
def __init__(self) -> None: | ||
super().__init__() |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,16 +4,22 @@ | |||||||||||||
|
||||||||||||||
from ragna import assistants | ||||||||||||||
from ragna._compat import anext | ||||||||||||||
from ragna.assistants._api import ApiAssistant | ||||||||||||||
from ragna.assistants._api import ( | ||||||||||||||
AuthenticatedApiAssistant, | ||||||||||||||
UnauthenticatedApiAssistant, | ||||||||||||||
_ApiAssistant, | ||||||||||||||
) | ||||||||||||||
from ragna.core import RagnaException | ||||||||||||||
from tests.utils import skip_on_windows | ||||||||||||||
|
||||||||||||||
API_ASSISTANTS = [ | ||||||||||||||
assistant | ||||||||||||||
for assistant in assistants.__dict__.values() | ||||||||||||||
if isinstance(assistant, type) | ||||||||||||||
and issubclass(assistant, ApiAssistant) | ||||||||||||||
and assistant is not ApiAssistant | ||||||||||||||
and issubclass(assistant, _ApiAssistant) | ||||||||||||||
and assistant is not _ApiAssistant | ||||||||||||||
and assistant is not AuthenticatedApiAssistant | ||||||||||||||
and assistant is not UnauthenticatedApiAssistant | ||||||||||||||
Comment on lines
+19
to
+22
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.
Suggested change
|
||||||||||||||
] | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
|
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.
This is the default return if we don't override the method. Meaning, we can just leave it out here.