Package: azure-ai-personalizer
Azure Personalizer client library for PythonAzure Personalizeris 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 startedPrerequisitesPython 3.7 or later is required to use this package.You must have anAzure subscriptionand aPersonalizer resourceto use this package.Install the packageInstall the Azure Personalizer client library for Python withpip:pipinstallazure-ai-personalizerNote: This version of the client library defaults to the2022-09-01-previewversion of the service.This table shows the relationship between SDK versions and supported API versions of the service:SDK versionSupported API version of service1.0.0b12022-09-01-previewKey conceptsPersonalizerClientThesynchronous PersonalizerClientandasynchronous PersonalizerClientprovide 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 thelearning mode,exploration percentage.Run counterfactual evaluations on prior historical event data.Rank a set of actions, then activate and reward the event.Usemulti-slot personalizationwhen there are more than one slots.Manage the properties of the Personalizer service.Run counterfactual evaluations on prior historical event data.ExamplesThe following examples outline the main scenarios when using personalizer in single-slot and multi-slot configurations.Send rank and reward when using a single slotSend rank and reward when using multiple slotsSend rank and rewardfromazure.ai.personalizerimportPersonalizerClientfromazure.core.credentialsimportAzureKeyCredentialendpoint="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 rewardfromazure.ai.personalizerimportPersonalizerClientfromazure.core.credentialsimportAzureKeyCredentialendpoint="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}]})TroubleshootingGeneralPersonalizer client library will raise exceptions defined inAzure Core.LoggingThis library uses the standardlogginglibrary for logging.Basic information about HTTP sessions (URLs, headers, etc.) is logged atINFOlevel.DetailedDEBUGlevel logging, including request/response bodies andunredactedheaders, can be enabled on the client or per-operation with thelogging_enablekeyword argument.See full SDK logging documentation with exampleshere.Optional ConfigurationOptional keyword arguments can be passed in at the client and per-operation level.
The azure-corereference documentationdescribes available configurations for retries, logging, transport protocols, and more.Next stepsContributingThis 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, visitcla.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 theMicrosoft Open Source Code of Conduct. For more information see theCode of Conduct FAQor contactopencode@microsoft.comwith any additional questions or comments.