private static async Task <MultiSlotRankResponse> SendMultiSlotRank(HttpClient client, string rankRequestBody, string rankUrl) { var rankBuilder = new UriBuilder(new Uri(rankUrl)); HttpRequestMessage rankRequest = new HttpRequestMessage(HttpMethod.Post, rankBuilder.Uri); rankRequest.Content = new StringContent(rankRequestBody, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.SendAsync(rankRequest); MultiSlotRankResponse rankResponse = JsonSerializer.Deserialize <MultiSlotRankResponse>(await response.Content.ReadAsByteArrayAsync()); return(rankResponse); }
static async Task Main(string[] args) { Console.WriteLine($"Welcome to this Personalizer Quickstart!\n" + $"This code will help you understand how to use the Personalizer APIs (multislot rank and multislot reward).\n" + $"Each iteration represents a user interaction and will demonstrate how context, actions, slots, and rewards work.\n" + $"Note: Personalizer AI models learn from a large number of user interactions:\n" + $"You won't be able to tell the difference in what Personalizer returns by simulating a few events by hand.\n" + $"If you want a sample that focuses on seeing how Personalizer learns, see the Python Notebook sample."); IList <Action> actions = GetActions(); IList <Slot> slots = GetSlots(); using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("ocp-apim-subscription-key", ResourceKey); int iteration = 1; bool runLoop = true; do { Console.WriteLine($"\nIteration: {iteration++}"); string timeOfDayFeature = GetTimeOfDayForContext(); string deviceFeature = GetDeviceForContext(); IList <Context> context = GetContext(timeOfDayFeature, deviceFeature); string eventId = Guid.NewGuid().ToString(); string rankRequestBody = JsonSerializer.Serialize(new MultiSlotRankRequest() { ContextFeatures = context, Actions = actions, Slots = slots, EventId = eventId, DeferActivation = false }); //Ask Personalizer what action to show for each slot MultiSlotRankResponse multiSlotRankResponse = await SendMultiSlotRank(client, rankRequestBody, MultiSlotRankUrl); MultiSlotReward multiSlotRewards = new MultiSlotReward() { Reward = new List <SlotReward>() }; for (int i = 0; i < multiSlotRankResponse.Slots.Count(); ++i) { Console.WriteLine($"\nPersonalizer service decided you should display: { multiSlotRankResponse.Slots[i].RewardActionId} in slot {multiSlotRankResponse.Slots[i].Id}. Is this correct? (y/n)"); SlotReward reward = new SlotReward() { SlotId = multiSlotRankResponse.Slots[i].Id }; string answer = GetKey(); if (answer == "Y") { reward.Value = 1; Console.WriteLine("\nGreat! The application will send Personalizer a reward of 1 so it learns from this choice of action for this slot."); } else if (answer == "N") { reward.Value = 0; Console.WriteLine("\nYou didn't like the recommended item. The application will send Personalizer a reward of 0 for this choice of action for this slot."); } else { reward.Value = 0; Console.WriteLine("\nEntered choice is invalid. Service assumes that you didn't like the recommended item."); } multiSlotRewards.Reward.Add(reward); } string rewardRequestBody = JsonSerializer.Serialize(multiSlotRewards); // Send the reward for the action based on user response for each slot. await SendMultiSlotReward(client, rewardRequestBody, MultiSlotRewardUrlBase, multiSlotRankResponse.EventId); Console.WriteLine("\nPress q to break, any other key to continue:"); runLoop = !(GetKey() == "Q"); } while (runLoop); } }