Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Evaluating Recommendations Before Updating Website
3.
Getting Recommendations for Prediction
4.
Price Reranking in Recommendation AI
5.
Using Recommendations Filter for Prediction
6.
Rejoining User Events in Recommendation AI
7.
Removing User Events in Recommendation AI
8.
Managing Catalog Information
8.1.
Uploading a product catalog
8.2.
Updating product information
8.3.
Deleting Product Information
8.4.
Retrieving a Product Item
9.
Error Reporting in Recommendation AI
9.1.
Aggregating Integration Errors
9.2.
Specific Integration Operations Status
9.3.
Types of Error in Recommendation AI
9.4.
Unjoined Events in Recommendation AI
10.
Frequently Asked Questions
10.1.
What is supervised machine learning?
10.2.
What is unsupervised machine learning?
10.3.
What is reinforcement machine learning?
11.
Conclusion
Last Updated: Mar 27, 2024
Medium

Advanced concepts of Recommendations AI

Author Ayush Mishra
1 upvote

Introduction

A recommendation system is a machine learning or artificial intelligence (AI) algorithm that suggests or recommends additional products to customers using big data.

To help users find products and services they might not have found on their own, recommender systems are very helpful. 

In this blog, we will discuss Advanced concepts of Recommendation AI in deep detail. The series of Recommendations AI consists of three parts. This is the final part; for the other two parts, you may refer to Introduction to Recommendations AI and Basics of Recommendations AI.

Recommendation AI

Evaluating Recommendations Before Updating Website

A recommendation is evaluated before updating the website to verify that your model and serving configuration are performing as you had hoped.

Steps to preview recommendations are:

✏️ Visit the Google Cloud console's Retail Evaluate page.

✏️ Select the Recommendations tab if it isn't already selected.

✏️ Choose the serving configuration that you want to see in advance.

✏️ Enter a visitor ID if you want to see a preview of recommendations for that user.

✏️ To get associated recommendations for an item, click Add item and enter a product ID if the Associated items section is visible. Multiple associated items can be added.

Items can be added only when the serving configuration's model type calls for products as input for recommendations. "Recommended for You" models don't need associated items entered.

✏️ To view the outcome of the prediction, click Prediction preview.

Getting Recommendations for Prediction

Send a POST request with the appropriate request body to the predict REST method to get a suggestion:

🦾 You must use a service account with the role "Retail Viewer" or higher.

🦾 The serving configuration where you use the predictions should be substituted for SERVING_CONFIG_ID.

🦾 If you imported Google Analytics 360 user events using BigQuery, set visitorId to the Google Analytics 

client ID. 

🦾 If you're running an A/B experiment, set experimentIds to the ID for this experiment group.

🦾 For the user action that started the request for a recommendation, provide a user event object.

🦾 Offer a filter as an option to limit the possible products returned.

 

curl -X POST \
    -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    --data '{
    
              "filter": "FILTER_STRING",
              "validateOnly": false,
              "userEvent": {
              
                  "eventType": "detail-page-view",
                  "visitorId": "VISITOR_ID",
                  "userInfo": {
                      "userId": "USER_ID",
                      "ipAddress": "IP_ADDRESS",
                      "userAgent": "USER_AGENT"
                      
                  },
                  "experimentIds": "EXPERIMENT_GROUP",
                  "productDetails": [{
                  
                      "product": {
                        "id": "PRODUCT_ID"
                     }
                  }]
              }
            }' \


https://retail.googleapis.com/v2/projects/PROJECT_ID/locations/global/catalogs/default_catalog/placements/SERVING_CONFIG_ID:predict

 

You should observe outcomes resembling these:

{

  "results": [{"id": "sample-id-1"}, {"id": "sample-id-2"}],
  
  "attribution_token": "sample-atr-token"
}

 

Any URL you provide in response to this forecast must have the attribution token value associated with it and be returned with user events for those URLs.

Price Reranking in Recommendation AI

Price reranking arranges recommended products with a similar recommendation probability in ascending order of price, starting with the most expensive products.

It can be configured per prediction request or at the serving configuration level.

The price reranking setting you select when building a serving configuration in the Google Cloud console automatically applies to all recommendations served by that configuration.

Using Recommendations Filter for Prediction

By using the filter field, you can filter the recommendations returned by Recommendations AI. 

The filter field accepts two types of filter specifications:

Tag Expressions: You can specify that only products that match every tag you filter on are returned as recommendations if you added tag values to your products before uploading them.

The boolean operators OR and NOT may be used in tag expressions, but they must be separated from the tag values by one or more spaces.

 

filterOutOfStockItems: Filtering out items with a stockState of OUT_OF_STOCK is done using the filterOutOfStockItems flag.
 

Examples of filter strings:

1️⃣ "filter": "tag=\"spring-sale\"" 

2️⃣ "filter": "filterOutOfStockItems"

3️⃣ "filter": "tag=\"spring-sale\" tag=\"exclusive\" filterOutOfStockItems"

4️⃣ "filter": "tag=(\"spring-sale\" OR \"exclusive\") tag=(-\"items-to-exclude\") filterOutOfStockItems"

Rejoining User Events in Recommendation AI

To rejoin user events, set userEventRejoinScope to the appropriate value for the events you're rejoining:

1. USER_EVENT_REJOIN_SCOPE_UNSPECIFIED: Default. For both joined and unjoined events, trigger rejoin.

2. JOINED_EVENTS: Only re-trigger events that have been joined.

3. UNJOINED_EVENTS: Only unjoined events will trigger a rejoin.

 

The example that follows only causes a rejoin for unjoined events:

curl -X POST \

    -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    
    --data "{
     'userEventRejoinScope': 'UNJOINED_EVENTS'
     }" \
     
    "https://retail.googleapis.com/v2/projects/PROJECT_ID/locations/global/catalogs/default_catalog/userEvents:rejoin"

 

The output of the above response is:

{
  "name": "projects/PROJECT_ID/locations/global/catalogs/default_catalog/operations/OPERATION_ID"
}

 

You can look up the rejoin's status. OPERATION_ID should be replaced with the operation ID that the rejoin method returns:

curl -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \

"https://retail.googleapis.com/v2/projects/PROJECT_ID/locations/global/catalogs/default_catalog/operations/OPERATION_ID"

 

The operation status changes from running to doing when it is finished:

{
  "name": "projects/PROJECT_ID/locations/global/catalogs/default_catalog/operations/OPERATION_ID",
  "done": true,
  "response": {
  
    "@type": "type.googleapis.com/google.cloud.retail.v2.RejoinUserEventsResponse",
    "rejoinedUserEventsCount": "1"
    
  }
}

 

Example of an operation status:

{
  "name": "projects/PROJECT_ID/locations/global/catalogs/default_catalog/operations/OPERATION_ID",
  "done": true,
  "response": {
  
    "@type": "type.googleapis.com/google.cloud.retail.v2.PurgeUserEventsResponse",
    "purgedEventsCount": "1"
    
  }
}

 

The delete will be forced to happen if the force field is true.

curl -X POST \
  -H "Authorization: Bearer "$(gcloud auth application-default print-access-token)"" \
  -H "Content-Type: application/json; charset=utf-8" \
  
  --data '{
  
    "filter":"eventTime > \"2019-12-23T18:25:43.511Z\" eventTime < \"2019-12-23T18:30:43.511Z\"",
    "force":"true"
    
  }' \
  
"https://retail.googleapis.com/v2/projects/PROJECT_ID/locations/global/catalogs/default_catalog/userEvents:purge"

Removing User Events in Recommendation AI

Utilizing the userEvents.purge method, you can get rid of any user events that were improperly recorded.

Using a filter string, you can specify the events you want to ignore. Applying a filter to the eventTimeeventTypevisitorID, and userID fields support the selective deletion of user events.  

The time range in this filtering example must adhere to the Zulu Time date format. Force field settings are set to false.

curl -X POST \

  -H "Authorization: Bearer "$(gcloud auth application-default print-access-token)"" \
  -H "Content-Type: application/json; charset=utf-8" \
  
  --data '{
    "filter":"eventTime > \"2019-12-23T18:25:43.511Z\" eventTime < \"2019-12-23T18:30:43.511Z\"",
    "force":"false"
  }' \
  
  "https://retail.googleapis.com/v2/projects/PROJECT_ID/locations/global/catalogs/default_catalog/userEvents:purge"

 

Purge-user-events-54321 is the operation_ID, and you should receive a response object that resembles this:

{
  "name": "projects/PROJECT_ID/locations/global/catalogs/default_catalog/operations/purge-user-events-54321"
}

 

This example asks for the status of the operation:

curl -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \

"https://retail.googleapis.com/v2/projects/PROJECT_ID/locations/global/catalogs/default_catalog/operations/OPERATION_ID"

Managing Catalog Information

After importing a catalog into the Retail API, this part will show how to manage your product information. Your catalog contains products that you can read, write, and delete.

Uploading a product catalog

Steps to upload a product catalog:

✍️ Create a single product item by utilizing the products.create REST method.

curl -X POST \

     -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
     -H "Content-Type: application/json; charset=utf-8" \
     
     --data '{
      "title": "PRODUCT_TITLE",
      "categories": "Shoes & Accessories > Shoes"
    }' \
    
    "https://retail.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/catalogs/default_catalog/branches/0/products?productId=PRODUCT_ID"

 

✍️ The product object is returned if the request is successful, as shown in the example below.

{
  "name": "projects/PROJECT_NUMBER/locations/global/catalogs/default_catalog/branches/0/products/PRODUCT_ID",
  "id": "PRODUCT_ID",
  "primaryProductId": "PRODUCT_ID",
  "type": "PRIMARY",
  "categories": [
    "Shoes & Accessories \u003e Shoes"
  ],
  
  "title": "product title",
  "availability": "IN_STOCK"
}

Updating product information

Steps to updating product information:

✔️ Use the products.patch method to update product information.

The example that follows modifies a product's title is:

curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    
    --data "{
       "title": "new-title-value"
    }" \
    
    "https://retail.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/catalogs/default_catalog/branches/0/products/PRODUCT_ID?updateMask=title"

 

The product object is returned if the request is successful, as shown in the example below.

{
  "name": "projects/PROJECT_NUMBER/locations/global/catalogs/default_catalog/branches/0/products/PRODUCT_ID",
  "id": "PRODUCT_ID",
  "primaryProductId": "PRODUCT_ID",
  "type": "PRIMARY",
  "categories": [
    "Shoes & Accessories > Shoes"
  ],
  
  "title": "new product title",
  "availability": "IN_STOCK"
}

Deleting Product Information

Steps to updating product information:

✍️ Use the delete method to remove a product, replacing PRODUCT_ID with the ID of the item you wish to remove.

curl -X DELETE \

-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \

 "https://retail.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/catalogs/default_catalog/branches/0/products/PRODUCT_ID"

Retrieving a Product Item

GET requests can be made to the products endpoint and replacing PRODUCT_ID with the ID of the product you want to retrieve, you can retrieve a specific product item:

curl -X GET \

-H "Authorization: Bearer $(gcloud auth application-default print-access-token)"  \
"https://retail.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/catalogs/default_catalog/branches/0/products/PRODUCT_ID"

 

The output of the above request is:

{
    "name": "projects/PROJECT_NUMBER/locations/global/catalogs/default_catalog/branches/0/products/PRODUCT_ID"
    "id": "PRODUCT_ID",
    "primaryProductId": "PRODUCT_ID",
    "type": "PRIMARY",
    "categories": [
      "Shoes & Accessories \u003e Shoes"
    ],
    "title": "product title",
    "availability": "IN_STOCK"
}

Error Reporting in Recommendation AI

Error Reporting

 

In this part of Recommendation AI, we will see how to obtain details about errors during operations using the Retail API, including user events and catalog imports.

Aggregating Integration Errors

Use the Monitoring tab on the Monitoring & Analytics page to view the aggregated errors produced by your data upload processes, prediction requests, and search requests.

All errors for the Retail API are shown on the Monitoring & Analytics page. The product catalog, user events, Recommendations AI predictions, Retail Search results, and models have errors that can be viewed.

Click the error itself to view the logs for a specific error in Cloud Logging.

By expanding that log, you can view specific error logs individually. The request's payloads, both for the request and the response, as well as any errors, are all detailed in the error logs.

Specific Integration Operations Status

In the Activity status window, you can check the progress of a particular integration operation:

🚀 Go to the Google Cloud console's Retail Data page.

🚀 Choose Activity status. 

The long-running operations on your product catalog, user events, and controls are displayed in the Activity Status window.

🚀 To view the log files for any operation that has encountered a problem, click View logs in the Detail column in Cloud Logging.

Types of Error in Recommendation AI

Some errors in Recommendation AI are:-

1. FIELD_VALUE_TOO_SMALL: If a field's value is less than the required minimum, such as a negative price, it is considered to be too small.

2. FIELD_VALUE_EXCEEDED: The value entered in the field exceeds the permissible upper limit.

3. FIELD_SIZE_EXCEEDED: There are more entries in the field than allowed.

4. MISSING_FIELD: A necessary field value is missing, such as the title of a catalog item.

5. INVALID_TIMESTAMP: The timestamp is invalid because it is formatted improperly or is too far in the future.

6. INVALID_LANGUAGE_CODE: The timestamp is invalid because it is formatted improperly or is too far in the future.

7. INVALID_RESOURCE_ID: The resource ID is incorrect; an illustration would be a resource name that contains an invalid catalog id.

8. INVALID_FORMAT: The field is improperly formatted, such as with a truncated string

9. INVALID_API_KEY: The API key does not match the project in your request.

10. BATCH_ERROR: The request contains multiple errors, such as a 10-item inline import that fails validation for a variety of reasons.

Unjoined Events in Recommendation AI

Unjoined events occur when a user event or API request makes reference to a product that hasn't yet been added to Retail. Unjoined user events and requests are still logged and processed, but neither can be used to improve the model's predictive power in the future.

Because of the above reason, you should make sure that the percentage of unlogged events for both user events and prediction requests is very low.

Frequently Asked Questions

What is supervised machine learning?

Supervised learning requires outside supervision for the machine to learn from data. The supervised learning models are trained using the labeled dataset.

What is unsupervised machine learning?

Unsupervised learning is a type of machine learning where the computer can make its own decisions based solely on the data it is given. Unsupervised models can be trained using the unlabeled dataset.

What is reinforcement machine learning?

In reinforcement learning, an agent produces actions to interact with its environment and learns from feedback. Rewards are given to the agent as feedback.

Conclusion

Congratulations on finishing the blog! We have studied Advanced Concepts of  Recommendation AI. We further looked at the prediction, managing user events, managing catalog, and error reporting in Recommendation AI
We sincerely hope that this blog has improved your understanding of Recommendation AI, and if you want to learn more, then you can check articles on:-

 

Please refer to our guided pathways on Code studio to learn more about DSACompetitive ProgrammingJavaScriptSystem Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.

Please do upvote our blogs if you find them helpful and informative!

Happy learning!

Live masterclass