-
Notifications
You must be signed in to change notification settings - Fork 264
Add recommendations backend boilerplate #4301
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
Merged
akolson
merged 8 commits into
learningequality:unstable
from
akolson:add-recommendations-backend-boilerplate
Sep 29, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5ae448b
adds Recommendations backend boilerplate class
akolson 051968a
updates test
akolson ea5396a
Refactors file locations
akolson 608be48
Refactors boilerplate
akolson af36041
Updates branch with latest dev changes
akolson ba921bd
Refactors boilerplate
akolson 3d8cf19
updates typo in factory class
akolson 7a19729
Refactors boilerplate to remove ambiguity
akolson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
contentcuration/contentcuration/tests/utils/test_recommendations.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from django.test import TestCase | ||
|
|
||
| from contentcuration.utils.recommendations import Recommendations | ||
|
|
||
|
|
||
| class RecommendationsTestCase(TestCase): | ||
| def test_backend_initialization(self): | ||
| recomendations = Recommendations() | ||
| self.assertIsNotNone(recomendations) | ||
| self.assertIsInstance(recomendations.get_instance(), Recommendations) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| from typing import Union | ||
|
|
||
| from automation.utils.appnexus.base import Adapter | ||
| from automation.utils.appnexus.base import Backend | ||
| from automation.utils.appnexus.base import BackendFactory | ||
| from automation.utils.appnexus.base import BackendRequest | ||
| from automation.utils.appnexus.base import BackendResponse | ||
|
|
||
|
|
||
| class RecommendationsBackendRequest(BackendRequest): | ||
| pass | ||
|
|
||
|
|
||
| class RecommedationsRequest(RecommendationsBackendRequest): | ||
| def __init__(self) -> None: | ||
| super().__init__() | ||
|
|
||
|
|
||
| class EmbeddingsRequest(RecommendationsBackendRequest): | ||
| def __init__(self) -> None: | ||
| super().__init__() | ||
|
|
||
|
|
||
| class RecommendationsBackendResponse(BackendResponse): | ||
| pass | ||
|
|
||
|
|
||
| class RecommendationsResponse(RecommendationsBackendResponse): | ||
| def __init__(self) -> None: | ||
| pass | ||
|
|
||
|
|
||
| class EmbeddingsResponse(RecommendationsBackendResponse): | ||
| def __init__(self) -> None: | ||
| pass | ||
|
|
||
|
|
||
| class RecommendationsBackendFactory(BackendFactory): | ||
| def create_backend(self) -> Backend: | ||
| # Return backend based on some setting. | ||
| return super().create_backend() | ||
|
|
||
|
|
||
| class RecommendationsAdapter(Adapter): | ||
|
|
||
| def generate_embedding(self, text) -> EmbeddingsResponse: | ||
| request = EmbeddingsRequest() | ||
| return self.backend.make_request(request) | ||
|
|
||
| def get_recommendations(self) -> RecommendationsResponse: | ||
| request = RecommedationsRequest() | ||
| return self.backend.make_request(request) | ||
|
|
||
|
|
||
| class Recommendations(Backend): | ||
|
|
||
| def connect(self) -> None: | ||
| return super().connect() | ||
|
|
||
| def make_request(self, request) -> Union[EmbeddingsResponse, RecommendationsResponse]: | ||
| return super().make_request(request) | ||
|
|
||
| @classmethod | ||
| def _create_instance(cls) -> 'Recommendations': | ||
| return cls() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 was just curious, assuming that we would be needing this connect method we might want to check in other functions that request, and make_request if the connection is currently alive? if its not then we may want to establish the connection first? I believe this should not be needed in the class code itself and should be handled by the developer while using the class but a safety check before making requests can be useful?
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.
Yes @ozer550 you raise a great point. I am fine with either removing it or it remaining. Though the latter enforces what needs to be done and rids the make_request from any connection detail(Single responsibility pattern!!). I am almost certain all backends have some form of connection. Its very unlikely that they keep their door open to you all the time 🙂
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.
yes, this makes sense! I think we should let it remain here.