跳转到主要内容

Microsoft Azure Personalizer Client Library for Python

项目描述

Azure Personalizer客户端库

Azure Personalizer 是一种基于云的服务,可以帮助您的应用程序选择最佳的内容项目向用户展示。您可以使用Personalizer服务来确定向购物者推荐什么产品或确定广告的最佳位置。在内容向用户展示后,您的应用程序会监控用户的反应并将奖励分数报告给Personalizer服务。这确保了机器学习模型的持续改进以及Personalizer根据其收到的上下文信息选择最佳内容项目的能力。

入门

先决条件

安装包

使用 pip 安装 Azure Personalizer Python 客户端库

pip install azure-ai-personalizer

注意:此版本客户端库默认使用服务的 2022-09-01-preview 版本。

此表显示了 SDK 版本与服务支持的 API 版本之间的关系

SDK 版本 服务支持的 API 版本
1.0.0b1 2022-09-01-preview

关键概念

PersonalizerClient

同步的 PersonalizerClient 和异步的 PersonalizerClient 提供同步和异步操作来

  • 管理 Personalizer 服务的机器学习模型和学习设置。
  • 管理 Personalizer 服务的属性,例如 学习模式探索百分比
  • 对先前历史事件数据进行反事实评估。
  • 对一组操作进行排序,然后激活并奖励事件。
  • 当有多个槽时,使用 多槽个性化
  • 管理 Personalizer 服务的属性。
  • 对先前历史事件数据进行反事实评估。

示例

以下示例概述了在单槽和双槽配置中使用 Personalizer 的主要场景。

发送排名和奖励

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})

发送多槽排名和奖励

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}]})

故障排除

一般

Personalizer 客户端库将抛出定义在 Azure Core 中的异常。

日志记录

此库使用标准的 logging 库进行日志记录。

HTTP 会话(URL、标题等)的基本信息在 INFO 级别进行日志记录。

详细的 DEBUG 级别日志记录,包括请求/响应体和 未脱敏 的标题,可以通过 logging_enable 关键字参数在客户端或每个操作中启用。

有关示例,请参阅完整的 SDK 日志记录文档此处

可选配置

可以在客户端和每个操作级别传递可选关键字参数。azure-core 参考文档 描述了可用的重试、日志记录、传输协议等配置。

下一步

贡献

此项目欢迎贡献和建议。大多数贡献都需要您同意贡献者许可协议(CLA),声明您有权,并且实际上确实授予我们使用您贡献的权利。有关详细信息,请访问 cla.microsoft.com

提交拉取请求时,CLA-bot 将自动确定您是否需要提供 CLA,并相应地装饰 PR(例如,标签、注释)。只需遵循机器人提供的说明即可。您只需在整个使用我们的 CLA 的所有存储库中做一次。

本项目采用了 Microsoft 开源代码行为准则。有关更多信息,请参阅 代码行为准则常见问题解答 或联系 opencode@microsoft.com 以获取任何其他问题或评论。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

azure-ai-personalizer-1.0.0b1.zip (107.9 kB 查看哈希值)

上传时间

构建分布

azure_ai_personalizer-1.0.0b1-py3-none-any.whl (71.9 kB 查看哈希值)

上传时间 Python 3

由以下组织支持