You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2021/01/05 19:08:40 UTC

[GitHub] [beam] pabloem commented on a change in pull request #13617: [BEAM-11289] [Python] Integrate Google Cloud Recommendations AI functionality

pabloem commented on a change in pull request #13617:
URL: https://github.com/apache/beam/pull/13617#discussion_r549415714



##########
File path: sdks/python/apache_beam/ml/gcp/recommendations_ai.py
##########
@@ -0,0 +1,472 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+"""A connector for sending API requests to the GCP Recommendations AI API (https://cloud.google.com/recommendations)."""
+
+from __future__ import absolute_import
+
+import itertools
+from cachetools.func import ttl_cache
+from google.protobuf.json_format import MessageToDict
+from typing import Any, Dict, Iterable, List, Tuple, Union
+
+from apache_beam import TimeDomain, typehints, pvalue
+from apache_beam.coders.coders import (IterableCoder, ProtoCoder, StrUtf8Coder, TupleCoder, MapCoder, PickleCoder)
+from apache_beam.metrics import Metrics
+from apache_beam.options.pipeline_options import GoogleCloudOptions
+from apache_beam.transforms import DoFn, ParDo, PTransform
+from apache_beam.transforms.userstate import BagStateSpec, TimerSpec, on_timer
+
+try:
+    from google.cloud import recommendationengine
+except ImportError:
+    raise ImportError('Google Cloud Recommendation AI not supported for this execution '
+                      'environment (could not import google.cloud.recommendationengine).')
+
+__all__ = ['CreateCatalogItem', 'WriteUserEvent', 'ImportCatalogItems', 'ImportUserEvents', 'PredictUserEvent']
+
+
+@ttl_cache(maxsize=128, ttl=3600)
+def get_recommendation_prediction_client():
+    """Returns a Recommendation AI - Prediction Service client."""
+    _client = recommendationengine.PredictionServiceClient()
+    return _client
+
+
+@ttl_cache(maxsize=128, ttl=3600)
+def get_recommendation_catalog_client():
+    """Returns a Recommendation AI - Catalog Service client."""
+    _client = recommendationengine.CatalogServiceClient()
+    return _client
+
+
+@ttl_cache(maxsize=128, ttl=3600)
+def get_recommendation_user_event_client():
+    """Returns a Recommendation AI - UserEvent Service client."""
+    _client = recommendationengine.UserEventServiceClient()
+    return _client
+
+
+class CreateCatalogItem(PTransform):
+    """Creates catalogitem information.
+    The ``PTranform`` returns a PCollectionTuple with a PCollections of successfully and failed created CatalogItems.
+    
+    Example usage: 
+    pipeline | CreateCatalogItem(project='example-gcp-project', catalog_name='my-catalog')
+    """
+
+    def __init__(self, project=None, retry=None, timeout=120, metadata=None, catalog_name="default_catalog"):

Review comment:
       can you add types to the code instead of the comments?

##########
File path: sdks/python/apache_beam/ml/gcp/recommendations_ai.py
##########
@@ -0,0 +1,472 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+"""A connector for sending API requests to the GCP Recommendations AI API (https://cloud.google.com/recommendations)."""

Review comment:
       it would be good if you could add a more complete writeup with examples on how to use the transforms here. thoughts?

##########
File path: sdks/python/apache_beam/ml/gcp/recommendations_ai.py
##########
@@ -0,0 +1,472 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+"""A connector for sending API requests to the GCP Recommendations AI API (https://cloud.google.com/recommendations)."""
+
+from __future__ import absolute_import
+
+import itertools
+from cachetools.func import ttl_cache
+from google.protobuf.json_format import MessageToDict
+from typing import Any, Dict, Iterable, List, Tuple, Union
+
+from apache_beam import TimeDomain, typehints, pvalue
+from apache_beam.coders.coders import (IterableCoder, ProtoCoder, StrUtf8Coder, TupleCoder, MapCoder, PickleCoder)
+from apache_beam.metrics import Metrics
+from apache_beam.options.pipeline_options import GoogleCloudOptions
+from apache_beam.transforms import DoFn, ParDo, PTransform
+from apache_beam.transforms.userstate import BagStateSpec, TimerSpec, on_timer
+
+try:
+    from google.cloud import recommendationengine
+except ImportError:
+    raise ImportError('Google Cloud Recommendation AI not supported for this execution '
+                      'environment (could not import google.cloud.recommendationengine).')
+
+__all__ = ['CreateCatalogItem', 'WriteUserEvent', 'ImportCatalogItems', 'ImportUserEvents', 'PredictUserEvent']
+
+
+@ttl_cache(maxsize=128, ttl=3600)
+def get_recommendation_prediction_client():
+    """Returns a Recommendation AI - Prediction Service client."""
+    _client = recommendationengine.PredictionServiceClient()
+    return _client
+
+
+@ttl_cache(maxsize=128, ttl=3600)
+def get_recommendation_catalog_client():
+    """Returns a Recommendation AI - Catalog Service client."""
+    _client = recommendationengine.CatalogServiceClient()
+    return _client
+
+
+@ttl_cache(maxsize=128, ttl=3600)
+def get_recommendation_user_event_client():
+    """Returns a Recommendation AI - UserEvent Service client."""
+    _client = recommendationengine.UserEventServiceClient()
+    return _client
+
+
+class CreateCatalogItem(PTransform):
+    """Creates catalogitem information.
+    The ``PTranform`` returns a PCollectionTuple with a PCollections of successfully and failed created CatalogItems.
+    
+    Example usage: 
+    pipeline | CreateCatalogItem(project='example-gcp-project', catalog_name='my-catalog')
+    """
+
+    def __init__(self, project=None, retry=None, timeout=120, metadata=None, catalog_name="default_catalog"):

Review comment:
       (i mean python type hints :))

##########
File path: sdks/python/apache_beam/ml/gcp/recommendations_ai_test.py
##########
@@ -0,0 +1,179 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+"""Unit tests for Recommendations AI transforms."""
+
+# TODO: write unit tests for transforms
+
+from __future__ import absolute_import
+
+import mock
+import unittest
+
+import apache_beam as beam
+from apache_beam.metrics import MetricsFilter
+
+try:
+    from google.cloud import recommendationengine
+
+    from apache_beam.ml.gcp import recommendations_ai
+except ImportError:
+    recommendationengine = None
+    raise ImportError('Google Cloud Recommendation AI not supported for this execution '
+                      'environment (could not import google.cloud.recommendationengine).')
+
+
+# @unittest.skipIf(recommendationengine is None, "Recommendations AI dependencies not installed.")

Review comment:
       you should probably keep this line

##########
File path: sdks/python/apache_beam/ml/gcp/recommendations_ai.py
##########
@@ -0,0 +1,472 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+"""A connector for sending API requests to the GCP Recommendations AI API (https://cloud.google.com/recommendations)."""

Review comment:
       (though I see that you have examples further down - so maybe just a writeup will be enough)




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org