Rotates Amazon Personalize filters on a schedule based on dynamic templates

Overview

Amazon Personalize Filter Rotation

This project contains the source code and supporting files for deploying a serverless application that provides automatic filter rotation capabilities for Amazon Personalize, an AI service from AWS that allows you to create custom ML recommenders based on your data. Highlights include:

  • Creates filters based on a dynamic filter naming template you provide
  • Builds filter expressions based on a dynamic filter expression template you provide
  • Deletes filters based on a dynamic matching expression you provide (optional)
  • Publishes events to Amazon EventBridge when filters are created or deleted (optional)

Why is this important?

Amazon Personalize filters are a great way to have your business rules applied to recommendations before they are returned to your application. They can be used to include or exclude items from being recommended for a user based on a SQL-like syntax that considers the user's interaction history, item metadata, and user metadata. For example, only recommend movies that the user has watched or favorited in the past to populate a "Watch again" widget.

INCLUDE ItemID WHERE Interactions.event_type IN ('watched','favorited')

Or exclude products from being recommended that are currently out of stock.

EXCLUDE ItemID WHERE Items.out_of_stock IN ('yes')

You can even use dynamic filters where the filter expression values are specified at runtime. For example, only recommend movies for a specific genre.

INCLUDE ItemID WHERE Items.genre IN ($GENRES)

To use the filter above, you would pass the appropriate value(s) for the $GENRE variable when retrieving recommendations.

You can learn more about filters on the AWS Personalize blog here and here.

Filters are great! However, they do have some limitations. One of those limitations is being able to specify a dynamic value for a range query (i.e., <, <=, >, >=). For example, the following filter to limit recommendations to new items that were created since a rolling point in the past is not supported.

THIS WON'T WORK!

INCLUDE ItemID WHERE Items.creation_timestamp > $NEW_ITEM_THRESHOLD

The solution to this limitation is to use a filter expression with a hard-coded value for range queries.

THIS WORKS!

INCLUDE ItemID WHERE Items.creation_timestamp > 1633240824

However, this is not very flexible or maintainble since time marches on but your static filter expression does not. The workaround is to update your filter expression periodically to maintain a rolling window of time. Unfortunately filters cannot be updated so a new filter has to be created, your application has to transition to using the new filter, and then the previous filter can be safely deleted.

The purpose of this serverless application is to make this process easier to maintain by automating the creation and deletion of filters and allowing you to provide a dynamic expression that is resolved to the appropriate hard-coded value when the new filter is created.

Here's how it works

An AWS Lambda function is deployed by this application that is called on a recurring basis. You control the schedule which can be a cron expression or a rate expression. The function will only create a new filter if a filter does not already exist that matches the current filter name template and it will only delete existing filters that match the delete template. Therefore, it is fine to have the function run more often than necessary (i.e. if you don't have a predictable and consistent time when filters should be rotated).

The key to the filter rotation function are the templates used to verify that the current template exists and if existing template(s) are eligible to delete. Let's look at some examples. You provide these template values as CloudFormation parameters when you deploy this application.

Current filter name template

Let's say you want to use a filter that only recommends recently created items. The CREATION_TIMESTAMP column in the items dataset is a convenient field to use for this. This column name is reserved and is used to support the cold item exploration feature of the aws-user-personalization recipe. Values must be expressed in the Unix timestamp format as long's (i.e. number of seconds since the Epoch). The following expression limits items that were created in the last month (1633240824 is the Unix timestamp from 1 month ago as of this writing).

INCLUDE ItemID WHERE Items.creation_timestamp > 1633240824

Alternatively, you can use a custom metadata column for the filter that uses a more coarse and/or human readable format but is still comparable for range queries, like YYYYMMDD.

INCLUDE ItemID WHERE Items.published_date > 20211001

As noted earlier, filters cannot be updated. Therefore you can't just change the filter expression of the filter. Instead, you have to create a new filter with a new expression, switch your application to use the new filter, and then delete the old filter. This requires using a predictable naming standard for filters so applications can automatically switch to using the new filter without a coding change. Continuing with the creation timestamp theme, the filter name could be something like.

filter-include-recent-items-20211101

Assuming we want to rotate this filter each day, the next day's filter name would be filter-include-recent-items-20211004, then filter-include-recent-items-20211005, and so on as time passes. Since there are limits on how many active filters you can have at any time, you cannot precreate filters. Instead, this application will dynamically create new filters. What is needed is a template that defines the filter name that can be resolved when the rotation script runs.

filter-include-recent-items-{{datetime_format(now,'%Y%m%d')}}

The above filter name template will resolve and replace the expression within the {{ and }} characters (handlebars or mustaches). In this case, we are taking the current time expressed as now and formatting it using the %Y%m%d date format expression. The result (as of today) is 20211102. If the rotation function finds an existing filter with this name, a new filter does not need to be created. Otherwise, a new filter is created using filter-include-recent-items-20211102 as the name.

The PersonalizeCurrentFilterNameTemplate CloudFormation template parameter is how you specify your own custom filter name template.

The functions and operators available to use in templates is described below.

Current filter expression template

When rotating and creating the new filter, we also may have to dynamically resolve the actual filter expression. The PersonalizeCurrentFilterExpressionTemplate CloudFormation template parameter can be used for this. Some examples.

INCLUDE ItemID WHERE Items.CREATION_TIMESTAMP > {{int(unixtime(now - timedelta_days(30)))}}
INCLUDE ItemID WHERE Items.published_date > {{datetime_format(now - timedelta_days(30),'%Y%m%d')}}

The above templates produce a hard-coded filter expression based on current time when they're resolved. The first produces a Unix timestamp (expressed in seconds as required by Personalize for CREATION_TIMESTAMP) that is 30 days ago. The second template produces an integer representing the date in YYYYMMDD format from 30 days ago.

Delete filter match template

Finally, we need to clean up old filters after we have transitioned to a newer version of the filter. A filter name matching template can be used for this and can be written in such a way to delay the delete for some time after the new filter is created. This gives your application time to transition from the old filter to the new filter. The PersonalizeDeleteFilterMatchTemplate CloudFormation template parameter is where you specify the delete filter match template.

The following delete filter match template will match on filters with a filter name that starts with filter-include-recent-items- and has a suffix that is more than one day older than today. In other words, we have 1 day to transition to the new filter before the old filter is deleted.

starts_with(filter.name,'filter-include-recent-items-') and int(end(filter.name,8)) < int(datetime_format(now - timedelta_days(1),'%Y%m%d'))

Any filters that trigger this template to resolve to true will be deleted. All others will be left alone. Note that all fields available in the FilterSummary of the ListFilters API response are available to this template. For example, the template above matches on filter.name. Other filter summary fields such as filter.status, filter.creationDateTime, and filter.lastUpdatedDateTime can also be inspected in the template.

Filter events

If you'd like to synchronize your application's configuration or be notified when a filter is created or deleted, you can optionally configure the rotator function to publish events to Amazon EventBridge. When events are enabled, there are two event detail types published by the rotator function: PersonalizeFilterCreated and PersonalizeFilterDeleted. Both have an event Source of personalize.filter.rotator and include details on the filter created or deleted. This allows you to setup EventBridge rules to process events as you please. For example, when a new filter is created, you can receive the PersonalizeFilterCreated event in a Lambda function to update your application's configuration to switch to using the new filter.

Filter template syntax

The Simple Eval library is used as the foundation of for the template syntax. It provides a safer and more sandboxed alternative than using Python's eval function. Check the Simple Eval library documentation for details on the functions available and examples.

The following additional functions were added as part of this application to make writing templates easier for rotating filters.

  • unixtime(value): Returns the Unix timestamp value given a string, datetime, date, or time. If a string is provided, it will be parsed into a datetime first.
  • datetime_format(date, pattern): Formats a datetime, date, or time using the specified pattern.
  • timedelta_days(int): Returns a timedelta for a number of days. Can be used for date math.
  • timedelta_hours(int): Returns a timedelta for a number of hours. Can be used for date math.
  • timedelta_minutes(int): Returns a timedelta for a number of minutes. Can be used for date math.
  • timedelta_seconds(int): Returns a timedelta for a number of seconds. Can be used for date math.
  • starts_with(str, prefix): Returns True if the string value starts with prefix.
  • ends_with(str, suffix): Returns True if the string value ends with suffix.
  • start(str, num): Returns the first num characters of the string value
  • end(str, num): Returns the last num characters of the string value
  • now: Current datetime

Installing the application

IMPORTANT NOTE: Deploying this application in your AWS account will create and consume AWS resources, which will cost money. The Lambda function is called according to the schedule you provide but typically should not need to be called more often than hourly. Personalize does not charge for filters but your account does have a limit on the number of filters that are active at any time. There are also limits on how many filters can be in a pending or in-progress status at any point in time. Therefore, if after installing this application you choose not to use it as part of your solution, be sure to follow the Uninstall instructions in the next section to avoid ongoing charges and to clean up all data.

This application uses the AWS Serverless Application Model (SAM) to build and deploy resources into your AWS account.

To use the SAM CLI, you need the following tools installed locally.

To build and deploy the application for the first time, run the following in your shell:

sam build --use-container --cached
sam deploy --guided --capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND

If you receive an error from the first command about not being able to download the Docker image from public.ecr.aws, you may need to login. Run the following command and then retry the above two commands.

aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws

The first command will build the source of the application. The second command will package and deploy the application to your AWS account, with a series of prompts:

Prompt/Parameter Description Default
Stack Name The name of the stack to deploy to CloudFormation. This should be unique to your account and region. personalize-filter-rotator
AWS Region The AWS region you want to deploy this application to. Your current region
Parameter PersonalizeDatasetGroupArn Amazon Personalize dataset group ARN to rotate filters within.
Parameter PersonalizeCurrentFilterNameTemplate Template to use when checking and creating the current filter.
Parameter PersonalizeCurrentFilterExpressionTemplate Template to use when building the filter expression when creating the current filter.
Parameter PersonalizeDeleteFilterMatchTemplate (optional) Template to use to match existing filters that should be deleted.
Parameter RotationSchedule Cron or rate expression to control how often the rotation function is called. rate(1 day)
Parameter Timezone Set the timezone of the rotator function's Lambda environment to match your own.
Parameter PublishFilterEvents Whether to publish events to the default EventBridge bus when filters are created and deleted. Yes
Confirm changes before deploy If set to yes, any CloudFormation change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes.
Allow SAM CLI IAM role creation Since this application creates IAM roles to allow the Lambda functions to access AWS services, this setting must be Yes.
Save arguments to samconfig.toml If set to yes, your choices will be saved to a configuration file inside the application, so that in the future you can just re-run sam deploy without parameters to deploy changes to your application.

TIP: The SAM command-line tool provides the option to save your parameter values in a local file (samconfig.toml) so they're available as defaults the next time you deploy the app. However, SAM wraps your parameter values in double-quotes. Therefore, if your template parameter values contain embedded string values (like the date format expressions shown in the examples above), be sure to use single-quotes for those embedded values. Otherwise, your parameter values will not be properly preserved.

Uninstalling the application

To remove the resources created by this application in your AWS account, use the AWS CLI. Assuming you used the default application name for the stack name (personalize-filter-rotator), you can run the following:

aws cloudformation delete-stack --stack-name personalize-filter-rotator

Alternatively, you can delete the stack in CloudFormation in the AWS console.

Reporting issues

If you encounter a bug, please create a new issue with as much detail as possible and steps for reproducing the bug. Similarly, if you have an idea for an improvement, please add an issue as well. Pull requests are also welcome! See the Contributing Guidelines for more details.

License summary

This sample code is made available under a modified MIT license. See the LICENSE file.

Owner
James Jory
Applied AI Solutions Architect
James Jory
Easy to use phishing tool with 63 website templates. Author is not responsible for any misuse.

PyPhisher [+] Created By KasRoudra [+] Description : Ultimate phishing tool in python. Includes popular websites like facebook, twitter, instagram, gi

KasRoudra 1.1k Jan 01, 2023
Python wrapper for the Intercom API.

python-intercom Not officially supported Please note that this is NOT an official Intercom SDK. The third party that maintained it reached out to us t

Intercom 215 Dec 22, 2022
To send an Instagram message using Python

To send an Instagram message using Python, you must have an Instagram account and install the Instabot library in your Python virtual environment.

Coding Taggers 1 Dec 18, 2021
Instagram story report with python

instagram-story-report Mass reports a victim stories. Made for fun, but can be used for chaos Single session and multi session support Login, choose a

Joshua Solo 8 May 08, 2022
A Python API for Connected 2

connected API for Connected 2 api for the { connected 2 } programmer : api report api follow api check username api forget password api Search api cha

2 Jun 05, 2022
An Anime Themed Fast And Safe Group Managing Bot.

Ξ L I N Λ 👸 A Powerful, Smart And Simple Group Manager bot Avaiilable a latest version as Ξ L I N Λ 👸 on Telegram Self-hosting (For Devs) vps # Inst

7 Nov 12, 2022
Most Simple & Powefull web3 Trade Bot (WINDOWS LINUX) Suport BSC ETH

Most Simple & Powefull Trade Bot (WINDOWS LINUX) What Are Some Pros And Cons Of Owning A Sniper Bot? While having a sniper bot is typically an advanta

GUI BOT 4 Jan 25, 2022
Discord bot do sprawdzania ceny pizzy.

Discord bot do sprawdzania ceny pizzy w pizzeri Bombola. Umieszczony jest na platformie Heroku, dzięki czemu działa 24/7. Commands List Info: Jako com

1 Sep 18, 2021
Scratch2py or S2py is a easy to use, versatile tool to communicate with the Scratch API Based of Scratch2py

Scratch2py Scratch2py or S2py is a easy to use, versatile tool to communicate with the Scratch API Based of Scratch2py Installation Run this command i

2 Jan 13, 2022
Source code of BobuxAdmin bot from Bobux Bot Development server.

BobuxAdmin Source code of BobuxAdmin bot from Bobux Bot Development server. The bot is written with usage of disnake and SQLite database. Functionalit

Bobux Bot Developers 3 Dec 29, 2022
A discord bot with information and template tracking for pxls.space.

pyCharity A discord bot with information and template tracking for pxls.space. Inspired by Mikarific's Charity bot. Try out the beta version on your s

1 Dec 03, 2021
Instagram auto reporting tool 100% working

INSTA REPORTER Instagram auto reporting tool 100% working Description this tool is made by Guccifer Shubham (shubhushubhu99) and by using this tool yo

Guccifer Shubham 26 Dec 28, 2022
Best DDoS Attack Script Python3, Cyber Attack With 40 Methods

MXDDoS - DDoS Attack Script With 40 Methods (Code Lang - Python 3) Please Don't Attack '.gov' and '.ir' Websites :) Features And Methods 💣 Layer7 GET

7 Mar 07, 2022
Clash of Clans developer unofficial api Wrapper to generate ip based token

Clash of Clans developer unofficial api Wrapper to generate ip based token

Aryan Vikash 6 Apr 01, 2022
Command-line program to download videos from YouTube.com and other video sites

youtube-dl - download videos from youtube.com or other video platforms INSTALLATION DESCRIPTION OPTIONS CONFIGURATION OUTPUT TEMPLATE FORMAT SELECTION

youtube-dl 116.4k Jan 07, 2023
A Serverless Application Model stack that persists the $XRP price to the XRPL every minute as a TrustLine. There are no servers, it is effectively a "smart contract" in Python for the XRPL.

xrpl-price-persist-oracle-sam This is a XRPL Oracle that publishes external data into the XRPL. This Oracle was inspired by XRPL-Labs/XRPL-Persist-Pri

Joseph Chiocchi 11 Dec 17, 2022
Ini Hanya Shortcut Untuk Menambahkan Kunci Tambahan Pada Termux & Membantu Para Nub Yang Decode Script Orang:v

Ini Hanya Shortcut Untuk Menambahkan Kunci Tambahan Pada Termux & Membantu Para Nub Yang Decode Script Orang:v

Lord_Ammar 1 Jan 23, 2022
Telegram bot implementing Lex Arcana using python-telegram-bot library.

Lex Arcana Telegram Bot 🤖 Telegram bot implementing Lex Arcana using python-telegram-bot library. This bot was evaluated for the course "Computer Eng

Nicolò Sonnino 6 Jun 22, 2022
Python-based Snapchat score booster using pyautogui module

Snapchat Snapscore Botter Python-based Snapchat score booster using pyautogui module. Click here to report bugs. Usage Download ZIP here and extract t

477 Dec 31, 2022
Interact and easily use Google Chat room webhooks.

Chat Webhooks Easily interact and send messages with Google Chat's webhooks feature. This API is small, but should be a nice framework for working wit

BD103 2 Dec 13, 2021