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

Drop @register_assistant decorator #98

Merged
merged 9 commits into from
Jun 19, 2024
Merged

Conversation

pamella
Copy link
Collaborator

@pamella pamella commented Jun 19, 2024

Issues

Resolves #97

Description

Drop @register_assistant decorator

Screencast.from.19-06-2024.11.18.39.webm
Screencast.from.19-06-2024.11.16.55.webm

Discussion notes

Note

Hey there, I opened this draft to get your thoughts on the new approach.

[Click to see] Resolved discussion notes
  1. We could create a get_assistant_cls at django_ai_assistant/helpers/assistants.py, but we would need to use an alias when importing it in django_ai_assistant/helpers/use_cases.py or renaming the existing get_assistant_cls

    def get_assistant_cls(assistant_id: str) -> dict[str, type[AIAssistant]]:
        return AIAssistant._get_assistant_cls_registry()[assistant_id]

We opted for the alias.

  1. Without the decorator, the test_list_assistants_with_results is currently failing because it returns all AIAssistant classes created within tests/. Could it be an issue?

    image

We used __init_subclass__ in AIAssistant, and @pytest.fixture(scope="module", autouse=True) in the test files.

@pamella pamella requested a review from fjsj June 19, 2024 11:48
raise AIAssistantNotDefinedError(f"Assistant with id={assistant_id} not found")
assistant_cls = ASSISTANT_CLS_REGISTRY[assistant_id]
assistant_cls = get_assistant_cls_registry()[assistant_id]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For this usage, we could have a get_assistant_cls helper but there are a few side effects (please see the PR description for more details).

@fjsj
Copy link
Member

fjsj commented Jun 19, 2024

Overall LGTM.

We could create a get_assistant_cls at django_ai_assistant/helpers/assistants.py, but we would need to use an alias when importing it in django_ai_assistant/helpers/use_cases.py or renaming the existing get_assistant_cls

Alias is fine on the use_cases import.

Without the decorator, the test_list_assistants_with_results is currently failing because it returns all AIAssistant classes created within tests/. Could it be an issue?

That's bad because it makes unit test harder... is the problem because the __subclasses__ call isn't updated? Perhaps if we use __init_subclass__ we can solve that? See: https://stackoverflow.com/a/53101259

@pamella
Copy link
Collaborator Author

pamella commented Jun 19, 2024

Alias is fine on the use_cases import.

All right, I'll go for it.

That's bad because it makes unit test harder... is the problem because the __subclasses__ call isn't updated? Perhaps if we use __init_subclass__ we can solve that? See: https://stackoverflow.com/a/53101259

I can refactor the code to use __init_subclass__, but it doesn't solve the unit testing problem. I'm checking alternatives.

@pamella
Copy link
Collaborator Author

pamella commented Jun 19, 2024

I can refactor the code to use __init_subclass__, but it doesn't solve the unit testing problem. I'm checking alternatives.

I added the __init_subclass__ to the AIAssistant, and @pytest.fixture(scope="module", autouse=True) in the test files.
https://docs.pytest.org/en/7.1.x/how-to/fixtures.html?highlight=scope#fixture-scopes
image

@pamella pamella marked this pull request as ready for review June 19, 2024 13:22
@pamella pamella changed the title [Experimental] Drop @register_assistant decorator Drop @register_assistant decorator Jun 19, 2024
"""
super().__init_subclass__(**kwargs)
if hasattr(cls, "id") and cls.id is not None:
if not re.match(r"^[a-zA-Z0-9_-]+$", cls.id):
Copy link
Member

Choose a reason for hiding this comment

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

Can we remove the re.match from the init?

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 added the verification in the __init_subclass__ method because it occurs at the time the subclass is created, not when an instance is initialized (__init__). This way, inconsistencies in the id could be detected earlier, preventing the creation of invalid subclasses.

Copy link
Member

Choose a reason for hiding this comment

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

yes, that's good, but can we remove the one from __init__?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, will do. I will move all checks from __init__ to __init_subclass__

return cls
def get_assistant_cls(assistant_id: str) -> type[AIAssistant]:
"""Get the AIAssistant class for the given assistant ID."""
return AIAssistant.get_registry()[assistant_id]
Copy link
Member

Choose a reason for hiding this comment

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

Since we're adding classmethods to AIAssistant, maybe just make these classmethods too? This will simplify the API.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, I actually prefer this way too

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done (commit b04abb4)

Copy link
Member

@fjsj fjsj left a comment

Choose a reason for hiding this comment

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

Overall LGTM, but please check comments.
Also, please update docs.

@pamella pamella mentioned this pull request Jun 19, 2024
@pamella pamella requested a review from fjsj June 19, 2024 14:30
Copy link
Contributor

@amandasavluchinske amandasavluchinske left a comment

Choose a reason for hiding this comment

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

Nice! Much cleaner :)

@fjsj fjsj merged commit 31e86b5 into main Jun 19, 2024
6 checks passed
@fjsj fjsj deleted the exp/drop-register-decorator branch June 19, 2024 15:56
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.

Investigate whether we can drop @register_assistant
3 participants