/// <summary>Snippet for Predict</summary> public async Task PredictRequestObjectAsync() { // Snippet: PredictAsync(PredictRequest, CallSettings) // 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; // End snippet }
/// <summary>Snippet for Predict</summary> public void PredictResourceNames() { // Snippet: Predict(PlacementName, UserEvent, string, int?, CallSettings) // 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; // End snippet }
/// <summary> /// 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](/recommendations-ai/docs/setting-up#register-key). /// </summary> /// <param name="name"> /// 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 /// </param> /// <param name="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. /// </param> /// <param name="pageToken"> /// The token returned from the previous request. A value of <c>null</c> or an empty string retrieves the first /// page. /// </param> /// <param name="pageSize"> /// The size of page to request. The response will not be larger than this, but may be smaller. A value of /// <c>null</c> or <c>0</c> uses a server-defined page size. /// </param> /// <param name="callSettings">If not null, applies overrides to this RPC call.</param> /// <returns> /// A pageable asynchronous sequence of <see cref="PredictResponse.Types.PredictionResult"/> resources. /// </returns> public virtual gax::PagedAsyncEnumerable <PredictResponse, PredictResponse.Types.PredictionResult> PredictAsync(PlacementName name, UserEvent userEvent, string pageToken = null, int?pageSize = null, gaxgrpc::CallSettings callSettings = null) => PredictAsync(new PredictRequest { PlacementName = gax::GaxPreconditions.CheckNotNull(name, nameof(name)), UserEvent = gax::GaxPreconditions.CheckNotNull(userEvent, nameof(userEvent)), PageToken = pageToken ?? "", PageSize = pageSize ?? 0, }, callSettings);