File: multi_slot_rank_actions_and_reward_events.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (86 lines) | stat: -rw-r--r-- 3,320 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: multi_slot_rank_actions_and_reward_events.py
DESCRIPTION:
    This sample demos sending a rank and reward call to personalizer for multi-slot configuration.
USAGE: python multi_slot_rank_actions_and_reward_events.py
Environment variables PERSONALIZER_ENDPOINT and PERSONALIZER_API_KEY must be set as per your personalizer instance.
"""

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


def main():
    try:
        endpoint = os.environ["PERSONALIZER_ENDPOINT_MULTI_SLOT"]
    except KeyError:
        print("PERSONALIZER_ENDPOINT_MULTI_SLOT must be set.")
        sys.exit(1)

    try:
        api_key = os.environ["PERSONALIZER_API_KEY_MULTI_SLOT"]
    except KeyError:
        print("PERSONALIZER_API_KEY_MULTI_SLOT must be set.")
        sys.exit(1)

    client = PersonalizerClient(endpoint, AzureKeyCredential(api_key))

    # 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,
    }
    print("Sending multi-slot rank request")
    rank_response = client.rank_multi_slot(request)
    print("Rank returned response with event id {} and recommended the following:".format(rank_response.get("eventId")))
    for slot in rank_response.get("slots"):
        print("Action: {} for slot: {}".format(slot.get("rewardActionId"), slot.get("id")))

    # The event response will be determined by how the user interacted with the action that was presented to them.
    # Let us say that they like the action presented to them for the Main Article slot. So we associate a reward of 1.
    print("Sending reward event for Main Article slot")
    client.reward_multi_slot(rank_response.get("eventId"), {"reward": [{"slotId": "Main Article", "value": 1.0}]})
    print("Completed sending reward response")


if __name__ == "__main__":
    main()