-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4306 from ozer550/Manager_Class_Appnexus
Implements Automation Manager Class
- Loading branch information
Showing
3 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
contentcuration/contentcuration/tests/utils/test_automation_manager.py
This file contains 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,40 @@ | ||
import unittest | ||
from unittest.mock import MagicMock | ||
|
||
from contentcuration.utils.automation_manager import AutomationManager | ||
|
||
|
||
class AutomationManagerTestCase(unittest.TestCase): | ||
def setUp(self): | ||
self.automation_manager = AutomationManager() | ||
|
||
def test_creation(self): | ||
# Check if an instance of AutomationManager is created successfully | ||
self.assertIsInstance(self.automation_manager, AutomationManager) | ||
|
||
def test_generate_embedding(self): | ||
text = "Some text that needs to be embedded" | ||
# Mock the generate_embedding method of RecommendationsAdapter | ||
# as the implementation is yet to be done | ||
self.automation_manager.recommendations_backend_adapter.generate_embedding = MagicMock(return_value=[0.1, 0.2, 0.3]) | ||
embedding_vector = self.automation_manager.generate_embedding(text) | ||
self.assertIsNotNone(embedding_vector) | ||
|
||
def test_embedding_exists(self): | ||
embedding_vector = [0.1, 0.2, 0.3] | ||
# Currently no solid implementation exists for this | ||
# So the embadding_exists function returns true anyways | ||
exists = self.automation_manager.embedding_exists(embedding_vector) | ||
self.assertTrue(exists) | ||
|
||
def test_load_recommendations(self): | ||
embedding_vector = [0.1, 0.2, 0.3] | ||
self.automation_manager.recommendations_backend_adapter.get_recommendations = MagicMock(return_value=["item1", "item2"]) | ||
recommendations = self.automation_manager.load_recommendations(embedding_vector) | ||
self.assertIsInstance(recommendations, list) | ||
|
||
def test_cache_embeddings(self): | ||
embeddings_list = [[0.1, 0.2, 0.3]] | ||
# Currently the function returns true anyways | ||
success = self.automation_manager.cache_embeddings(embeddings_list) | ||
self.assertTrue(success) |
52 changes: 52 additions & 0 deletions
52
contentcuration/contentcuration/utils/automation_manager.py
This file contains 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,52 @@ | ||
from contentcuration.utils.recommendations import RecommendationsAdapter | ||
from contentcuration.utils.recommendations import RecommendationsBackendFactory | ||
|
||
|
||
class AutomationManager: | ||
def __init__(self): | ||
self.recommendations_backend_factory = RecommendationsBackendFactory() | ||
self.recommendations_backend_instance = self.recommendations_backend_factory.create_backend() | ||
self.recommendations_backend_adapter = RecommendationsAdapter(self.recommendations_backend_instance) | ||
|
||
def generate_embedding(self, text): | ||
""" | ||
Generate an embedding vector for the given text. | ||
Args: | ||
text (str): The text for which to generate an embedding. | ||
Returns: | ||
Vector: The generated embedding vector. | ||
""" | ||
embedding_vector = self.recommendations_backend_adapter.generate_embedding(text=text) | ||
return embedding_vector | ||
|
||
def embedding_exists(self, embedding): | ||
""" | ||
Check if the given embedding vector exists. | ||
Args: | ||
embedding (Vector): The embedding vector to check. | ||
Returns: | ||
bool: True if the embedding exists, False otherwise. | ||
""" | ||
return self.recommendations_backend_adapter.embedding_exists(embedding=embedding) | ||
|
||
def load_recommendations(self, embedding): | ||
""" | ||
Load recommendations based on the given embedding vector. | ||
Args: | ||
embedding (Vector): The embedding vector to use for recommendations. | ||
Returns: | ||
list: A list of recommended items. | ||
""" | ||
# Need to extract the recommendation list from the ResponseObject and change the return statement | ||
self.recommendations_backend_adapter.get_recommendations(embedding=embedding) | ||
return [] | ||
|
||
def cache_embeddings(self, embeddings): | ||
""" | ||
Cache a list of embedding vectors. | ||
Args: | ||
embeddings (list): A list of embedding vectors to cache. | ||
Returns: | ||
bool: True if caching was successful, False otherwise. | ||
""" | ||
return self.recommendations_backend_adapter.cache_embeddings(embeddings) |
This file contains 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