Skip to content

Commit

Permalink
Thick clent feature (#5)
Browse files Browse the repository at this point in the history
* 12767907: Add a package dependency on Microsoft.RL

* Add RankProcessor

* Remove unused file

* Remove some key

* Toggle commented codes so that most tests can pass

* Getting the required config details for rankprocessor to enable livemode

* Updating the wrong variable name

* Cleanup

* Revert "12767907: Add a package dependency on Microsoft.RL"

This reverts commit c586920.

* Revert "Add RankProcessor"

This reverts commit 43967d3.

* Revert "Remove unused file"

This reverts commit 1e400a8.

* Revert "Toggle commented codes so that most tests can pass"

This reverts commit 7ec50ec.

* Delete DisposeHelper.cs

* Revert "Revert "12767907: Add a package dependency on Microsoft.RL""

This reverts commit d6df6b5.

* Cleanup after reverting

* generating client configuration using autorest

* Revert "generating client configuration using autorest"

This reverts commit 6b75695.

* Configuration details for livemodel

* cleanup

* cleanup

* Added ToDo comments

* correct the version in endpoints

Co-authored-by: Tejaswi Paruchuri <[email protected]>
Co-authored-by: Tparuchuri <[email protected]>
  • Loading branch information
3 people authored Jan 25, 2022
1 parent f546ce5 commit 7616f7c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 10 deletions.
1 change: 1 addition & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Used for azure-sdk-tools repo until issue https://github.com/Azure/azure-sdk-tools/issues/1329 is addressed
-->
<add key="azure-sdk-tools" value="https://azuresdkartifacts.blob.core.windows.net/azure-sdk-tools/index.json" />
<add key="air-msrai-decisionservice-Consumption" value="https://msazure.pkgs.visualstudio.com/one/_packaging/air-msrai-decisionservice-Consumption/nuget/v3/index.json" />
</packageSources>
<disabledPackageSources>
<clear />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

<ItemGroup>
<PackageReference Include="Azure.Core" />
<PackageReference Include="Microsoft.RL" VersionOverride="1.0.18120003-INTERNAL" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
using Rl.Net;

namespace Azure.AI.Personalizer
{
Expand All @@ -17,10 +21,17 @@ public class PersonalizerClient
private readonly ClientDiagnostics _clientDiagnostics;
private readonly HttpPipeline _pipeline;
private readonly bool _isLocalInference;
private string stringEndpoint;
private string apiKey;

internal RankRestClient RankRestClient { get; set; }
internal EventsRestClient EventsRestClient { get; set; }
internal MultiSlotRestClient MultiSlotRestClient { get; set; }
internal MultiSlotEventsRestClient MultiSlotEventsRestClient { get; set; }
internal ServiceConfigurationRestClient ServiceConfigurationRestClient { get; set; }
internal PolicyRestClient PolicyRestClient { get; set; }
internal PersonalizerServiceProperties _personalizerServiceProperties { get; set; }
internal PersonalizerPolicy _personalizerPolicy { get; set; }

/// <summary> Initializes a new instance of Personalizer Client for mocking. </summary>
protected PersonalizerClient()
Expand All @@ -46,11 +57,13 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, Personalizer
_clientDiagnostics = new ClientDiagnostics(options);
string[] scopes = { "https://cognitiveservices.azure.com/.default" };
_pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, scopes));
string stringEndpoint = endpoint.AbsoluteUri;
stringEndpoint = endpoint.AbsoluteUri;
RankRestClient = new RankRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
EventsRestClient = new EventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
MultiSlotRestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
MultiSlotEventsRestClient = new MultiSlotEventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
ServiceConfigurationRestClient = new ServiceConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
PolicyRestClient = new PolicyRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
}

/// <summary> Initializes a new instance of PersonalizerClient. </summary>
Expand All @@ -62,12 +75,18 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, bool isLocal
this(endpoint, credential, options)
{
_isLocalInference = isLocalInference;
if (isLocalInference)
{
//Intialize liveModel and call Rank processor
//ToDo:TASK 13057958: Working on changes to support token authentication in RLClient
//Configuration config = GetConfigurationForLiveModel("Token", "token");
}
}

/// <summary> Initializes a new instance of PersonalizerClient. </summary>
/// <param name="endpoint"> Supported Cognitive Services endpoint. </param>
/// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
public PersonalizerClient(Uri endpoint, TokenCredential credential) : this(endpoint, credential, null){ }
public PersonalizerClient(Uri endpoint, TokenCredential credential) : this(endpoint, credential, null) { }

/// <summary> Initializes a new instance of PersonalizerClient. </summary>
/// <param name="endpoint"> Supported Cognitive Services endpoint. </param>
Expand All @@ -83,15 +102,17 @@ public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, Personali
{
throw new ArgumentNullException(nameof(credential));
}

apiKey = credential.Key;
options ??= new PersonalizerClientOptions();
_clientDiagnostics = new ClientDiagnostics(options);
_pipeline = HttpPipelineBuilder.Build(options, new AzureKeyCredentialPolicy(credential, "Ocp-Apim-Subscription-Key"));
string stringEndpoint = endpoint.AbsoluteUri;
stringEndpoint = endpoint.AbsoluteUri;
RankRestClient = new RankRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
EventsRestClient = new EventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
MultiSlotRestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
MultiSlotEventsRestClient = new MultiSlotEventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
ServiceConfigurationRestClient = new ServiceConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
PolicyRestClient = new PolicyRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
}

/// <summary> Initializes a new instance of PersonalizerClient. </summary>
Expand All @@ -103,12 +124,17 @@ public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, bool isLo
this(endpoint, credential, options)
{
_isLocalInference = isLocalInference;
if (isLocalInference)
{
//Intialize liveModel and Rankprocessor
Configuration config = GetConfigurationForLiveModel("apiKey", apiKey);
}
}

/// <summary> Initializes a new instance of PersonalizerClient. </summary>
/// <param name="endpoint"> Supported Cognitive Services endpoint. </param>
/// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
public PersonalizerClient(Uri endpoint, AzureKeyCredential credential): this(endpoint, credential, null) { }
public PersonalizerClient(Uri endpoint, AzureKeyCredential credential) : this(endpoint, credential, null) { }

/// <summary> Initializes a new instance of MultiSlotEventsClient. </summary>
/// <param name="clientDiagnostics"> The handler for diagnostic messaging in the client. </param>
Expand All @@ -134,7 +160,15 @@ public virtual async Task<Response<PersonalizerRankResult>> RankAsync(Personaliz
scope.Start();
try
{
return await RankRestClient.RankAsync(options, cancellationToken).ConfigureAwait(false);
if (_isLocalInference)
{
//return RankProcessor result here
return null;
}
else
{
return await RankRestClient.RankAsync(options, cancellationToken).ConfigureAwait(false);
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -177,7 +211,15 @@ public virtual Response<PersonalizerRankResult> Rank(PersonalizerRankOptions opt
scope.Start();
try
{
return RankRestClient.Rank(options, cancellationToken);
if (_isLocalInference)
{
//return RankProcessor result here
return null;
}
else
{
return RankRestClient.Rank(options, cancellationToken);
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -482,5 +524,33 @@ public virtual Response ActivateMultiSlot(string eventId, CancellationToken canc
throw;
}
}

/// <summary> Gets the configuration details for the live model to use </summary>
internal Configuration GetConfigurationForLiveModel(string authType, string authValue)
{
_personalizerServiceProperties = ServiceConfigurationRestClient.Get();
_personalizerPolicy = PolicyRestClient.Get();
Configuration config = new Configuration();
// set up the model
if (authType == "apiKey")
{
config["http.api.key"] = authValue;
}
else
{
//ToDo: TASK 13057958 Working on changes to support token authentication in RLClient
//config["http.token.key"] = authValue;
}
config["interaction.http.api.host"] = stringEndpoint+"personalizer/v1.1-preview.1/logs/interactions";
config["observation.http.api.host"] = stringEndpoint+"personalizer/v1.1-preview.1/logs/observations";
//ToDo: TASK 13057958 Working on changes to support model api in RL.Net
config["model.blob.uri"] = stringEndpoint + "personalizer/v1.1-preview.1/model";
config["vw.commandline"] = _personalizerPolicy.Arguments;
config["protocol.version"] = "2";
config["initial_exploration.epsilon"] = Convert.ToString(_personalizerServiceProperties.ExplorationPercentage, CultureInfo.InvariantCulture);
config["rank.learning.mode"] = Convert.ToString(_personalizerServiceProperties.LearningMode, CultureInfo.InvariantCulture);
//return the config model
return config;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public abstract class PersonalizerTestBase : RecordedTestBase<PersonalizerTestEn
{
public static bool IsTestTenant = false;

public PersonalizerTestBase(bool isAsync): base(isAsync)
public PersonalizerTestBase(bool isAsync) : base(isAsync)
{
// TODO: Compare bodies again when https://github.com/Azure/azure-sdk-for-net/issues/22219 is resolved.
Matcher = new RecordMatcher(compareBodies: false);
Sanitizer = new PersonalizerRecordedTestSanitizer();
}

protected async Task<PersonalizerClient> GetPersonalizerClientAsync(bool isSingleSlot = false)
protected async Task<PersonalizerClient> GetPersonalizerClientAsync(bool isSingleSlot = false, bool isLocalInference = false)
{
string endpoint = isSingleSlot ? TestEnvironment.SingleSlotEndpoint : TestEnvironment.MultiSlotEndpoint;
string apiKey = isSingleSlot ? TestEnvironment.SingleSlotApiKey : TestEnvironment.MultiSlotApiKey;
Expand All @@ -29,7 +29,16 @@ protected async Task<PersonalizerClient> GetPersonalizerClientAsync(bool isSingl
}
var credential = new AzureKeyCredential(apiKey);
var options = InstrumentClientOptions(new PersonalizerClientOptions());
PersonalizerClient personalizerClient = new PersonalizerClient(new Uri(endpoint), credential, options);
PersonalizerClient personalizerClient = null;
if (isLocalInference)
{
personalizerClient = new PersonalizerClient(new Uri(endpoint), credential, true, options);
}
else
{
personalizerClient = new PersonalizerClient(new Uri(endpoint), credential, options);
}

personalizerClient = InstrumentClient(personalizerClient);
return personalizerClient;
}
Expand Down

0 comments on commit 7616f7c

Please sign in to comment.