示例#1
0
 public virtual async Task <Response <PersonalizerRankResult> > RankAsync(PersonalizerRankOptions rankRequest, CancellationToken cancellationToken = default)
 {
     using var scope = _clientDiagnostics.CreateScope("RankClient.Rank");
     scope.Start();
     try
     {
         return(await RestClient.RankAsync(rankRequest, cancellationToken).ConfigureAwait(false));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
示例#2
0
 public virtual Response <PersonalizerRankResult> Rank(PersonalizerRankOptions rankRequest, CancellationToken cancellationToken = default)
 {
     using var scope = _clientDiagnostics.CreateScope("RankClient.Rank");
     scope.Start();
     try
     {
         return(RestClient.Rank(rankRequest, cancellationToken));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
示例#3
0
        public Response <PersonalizerRankResult> Rank(PersonalizerRankOptions options)
        {
            string eventId = options.EventId;

            if (String.IsNullOrEmpty(eventId))
            {
                eventId = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
            }

            HashSet <string> excludedSet = new HashSet <string>(options.ExcludedActions);

            // Store the original action list
            List <PersonalizerRankableAction> originalActions = new List <PersonalizerRankableAction>();
            List <PersonalizerRankableAction> rankableActions = new List <PersonalizerRankableAction>();
            List <PersonalizerRankableAction> excludedActions = new List <PersonalizerRankableAction>();
            int idx = 0;

            foreach (var action in options.Actions)
            {
                PersonalizerRankableAction actionCopy = new PersonalizerRankableAction(action.Id, action.Features);
                actionCopy.Index = idx;
                originalActions.Add(actionCopy);
                if (excludedSet.Contains(actionCopy.Id))
                {
                    excludedActions.Add(actionCopy);
                }
                else
                {
                    rankableActions.Add(actionCopy);
                }
                ++idx;
            }

            // Convert options to the compatible parameter for ChooseRank
            var         contextJson = RlObjectConverter.ConvertToContextJson(options.ContextFeatures, rankableActions);
            ActionFlags flags       = options.DeferActivation == true ? ActionFlags.Deferred : ActionFlags.Default;

            // Call ChooseRank of local RL.Net
            RankingResponseWrapper rankingResponseWrapper = liveModel.ChooseRank(eventId, contextJson, flags);

            // Convert response to PersonalizerRankResult
            var value = RlObjectConverter.GenerateRankResult(originalActions, rankableActions, excludedActions, rankingResponseWrapper, options.EventId);

            return(Response.FromValue(value, default));
        }
示例#4
0
        public async Task <Response <PersonalizerRankResult> > RankAsync(PersonalizerRankOptions rankRequest, CancellationToken cancellationToken = default)
        {
            if (rankRequest == null)
            {
                throw new ArgumentNullException(nameof(rankRequest));
            }

            using var message = CreateRankRequest(rankRequest);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            switch (message.Response.Status)
            {
            case 201:
            {
                PersonalizerRankResult value = default;
                using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                value = PersonalizerRankResult.DeserializePersonalizerRankResult(document.RootElement);
                return(Response.FromValue(value, message.Response));
            }
示例#5
0
        internal HttpMessage CreateRankRequest(PersonalizerRankOptions rankRequest)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;

            request.Method = RequestMethod.Post;
            var uri = new RawRequestUriBuilder();

            uri.AppendRaw(endpoint, false);
            uri.AppendRaw("/personalizer/v1.1-preview.1", false);
            uri.AppendPath("/rank", false);
            request.Uri = uri;
            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("Content-Type", "application/json");
            var content = new Utf8JsonRequestContent();

            content.JsonWriter.WriteObjectValue(rankRequest);
            request.Content = content;
            return(message);
        }
 /// <summary> Submit a Personalizer rank request. Receives a context and a list of actions. Returns which of the provided actions should be used by your application, in rewardActionId. </summary>
 /// <param name="options"> A Personalizer Rank request. </param>
 /// <param name="cancellationToken"> The cancellation token to use. </param>
 public virtual Response <PersonalizerRankResult> Rank(PersonalizerRankOptions options, CancellationToken cancellationToken = default)
 {
     using var scope = clientDiagnostics.CreateScope("PersonalizerClient.Rank");
     scope.Start();
     try
     {
         if (useLocalInference)
         {
             validateAndUpdateLiveModelConfig();
             return(rlNetProcessor.Value.Rank(options));
         }
         else
         {
             return(RankRestClient.Rank(options, cancellationToken));
         }
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
        /// <summary> Submit a Personalizer rank request. Receives a context and a list of actions. Returns which of the provided actions should be used by your application, in rewardActionId. </summary>
        /// <param name="actions">The set of actions the Personalizer service
        /// can pick from.
        /// The set should not contain more than 50 actions.
        /// The order of the actions does not affect the rank result but the
        /// order
        /// should match the sequence your application would have used to
        /// display them.
        /// The first item in the array will be used as Baseline item in
        /// Offline evaluations.</param>
        /// <param name="contextFeatures">Features of the context used for
        /// Personalizer as a dictionary of dictionaries. This depends on the application, and
        /// typically includes features about the current user, their
        /// device, profile information, aggregated data about time and date, etc.
        /// Features should not include personally identifiable information (PII),
        /// unique UserIDs, or precise timestamps. Need to be JSON serializable.
        /// https://docs.microsoft.com/azure/cognitive-services/personalizer/concepts-features.
        /// </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        public virtual Response <PersonalizerRankResult> Rank(IEnumerable <PersonalizerRankableAction> actions, IEnumerable <object> contextFeatures, CancellationToken cancellationToken = default)
        {
            PersonalizerRankOptions options = new PersonalizerRankOptions(actions, contextFeatures);

            return(Rank(options, cancellationToken));
        }
        /// <summary> Submit a Personalizer rank request. Receives a context and a list of actions. Returns which of the provided actions should be used by your application, in rewardActionId. </summary>
        /// <param name="actions">The set of actions the Personalizer service
        /// can pick from.
        /// The set should not contain more than 50 actions.
        /// The order of the actions does not affect the rank result but the
        /// order
        /// should match the sequence your application would have used to
        /// display them.
        /// The first item in the array will be used as Baseline item in
        /// Offline evaluations.</param>
        /// <param name="contextFeatures">Features of the context used for
        /// Personalizer as a dictionary of dictionaries. This depends on the application, and
        /// typically includes features about the current user, their
        /// device, profile information, aggregated data about time and date, etc.
        /// Features should not include personally identifiable information (PII),
        /// unique UserIDs, or precise timestamps. Need to be JSON serializable.
        /// https://docs.microsoft.com/azure/cognitive-services/personalizer/concepts-features.
        /// </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        public virtual async Task <Response <PersonalizerRankResult> > RankAsync(IEnumerable <PersonalizerRankableAction> actions, IEnumerable <object> contextFeatures, CancellationToken cancellationToken = default)
        {
            PersonalizerRankOptions options = new PersonalizerRankOptions(actions, contextFeatures);

            return(await RankAsync(options, cancellationToken).ConfigureAwait(false));
        }