private async Task RankWithNoOptions(PersonalizerClient client) { IList <object> contextFeatures = new List <object>() { new { Features = new { day = "tuesday", time = "night", weather = "rainy" } }, new { Features = new { userId = "1234", payingUser = true, favoriteGenre = "documentary", hoursOnSite = 0.12, lastwatchedType = "movie" } } }; IList <PersonalizerRankableAction> actions = new List <PersonalizerRankableAction>(); actions.Add( new PersonalizerRankableAction( id: "Person1", features: new List <object>() { new { videoType = "documentary", videoLength = 35, director = "CarlSagan" }, new { mostWatchedByAge = "30-35" } } )); actions.Add( new PersonalizerRankableAction( id: "Person2", features: new List <object>() { new { videoType = "documentary", videoLength = 35, director = "CarlSagan" }, new { mostWatchedByAge = "40-45" } } )); // Action PersonalizerRankResult response = await client.RankAsync(actions, contextFeatures); Assert.AreEqual(actions.Count, response.Ranking.Count); }
protected async Task <PersonalizerClient> GetPersonalizerClientAsync(bool isSingleSlot = false, bool useLocalInference = false, float subsampleRate = 1.0f) { string endpoint = isSingleSlot ? TestEnvironment.SingleSlotEndpoint : TestEnvironment.MultiSlotEndpoint; string apiKey = isSingleSlot ? TestEnvironment.SingleSlotApiKey : TestEnvironment.MultiSlotApiKey; PersonalizerAdministrationClient adminClient = GetAdministrationClient(isSingleSlot); if (!isSingleSlot) { await EnableMultiSlot(adminClient); } var credential = new AzureKeyCredential(apiKey); var options = InstrumentClientOptions(new PersonalizerClientOptions(useLocalInference: useLocalInference, subsampleRate: subsampleRate)); PersonalizerClient personalizerClient = null; if (useLocalInference) { if (Mode == RecordedTestMode.Playback) { RlNetProcessor rlNetProcessor = SetupRlNetProcessor(); personalizerClient = new PersonalizerClientForTest(new Uri(endpoint), credential, true, rlNetProcessor, options: options, subsampleRate: subsampleRate); } else { personalizerClient = new PersonalizerClient(new Uri(endpoint), credential, options: options); } } else { personalizerClient = new PersonalizerClient(new Uri(endpoint), credential, options); } personalizerClient = InstrumentClient(personalizerClient); return(personalizerClient); }
static void SimulateRandomUserPreferences() { for (int i = 0; i < 1000; i++) { Random random = new Random(DateTime.Now.Millisecond); IList <RankableAction> actions = GetActions(); PersonalizerClient client = InitializePersonalizerClient(ServiceEndpoint); IList <object> currentContext = new List <object>() { new { professiom = random.Next(1, 5) }, new { preference = random.Next(1, 3) } }; IList <string> excludeActions = new List <string> { "http://codestories.gr/index.php/2018/10/21/297/" }; // Generate an ID to associate with the request. string eventId = Guid.NewGuid().ToString(); var request = new RankRequest(actions, currentContext, excludeActions, eventId); RankResponse response = client.Rank(request); client.Reward(response.EventId, new RewardRequest(random.Next(0, 2))); } }
public async Task RankNullParameters() { PersonalizerClient client = GetPersonalizerClient(); IList <PersonalizerRankableAction> actions = new List <PersonalizerRankableAction>(); actions.Add (new PersonalizerRankableAction( id: "Person", features: new List <object>() { new { videoType = "documentary", videoLength = 35, director = "CarlSagan" }, new { mostWatchedByAge = "30-35" } } )); var request = new PersonalizerRankOptions(actions); // Action PersonalizerRankResult response = await client.RankAsync(request); // Assert Assert.AreEqual(actions.Count, response.Ranking.Count); for (int i = 0; i < response.Ranking.Count; i++) { Assert.AreEqual(actions[i].Id, response.Ranking[i].Id); } }
public async Task SingleSlotEventsTests() { PersonalizerClient client = await GetPersonalizerClientAsync(isSingleSlot : true); await Reward(client); await Activate(client); }
public async Task SingleSlotRankLocalInferenceTests() { Assert.ThrowsAsync <ArgumentOutOfRangeException>(async() => await GetPersonalizerClientAsync(isSingleSlot: true, useLocalInference: true, subsampleRate: 1.01f)); Assert.ThrowsAsync <ArgumentOutOfRangeException>(async() => await GetPersonalizerClientAsync(isSingleSlot: true, useLocalInference: true, subsampleRate: 0f)); PersonalizerClient client = await GetPersonalizerClientAsync(isSingleSlot : true, useLocalInference : true); await SingleSlotRankTests(client); }
private async Task Reward(PersonalizerClient client) { PersonalizerSlotReward slotReward = new PersonalizerSlotReward("testSlot", 1); PersonalizerRewardMultiSlotOptions rewardRequest = new PersonalizerRewardMultiSlotOptions(new List <PersonalizerSlotReward> { slotReward }); await client.RewardMultiSlotAsync("123456789", rewardRequest); }
public async Task SingleSlotRankTests() { PersonalizerClient client = await GetPersonalizerClientAsync(isSingleSlot : true); await RankNullParameters(client); await RankServerFeatures(client); await RankNullParameters(client); }
// </classVariables> // <mainLoop> static void 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."); int iteration = 1; bool runLoop = true; IList <PersonalizerRankableAction> actions = GetActions(); IList <PersonalizerSlotOptions> slots = GetSlots(); PersonalizerClient client = InitializePersonalizerClient(new Uri(ServiceEndpoint)); do { Console.WriteLine("\nIteration: " + iteration++); string timeOfDayFeature = GetTimeOfDayForContext(); string deviceFeature = GetDeviceForContext(); IList <object> currentContext = GetContext(timeOfDayFeature, deviceFeature); string eventId = Guid.NewGuid().ToString(); var multiSlotRankOptions = new PersonalizerRankMultiSlotOptions(actions, slots, currentContext, eventId); PersonalizerMultiSlotRankResult multiSlotRankResult = client.RankMultiSlot(multiSlotRankOptions); for (int i = 0; i < multiSlotRankResult.Slots.Count(); ++i) { string slotId = multiSlotRankResult.Slots[i].SlotId; Console.WriteLine($"\nPersonalizer service decided you should display: { multiSlotRankResult.Slots[i].RewardActionId} in slot {slotId}. Is this correct? (y/n)"); string answer = GetKey(); if (answer == "Y") { client.RewardMultiSlot(eventId, slotId, 1f); 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") { client.RewardMultiSlot(eventId, slotId, 0f); 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 { client.RewardMultiSlot(eventId, slotId, 0f); Console.WriteLine("\nEntered choice is invalid. Service assumes that you didn't like the recommended item."); } } Console.WriteLine("\nPress q to break, any other key to continue:"); runLoop = !(GetKey() == "Q"); } while (runLoop); }
protected PersonalizerClient GetPersonalizerClient() { var credential = new AzureKeyCredential(TestEnvironment.ApiKey); var options = InstrumentClientOptions(new PersonalizerClientOptions()); PersonalizerClient personalizerClient = new PersonalizerClient(new Uri(TestEnvironment.Endpoint), credential, options); personalizerClient = InstrumentClient(personalizerClient); return(personalizerClient); }
private async Task MultiSlotTestInner(PersonalizerClient client) { await RankMultiSlotNullParameters(client); await RankMultiSlotNoOptions(client); await RankMultiSlot(client); await Reward(client); await RewardForOneSlot(client); await Activate(client); }
private PersonalizerClient InitializePersonalizerClient(string endpoint, string apiKey) { PersonalizerClient client = new PersonalizerClient(new ApiKeyServiceClientCredentials(apiKey)) { Endpoint = endpoint }; return(client); }
public HomeController(IConfiguration configuration) { var personalizerApiKey = configuration["PersonalizerApiKey"]; var personalizerServiceEndpoint = configuration["PersonalizerServiceEndpoint"]; _personalizerClient = new PersonalizerClient(new ApiKeyServiceClientCredentials(personalizerApiKey)) { Endpoint = personalizerServiceEndpoint }; }
/// <summary> /// Initializes the personalizer client. /// </summary> /// <param name="url">Azure endpoint</param> /// <returns>Personalizer client instance</returns> static PersonalizerClient InitializePersonalizerClient(string url) { PersonalizerClient client = new PersonalizerClient( new ApiKeyServiceClientCredentials(ApiKey)) { Endpoint = url }; return(client); }
/// <summary> /// Initializes the personalization client. /// </summary> /// <param name="url">Azure endpoint</param> /// <param name="serviceKey">subscription key</param> /// <returns>Personalization client instance</returns> private static PersonalizerClient InitializePersonalizationClient(string url, string serviceKey) { PersonalizerClient client = new PersonalizerClient( new ApiKeyServiceClientCredentials(serviceKey)) { Endpoint = url }; return(client); }
public PersonalizerClient InitializePersonalizerClient() { PersonalizerClient client = new PersonalizerClient( new ApiKeyServiceClientCredentials(Environment.GetEnvironmentVariable("PersonalizerApiKey"))) { Endpoint = Environment.GetEnvironmentVariable("PersonalizerServiceEndpoint") }; return(client); }
private const string ServiceEndpoint = "https://westeurope.api.cognitive.microsoft.com/"; //"https://westus2.api.cognitive.microsoft.com/"; static void Main(string[] args) { // Initialize Personalizer client. PersonalizerClient client = InitializePersonalizerClient(ServiceEndpoint); Simulator sim = new Simulator(client); sim.SimulateEvents(100); }
/// <summary> /// Initializes a new instance of the <see cref="LuisBot"/> class. /// </summary> /// <param name="services">Services configured from the ".bot" file.</param> /// <param name="personalizerClient">Client used to rank suggestions for user/reward good suggestions</param> public LuisBot(BotServices services, PersonalizerClient personalizerClient) { _services = services ?? throw new ArgumentNullException(nameof(services)); _rlFeaturesManager = services.RLContextManager; _personalizerClient = personalizerClient; if (!_services.LuisServices.ContainsKey(LuisKey)) { throw new ArgumentException($"Invalid configuration. Please check your '.bot' file for a LUIS service named '{LuisKey}'."); } }
public async Task MultiSlotTest() { PersonalizerClient client = await GetPersonalizerClientAsync(isSingleSlot : false); await RankMultiSlotNullParameters(client); await RankMultiSlotNoOptions(client); await RankMultiSlot(client); await Reward(client); await RewardForOneSlot(client); await Activate(client); }
// Private helper methods and data. private PersonalizerClient CreatePersonalizer() { string Endpoint = $"https://{personalizerResourceName}.cognitiveservices.azure.com"; client = new PersonalizerClient( new ApiKeyServiceClientCredentials(personalizerEndpointKey)) { Endpoint = Endpoint }; return(client); }
public MainPage() { InitializeComponent(); Device.BeginInvokeOnMainThread(async() => await Init()); client = new PersonalizerClient(new ApiKeyServiceClientCredentials(apiKey)); client.Endpoint = endpoint; News.ItemTapped += News_ItemTapped; }
public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log) { try { string requestBody = String.Empty; using (StreamReader sReader = new StreamReader(req.Body)) { requestBody = await sReader.ReadToEndAsync(); } //Send to SQL db var sendIceCreamOrderRequest = JsonConvert.DeserializeObject <SendIceCreamOrderRequest>(requestBody); var contextOptions = new DbContextOptionsBuilder <ColdStartContext>() .UseSqlServer(Environment.GetEnvironmentVariable("AzureSqlDatabase", EnvironmentVariableTarget.Process)) .Options; using (var context = new ColdStartContext(contextOptions)) { var preorder = await context.Orders.AddAsync(sendIceCreamOrderRequest.Preorder); await context.SaveChangesAsync(); //Send to Azure Queuee var storageConnectionString = Environment.GetEnvironmentVariable("AzureQueueStorage", EnvironmentVariableTarget.Process); var queueClient = new QueueClient(storageConnectionString, QueueName); var preOrderBytes = System.Text.Encoding.UTF8.GetBytes(preorder.Entity.toJson()); await queueClient.SendMessageAsync(Convert.ToBase64String(preOrderBytes)); } //Send personalizer options var personalizerEndpoint = Environment.GetEnvironmentVariable("AzurePersonalizerEndpoint", EnvironmentVariableTarget.Process); var personalizerKey = Environment.GetEnvironmentVariable("AzurePersonalizerKey", EnvironmentVariableTarget.Process); if (!string.IsNullOrWhiteSpace(personalizerEndpoint) || !string.IsNullOrWhiteSpace(personalizerKey)) { var personalizerClient = new PersonalizerClient( new ApiKeyServiceClientCredentials(personalizerKey)) { Endpoint = personalizerEndpoint }; var request = new RewardRequest(sendIceCreamOrderRequest.IsRecommended ? 1 : 0); await personalizerClient.RewardAsync(sendIceCreamOrderRequest.EventId, request); } return(new OkObjectResult(true)); } catch (Exception ex) { return(new BadRequestObjectResult($"Exception happened when sending: {ex}")); } }
private async Task RewardAsync(ITurnContext turnContext, string eventId, double reward, CancellationToken cancellationToken) { await turnContext.SendActivityAsync( "===== DEBUG MESSAGE CALL REWARD =====\n" + "Calling Reward:\n" + $"eventId = {eventId}, reward = {reward}\n", cancellationToken : cancellationToken); var client = new PersonalizerClient( new ApiKeyServiceClientCredentials(_rlFeaturesManager.SubscriptionKey)) { Endpoint = _rlFeaturesManager.RLFeatures.HostName.ToString() }; await client.RewardAsync(eventId, new RewardRequest(reward), cancellationToken); }
public async Task <IActionResult> PutRecommendationAsSelected(object eventId) { var apiKey = (await _context.AppSettings.FirstOrDefaultAsync(setting => setting.EnumCode == (int)Enums.AppSetting.PersonalizerApiKey)).Value; var serviceEndpoint = (await _context.AppSettings.FirstOrDefaultAsync(setting => setting.EnumCode == (int)Enums.AppSetting.PersonalizerServiceEndpoint)).Value; PersonalizerClient client = new PersonalizerClient(new ApiKeyServiceClientCredentials(apiKey)) { Endpoint = serviceEndpoint }; var eventIdAsString = eventId.ToString(); await client.RewardAsync(eventIdAsString, new RewardRequest(1)); return(NoContent()); }
protected async Task <PersonalizerClient> GetPersonalizerClientAsync(bool isSingleSlot = false) { string endpoint = isSingleSlot ? TestEnvironment.SingleSlotEndpoint : TestEnvironment.MultiSlotEndpoint; string apiKey = isSingleSlot ? TestEnvironment.SingleSlotApiKey : TestEnvironment.MultiSlotApiKey; PersonalizerAdministrationClient adminClient = GetAdministrationClient(isSingleSlot); if (!isSingleSlot) { await EnableMultiSlot(adminClient); } var credential = new AzureKeyCredential(apiKey); var options = InstrumentClientOptions(new PersonalizerClientOptions()); PersonalizerClient personalizerClient = new PersonalizerClient(new Uri(endpoint), credential, options); personalizerClient = InstrumentClient(personalizerClient); return(personalizerClient); }
private async Task RankMultiSlotNoOptions(PersonalizerClient client) { // Action PersonalizerMultiSlotRankResult response = await client.RankMultiSlotAsync(actions, slots, contextFeatures); // Assert Assert.AreEqual(slots.Count, response.Slots.Count); // Assertions for first slot PersonalizerSlotResult responseSlot1 = response.Slots[0]; Assert.AreEqual(slot1.Id, responseSlot1.SlotId); Assert.AreEqual("NewsArticle", responseSlot1.RewardActionId); // Assertions for second slot PersonalizerSlotResult responseSlot2 = response.Slots[1]; Assert.AreEqual(slot2.Id, responseSlot2.SlotId); Assert.AreEqual("SportsArticle", responseSlot2.RewardActionId); }
public async Task RankServerFeatures() { PersonalizerClient client = GetPersonalizerClient(); IList <object> contextFeatures = new List <object>() { new { Features = new { day = "tuesday", time = "night", weather = "rainy" } }, new { Features = new { userId = "1234", payingUser = true, favoriteGenre = "documentary", hoursOnSite = 0.12, lastwatchedType = "movie" } } }; IList <PersonalizerRankableAction> actions = new List <PersonalizerRankableAction>(); actions.Add( new PersonalizerRankableAction( id: "Person1", features: new List <object>() { new { videoType = "documentary", videoLength = 35, director = "CarlSagan" }, new { mostWatchedByAge = "30-35" } } )); actions.Add( new PersonalizerRankableAction( id: "Person2", features: new List <object>() { new { videoType = "documentary", videoLength = 35, director = "CarlSagan" }, new { mostWatchedByAge = "40-45" } } )); IList <string> excludeActions = new List <string> { "Person1" }; string eventId = "123456789"; var request = new PersonalizerRankOptions(actions, contextFeatures, excludeActions, eventId); // Action PersonalizerRankResult response = await client.RankAsync(request); // Assert Assert.AreEqual(eventId, response.EventId); Assert.AreEqual(actions.Count, response.Ranking.Count); for (int i = 0; i < response.Ranking.Count; i++) { Assert.AreEqual(actions[i].Id, response.Ranking[i].Id); } }
private async Task RankMultiSlotNullParameters(PersonalizerClient client) { PersonalizerRankMultiSlotOptions request = new PersonalizerRankMultiSlotOptions(actions, slots); // Action PersonalizerMultiSlotRankResult response = await client.RankMultiSlotAsync(request); // Assert Assert.AreEqual(slots.Count, response.Slots.Count); // Assertions for first slot PersonalizerSlotResult responseSlot1 = response.Slots[0]; Assert.AreEqual(slot1.Id, responseSlot1.SlotId); Assert.AreEqual("NewsArticle", responseSlot1.RewardActionId); // Assertions for second slot PersonalizerSlotResult responseSlot2 = response.Slots[1]; Assert.AreEqual(slot2.Id, responseSlot2.SlotId); Assert.AreEqual("SportsArticle", responseSlot2.RewardActionId); }
public JournalEntryService(IConfiguration configuration, KeyVaultClient keyVaultClient) { _keyVaultClient = keyVaultClient; _configuration = configuration; _textAnalyticsClient = new TextAnalyticsClient( new Uri(_configuration.GetSection("Secrets:TextAnalytics:Endpoint").Value), new AzureKeyCredential(_keyVaultClient .GetSecretAsync( _configuration.GetSection("Secrets:TextAnalytics:Identifier").Value) .Result.Value)); _personalizerClient = new PersonalizerClient( new ApiKeyServiceClientCredentials(_keyVaultClient .GetSecretAsync( _configuration.GetSection("Secrets:Personalizer:Identifier").Value) .Result.Value)) { Endpoint = _configuration.GetSection("Secrets:Personalizer:Endpoint").Value }; }
public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log) { try { string requestBody = String.Empty; using (StreamReader sReader = new StreamReader(req.Body)) { requestBody = await sReader.ReadToEndAsync(); } var recommendationRequest = JsonConvert.DeserializeObject <RecommendationRequest>(requestBody); var personalizerEndpoint = Environment.GetEnvironmentVariable("AzurePersonalizerEndpoint", EnvironmentVariableTarget.Process); var personalizerKey = Environment.GetEnvironmentVariable("AzurePersonalizerKey", EnvironmentVariableTarget.Process); if (!string.IsNullOrWhiteSpace(personalizerEndpoint) || !string.IsNullOrWhiteSpace(personalizerKey)) { var personalizerClient = new PersonalizerClient( new ApiKeyServiceClientCredentials(personalizerKey)) { Endpoint = personalizerEndpoint }; var actions = GetRankableActions(recommendationRequest.Catalogitems); var contextFeatures = GetContextFeatures(recommendationRequest.UserName); var excludeActions = new List <string>(); var request = new RankRequest(actions, contextFeatures, excludeActions, Guid.NewGuid().ToString()); var response = await personalizerClient.RankAsync(request); var recommendationReponse = new RecommendationResponse { eventId = response.EventId, PrefferedItemId = Convert.ToInt32(response.RewardActionId) }; return(new OkObjectResult(recommendationReponse)); } return(new BadRequestObjectResult("No endpoint or key configured")); } catch (Exception ex) { Console.WriteLine(ex); return(new BadRequestObjectResult(ex)); } }