Google Cloud Recommendations AI Client Library

0.13.0 · active · verified Fri Apr 10

The Google Cloud Recommendations AI API client library enables developers to integrate recommendation capabilities into their applications. It provides functionalities for managing product catalogs, ingesting user events, and generating personalized recommendations. The current version is `0.13.0`, and new releases typically occur frequently as part of the broader Google Cloud Python client libraries monorepo.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate the `CatalogServiceClient` and list existing catalog items. Replace 'your-gcp-project-id' and 'global' with your actual Google Cloud project ID and the relevant location, or set them via environment variables. This requires the Recommendations AI API to be enabled and your service account to have appropriate permissions (e.g., `Recommendations AI Admin`).

import os
from google.cloud.recommendations_ai_v1beta1 import CatalogServiceClient

project_id = os.environ.get('GCP_PROJECT_ID', 'your-gcp-project-id')
location_id = os.environ.get('GCP_LOCATION_ID', 'global') # Recommendations AI often uses 'global'

def list_first_five_catalog_items():
    """Lists the first five catalog items in the default catalog."""
    client = CatalogServiceClient()
    # The parent for listing catalog items is a catalog resource.
    # 'default_catalog' is the common default for Recommendations AI.
    parent = f"projects/{project_id}/locations/{location_id}/catalogs/default_catalog"

    request = {
        "parent": parent,
        "page_size": 5
    }

    print(f"Listing catalog items for parent: {parent}")
    try:
        page_iterator = client.list_catalog_items(request=request)

        found_items = False
        for item in page_iterator:
            print(f"Catalog Item ID: {item.id}, Title: {item.title}")
            found_items = True
        
        if not found_items:
            print("No catalog items found. Ensure your catalog is populated.")

    except Exception as e:
        print(f"An error occurred: {e}")
        print("Make sure 'GCP_PROJECT_ID' and 'GCP_LOCATION_ID' environment variables are set or replaced.")
        print("Also, ensure the Recommendations AI API is enabled and your service account has permissions.")

if __name__ == '__main__':
    list_first_five_catalog_items()

view raw JSON →