Skip to main content

Microsoft Azure Personalizer Client Library for Python

Project description

Azure Personalizer client library for Python

Azure Personalizer is a cloud-based service that helps your applications choose the best content item to show your users. You can use the Personalizer service to determine what product to suggest to shoppers or to figure out the optimal position for an advertisement. After the content is shown to the user, your application monitors the user's reaction and reports a reward score back to the Personalizer service. This ensures continuous improvement of the machine learning model, and Personalizer's ability to select the best content item based on the contextual information it receives.

Getting started

Prerequisites

Install the package

Install the Azure Personalizer client library for Python with pip:

pip install azure-ai-personalizer

Note: This version of the client library defaults to the 2022-09-01-preview version of the service.

This table shows the relationship between SDK versions and supported API versions of the service:

SDK version Supported API version of service
1.0.0b1 2022-09-01-preview

Key concepts

PersonalizerClient

The synchronous PersonalizerClient and asynchronous PersonalizerClient provide synchronous and asynchronous operations to:

  • Manage the machine learning model and learning settings for the Personalizer service.
  • Manage the properties of the Personalizer service such as the learning mode, exploration percentage.
  • Run counterfactual evaluations on prior historical event data.
  • Rank a set of actions, then activate and reward the event.
  • Use multi-slot personalization when there are more than one slots.
  • Manage the properties of the Personalizer service.
  • Run counterfactual evaluations on prior historical event data.

Examples

The following examples outline the main scenarios when using personalizer in single-slot and multi-slot configurations.

Send rank and reward

from azure.ai.personalizer import PersonalizerClient
from azure.core.credentials import AzureKeyCredential

endpoint = "https://<my-personalizer-instance>.cognitiveservices.azure.com/"
credential = AzureKeyCredential("<api_key>")

client = PersonalizerClient(endpoint, credential)

# The list of actions to be ranked with metadata associated for each action.
actions = [
    {
        "id": "Video1",
        "features": [
            {"videoType": "documentary", "videoLength": 35, "director": "CarlSagan"},
            {"mostWatchedByAge": "50-55"},
        ],
    },
    {
        "id": "Video2",
        "features": [
            {"videoType": "movie", "videoLength": 120, "director": "StevenSpielberg"},
            {"mostWatchedByAge": "40-45"},
        ],
    },
]

# Context of the user to which the action must be presented.
context_features = [
    {"currentContext": {"day": "tuesday", "time": "night", "weather": "rainy"}},
    {
        "userContext": {
            "payingUser": True,
            "favoriteGenre": "documentary",
            "hoursOnSite": 0.12,
            "lastWatchedType": "movie",
        },
    },
]

request = {
    "actions": actions,
    "contextFeatures": context_features,
}

rank_response = client.rank(request)
print("Sending reward event")
client.reward(rank_response.get("eventId"), {"value": 1.0})

Send multi-slot rank and reward

from azure.ai.personalizer import PersonalizerClient
from azure.core.credentials import AzureKeyCredential

endpoint = "https://<my-personalizer-instance>.cognitiveservices.azure.com/"
credential = AzureKeyCredential("<api_key>")

client = PersonalizerClient(endpoint, credential)

# We want to rank the actions for two slots.
slots = [
    {
        "id": "Main Article",
        "baselineAction": "NewsArticle",
        "positionFeatures": [{"Size": "Large", "Position": "Top Middle"}],
    },
    {
        "id": "Side Bar",
        "baselineAction": "SportsArticle",
        "positionFeatures": [{"Size": "Small", "Position": "Bottom Right"}],
    },
]

# The list of actions to be ranked with metadata associated for each action.
actions = [
    {"id": "NewsArticle", "features": [{"type": "News"}]},
    {"id": "SportsArticle", "features": [{"type": "Sports"}]},
    {"id": "EntertainmentArticle", "features": [{"type": "Entertainment"}]},
]

# Context of the user to which the action must be presented.
context_features = [
    {"user": {"profileType": "AnonymousUser", "latLong": "47.6,-122.1"}},
    {"environment": {"dayOfMonth": "28", "monthOfYear": "8", "weather": "Sunny"}},
    {"device": {"mobile": True, "windows": True}},
    {"recentActivity": {"itemsInCart": 3}},
]

request = {
    "slots": slots,
    "actions": actions,
    "contextFeatures": context_features,
}
rank_response = client.rank_multi_slot(request)
print("Sending reward event for Main Article slot")
client.reward_multi_slot(
    rank_response.get("eventId"),
    {"reward": [{"slotId": "Main Article", "value": 1.0}]})

Troubleshooting

General

Personalizer client library will raise exceptions defined in Azure Core.

Logging

This library uses the standard logging library for logging.

Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO level.

Detailed DEBUG level logging, including request/response bodies and unredacted headers, can be enabled on the client or per-operation with the logging_enable keyword argument.

See full SDK logging documentation with examples here.

Optional Configuration

Optional keyword arguments can be passed in at the client and per-operation level. The azure-core reference documentation describes available configurations for retries, logging, transport protocols, and more.

Next steps

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

azure-ai-personalizer-1.0.0b1.zip (107.9 kB view details)

Uploaded Source

Built Distribution

azure_ai_personalizer-1.0.0b1-py3-none-any.whl (71.9 kB view details)

Uploaded Python 3

File details

Details for the file azure-ai-personalizer-1.0.0b1.zip.

File metadata

  • Download URL: azure-ai-personalizer-1.0.0b1.zip
  • Upload date:
  • Size: 107.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.28.1 setuptools/58.1.0 requests-toolbelt/0.10.1 tqdm/4.64.1 CPython/3.9.15

File hashes

Hashes for azure-ai-personalizer-1.0.0b1.zip
Algorithm Hash digest
SHA256 d5ca96fff31b716575bfac1e3349f40c6bb3ec4837d0cabc7d7f8e32e47c226c
MD5 9c062036c54d960053ee461b677299ad
BLAKE2b-256 06c90f80a0ecee7b9a176be4fdd20f8ad393d53fd248415935d5a88197875c26

See more details on using hashes here.

File details

Details for the file azure_ai_personalizer-1.0.0b1-py3-none-any.whl.

File metadata

  • Download URL: azure_ai_personalizer-1.0.0b1-py3-none-any.whl
  • Upload date:
  • Size: 71.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.28.1 setuptools/58.1.0 requests-toolbelt/0.10.1 tqdm/4.64.1 CPython/3.9.15

File hashes

Hashes for azure_ai_personalizer-1.0.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 e2cc20bb1e39f3fab69b75da8938ee24b4fefdc4a02efa065fba96253a70048a
MD5 180620e77211a6e98f18f7626816c3dd
BLAKE2b-256 9dd6c1332aa8067085768541b642b5f8356cdaf3d24ec2b39cc4d61dfc9bd455

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page