Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tparuchuri committed Jan 14, 2022
1 parent 7bef96f commit db8c651
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
Expand All @@ -26,6 +27,7 @@ public class PersonalizerClient
internal MultiSlotEventsRestClient MultiSlotEventsRestClient { get; set; }

internal Models.ClientConfigurationRestClient ClientConfigurationRestClient { get; set; }
internal Lazy<Models.PersonalizerClientProperties> result { get; set; }

/// <summary> Initializes a new instance of Personalizer Client for mocking. </summary>
protected PersonalizerClient()
Expand Down Expand Up @@ -63,15 +65,17 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, Personalizer
/// <param name="endpoint"> Supported Cognitive Services endpoint. </param>
/// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
/// <param name="isLocalInference"> A flag to determine whether to use local inference. </param>
/// <param name="configuration"> A configuration to use local reference. </param>
/// <param name="options"> The options for configuring the client. </param>
public PersonalizerClient(Uri endpoint, TokenCredential credential, bool isLocalInference, Configuration configuration, PersonalizerClientOptions options = null) :
public PersonalizerClient(Uri endpoint, TokenCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) :
this(endpoint, credential, options)
{
_isLocalInference = isLocalInference;
LiveModel liveModel = new LiveModel(configuration);
liveModel.Init();
_rankProcessor = new RankProcessor(liveModel);
if (isLocalInference)
{
LiveModel liveModel = new LiveModel(GetClientConfigurationForLiveModel());
liveModel.Init();
_rankProcessor = new RankProcessor(liveModel);
}
}

/// <summary> Initializes a new instance of PersonalizerClient. </summary>
Expand Down Expand Up @@ -109,15 +113,17 @@ public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, Personali
/// <param name="endpoint"> Supported Cognitive Services endpoint. </param>
/// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
/// <param name="isLocalInference"> A flag to determine whether to use local inference. </param>
/// <param name="configuration"> A configuration to use local reference. </param>
/// <param name="options"> The options for configuring the client. </param>
public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, bool isLocalInference, Configuration configuration, PersonalizerClientOptions options = null) :
public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) :
this(endpoint, credential, options)
{
_isLocalInference = isLocalInference;
LiveModel liveModel = new LiveModel(configuration);
liveModel.Init();
_rankProcessor = new RankProcessor(liveModel);
if (isLocalInference)
{
LiveModel liveModel = new LiveModel(GetClientConfigurationForLiveModel());
liveModel.Init();
_rankProcessor = new RankProcessor(liveModel);
}
}

/// <summary> Initializes a new instance of PersonalizerClient. </summary>
Expand Down Expand Up @@ -156,8 +162,6 @@ public virtual async Task<Response<PersonalizerRankResult>> RankAsync(Personaliz
}
else
{
Models.PersonalizerClientProperties result = await ClientConfigurationRestClient.PostAsync(cancellationToken).ConfigureAwait(false);
Console.WriteLine("InternalId: "+result.ApplicationID+"\nStorageBlobUri: "+result.ModelBlobUri+"\nLearningMode: "+result.ProtocolVersion+"\nExploration Percentage: "+result.InitialExplorationEpsilon+"\ninitialCommandline: "+result.InitialCommandLine);
return await RankRestClient.RankAsync(options, cancellationToken).ConfigureAwait(false);
}
}
Expand Down Expand Up @@ -189,8 +193,6 @@ public virtual async Task<Response<PersonalizerRankResult>> RankAsync(Personaliz
/// <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)
{
Models.PersonalizerClientProperties result = await ClientConfigurationRestClient.PostAsync(cancellationToken).ConfigureAwait(false);
Console.WriteLine("InternalId: " + result.ApplicationID + "\nStorageBlobUri: " + result.ModelBlobUri + "\nLearningMode: " + result.ProtocolVersion + "\nExploration Percentage: " + result.InitialExplorationEpsilon + "\ninitialCommandline: " + result.InitialCommandLine);
PersonalizerRankOptions options = new PersonalizerRankOptions(actions, contextFeatures);
return await RankAsync(options, cancellationToken).ConfigureAwait(false);
}
Expand All @@ -210,9 +212,7 @@ public virtual Response<PersonalizerRankResult> Rank(PersonalizerRankOptions opt
}
else
{
Models.PersonalizerClientProperties result = ClientConfigurationRestClient.Post(cancellationToken);
Console.WriteLine("InternalId: " + result.ApplicationID + "\nStorageBlobUri: " + result.ModelBlobUri + "\nLearningMode: " + result.ProtocolVersion + "\nExploration Percentage: " + result.InitialExplorationEpsilon + "\ninitialCommandline: " + result.InitialCommandLine);
return RankRestClient.Rank(options, cancellationToken);
return RankRestClient.Rank(options, cancellationToken);
}
}
catch (Exception e)
Expand Down Expand Up @@ -518,5 +518,24 @@ public virtual Response ActivateMultiSlot(string eventId, CancellationToken canc
throw;
}
}

/// <summary> Gets the configuration details for the live model to use </summary>
internal Configuration GetClientConfigurationForLiveModel()
{
result = new Lazy<Models.PersonalizerClientProperties>(() => ClientConfigurationRestClient.Post());
Console.WriteLine("InternalId: " + result.Value.ApplicationID + "\nStorageBlobUri: " + result.Value.ModelBlobUri + "\nLearningMode: " + result.Value.LearningMode + "\nExploration Percentage: " + result.Value.InitialExplorationEpsilon + "\ninitialCommandline: " + result.Value.InitialCommandLine);

Configuration config = new Configuration();
// configure the personalizer loop
config["appid"] = result.Value.ApplicationID;

// set up the model
config["model.source"] = result.Value.ModelBlobUri;
config["model.vw.initial_command_line"] = result.Value.InitialCommandLine;
config["initial_exploration.epsilon"] = Convert.ToString(result.Value.InitialExplorationEpsilon, CultureInfo.InvariantCulture);
config["rank.learning.mode"] = Convert.ToString(result.Value.LearningMode, CultureInfo.InvariantCulture);
//return the config model
return config;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ protected async Task<PersonalizerClient> GetPersonalizerClientAsync(bool isSingl
{
string endpoint = isSingleSlot ? TestEnvironment.SingleSlotEndpoint : TestEnvironment.MultiSlotEndpoint;
string apiKey = isSingleSlot ? TestEnvironment.SingleSlotApiKey : TestEnvironment.MultiSlotApiKey;
//string endpoint = isSingleSlot ? "https://autoopte2etest3.ppe.cognitiveservices.azure.com" : TestEnvironment.MultiSlotEndpoint;
//string apiKey = isSingleSlot ? "{personalizerApiKey}" : TestEnvironment.MultiSlotApiKey;
PersonalizerAdministrationClient adminClient = GetAdministrationClient(isSingleSlot);
if (!isSingleSlot)
{
Expand All @@ -35,29 +33,7 @@ protected async Task<PersonalizerClient> GetPersonalizerClientAsync(bool isSingl
PersonalizerClient personalizerClient = null;
if (isLocalInference)
{
Configuration config = new Configuration();

// configure the personalizer loop
config["appid"] = "appId1";

// set up the model
config["model.source"] = "FILE_MODEL_DATA";
config["model_file_loader.file_name"] = "modelfile.data";
config["model.backgroundrefresh"] = "false";

config["interaction.http.api.host"] = "https://{personalizerEndPoint}/personalizer/v1.1-preview.2/logs/interactions";
config["observation.http.api.host"] = "https://{personalizerEndPoint}/personalizer/v1.1-preview.2/logs/observations";
config["http.api.key"] = "{personalizerApiKey}";

config["InitialCommandLine"] = "--cb_explore_adf --epsilon 0.2 --power_t 0 -l 0.001 --cb_type mtr -q ::";
config["InitialExplorationEpsilon"] = "1";
config["LearningMode"] = "Online";
config["ProtocolVersion"] = "2";
config["LearningMode"] = "Online";
config["observation.sender.implementation"] = "OBSERVATION_HTTP_API_SENDER";
config["interaction.sender.implementation"] = "INTERACTION_HTTP_API_SENDER";

personalizerClient = new PersonalizerClient(new Uri(endpoint), credential, true, config, options);
personalizerClient = new PersonalizerClient(new Uri(endpoint), credential, true, options);
}
else
{
Expand All @@ -72,8 +48,6 @@ protected PersonalizerAdministrationClient GetAdministrationClient(bool isSingle
{
string endpoint = isSingleSlot ? TestEnvironment.SingleSlotEndpoint : TestEnvironment.MultiSlotEndpoint;
string apiKey = isSingleSlot ? TestEnvironment.SingleSlotApiKey : TestEnvironment.MultiSlotApiKey;
//string endpoint = isSingleSlot ? "https://autoopte2etest3.ppe.cognitiveservices.azure.com" : TestEnvironment.MultiSlotEndpoint;
//string apiKey = isSingleSlot ? "{personalizerApiKey}" : TestEnvironment.MultiSlotApiKey;
var credential = new AzureKeyCredential(apiKey);
var options = InstrumentClientOptions(new PersonalizerClientOptions());
PersonalizerAdministrationClient personalizerAdministrationClient = new PersonalizerAdministrationClient(new Uri(endpoint), credential, options);
Expand Down

0 comments on commit db8c651

Please sign in to comment.