Recommendations AI v1beta1 API - Class PredictionServiceClient (2.0.0-beta05)

public abstract class PredictionServiceClient

Reference documentation and code samples for the Recommendations AI v1beta1 API class PredictionServiceClient.

PredictionService client wrapper, for convenient use.

Inheritance

object > PredictionServiceClient

Namespace

Google.Cloud.RecommendationEngine.V1Beta1

Assembly

Google.Cloud.RecommendationEngine.V1Beta1.dll

Remarks

Service for making recommendation prediction.

Properties

DefaultEndpoint

public static string DefaultEndpoint { get; }

The default endpoint for the PredictionService service, which is a host of "recommendationengine.googleapis.com" and a port of 443.

Property Value
Type Description
string

DefaultScopes

public static IReadOnlyList<string> DefaultScopes { get; }

The default PredictionService scopes.

Property Value
Type Description
IReadOnlyListstring
Remarks

The default PredictionService scopes are:

GrpcClient

public virtual PredictionService.PredictionServiceClient GrpcClient { get; }

The underlying gRPC PredictionService client

Property Value
Type Description
PredictionServicePredictionServiceClient

ServiceMetadata

public static ServiceMetadata ServiceMetadata { get; }

The service metadata associated with this client type.

Property Value
Type Description
ServiceMetadata

Methods

Create()

public static PredictionServiceClient Create()

Synchronously creates a PredictionServiceClient using the default credentials, endpoint and settings. To specify custom credentials or other settings, use PredictionServiceClientBuilder.

Returns
Type Description
PredictionServiceClient

The created PredictionServiceClient.

CreateAsync(CancellationToken)

public static Task<PredictionServiceClient> CreateAsync(CancellationToken cancellationToken = default)

Asynchronously creates a PredictionServiceClient using the default credentials, endpoint and settings. To specify custom credentials or other settings, use PredictionServiceClientBuilder.

Parameter
Name Description
cancellationToken CancellationToken

The CancellationToken to use while creating the client.

Returns
Type Description
TaskPredictionServiceClient

The task representing the created PredictionServiceClient.

Predict(PlacementName, UserEvent, string, int?, CallSettings)

public virtual PagedEnumerable<PredictResponse, PredictResponse.Types.PredictionResult> Predict(PlacementName name, UserEvent userEvent, string pageToken = null, int? pageSize = null, CallSettings callSettings = null)

Makes a recommendation prediction. If using API Key based authentication, the API Key must be registered using the [PredictionApiKeyRegistry][google.cloud.recommendationengine.v1beta1.PredictionApiKeyRegistry] service. Learn more.

Parameters
Name Description
name PlacementName

Required. Full resource name of the format: {name=projects/*/locations/global/catalogs/default_catalog/eventStores/default_event_store/placements/*} The id of the recommendation engine placement. This id is used to identify the set of models that will be used to make the prediction.

We currently support three placements with the following IDs by default:

  • shopping_cart: Predicts items frequently bought together with one or more catalog items in the same shopping session. Commonly displayed after add-to-cart events, on product detail pages, or on the shopping cart page.

  • home_page: Predicts the next product that a user will most likely engage with or purchase based on the shopping or viewing history of the specified userId or visitorId. For example - Recommendations for you.

  • product_detail: Predicts the next product that a user will most likely engage with or purchase. The prediction is based on the shopping or viewing history of the specified userId or visitorId and its relevance to a specified CatalogItem. Typically used on product detail pages. For example - More items like this.

  • recently_viewed_default: Returns up to 75 items recently viewed by the specified userId or visitorId, most recent ones first. Returns nothing if neither of them has viewed any items yet. For example - Recently viewed.

The full list of available placements can be seen at https://console.cloud.google.com/recommendation/datafeeds/default_catalog/dashboard

userEvent UserEvent

Required. Context about the user, what they are looking at and what action they took to trigger the predict request. Note that this user event detail won't be ingested to userEvent logs. Thus, a separate userEvent write request is required for event logging.

pageToken string

The token returned from the previous request. A value of null or an empty string retrieves the first page.

pageSize int

The size of page to request. The response will not be larger than this, but may be smaller. A value of null or 0 uses a server-defined page size.

callSettings CallSettings

If not null, applies overrides to this RPC call.

Returns
Type Description
PagedEnumerablePredictResponsePredictResponseTypesPredictionResult

A pageable sequence of PredictResponse.Types.PredictionResult resources.

Example
// Create client
PredictionServiceClient predictionServiceClient = PredictionServiceClient.Create();
// Initialize request argument(s)
PlacementName name = PlacementName.FromProjectLocationCatalogEventStorePlacement("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]");
UserEvent userEvent = new UserEvent();
// Make the request
PagedEnumerable<PredictResponse, PredictResponse.Types.PredictionResult> response = predictionServiceClient.Predict(name, userEvent);

// Iterate over all response items, lazily performing RPCs as required
foreach (PredictResponse.Types.PredictionResult item in response)
{
    // Do something with each item
    Console.WriteLine(item);
}

// Or iterate over pages (of server-defined size), performing one RPC per page
foreach (PredictResponse page in response.AsRawResponses())
{
    // Do something with each page of items
    Console.WriteLine("A page of results:");
    foreach (PredictResponse.Types.PredictionResult item in page)
    {
        // Do something with each item
        Console.WriteLine(item);
    }
}

// Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
int pageSize = 10;
Page<PredictResponse.Types.PredictionResult> singlePage = response.ReadPage(pageSize);
// Do something with the page of items
Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
foreach (PredictResponse.Types.PredictionResult item in singlePage)
{
    // Do something with each item
    Console.WriteLine(item);
}
// Store the pageToken, for when the next page is required.
string nextPageToken = singlePage.NextPageToken;

Predict(PredictRequest, CallSettings)

public virtual PagedEnumerable<PredictResponse, PredictResponse.Types.PredictionResult> Predict(PredictRequest request, CallSettings callSettings = null)

Makes a recommendation prediction. If using API Key based authentication, the API Key must be registered using the [PredictionApiKeyRegistry][google.cloud.recommendationengine.v1beta1.PredictionApiKeyRegistry] service. Learn more.

Parameters
Name Description
request PredictRequest

The request object containing all of the parameters for the API call.

callSettings CallSettings

If not null, applies overrides to this RPC call.

Returns
Type Description
PagedEnumerablePredictResponsePredictResponseTypesPredictionResult

A pageable sequence of PredictResponse.Types.PredictionResult resources.

Example
// Create client
PredictionServiceClient predictionServiceClient = PredictionServiceClient.Create();
// Initialize request argument(s)
PredictRequest request = new PredictRequest
{
    PlacementName = PlacementName.FromProjectLocationCatalogEventStorePlacement("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]"),
    UserEvent = new UserEvent(),
    Filter = "",
    DryRun = false,
    Params = { { "", new Value() }, },
    Labels = { { "", "" }, },
};
// Make the request
PagedEnumerable<PredictResponse, PredictResponse.Types.PredictionResult> response = predictionServiceClient.Predict(request);

// Iterate over all response items, lazily performing RPCs as required
foreach (PredictResponse.Types.PredictionResult item in response)
{
    // Do something with each item
    Console.WriteLine(item);
}

// Or iterate over pages (of server-defined size), performing one RPC per page
foreach (PredictResponse page in response.AsRawResponses())
{
    // Do something with each page of items
    Console.WriteLine("A page of results:");
    foreach (PredictResponse.Types.PredictionResult item in page)
    {
        // Do something with each item
        Console.WriteLine(item);
    }
}

// Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
int pageSize = 10;
Page<PredictResponse.Types.PredictionResult> singlePage = response.ReadPage(pageSize);
// Do something with the page of items
Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
foreach (PredictResponse.Types.PredictionResult item in singlePage)
{
    // Do something with each item
    Console.WriteLine(item);
}
// Store the pageToken, for when the next page is required.
string nextPageToken = singlePage.NextPageToken;

Predict(string, UserEvent, string, int?, CallSettings)

public virtual PagedEnumerable<PredictResponse, PredictResponse.Types.PredictionResult> Predict(string name, UserEvent userEvent, string pageToken = null, int? pageSize = null, CallSettings callSettings = null)

Makes a recommendation prediction. If using API Key based authentication, the API Key must be registered using the [PredictionApiKeyRegistry][google.cloud.recommendationengine.v1beta1.PredictionApiKeyRegistry] service. Learn more.

Parameters
Name Description
name string

Required. Full resource name of the format: {name=projects/*/locations/global/catalogs/default_catalog/eventStores/default_event_store/placements/*} The id of the recommendation engine placement. This id is used to identify the set of models that will be used to make the prediction.

We currently support three placements with the following IDs by default:

  • shopping_cart: Predicts items frequently bought together with one or more catalog items in the same shopping session. Commonly displayed after add-to-cart events, on product detail pages, or on the shopping cart page.

  • home_page: Predicts the next product that a user will most likely engage with or purchase based on the shopping or viewing history of the specified userId or visitorId. For example - Recommendations for you.

  • product_detail: Predicts the next product that a user will most likely engage with or purchase. The prediction is based on the shopping or viewing history of the specified userId or visitorId and its relevance to a specified CatalogItem. Typically used on product detail pages. For example - More items like this.

  • recently_viewed_default: Returns up to 75 items recently viewed by the specified userId or visitorId, most recent ones first. Returns nothing if neither of them has viewed any items yet. For example - Recently viewed.

The full list of available placements can be seen at https://console.cloud.google.com/recommendation/datafeeds/default_catalog/dashboard

userEvent UserEvent

Required. Context about the user, what they are looking at and what action they took to trigger the predict request. Note that this user event detail won't be ingested to userEvent logs. Thus, a separate userEvent write request is required for event logging.

pageToken string

The token returned from the previous request. A value of null or an empty string retrieves the first page.

pageSize int

The size of page to request. The response will not be larger than this, but may be smaller. A value of null or 0 uses a server-defined page size.

callSettings CallSettings

If not null, applies overrides to this RPC call.

Returns
Type Description
PagedEnumerablePredictResponsePredictResponseTypesPredictionResult

A pageable sequence of PredictResponse.Types.PredictionResult resources.

Example
// Create client
PredictionServiceClient predictionServiceClient = PredictionServiceClient.Create();
// Initialize request argument(s)
string name = "projects/[PROJECT]/locations/[LOCATION]/catalogs/[CATALOG]/eventStores/[EVENT_STORE]/placements/[PLACEMENT]";
UserEvent userEvent = new UserEvent();
// Make the request
PagedEnumerable<PredictResponse, PredictResponse.Types.PredictionResult> response = predictionServiceClient.Predict(name, userEvent);

// Iterate over all response items, lazily performing RPCs as required
foreach (PredictResponse.Types.PredictionResult item in response)
{
    // Do something with each item
    Console.WriteLine(item);
}

// Or iterate over pages (of server-defined size), performing one RPC per page
foreach (PredictResponse page in response.AsRawResponses())
{
    // Do something with each page of items
    Console.WriteLine("A page of results:");
    foreach (PredictResponse.Types.PredictionResult item in page)
    {
        // Do something with each item
        Console.WriteLine(item);
    }
}

// Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
int pageSize = 10;
Page<PredictResponse.Types.PredictionResult> singlePage = response.ReadPage(pageSize);
// Do something with the page of items
Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
foreach (PredictResponse.Types.PredictionResult item in singlePage)
{
    // Do something with each item
    Console.WriteLine(item);
}
// Store the pageToken, for when the next page is required.
string nextPageToken = singlePage.NextPageToken;

PredictAsync(PlacementName, UserEvent, string, int?, CallSettings)

public virtual PagedAsyncEnumerable<PredictResponse, PredictResponse.Types.PredictionResult> PredictAsync(PlacementName name, UserEvent userEvent, string pageToken = null, int? pageSize = null, CallSettings callSettings = null)

Makes a recommendation prediction. If using API Key based authentication, the API Key must be registered using the [PredictionApiKeyRegistry][google.cloud.recommendationengine.v1beta1.PredictionApiKeyRegistry] service. Learn more.

Parameters
Name Description
name PlacementName

Required. Full resource name of the format: {name=projects/*/locations/global/catalogs/default_catalog/eventStores/default_event_store/placements/*} The id of the recommendation engine placement. This id is used to identify the set of models that will be used to make the prediction.

We currently support three placements with the following IDs by default:

  • shopping_cart: Predicts items frequently bought together with one or more catalog items in the same shopping session. Commonly displayed after add-to-cart events, on product detail pages, or on the shopping cart page.

  • home_page: Predicts the next product that a user will most likely engage with or purchase based on the shopping or viewing history of the specified userId or visitorId. For example - Recommendations for you.

  • product_detail: Predicts the next product that a user will most likely engage with or purchase. The prediction is based on the shopping or viewing history of the specified userId or visitorId and its relevance to a specified CatalogItem. Typically used on product detail pages. For example - More items like this.

  • recently_viewed_default: Returns up to 75 items recently viewed by the specified userId or visitorId, most recent ones first. Returns nothing if neither of them has viewed any items yet. For example - Recently viewed.

The full list of available placements can be seen at https://console.cloud.google.com/recommendation/datafeeds/default_catalog/dashboard

userEvent UserEvent

Required. Context about the user, what they are looking at and what action they took to trigger the predict request. Note that this user event detail won't be ingested to userEvent logs. Thus, a separate userEvent write request is required for event logging.

pageToken string

The token returned from the previous request. A value of null or an empty string retrieves the first page.

pageSize int

The size of page to request. The response will not be larger than this, but may be smaller. A value of null or 0 uses a server-defined page size.

callSettings CallSettings

If not null, applies overrides to this RPC call.

Returns
Type Description
PagedAsyncEnumerablePredictResponsePredictResponseTypesPredictionResult

A pageable asynchronous sequence of PredictResponse.Types.PredictionResult resources.

Example
// Create client
PredictionServiceClient predictionServiceClient = await PredictionServiceClient.CreateAsync();
// Initialize request argument(s)
PlacementName name = PlacementName.FromProjectLocationCatalogEventStorePlacement("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]");
UserEvent userEvent = new UserEvent();
// Make the request
PagedAsyncEnumerable<PredictResponse, PredictResponse.Types.PredictionResult> response = predictionServiceClient.PredictAsync(name, userEvent);

// Iterate over all response items, lazily performing RPCs as required
await response.ForEachAsync((PredictResponse.Types.PredictionResult item) =>
{
    // Do something with each item
    Console.WriteLine(item);
});

// Or iterate over pages (of server-defined size), performing one RPC per page
await response.AsRawResponses().ForEachAsync((PredictResponse page) =>
{
    // Do something with each page of items
    Console.WriteLine("A page of results:");
    foreach (PredictResponse.Types.PredictionResult item in page)
    {
        // Do something with each item
        Console.WriteLine(item);
    }
});

// Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
int pageSize = 10;
Page<PredictResponse.Types.PredictionResult> singlePage = await response.ReadPageAsync(pageSize);
// Do something with the page of items
Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
foreach (PredictResponse.Types.PredictionResult item in singlePage)
{
    // Do something with each item
    Console.WriteLine(item);
}
// Store the pageToken, for when the next page is required.
string nextPageToken = singlePage.NextPageToken;

PredictAsync(PredictRequest, CallSettings)

public virtual PagedAsyncEnumerable<PredictResponse, PredictResponse.Types.PredictionResult> PredictAsync(PredictRequest request, CallSettings callSettings = null)

Makes a recommendation prediction. If using API Key based authentication, the API Key must be registered using the [PredictionApiKeyRegistry][google.cloud.recommendationengine.v1beta1.PredictionApiKeyRegistry] service. Learn more.

Parameters
Name Description
request PredictRequest

The request object containing all of the parameters for the API call.

callSettings CallSettings

If not null, applies overrides to this RPC call.

Returns
Type Description
PagedAsyncEnumerablePredictResponsePredictResponseTypesPredictionResult

A pageable asynchronous sequence of PredictResponse.Types.PredictionResult resources.

Example
// Create client
PredictionServiceClient predictionServiceClient = await PredictionServiceClient.CreateAsync();
// Initialize request argument(s)
PredictRequest request = new PredictRequest
{
    PlacementName = PlacementName.FromProjectLocationCatalogEventStorePlacement("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]"),
    UserEvent = new UserEvent(),
    Filter = "",
    DryRun = false,
    Params = { { "", new Value() }, },
    Labels = { { "", "" }, },
};
// Make the request
PagedAsyncEnumerable<PredictResponse, PredictResponse.Types.PredictionResult> response = predictionServiceClient.PredictAsync(request);

// Iterate over all response items, lazily performing RPCs as required
await response.ForEachAsync((PredictResponse.Types.PredictionResult item) =>
{
    // Do something with each item
    Console.WriteLine(item);
});

// Or iterate over pages (of server-defined size), performing one RPC per page
await response.AsRawResponses().ForEachAsync((PredictResponse page) =>
{
    // Do something with each page of items
    Console.WriteLine("A page of results:");
    foreach (PredictResponse.Types.PredictionResult item in page)
    {
        // Do something with each item
        Console.WriteLine(item);
    }
});

// Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
int pageSize = 10;
Page<PredictResponse.Types.PredictionResult> singlePage = await response.ReadPageAsync(pageSize);
// Do something with the page of items
Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
foreach (PredictResponse.Types.PredictionResult item in singlePage)
{
    // Do something with each item
    Console.WriteLine(item);
}
// Store the pageToken, for when the next page is required.
string nextPageToken = singlePage.NextPageToken;

PredictAsync(string, UserEvent, string, int?, CallSettings)

public virtual PagedAsyncEnumerable<PredictResponse, PredictResponse.Types.PredictionResult> PredictAsync(string name, UserEvent userEvent, string pageToken = null, int? pageSize = null, CallSettings callSettings = null)

Makes a recommendation prediction. If using API Key based authentication, the API Key must be registered using the [PredictionApiKeyRegistry][google.cloud.recommendationengine.v1beta1.PredictionApiKeyRegistry] service. Learn more.

Parameters
Name Description
name string

Required. Full resource name of the format: {name=projects/*/locations/global/catalogs/default_catalog/eventStores/default_event_store/placements/*} The id of the recommendation engine placement. This id is used to identify the set of models that will be used to make the prediction.

We currently support three placements with the following IDs by default:

  • shopping_cart: Predicts items frequently bought together with one or more catalog items in the same shopping session. Commonly displayed after add-to-cart events, on product detail pages, or on the shopping cart page.

  • home_page: Predicts the next product that a user will most likely engage with or purchase based on the shopping or viewing history of the specified userId or visitorId. For example - Recommendations for you.

  • product_detail: Predicts the next product that a user will most likely engage with or purchase. The prediction is based on the shopping or viewing history of the specified userId or visitorId and its relevance to a specified CatalogItem. Typically used on product detail pages. For example - More items like this.

  • recently_viewed_default: Returns up to 75 items recently viewed by the specified userId or visitorId, most recent ones first. Returns nothing if neither of them has viewed any items yet. For example - Recently viewed.

The full list of available placements can be seen at https://console.cloud.google.com/recommendation/datafeeds/default_catalog/dashboard

userEvent UserEvent

Required. Context about the user, what they are looking at and what action they took to trigger the predict request. Note that this user event detail won't be ingested to userEvent logs. Thus, a separate userEvent write request is required for event logging.

pageToken string

The token returned from the previous request. A value of null or an empty string retrieves the first page.

pageSize int

The size of page to request. The response will not be larger than this, but may be smaller. A value of null or 0 uses a server-defined page size.

callSettings CallSettings

If not null, applies overrides to this RPC call.

Returns
Type Description
PagedAsyncEnumerablePredictResponsePredictResponseTypesPredictionResult

A pageable asynchronous sequence of PredictResponse.Types.PredictionResult resources.

Example
// Create client
PredictionServiceClient predictionServiceClient = await PredictionServiceClient.CreateAsync();
// Initialize request argument(s)
string name = "projects/[PROJECT]/locations/[LOCATION]/catalogs/[CATALOG]/eventStores/[EVENT_STORE]/placements/[PLACEMENT]";
UserEvent userEvent = new UserEvent();
// Make the request
PagedAsyncEnumerable<PredictResponse, PredictResponse.Types.PredictionResult> response = predictionServiceClient.PredictAsync(name, userEvent);

// Iterate over all response items, lazily performing RPCs as required
await response.ForEachAsync((PredictResponse.Types.PredictionResult item) =>
{
    // Do something with each item
    Console.WriteLine(item);
});

// Or iterate over pages (of server-defined size), performing one RPC per page
await response.AsRawResponses().ForEachAsync((PredictResponse page) =>
{
    // Do something with each page of items
    Console.WriteLine("A page of results:");
    foreach (PredictResponse.Types.PredictionResult item in page)
    {
        // Do something with each item
        Console.WriteLine(item);
    }
});

// Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
int pageSize = 10;
Page<PredictResponse.Types.PredictionResult> singlePage = await response.ReadPageAsync(pageSize);
// Do something with the page of items
Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
foreach (PredictResponse.Types.PredictionResult item in singlePage)
{
    // Do something with each item
    Console.WriteLine(item);
}
// Store the pageToken, for when the next page is required.
string nextPageToken = singlePage.NextPageToken;

ShutdownDefaultChannelsAsync()

public static Task ShutdownDefaultChannelsAsync()

Shuts down any channels automatically created by Create() and CreateAsync(CancellationToken). Channels which weren't automatically created are not affected.

Returns
Type Description
Task

A task representing the asynchronous shutdown operation.

Remarks

After calling this method, further calls to Create() and CreateAsync(CancellationToken) will create new channels, which could in turn be shut down by another call to this method.