From c5869208c68ea592562e6d05278bfce26ac748de Mon Sep 17 00:00:00 2001 From: John Huang Date: Fri, 10 Dec 2021 18:29:29 -0800 Subject: [PATCH 01/22] 12767907: Add a package dependency on Microsoft.RL --- NuGet.Config | 1 + 1 file changed, 1 insertion(+) diff --git a/NuGet.Config b/NuGet.Config index be103949f661..7b6da4adea45 100644 --- a/NuGet.Config +++ b/NuGet.Config @@ -10,6 +10,7 @@ Used for azure-sdk-tools repo until issue https://github.com/Azure/azure-sdk-tools/issues/1329 is addressed --> + From 43967d3a10108da239119b6a06e889efcade6c75 Mon Sep 17 00:00:00 2001 From: John Huang Date: Fri, 24 Dec 2021 01:42:33 -0800 Subject: [PATCH 02/22] Add RankProcessor --- .../src/Azure.Core.TestFramework.csproj | 2 + sdk/personalizer/Azure.AI.Personalizer.sln | 10 ++- .../src/Azure.AI.Personalizer.csproj | 4 +- .../PersonalizerRankResult.Serialization.cs | 64 +++++++++++----- .../src/Generated/MultiSlotClient.cs | 15 +++- .../src/Generated/RankClient.cs | 24 +++++- .../src/Generated/RankProcessor.cs | 53 +++++++++++++ .../src/Models/DecisionContext.cs | 75 +++++++++++++++++++ .../src/Models/DecisionContextDocument.cs | 57 ++++++++++++++ .../Models/DecisionContextDocumentSource.cs | 19 +++++ .../src/Models/DisposeHelper.cs | 23 ++++++ .../src/Models/JsonRawStringListConverter.cs | 59 +++++++++++++++ .../src/Models/PersonalizerClient.cs | 33 +++++++- .../src/Models/PersonalizerRankOptions.cs | 2 +- .../src/Models/RankProcessor.cs | 7 ++ .../tests/Azure.AI.Personalizer.Tests.csproj | 1 + .../Infrastructure/PersonalizerTestBase.cs | 48 ++++++++++-- .../tests/Personalizer/RankTests.cs | 42 +++++++++++ 18 files changed, 502 insertions(+), 36 deletions(-) create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankProcessor.cs create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContext.cs create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocument.cs create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocumentSource.cs create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/JsonRawStringListConverter.cs create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/RankProcessor.cs diff --git a/sdk/core/Azure.Core.TestFramework/src/Azure.Core.TestFramework.csproj b/sdk/core/Azure.Core.TestFramework/src/Azure.Core.TestFramework.csproj index 46a5010dcedf..c805fd70cea1 100644 --- a/sdk/core/Azure.Core.TestFramework/src/Azure.Core.TestFramework.csproj +++ b/sdk/core/Azure.Core.TestFramework/src/Azure.Core.TestFramework.csproj @@ -1,6 +1,8 @@  $(RequiredTargetFrameworks);net47 + $(NoWarn);SYSLIB0023 + $(NoWarn);CS8032 true diff --git a/sdk/personalizer/Azure.AI.Personalizer.sln b/sdk/personalizer/Azure.AI.Personalizer.sln index 791da89ce047..c0732360d38b 100644 --- a/sdk/personalizer/Azure.AI.Personalizer.sln +++ b/sdk/personalizer/Azure.AI.Personalizer.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.0.31410.414 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.32002.261 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.AI.Personalizer", "Azure.AI.Personalizer\src\Azure.AI.Personalizer.csproj", "{B940AE9B-814A-4A20-9C81-3B8E5345E3B4}" EndProject @@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Core.TestFramework", "..\core\Azure.Core.TestFramework\src\Azure.Core.TestFramework.csproj", "{0F88C67F-34D2-4C68-B5BF-08A547D4CC2E}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Core", "..\core\Azure.Core\src\Azure.Core.csproj", "{CFB35402-69EB-448F-82B7-2D284730B0A6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -35,6 +37,10 @@ Global {0F88C67F-34D2-4C68-B5BF-08A547D4CC2E}.Debug|Any CPU.Build.0 = Debug|Any CPU {0F88C67F-34D2-4C68-B5BF-08A547D4CC2E}.Release|Any CPU.ActiveCfg = Release|Any CPU {0F88C67F-34D2-4C68-B5BF-08A547D4CC2E}.Release|Any CPU.Build.0 = Release|Any CPU + {CFB35402-69EB-448F-82B7-2D284730B0A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CFB35402-69EB-448F-82B7-2D284730B0A6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CFB35402-69EB-448F-82B7-2D284730B0A6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CFB35402-69EB-448F-82B7-2D284730B0A6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Azure.AI.Personalizer.csproj b/sdk/personalizer/Azure.AI.Personalizer/src/Azure.AI.Personalizer.csproj index 56be15a9d198..e08b8fa5a7c0 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Azure.AI.Personalizer.csproj +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Azure.AI.Personalizer.csproj @@ -1,4 +1,4 @@ - + Microsoft Azure.AI.Personalizer client library 2.0.0-beta.2 @@ -32,6 +32,8 @@ + + diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerRankResult.Serialization.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerRankResult.Serialization.cs index 4f513e9a97ba..7a6eb075af4c 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerRankResult.Serialization.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerRankResult.Serialization.cs @@ -18,32 +18,62 @@ internal static PersonalizerRankResult DeserializePersonalizerRankResult(JsonEle Optional> ranking = default; Optional eventId = default; Optional rewardActionId = default; - foreach (var property in element.EnumerateObject()) + if (element.ValueKind == JsonValueKind.Object) { - if (property.NameEquals("ranking")) + foreach (var property in element.EnumerateObject()) { - if (property.Value.ValueKind == JsonValueKind.Null) + if (property.NameEquals("ranking")) { - property.ThrowNonNullablePropertyIsNull(); + if (property.Value.ValueKind == JsonValueKind.Null) + { + property.ThrowNonNullablePropertyIsNull(); + continue; + } + List array = new List(); + foreach (var item in property.Value.EnumerateArray()) + { + array.Add(PersonalizerRankedAction.DeserializePersonalizerRankedAction(item)); + } + ranking = array; continue; } - List array = new List(); - foreach (var item in property.Value.EnumerateArray()) + if (property.NameEquals("eventId")) { - array.Add(PersonalizerRankedAction.DeserializePersonalizerRankedAction(item)); + eventId = property.Value.GetString(); + continue; + } + if (property.NameEquals("rewardActionId")) + { + rewardActionId = property.Value.GetString(); + continue; } - ranking = array; - continue; - } - if (property.NameEquals("eventId")) - { - eventId = property.Value.GetString(); - continue; } - if (property.NameEquals("rewardActionId")) + } + else if (element.ValueKind == JsonValueKind.Array) + { + // TODO: This part is not verified! + foreach (var property in element.EnumerateArray()) { - rewardActionId = property.Value.GetString(); - continue; + if (property.Equals("ranking")) + { + List array = new List(); + foreach (var item in property.EnumerateArray()) + { + array.Add(PersonalizerRankedAction.DeserializePersonalizerRankedAction(item)); + } + ranking = array; + continue; + } + if (property.Equals("eventId")) + { + eventId = property.GetString(); + continue; + } + if (property.Equals("rewardActionId")) + { + rewardActionId = property.GetString(); + continue; + } } } return new PersonalizerRankResult(Optional.ToList(ranking), eventId.Value, rewardActionId.Value); diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs index 5691244d0075..fda9873265d7 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs @@ -11,6 +11,7 @@ using Azure; using Azure.Core; using Azure.Core.Pipeline; +using Rl.Net; namespace Azure.AI.Personalizer { @@ -20,6 +21,8 @@ internal partial class MultiSlotClient private readonly ClientDiagnostics _clientDiagnostics; private readonly HttpPipeline _pipeline; private readonly bool _isLocalInference; + private readonly RankProcessor _rankProcessor; + internal MultiSlotRestClient RestClient { get; } /// Initializes a new instance of MultiSlotClient for mocking. @@ -53,11 +56,15 @@ public MultiSlotClient(string endpoint, TokenCredential credential, Personalizer /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. /// A flag to determine whether to use local inference. + /// A configuration to use local reference. /// The options for configuring the client. - public MultiSlotClient(string endpoint, TokenCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) : + public MultiSlotClient(string endpoint, TokenCredential credential, bool isLocalInference, Configuration configuration, PersonalizerClientOptions options = null) : this(endpoint, credential, options) { _isLocalInference = isLocalInference; + LiveModel liveModel = new LiveModel(configuration); + liveModel.Init(); + _rankProcessor = new RankProcessor(liveModel); } /// Initializes a new instance of MultiSlotClient. @@ -85,11 +92,15 @@ public MultiSlotClient(string endpoint, AzureKeyCredential credential, Personali /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. /// A flag to determine whether to use local inference. + /// A configuration to use local reference. /// The options for configuring the client. - public MultiSlotClient(string endpoint, AzureKeyCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) : + public MultiSlotClient(string endpoint, AzureKeyCredential credential, bool isLocalInference, Configuration configuration, PersonalizerClientOptions options = null) : this(endpoint, credential, options) { _isLocalInference = isLocalInference; + LiveModel liveModel = new LiveModel(configuration); + liveModel.Init(); + _rankProcessor = new RankProcessor(liveModel); } /// Initializes a new instance of MultiSlotClient. diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs index d0473d9cf981..625328bb08a3 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs @@ -11,6 +11,7 @@ using Azure; using Azure.Core; using Azure.Core.Pipeline; +using Rl.Net; namespace Azure.AI.Personalizer { @@ -20,6 +21,7 @@ internal partial class RankClient private readonly ClientDiagnostics _clientDiagnostics; private readonly HttpPipeline _pipeline; private readonly bool _isLocalInference; + private readonly RankProcessor _rankProcessor; internal RankRestClient RestClient { get; } /// Initializes a new instance of RankClient for mocking. @@ -53,11 +55,15 @@ public RankClient(string endpoint, TokenCredential credential, PersonalizerClien /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. /// A flag to determine whether to use local inference. + /// A configuration to use local reference. /// The options for configuring the client. - public RankClient(string endpoint, TokenCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) : + public RankClient(string endpoint, TokenCredential credential, bool isLocalInference, Configuration configuration, PersonalizerClientOptions options = null) : this(endpoint, credential, options) { _isLocalInference = isLocalInference; + LiveModel liveModel = new LiveModel(configuration); + liveModel.Init(); + _rankProcessor = new RankProcessor(liveModel); } /// Initializes a new instance of RankClient. @@ -85,11 +91,16 @@ public RankClient(string endpoint, AzureKeyCredential credential, PersonalizerCl /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. /// A flag to determine whether to use local reference. + /// A configuration to use local reference. /// The options for configuring the client. - public RankClient(string endpoint, AzureKeyCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) : + public RankClient(string endpoint, AzureKeyCredential credential, bool isLocalInference, Configuration configuration, PersonalizerClientOptions options = null) : this(endpoint, credential, options) { _isLocalInference = isLocalInference; + LiveModel liveModel = new LiveModel(configuration); + liveModel.Init(); + _rankProcessor = new RankProcessor(liveModel); + } /// Initializes a new instance of RankClient. @@ -112,7 +123,14 @@ public virtual async Task> RankAsync(Personaliz scope.Start(); try { - return await RestClient.RankAsync(rankRequest, cancellationToken).ConfigureAwait(false); + if (_isLocalInference) + { + return _rankProcessor.Rank(rankRequest, cancellationToken); + } + else + { + return await RestClient.RankAsync(rankRequest, cancellationToken).ConfigureAwait(false); + } } catch (Exception e) { diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankProcessor.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankProcessor.cs new file mode 100644 index 000000000000..e5409cc726a2 --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankProcessor.cs @@ -0,0 +1,53 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System.Collections.Generic; +using System.Linq; +using System.Text.Json; +using System.Threading; +using Newtonsoft.Json; +using Rl.Net; + +namespace Azure.AI.Personalizer +{ + /// The Rank Processor. + internal partial class RankProcessor + { + private readonly LiveModel _liveModel; + internal PolicyRestClient RestClient { get; } + + /// Initializes a new instance of RankProcessor. + public RankProcessor(LiveModel liveModel) + { + this._liveModel = liveModel; + } + + /// 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. + /// A Personalizer Rank request. + /// The cancellation token to use. + public Response Rank(PersonalizerRankOptions options, CancellationToken cancellationToken = default) + { + // Remove excluded actions in options + HashSet excludedSet = new HashSet(options.ExcludedActions); + options.Actions = options.Actions.Where(action => !excludedSet.Contains(action.Id)); + + // Convert options to the compatible parameter for ChooseRank + DecisionContext decisionContext = new DecisionContext(options); + var contextJson = JsonConvert.SerializeObject(decisionContext); + ActionFlags flags = options.DeferActivation == true ? ActionFlags.Deferred : ActionFlags.Default; + + // Call ChooseRank of local RL.Net + RankingResponse rankingResponse = _liveModel.ChooseRank(options.EventId, contextJson, flags); + + // Convert response to PersonalizerRankResult + var responseJson = JsonConvert.SerializeObject(rankingResponse); + var responseDocument = JsonDocument.Parse(responseJson); + var value = PersonalizerRankResult.DeserializePersonalizerRankResult(responseDocument.RootElement); + return Response.FromValue(value, default); + } + } +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContext.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContext.cs new file mode 100644 index 000000000000..7b0144e43ec7 --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContext.cs @@ -0,0 +1,75 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Newtonsoft.Json; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; + +namespace Azure.AI.Personalizer +{ + /// The Decision Context. + public class DecisionContext + { + /// The Decision Context. + public DecisionContext() + { + } + + /// Initializes a new instance of DecisionContext. + /// Personalizer Rank Options + public DecisionContext(PersonalizerRankOptions rankRequest) + { + List jsonFeatures = rankRequest.ContextFeatures.Select(f => JsonConvert.SerializeObject(f)).ToList(); + this.SharedFromUrl = jsonFeatures; + + this.Documents = rankRequest.Actions + .Select(action => + { + string ids = null; + List jsonFeatures = action.Features.Select(f => JsonConvert.SerializeObject(f)).ToList(); + + //if (action.Ids != null) + //{ + // ids = string.Join(",", action.Ids); + //} + + var doc = new DecisionContextDocument + { + ID = ids, + JSON = jsonFeatures, + }; + + //if (action.ActionSet != null && action.ActionSet?.Id != null) + // doc.Source = new DecisionContextDocumentSource + // { + // Set = action.ActionSet.Id.Id, + // Parameter = action.ActionSet.Parameter + // }; + + return doc; + }).ToArray(); + + //this.Slots = decisionRequest.Slots? + // .Select(slot => new DecisionContextDocument { + // SlotId = slot.Id, + // SlotJson = slot.JsonFeatures + // }).ToArray(); + } + + /// Properties from url + [JsonProperty("FromUrl", NullValueHandling = NullValueHandling.Ignore)] + [JsonConverter(typeof(JsonRawStringListConverter))] +#pragma warning disable CA2227 // Collection properties should be read only + public List SharedFromUrl { get; set; } +#pragma warning restore CA2227 // Collection properties should be read only + + /// Properties of documents + [JsonProperty("_multi")] + public DecisionContextDocument[] Documents { get; set; } + + /// Properties of slots + [JsonProperty("_slots", NullValueHandling = NullValueHandling.Ignore)] + public DecisionContextDocument[] Slots { get; set; } + } +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocument.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocument.cs new file mode 100644 index 000000000000..5ccc4f69dc12 --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocument.cs @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Newtonsoft.Json; +using System.Collections.Generic; + +namespace Azure.AI.Personalizer +{ + /// The Decision Context Document. + public class DecisionContextDocument + { + /// + /// Supply _tag for online evaluation (VW/EvalOperation.cs) + /// + [JsonProperty("_tag", NullValueHandling = NullValueHandling.Ignore)] + public string ID + { + get; + set; + } + + /// + /// Provide source set feature. + /// + [JsonProperty("s", NullValueHandling = NullValueHandling.Ignore)] + public DecisionContextDocumentSource Source { get; set; } + + /// + /// Generic json features. + /// + [JsonProperty("j", NullValueHandling = NullValueHandling.Ignore)] + [JsonConverter(typeof(JsonRawStringListConverter))] +#pragma warning disable CA2227 // Collection properties should be read only + public List JSON { get; set; } +#pragma warning restore CA2227 // Collection properties should be read only + + /// + /// Keep as float[] arrays to improve marshalling speed. + /// + [JsonProperty("f", NullValueHandling = NullValueHandling.Ignore)] + public Dictionary FloatFeatures { get; } + + /// + /// Slot ID. + /// + [JsonProperty("_id", NullValueHandling = NullValueHandling.Ignore)] + [JsonConverter(typeof(JsonRawStringListConverter))] + public string SlotId { get; set; } + + /// + /// Generic slot json features. + /// + [JsonProperty("sj", NullValueHandling = NullValueHandling.Ignore)] + [JsonConverter(typeof(JsonRawStringListConverter))] + public List SlotJson { get; } + } +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocumentSource.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocumentSource.cs new file mode 100644 index 000000000000..c61e1c4e8819 --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocumentSource.cs @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Newtonsoft.Json; + +namespace Azure.AI.Personalizer +{ + /// The Decision Context Document Source. + public class DecisionContextDocumentSource + { + /// The set. + [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] + public string Set { get; set; } + + /// The parameter. + [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] + public string Parameter { get; set; } + } +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs new file mode 100644 index 000000000000..9381df42de7b --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Threading; + +namespace Azure.AI.Personalizer +{ + /// The class for Dispose helper + public static class DisposeHelper + { + /// Safe dispose + /// Configuration. + public static void SafeDispose(ref TDisposable disposable) where TDisposable : class, IDisposable + { + IDisposable localDisposable = Interlocked.Exchange(ref disposable, null); + if (localDisposable != null) + { + localDisposable.Dispose(); + } + } + } +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/JsonRawStringListConverter.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/JsonRawStringListConverter.cs new file mode 100644 index 000000000000..951a05b4e084 --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/JsonRawStringListConverter.cs @@ -0,0 +1,59 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Newtonsoft.Json; +using System; +using System.Collections.Generic; + +namespace Azure.AI.Personalizer +{ + /// Json raw string list converter + internal class JsonRawStringListConverter : JsonConverter + { + /// + /// Supports string only. + /// + public override bool CanConvert(Type objectType) + { + return objectType == typeof(List); + } + + /// + /// Not implemented. + /// + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + throw new NotImplementedException(); + } + + /// + /// Outputs the string contents as JSON. + /// + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + var valueStringEnumerable = value as List; + if (valueStringEnumerable != null) + { + writer.WriteStartArray(); + foreach (var str in valueStringEnumerable) + writer.WriteRawValue(str); + writer.WriteEndArray(); + return; + } + + serializer.Serialize(writer, value); + } + + /// + /// List of independently parseable JSON fragments. + /// + public static IEnumerable JsonFragments(object value) + { + var valueStringList = value as List; + if (valueStringList == null) + throw new ArgumentException($"Unsupported type: {value}"); + + return valueStringList; + } + } +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs index f8f6c223eb18..fa10d7552440 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Azure.Core; using Azure.Core.Pipeline; +using Rl.Net; namespace Azure.AI.Personalizer { @@ -17,6 +18,8 @@ public class PersonalizerClient private readonly ClientDiagnostics _clientDiagnostics; private readonly HttpPipeline _pipeline; private readonly bool _isLocalInference; + private readonly RankProcessor _rankProcessor; + internal RankRestClient RankRestClient { get; set; } internal EventsRestClient EventsRestClient { get; set; } internal MultiSlotRestClient MultiSlotRestClient { get; set; } @@ -57,11 +60,15 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, Personalizer /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. /// A flag to determine whether to use local inference. + /// A configuration to use local reference. /// The options for configuring the client. - public PersonalizerClient(Uri endpoint, TokenCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) : + public PersonalizerClient(Uri endpoint, TokenCredential credential, bool isLocalInference, Configuration configuration, PersonalizerClientOptions options = null) : this(endpoint, credential, options) { _isLocalInference = isLocalInference; + LiveModel liveModel = new LiveModel(configuration); + liveModel.Init(); + _rankProcessor = new RankProcessor(liveModel); } /// Initializes a new instance of PersonalizerClient. @@ -98,11 +105,15 @@ public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, Personali /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. /// A flag to determine whether to use local inference. + /// A configuration to use local reference. /// The options for configuring the client. - public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) : + public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, bool isLocalInference, Configuration configuration, PersonalizerClientOptions options = null) : this(endpoint, credential, options) { _isLocalInference = isLocalInference; + LiveModel liveModel = new LiveModel(configuration); + liveModel.Init(); + _rankProcessor = new RankProcessor(liveModel); } /// Initializes a new instance of PersonalizerClient. @@ -134,7 +145,14 @@ public virtual async Task> RankAsync(Personaliz scope.Start(); try { - return await RankRestClient.RankAsync(options, cancellationToken).ConfigureAwait(false); + if (_isLocalInference) + { + return _rankProcessor.Rank(options, cancellationToken); + } + else + { + return await RankRestClient.RankAsync(options, cancellationToken).ConfigureAwait(false); + } } catch (Exception e) { @@ -177,7 +195,14 @@ public virtual Response Rank(PersonalizerRankOptions opt scope.Start(); try { - return RankRestClient.Rank(options, cancellationToken); + if (_isLocalInference) + { + return _rankProcessor.Rank(options, cancellationToken); + } + else + { + return RankRestClient.Rank(options, cancellationToken); + } } catch (Exception e) { diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerRankOptions.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerRankOptions.cs index f11a8d2b5037..3997161ca47d 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerRankOptions.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerRankOptions.cs @@ -27,7 +27,7 @@ public partial class PersonalizerRankOptions /// 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. /// - public IEnumerable Actions { get; } + public IEnumerable Actions { get; set; } /// /// The set of action ids to exclude from ranking. diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/RankProcessor.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/RankProcessor.cs new file mode 100644 index 000000000000..66dc3c4a8d93 --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/RankProcessor.cs @@ -0,0 +1,7 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +namespace Azure.AI.Personalizer +{ + internal partial class RankProcessor {} +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj b/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj index 67a686a2e33c..b3f25e8aca74 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj +++ b/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj @@ -4,6 +4,7 @@ $(NoWarn);CS1591 + $(NoWarn);CS8032 diff --git a/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs b/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs index a27c34534ee9..c3449904f375 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs @@ -4,6 +4,7 @@ using System; using System.Threading.Tasks; using Azure.Core.TestFramework; +using Rl.Net; namespace Azure.AI.Personalizer.Tests { @@ -18,10 +19,12 @@ public PersonalizerTestBase(bool isAsync): base(isAsync) Sanitizer = new PersonalizerRecordedTestSanitizer(); } - protected async Task GetPersonalizerClientAsync(bool isSingleSlot = false) + protected async Task GetPersonalizerClientAsync(bool isSingleSlot = false, bool isLocalInference = false) { - string endpoint = isSingleSlot ? TestEnvironment.SingleSlotEndpoint : TestEnvironment.MultiSlotEndpoint; - string apiKey = isSingleSlot ? TestEnvironment.SingleSlotApiKey : TestEnvironment.MultiSlotApiKey; + //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 ? "f9201cb5e8924b589c392ab202c3b56f" : TestEnvironment.MultiSlotApiKey; PersonalizerAdministrationClient adminClient = GetAdministrationClient(isSingleSlot); if (!isSingleSlot) { @@ -29,15 +32,48 @@ protected async Task 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) + { + 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); + } + else + { + personalizerClient = new PersonalizerClient(new Uri(endpoint), credential, options); + } + personalizerClient = InstrumentClient(personalizerClient); return personalizerClient; } protected PersonalizerAdministrationClient GetAdministrationClient(bool isSingleSlot = false) { - string endpoint = isSingleSlot ? TestEnvironment.SingleSlotEndpoint : TestEnvironment.MultiSlotEndpoint; - string apiKey = isSingleSlot ? TestEnvironment.SingleSlotApiKey : TestEnvironment.MultiSlotApiKey; + //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 ? "f9201cb5e8924b589c392ab202c3b56f" : TestEnvironment.MultiSlotApiKey; var credential = new AzureKeyCredential(apiKey); var options = InstrumentClientOptions(new PersonalizerClientOptions()); PersonalizerAdministrationClient personalizerAdministrationClient = new PersonalizerAdministrationClient(new Uri(endpoint), credential, options); diff --git a/sdk/personalizer/Azure.AI.Personalizer/tests/Personalizer/RankTests.cs b/sdk/personalizer/Azure.AI.Personalizer/tests/Personalizer/RankTests.cs index 259d24e9cdcc..8c9c9b37e03a 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/tests/Personalizer/RankTests.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/tests/Personalizer/RankTests.cs @@ -22,6 +22,15 @@ public async Task SingleSlotRankTests() await RankNullParameters(client); } + [Test] + public async Task SingleSlotRankLocalInferenceTests() + { + PersonalizerClient client = await GetPersonalizerClientAsync(isSingleSlot: true, isLocalInference: true); + //await RankNullParameters(client); + await RankServerFeaturesLocalInference(client); + //await RankNullParameters(client); + } + private async Task RankNullParameters(PersonalizerClient client) { IList actions = new List(); @@ -75,6 +84,39 @@ private async Task RankServerFeatures(PersonalizerClient client) } } + private async Task RankServerFeaturesLocalInference(PersonalizerClient client) + { + IList contextFeatures = new List() { + new { Features = new { day = "tuesday", time = "night", weather = "rainy" } }, + new { Features = new { userId = "1234", payingUser = true, favoriteGenre = "documentary", hoursOnSite = 0.12, lastwatchedType = "movie" } } + }; + IList actions = new List(); + actions.Add( + new PersonalizerRankableAction( + id: "Person1", + features: + new List() { new { videoType = "documentary", videoLength = 35, director = "CarlSagan" }, new { mostWatchedByAge = "30-35" } } + )); + actions.Add( + new PersonalizerRankableAction( + id: "Person2", + features: + new List() { new { videoType = "documentary", videoLength = 35, director = "CarlSagan" }, new { mostWatchedByAge = "40-45" } } + )); + IList excludeActions = new List { "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 RankWithNoOptions(PersonalizerClient client) { IList contextFeatures = new List() { From 1e400a8db978fc13d32387b945ca295b193536fe Mon Sep 17 00:00:00 2001 From: John Huang Date: Fri, 24 Dec 2021 01:57:30 -0800 Subject: [PATCH 03/22] Remove unused file --- .../src/Models/DisposeHelper.cs | 23 ------------------- .../tests/Azure.AI.Personalizer.Tests.csproj | 3 +++ 2 files changed, 3 insertions(+), 23 deletions(-) delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs deleted file mode 100644 index 9381df42de7b..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Threading; - -namespace Azure.AI.Personalizer -{ - /// The class for Dispose helper - public static class DisposeHelper - { - /// Safe dispose - /// Configuration. - public static void SafeDispose(ref TDisposable disposable) where TDisposable : class, IDisposable - { - IDisposable localDisposable = Interlocked.Exchange(ref disposable, null); - if (localDisposable != null) - { - localDisposable.Dispose(); - } - } - } -} diff --git a/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj b/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj index b3f25e8aca74..b39fb2167a97 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj +++ b/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj @@ -6,6 +6,9 @@ $(NoWarn);CS1591 $(NoWarn);CS8032 + + + From 8e1be9808bd40b10b60230e087b6c0184a3139f8 Mon Sep 17 00:00:00 2001 From: John Huang Date: Fri, 24 Dec 2021 02:27:14 -0800 Subject: [PATCH 04/22] Remove some key --- .../tests/Infrastructure/PersonalizerTestBase.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs b/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs index c3449904f375..542b4f275068 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs @@ -24,7 +24,7 @@ protected async Task 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 ? "f9201cb5e8924b589c392ab202c3b56f" : TestEnvironment.MultiSlotApiKey; + string apiKey = isSingleSlot ? "{personalizerApiKey}" : TestEnvironment.MultiSlotApiKey; PersonalizerAdministrationClient adminClient = GetAdministrationClient(isSingleSlot); if (!isSingleSlot) { @@ -73,7 +73,7 @@ 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 ? "f9201cb5e8924b589c392ab202c3b56f" : TestEnvironment.MultiSlotApiKey; + 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); From 7ec50ecb5aa202416da0d28f4950938824d392d8 Mon Sep 17 00:00:00 2001 From: John Huang Date: Thu, 6 Jan 2022 17:14:15 -0800 Subject: [PATCH 05/22] Toggle commented codes so that most tests can pass --- .../Infrastructure/PersonalizerTestBase.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs b/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs index 542b4f275068..3ec1205ad477 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs @@ -12,7 +12,7 @@ public abstract class PersonalizerTestBase : RecordedTestBase GetPersonalizerClientAsync(bool isSingleSlot = false, bool isLocalInference = false) { - //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; + 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) { @@ -70,10 +70,10 @@ protected async Task GetPersonalizerClientAsync(bool isSingl protected PersonalizerAdministrationClient GetAdministrationClient(bool isSingleSlot = false) { - //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; + 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); From 0d83604f6781344e51f0a45c1dea4341b279e57e Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Thu, 13 Jan 2022 13:58:42 -0800 Subject: [PATCH 06/22] Getting the required config details for rankprocessor to enable livemode --- .../Models/ClientConfigurationRestClient.cs | 84 +++++++++++++++++++ .../src/Models/PersonalizerClient.cs | 11 +++ ...sonalizerClientProperties.Serialization.cs | 50 +++++++++++ .../Models/PersonalizerClientProperties.cs | 60 +++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfigurationRestClient.cs create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfigurationRestClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfigurationRestClient.cs new file mode 100644 index 000000000000..b169533f85ce --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfigurationRestClient.cs @@ -0,0 +1,84 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using Azure.Core; +using Azure.Core.Pipeline; + +namespace Azure.AI.Personalizer.Models +{ + internal class ClientConfigurationRestClient + { + private string endpoint; + private ClientDiagnostics _clientDiagnostics; + private HttpPipeline _pipeline; + + /// Initializes a new instance of ClientConfigurationRestClient. + /// The handler for diagnostic messaging in the client. + /// The HTTP pipeline for sending and receiving REST requests and responses. + /// Supported Cognitive Services endpoint. + /// is null. + public ClientConfigurationRestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string endpoint) + { + this.endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint)); + _clientDiagnostics = clientDiagnostics; + _pipeline = pipeline; + } + internal HttpMessage CreatePostRequest() + { + 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("/configurations/client", false); + request.Uri = uri; + request.Headers.Add("Accept", "application/json"); + return message; + } + + /// Get the Personalizer service configuration. + /// The cancellation token to use. + public async Task> PostAsync(CancellationToken cancellationToken = default) + { + using var message = CreatePostRequest(); + await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); + switch (message.Response.Status) + { + case 200: + { + PersonalizerClientProperties value = default; + using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false); + value = PersonalizerClientProperties.DeserializePersonalizerServiceProperties(document.RootElement); + return Response.FromValue(value, message.Response); + } + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + + /// Get the Personalizer service configuration. + /// The cancellation token to use. + public Response Post(CancellationToken cancellationToken = default) + { + using var message = CreatePostRequest(); + _pipeline.Send(message, cancellationToken); + switch (message.Response.Status) + { + case 200: + { + PersonalizerClientProperties value = default; + using var document = JsonDocument.Parse(message.Response.ContentStream); + value = PersonalizerClientProperties.DeserializePersonalizerServiceProperties(document.RootElement); + return Response.FromValue(value, message.Response); + } + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + } +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs index fa10d7552440..ba369971c1d3 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs @@ -25,6 +25,8 @@ public class PersonalizerClient internal MultiSlotRestClient MultiSlotRestClient { get; set; } internal MultiSlotEventsRestClient MultiSlotEventsRestClient { get; set; } + internal Models.ClientConfigurationRestClient ClientConfigurationRestClient { get; set; } + /// Initializes a new instance of Personalizer Client for mocking. protected PersonalizerClient() { @@ -54,6 +56,7 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, Personalizer EventsRestClient = new EventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotRestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotEventsRestClient = new MultiSlotEventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); + ClientConfigurationRestClient = new Models.ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); } /// Initializes a new instance of PersonalizerClient. @@ -99,6 +102,7 @@ public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, Personali EventsRestClient = new EventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotRestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotEventsRestClient = new MultiSlotEventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); + ClientConfigurationRestClient = new Models.ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); } /// Initializes a new instance of PersonalizerClient. @@ -132,6 +136,7 @@ internal PersonalizerClient(ClientDiagnostics clientDiagnostics, HttpPipeline pi EventsRestClient = new EventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotRestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotEventsRestClient = new MultiSlotEventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); + ClientConfigurationRestClient = new Models.ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); _clientDiagnostics = clientDiagnostics; _pipeline = pipeline; } @@ -151,6 +156,8 @@ public virtual async Task> 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); } } @@ -182,6 +189,8 @@ public virtual async Task> RankAsync(Personaliz /// The cancellation token to use. public virtual async Task> RankAsync(IEnumerable actions, IEnumerable 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); } @@ -201,6 +210,8 @@ public virtual Response 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); } } diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs new file mode 100644 index 000000000000..8c0db530150c --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs @@ -0,0 +1,50 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Personalizer.Models +{ + internal partial class PersonalizerClientProperties + { + internal static PersonalizerClientProperties DeserializePersonalizerServiceProperties(JsonElement element) + { + string applicationID = default; + string modelBlobUri = default; + float initialExplorationEpsilon = default; + PersonalizerLearningMode learningMode = default; + string initialCommandLine = default; + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("applicationID")) + { + applicationID = property.Value.GetString(); + continue; + } + if (property.NameEquals("modelBlobUri")) + { + modelBlobUri = property.Value.GetString(); + continue; + } + if (property.NameEquals("initialExplorationEpsilon")) + { + initialExplorationEpsilon = property.Value.GetSingle(); + continue; + } + if (property.NameEquals("learningMode")) + { + learningMode = new PersonalizerLearningMode(property.Value.GetString()); + continue; + } + if (property.NameEquals("initialCommandLine")) + { + learningMode = new PersonalizerLearningMode(property.Value.GetString()); + continue; + } + } + return new PersonalizerClientProperties(applicationID, modelBlobUri, initialExplorationEpsilon, learningMode, initialCommandLine); + } + } +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs new file mode 100644 index 000000000000..3571c63adcd7 --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs @@ -0,0 +1,60 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.Text; + +namespace Azure.AI.Personalizer.Models +{ + internal partial class PersonalizerClientProperties + { + internal PersonalizerClientProperties(string applicationID, string modelBlobUri, float initialExplorationEpsilon, PersonalizerLearningMode learningMode, string initialCommandLine) + { + ApplicationID = applicationID; + ModelBlobUri = modelBlobUri; + InitialExplorationEpsilon = initialExplorationEpsilon; + LearningMode = learningMode; + InitialCommandLine = initialCommandLine; + } + /// + /// Unique identifier for this Personalizer instance. + /// + public string ApplicationID { get; } + + /// + /// Event hub connection string for sending interactions. + /// + public string EventHubInteractionConnectionString { get;} + + /// + /// Event hub connection string for sending observations. + /// + public string EventHubObservationConnectionString { get; } + + /// + /// SAS Uri for the inference model. + /// + public string ModelBlobUri { get; } + + /// + /// Exploration value used before downloading model in CB. + /// + public float InitialExplorationEpsilon { get; } + + /// + /// Learning Mode setting. + /// + public PersonalizerLearningMode LearningMode { get; } + + /// + /// Command line used for prediction before downloading model. + /// + public string InitialCommandLine { get; } + + /// + /// Version used by reinforcement learning client. + /// + public int ProtocolVersion { get; } + } +} From 7bef96ff5b376571343423e4f47fe1d3070ef479 Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Thu, 13 Jan 2022 14:25:08 -0800 Subject: [PATCH 07/22] Updating the wrong variable name --- .../src/Models/PersonalizerClientProperties.Serialization.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs index 8c0db530150c..7b58d4c6a78e 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs @@ -40,7 +40,7 @@ internal static PersonalizerClientProperties DeserializePersonalizerServicePrope } if (property.NameEquals("initialCommandLine")) { - learningMode = new PersonalizerLearningMode(property.Value.GetString()); + initialCommandLine = property.Value.GetString(); continue; } } From db8c651723ce398238a6807f50df0294e09eace6 Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Fri, 14 Jan 2022 09:59:08 -0800 Subject: [PATCH 08/22] Cleanup --- .../src/Models/PersonalizerClient.cs | 53 +++++++++++++------ .../Infrastructure/PersonalizerTestBase.cs | 28 +--------- 2 files changed, 37 insertions(+), 44 deletions(-) diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs index ba369971c1d3..e4961a27ae8b 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Threading; using System.Threading.Tasks; using Azure.Core; @@ -26,6 +27,7 @@ public class PersonalizerClient internal MultiSlotEventsRestClient MultiSlotEventsRestClient { get; set; } internal Models.ClientConfigurationRestClient ClientConfigurationRestClient { get; set; } + internal Lazy result { get; set; } /// Initializes a new instance of Personalizer Client for mocking. protected PersonalizerClient() @@ -63,15 +65,17 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, Personalizer /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. /// A flag to determine whether to use local inference. - /// A configuration to use local reference. /// The options for configuring the client. - 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); + } } /// Initializes a new instance of PersonalizerClient. @@ -109,15 +113,17 @@ public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, Personali /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. /// A flag to determine whether to use local inference. - /// A configuration to use local reference. /// The options for configuring the client. - 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); + } } /// Initializes a new instance of PersonalizerClient. @@ -156,8 +162,6 @@ public virtual async Task> 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); } } @@ -189,8 +193,6 @@ public virtual async Task> RankAsync(Personaliz /// The cancellation token to use. public virtual async Task> RankAsync(IEnumerable actions, IEnumerable 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); } @@ -210,9 +212,7 @@ public virtual Response 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) @@ -518,5 +518,24 @@ public virtual Response ActivateMultiSlot(string eventId, CancellationToken canc throw; } } + + /// Gets the configuration details for the live model to use + internal Configuration GetClientConfigurationForLiveModel() + { + result = new Lazy(() => 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; + } } } diff --git a/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs b/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs index 3ec1205ad477..5e405912c32e 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs @@ -23,8 +23,6 @@ protected async Task 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) { @@ -35,29 +33,7 @@ protected async Task 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 { @@ -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); From d6df6b5a86d302b624df2978b8629f330f145ea3 Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Fri, 14 Jan 2022 11:01:14 -0800 Subject: [PATCH 09/22] Revert "12767907: Add a package dependency on Microsoft.RL" This reverts commit c5869208c68ea592562e6d05278bfce26ac748de. --- NuGet.Config | 1 - 1 file changed, 1 deletion(-) diff --git a/NuGet.Config b/NuGet.Config index 7b6da4adea45..be103949f661 100644 --- a/NuGet.Config +++ b/NuGet.Config @@ -10,7 +10,6 @@ Used for azure-sdk-tools repo until issue https://github.com/Azure/azure-sdk-tools/issues/1329 is addressed --> - From 70c9738424080580639d7443f8335489f5d8a4aa Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Fri, 14 Jan 2022 11:26:41 -0800 Subject: [PATCH 10/22] Revert "Add RankProcessor" This reverts commit 43967d3a10108da239119b6a06e889efcade6c75. --- .../src/Azure.Core.TestFramework.csproj | 2 - sdk/personalizer/Azure.AI.Personalizer.sln | 10 +-- .../src/Azure.AI.Personalizer.csproj | 4 +- .../PersonalizerRankResult.Serialization.cs | 64 +++++----------- .../src/Generated/MultiSlotClient.cs | 15 +--- .../src/Generated/RankClient.cs | 24 +----- .../src/Generated/RankProcessor.cs | 53 ------------- .../src/Models/DecisionContext.cs | 75 ------------------- .../src/Models/DecisionContextDocument.cs | 57 -------------- .../Models/DecisionContextDocumentSource.cs | 19 ----- .../src/Models/JsonRawStringListConverter.cs | 59 --------------- .../src/Models/PersonalizerClient.cs | 21 +----- .../src/Models/PersonalizerRankOptions.cs | 2 +- .../src/Models/RankProcessor.cs | 7 -- .../tests/Azure.AI.Personalizer.Tests.csproj | 1 - .../Infrastructure/PersonalizerTestBase.cs | 3 +- .../tests/Personalizer/RankTests.cs | 42 ----------- 17 files changed, 31 insertions(+), 427 deletions(-) delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankProcessor.cs delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContext.cs delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocument.cs delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocumentSource.cs delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/JsonRawStringListConverter.cs delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/RankProcessor.cs diff --git a/sdk/core/Azure.Core.TestFramework/src/Azure.Core.TestFramework.csproj b/sdk/core/Azure.Core.TestFramework/src/Azure.Core.TestFramework.csproj index c805fd70cea1..46a5010dcedf 100644 --- a/sdk/core/Azure.Core.TestFramework/src/Azure.Core.TestFramework.csproj +++ b/sdk/core/Azure.Core.TestFramework/src/Azure.Core.TestFramework.csproj @@ -1,8 +1,6 @@  $(RequiredTargetFrameworks);net47 - $(NoWarn);SYSLIB0023 - $(NoWarn);CS8032 true diff --git a/sdk/personalizer/Azure.AI.Personalizer.sln b/sdk/personalizer/Azure.AI.Personalizer.sln index c0732360d38b..791da89ce047 100644 --- a/sdk/personalizer/Azure.AI.Personalizer.sln +++ b/sdk/personalizer/Azure.AI.Personalizer.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.32002.261 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31410.414 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.AI.Personalizer", "Azure.AI.Personalizer\src\Azure.AI.Personalizer.csproj", "{B940AE9B-814A-4A20-9C81-3B8E5345E3B4}" EndProject @@ -17,8 +17,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Core.TestFramework", "..\core\Azure.Core.TestFramework\src\Azure.Core.TestFramework.csproj", "{0F88C67F-34D2-4C68-B5BF-08A547D4CC2E}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Core", "..\core\Azure.Core\src\Azure.Core.csproj", "{CFB35402-69EB-448F-82B7-2D284730B0A6}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -37,10 +35,6 @@ Global {0F88C67F-34D2-4C68-B5BF-08A547D4CC2E}.Debug|Any CPU.Build.0 = Debug|Any CPU {0F88C67F-34D2-4C68-B5BF-08A547D4CC2E}.Release|Any CPU.ActiveCfg = Release|Any CPU {0F88C67F-34D2-4C68-B5BF-08A547D4CC2E}.Release|Any CPU.Build.0 = Release|Any CPU - {CFB35402-69EB-448F-82B7-2D284730B0A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CFB35402-69EB-448F-82B7-2D284730B0A6}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CFB35402-69EB-448F-82B7-2D284730B0A6}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CFB35402-69EB-448F-82B7-2D284730B0A6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Azure.AI.Personalizer.csproj b/sdk/personalizer/Azure.AI.Personalizer/src/Azure.AI.Personalizer.csproj index e08b8fa5a7c0..56be15a9d198 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Azure.AI.Personalizer.csproj +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Azure.AI.Personalizer.csproj @@ -1,4 +1,4 @@ - + Microsoft Azure.AI.Personalizer client library 2.0.0-beta.2 @@ -32,8 +32,6 @@ - - diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerRankResult.Serialization.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerRankResult.Serialization.cs index 7a6eb075af4c..4f513e9a97ba 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerRankResult.Serialization.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerRankResult.Serialization.cs @@ -18,62 +18,32 @@ internal static PersonalizerRankResult DeserializePersonalizerRankResult(JsonEle Optional> ranking = default; Optional eventId = default; Optional rewardActionId = default; - if (element.ValueKind == JsonValueKind.Object) + foreach (var property in element.EnumerateObject()) { - foreach (var property in element.EnumerateObject()) + if (property.NameEquals("ranking")) { - if (property.NameEquals("ranking")) + if (property.Value.ValueKind == JsonValueKind.Null) { - if (property.Value.ValueKind == JsonValueKind.Null) - { - property.ThrowNonNullablePropertyIsNull(); - continue; - } - List array = new List(); - foreach (var item in property.Value.EnumerateArray()) - { - array.Add(PersonalizerRankedAction.DeserializePersonalizerRankedAction(item)); - } - ranking = array; + property.ThrowNonNullablePropertyIsNull(); continue; } - if (property.NameEquals("eventId")) + List array = new List(); + foreach (var item in property.Value.EnumerateArray()) { - eventId = property.Value.GetString(); - continue; - } - if (property.NameEquals("rewardActionId")) - { - rewardActionId = property.Value.GetString(); - continue; + array.Add(PersonalizerRankedAction.DeserializePersonalizerRankedAction(item)); } + ranking = array; + continue; } - } - else if (element.ValueKind == JsonValueKind.Array) - { - // TODO: This part is not verified! - foreach (var property in element.EnumerateArray()) + if (property.NameEquals("eventId")) { - if (property.Equals("ranking")) - { - List array = new List(); - foreach (var item in property.EnumerateArray()) - { - array.Add(PersonalizerRankedAction.DeserializePersonalizerRankedAction(item)); - } - ranking = array; - continue; - } - if (property.Equals("eventId")) - { - eventId = property.GetString(); - continue; - } - if (property.Equals("rewardActionId")) - { - rewardActionId = property.GetString(); - continue; - } + eventId = property.Value.GetString(); + continue; + } + if (property.NameEquals("rewardActionId")) + { + rewardActionId = property.Value.GetString(); + continue; } } return new PersonalizerRankResult(Optional.ToList(ranking), eventId.Value, rewardActionId.Value); diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs index fda9873265d7..5691244d0075 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs @@ -11,7 +11,6 @@ using Azure; using Azure.Core; using Azure.Core.Pipeline; -using Rl.Net; namespace Azure.AI.Personalizer { @@ -21,8 +20,6 @@ internal partial class MultiSlotClient private readonly ClientDiagnostics _clientDiagnostics; private readonly HttpPipeline _pipeline; private readonly bool _isLocalInference; - private readonly RankProcessor _rankProcessor; - internal MultiSlotRestClient RestClient { get; } /// Initializes a new instance of MultiSlotClient for mocking. @@ -56,15 +53,11 @@ public MultiSlotClient(string endpoint, TokenCredential credential, Personalizer /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. /// A flag to determine whether to use local inference. - /// A configuration to use local reference. /// The options for configuring the client. - public MultiSlotClient(string endpoint, TokenCredential credential, bool isLocalInference, Configuration configuration, PersonalizerClientOptions options = null) : + public MultiSlotClient(string 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); } /// Initializes a new instance of MultiSlotClient. @@ -92,15 +85,11 @@ public MultiSlotClient(string endpoint, AzureKeyCredential credential, Personali /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. /// A flag to determine whether to use local inference. - /// A configuration to use local reference. /// The options for configuring the client. - public MultiSlotClient(string endpoint, AzureKeyCredential credential, bool isLocalInference, Configuration configuration, PersonalizerClientOptions options = null) : + public MultiSlotClient(string 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); } /// Initializes a new instance of MultiSlotClient. diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs index 625328bb08a3..d0473d9cf981 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs @@ -11,7 +11,6 @@ using Azure; using Azure.Core; using Azure.Core.Pipeline; -using Rl.Net; namespace Azure.AI.Personalizer { @@ -21,7 +20,6 @@ internal partial class RankClient private readonly ClientDiagnostics _clientDiagnostics; private readonly HttpPipeline _pipeline; private readonly bool _isLocalInference; - private readonly RankProcessor _rankProcessor; internal RankRestClient RestClient { get; } /// Initializes a new instance of RankClient for mocking. @@ -55,15 +53,11 @@ public RankClient(string endpoint, TokenCredential credential, PersonalizerClien /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. /// A flag to determine whether to use local inference. - /// A configuration to use local reference. /// The options for configuring the client. - public RankClient(string endpoint, TokenCredential credential, bool isLocalInference, Configuration configuration, PersonalizerClientOptions options = null) : + public RankClient(string 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); } /// Initializes a new instance of RankClient. @@ -91,16 +85,11 @@ public RankClient(string endpoint, AzureKeyCredential credential, PersonalizerCl /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. /// A flag to determine whether to use local reference. - /// A configuration to use local reference. /// The options for configuring the client. - public RankClient(string endpoint, AzureKeyCredential credential, bool isLocalInference, Configuration configuration, PersonalizerClientOptions options = null) : + public RankClient(string 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); - } /// Initializes a new instance of RankClient. @@ -123,14 +112,7 @@ public virtual async Task> RankAsync(Personaliz scope.Start(); try { - if (_isLocalInference) - { - return _rankProcessor.Rank(rankRequest, cancellationToken); - } - else - { - return await RestClient.RankAsync(rankRequest, cancellationToken).ConfigureAwait(false); - } + return await RestClient.RankAsync(rankRequest, cancellationToken).ConfigureAwait(false); } catch (Exception e) { diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankProcessor.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankProcessor.cs deleted file mode 100644 index e5409cc726a2..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankProcessor.cs +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System.Collections.Generic; -using System.Linq; -using System.Text.Json; -using System.Threading; -using Newtonsoft.Json; -using Rl.Net; - -namespace Azure.AI.Personalizer -{ - /// The Rank Processor. - internal partial class RankProcessor - { - private readonly LiveModel _liveModel; - internal PolicyRestClient RestClient { get; } - - /// Initializes a new instance of RankProcessor. - public RankProcessor(LiveModel liveModel) - { - this._liveModel = liveModel; - } - - /// 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. - /// A Personalizer Rank request. - /// The cancellation token to use. - public Response Rank(PersonalizerRankOptions options, CancellationToken cancellationToken = default) - { - // Remove excluded actions in options - HashSet excludedSet = new HashSet(options.ExcludedActions); - options.Actions = options.Actions.Where(action => !excludedSet.Contains(action.Id)); - - // Convert options to the compatible parameter for ChooseRank - DecisionContext decisionContext = new DecisionContext(options); - var contextJson = JsonConvert.SerializeObject(decisionContext); - ActionFlags flags = options.DeferActivation == true ? ActionFlags.Deferred : ActionFlags.Default; - - // Call ChooseRank of local RL.Net - RankingResponse rankingResponse = _liveModel.ChooseRank(options.EventId, contextJson, flags); - - // Convert response to PersonalizerRankResult - var responseJson = JsonConvert.SerializeObject(rankingResponse); - var responseDocument = JsonDocument.Parse(responseJson); - var value = PersonalizerRankResult.DeserializePersonalizerRankResult(responseDocument.RootElement); - return Response.FromValue(value, default); - } - } -} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContext.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContext.cs deleted file mode 100644 index 7b0144e43ec7..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContext.cs +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using Newtonsoft.Json; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; - -namespace Azure.AI.Personalizer -{ - /// The Decision Context. - public class DecisionContext - { - /// The Decision Context. - public DecisionContext() - { - } - - /// Initializes a new instance of DecisionContext. - /// Personalizer Rank Options - public DecisionContext(PersonalizerRankOptions rankRequest) - { - List jsonFeatures = rankRequest.ContextFeatures.Select(f => JsonConvert.SerializeObject(f)).ToList(); - this.SharedFromUrl = jsonFeatures; - - this.Documents = rankRequest.Actions - .Select(action => - { - string ids = null; - List jsonFeatures = action.Features.Select(f => JsonConvert.SerializeObject(f)).ToList(); - - //if (action.Ids != null) - //{ - // ids = string.Join(",", action.Ids); - //} - - var doc = new DecisionContextDocument - { - ID = ids, - JSON = jsonFeatures, - }; - - //if (action.ActionSet != null && action.ActionSet?.Id != null) - // doc.Source = new DecisionContextDocumentSource - // { - // Set = action.ActionSet.Id.Id, - // Parameter = action.ActionSet.Parameter - // }; - - return doc; - }).ToArray(); - - //this.Slots = decisionRequest.Slots? - // .Select(slot => new DecisionContextDocument { - // SlotId = slot.Id, - // SlotJson = slot.JsonFeatures - // }).ToArray(); - } - - /// Properties from url - [JsonProperty("FromUrl", NullValueHandling = NullValueHandling.Ignore)] - [JsonConverter(typeof(JsonRawStringListConverter))] -#pragma warning disable CA2227 // Collection properties should be read only - public List SharedFromUrl { get; set; } -#pragma warning restore CA2227 // Collection properties should be read only - - /// Properties of documents - [JsonProperty("_multi")] - public DecisionContextDocument[] Documents { get; set; } - - /// Properties of slots - [JsonProperty("_slots", NullValueHandling = NullValueHandling.Ignore)] - public DecisionContextDocument[] Slots { get; set; } - } -} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocument.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocument.cs deleted file mode 100644 index 5ccc4f69dc12..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocument.cs +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using Newtonsoft.Json; -using System.Collections.Generic; - -namespace Azure.AI.Personalizer -{ - /// The Decision Context Document. - public class DecisionContextDocument - { - /// - /// Supply _tag for online evaluation (VW/EvalOperation.cs) - /// - [JsonProperty("_tag", NullValueHandling = NullValueHandling.Ignore)] - public string ID - { - get; - set; - } - - /// - /// Provide source set feature. - /// - [JsonProperty("s", NullValueHandling = NullValueHandling.Ignore)] - public DecisionContextDocumentSource Source { get; set; } - - /// - /// Generic json features. - /// - [JsonProperty("j", NullValueHandling = NullValueHandling.Ignore)] - [JsonConverter(typeof(JsonRawStringListConverter))] -#pragma warning disable CA2227 // Collection properties should be read only - public List JSON { get; set; } -#pragma warning restore CA2227 // Collection properties should be read only - - /// - /// Keep as float[] arrays to improve marshalling speed. - /// - [JsonProperty("f", NullValueHandling = NullValueHandling.Ignore)] - public Dictionary FloatFeatures { get; } - - /// - /// Slot ID. - /// - [JsonProperty("_id", NullValueHandling = NullValueHandling.Ignore)] - [JsonConverter(typeof(JsonRawStringListConverter))] - public string SlotId { get; set; } - - /// - /// Generic slot json features. - /// - [JsonProperty("sj", NullValueHandling = NullValueHandling.Ignore)] - [JsonConverter(typeof(JsonRawStringListConverter))] - public List SlotJson { get; } - } -} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocumentSource.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocumentSource.cs deleted file mode 100644 index c61e1c4e8819..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DecisionContextDocumentSource.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using Newtonsoft.Json; - -namespace Azure.AI.Personalizer -{ - /// The Decision Context Document Source. - public class DecisionContextDocumentSource - { - /// The set. - [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] - public string Set { get; set; } - - /// The parameter. - [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] - public string Parameter { get; set; } - } -} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/JsonRawStringListConverter.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/JsonRawStringListConverter.cs deleted file mode 100644 index 951a05b4e084..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/JsonRawStringListConverter.cs +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using Newtonsoft.Json; -using System; -using System.Collections.Generic; - -namespace Azure.AI.Personalizer -{ - /// Json raw string list converter - internal class JsonRawStringListConverter : JsonConverter - { - /// - /// Supports string only. - /// - public override bool CanConvert(Type objectType) - { - return objectType == typeof(List); - } - - /// - /// Not implemented. - /// - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) - { - throw new NotImplementedException(); - } - - /// - /// Outputs the string contents as JSON. - /// - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) - { - var valueStringEnumerable = value as List; - if (valueStringEnumerable != null) - { - writer.WriteStartArray(); - foreach (var str in valueStringEnumerable) - writer.WriteRawValue(str); - writer.WriteEndArray(); - return; - } - - serializer.Serialize(writer, value); - } - - /// - /// List of independently parseable JSON fragments. - /// - public static IEnumerable JsonFragments(object value) - { - var valueStringList = value as List; - if (valueStringList == null) - throw new ArgumentException($"Unsupported type: {value}"); - - return valueStringList; - } - } -} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs index e4961a27ae8b..c7f68973615d 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs @@ -8,7 +8,6 @@ using System.Threading.Tasks; using Azure.Core; using Azure.Core.Pipeline; -using Rl.Net; namespace Azure.AI.Personalizer { @@ -19,8 +18,6 @@ public class PersonalizerClient private readonly ClientDiagnostics _clientDiagnostics; private readonly HttpPipeline _pipeline; private readonly bool _isLocalInference; - private readonly RankProcessor _rankProcessor; - internal RankRestClient RankRestClient { get; set; } internal EventsRestClient EventsRestClient { get; set; } internal MultiSlotRestClient MultiSlotRestClient { get; set; } @@ -70,12 +67,9 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, bool isLocal this(endpoint, credential, options) { _isLocalInference = isLocalInference; - if (isLocalInference) - { - LiveModel liveModel = new LiveModel(GetClientConfigurationForLiveModel()); - liveModel.Init(); - _rankProcessor = new RankProcessor(liveModel); - } + LiveModel liveModel = new LiveModel(configuration); + liveModel.Init(); + _rankProcessor = new RankProcessor(liveModel); } /// Initializes a new instance of PersonalizerClient. @@ -156,14 +150,7 @@ public virtual async Task> RankAsync(Personaliz scope.Start(); try { - if (_isLocalInference) - { - return _rankProcessor.Rank(options, cancellationToken); - } - else - { - return await RankRestClient.RankAsync(options, cancellationToken).ConfigureAwait(false); - } + return await RankRestClient.RankAsync(options, cancellationToken).ConfigureAwait(false); } catch (Exception e) { diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerRankOptions.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerRankOptions.cs index 3997161ca47d..f11a8d2b5037 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerRankOptions.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerRankOptions.cs @@ -27,7 +27,7 @@ public partial class PersonalizerRankOptions /// 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. /// - public IEnumerable Actions { get; set; } + public IEnumerable Actions { get; } /// /// The set of action ids to exclude from ranking. diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/RankProcessor.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/RankProcessor.cs deleted file mode 100644 index 66dc3c4a8d93..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/RankProcessor.cs +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -namespace Azure.AI.Personalizer -{ - internal partial class RankProcessor {} -} diff --git a/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj b/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj index b39fb2167a97..6149fe7a0b39 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj +++ b/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj @@ -4,7 +4,6 @@ $(NoWarn);CS1591 - $(NoWarn);CS8032 diff --git a/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs b/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs index 5e405912c32e..8d9eaedc015b 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs @@ -4,7 +4,6 @@ using System; using System.Threading.Tasks; using Azure.Core.TestFramework; -using Rl.Net; namespace Azure.AI.Personalizer.Tests { @@ -19,7 +18,7 @@ public PersonalizerTestBase(bool isAsync) : base(isAsync) Sanitizer = new PersonalizerRecordedTestSanitizer(); } - protected async Task GetPersonalizerClientAsync(bool isSingleSlot = false, bool isLocalInference = false) + protected async Task GetPersonalizerClientAsync(bool isSingleSlot = false) { string endpoint = isSingleSlot ? TestEnvironment.SingleSlotEndpoint : TestEnvironment.MultiSlotEndpoint; string apiKey = isSingleSlot ? TestEnvironment.SingleSlotApiKey : TestEnvironment.MultiSlotApiKey; diff --git a/sdk/personalizer/Azure.AI.Personalizer/tests/Personalizer/RankTests.cs b/sdk/personalizer/Azure.AI.Personalizer/tests/Personalizer/RankTests.cs index 8c9c9b37e03a..259d24e9cdcc 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/tests/Personalizer/RankTests.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/tests/Personalizer/RankTests.cs @@ -22,15 +22,6 @@ public async Task SingleSlotRankTests() await RankNullParameters(client); } - [Test] - public async Task SingleSlotRankLocalInferenceTests() - { - PersonalizerClient client = await GetPersonalizerClientAsync(isSingleSlot: true, isLocalInference: true); - //await RankNullParameters(client); - await RankServerFeaturesLocalInference(client); - //await RankNullParameters(client); - } - private async Task RankNullParameters(PersonalizerClient client) { IList actions = new List(); @@ -84,39 +75,6 @@ private async Task RankServerFeatures(PersonalizerClient client) } } - private async Task RankServerFeaturesLocalInference(PersonalizerClient client) - { - IList contextFeatures = new List() { - new { Features = new { day = "tuesday", time = "night", weather = "rainy" } }, - new { Features = new { userId = "1234", payingUser = true, favoriteGenre = "documentary", hoursOnSite = 0.12, lastwatchedType = "movie" } } - }; - IList actions = new List(); - actions.Add( - new PersonalizerRankableAction( - id: "Person1", - features: - new List() { new { videoType = "documentary", videoLength = 35, director = "CarlSagan" }, new { mostWatchedByAge = "30-35" } } - )); - actions.Add( - new PersonalizerRankableAction( - id: "Person2", - features: - new List() { new { videoType = "documentary", videoLength = 35, director = "CarlSagan" }, new { mostWatchedByAge = "40-45" } } - )); - IList excludeActions = new List { "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 RankWithNoOptions(PersonalizerClient client) { IList contextFeatures = new List() { From ebbbdad4f91252996d4651192c877ed9848625f6 Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Fri, 14 Jan 2022 11:30:20 -0800 Subject: [PATCH 11/22] Revert "Remove unused file" This reverts commit 1e400a8db978fc13d32387b945ca295b193536fe. --- .../src/Models/DisposeHelper.cs | 23 +++++++++++++++++++ .../tests/Azure.AI.Personalizer.Tests.csproj | 3 --- 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs new file mode 100644 index 000000000000..9381df42de7b --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Threading; + +namespace Azure.AI.Personalizer +{ + /// The class for Dispose helper + public static class DisposeHelper + { + /// Safe dispose + /// Configuration. + public static void SafeDispose(ref TDisposable disposable) where TDisposable : class, IDisposable + { + IDisposable localDisposable = Interlocked.Exchange(ref disposable, null); + if (localDisposable != null) + { + localDisposable.Dispose(); + } + } + } +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj b/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj index 6149fe7a0b39..67a686a2e33c 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj +++ b/sdk/personalizer/Azure.AI.Personalizer/tests/Azure.AI.Personalizer.Tests.csproj @@ -5,9 +5,6 @@ $(NoWarn);CS1591 - - - From 9856a35e4e99d21c57cca4c43b1d8131c6c6e521 Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Fri, 14 Jan 2022 11:34:13 -0800 Subject: [PATCH 12/22] Revert "Toggle commented codes so that most tests can pass" This reverts commit 7ec50ecb5aa202416da0d28f4950938824d392d8. --- .../tests/Infrastructure/PersonalizerTestBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs b/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs index 8d9eaedc015b..da4d4e85482e 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs @@ -11,7 +11,7 @@ public abstract class PersonalizerTestBase : RecordedTestBase Date: Fri, 14 Jan 2022 11:40:44 -0800 Subject: [PATCH 13/22] Delete DisposeHelper.cs --- .../src/Models/DisposeHelper.cs | 23 ------------------- 1 file changed, 23 deletions(-) delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs deleted file mode 100644 index 9381df42de7b..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/DisposeHelper.cs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Threading; - -namespace Azure.AI.Personalizer -{ - /// The class for Dispose helper - public static class DisposeHelper - { - /// Safe dispose - /// Configuration. - public static void SafeDispose(ref TDisposable disposable) where TDisposable : class, IDisposable - { - IDisposable localDisposable = Interlocked.Exchange(ref disposable, null); - if (localDisposable != null) - { - localDisposable.Dispose(); - } - } - } -} From 86532d389bca7a685b7270ed115c113ac190b4e4 Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Fri, 14 Jan 2022 12:11:08 -0800 Subject: [PATCH 14/22] Revert "Revert "12767907: Add a package dependency on Microsoft.RL"" This reverts commit d6df6b5a86d302b624df2978b8629f330f145ea3. --- NuGet.Config | 1 + 1 file changed, 1 insertion(+) diff --git a/NuGet.Config b/NuGet.Config index be103949f661..7b6da4adea45 100644 --- a/NuGet.Config +++ b/NuGet.Config @@ -10,6 +10,7 @@ Used for azure-sdk-tools repo until issue https://github.com/Azure/azure-sdk-tools/issues/1329 is addressed --> + From c37c4d304babd3c49bd6dd959f9778d3ed977f96 Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Fri, 14 Jan 2022 12:20:58 -0800 Subject: [PATCH 15/22] Cleanup after reverting --- .../src/Azure.AI.Personalizer.csproj | 1 + .../src/Models/PersonalizerClient.cs | 42 ++++++++++++------- .../Infrastructure/PersonalizerTestBase.cs | 4 +- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Azure.AI.Personalizer.csproj b/sdk/personalizer/Azure.AI.Personalizer/src/Azure.AI.Personalizer.csproj index 56be15a9d198..701ab2461555 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Azure.AI.Personalizer.csproj +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Azure.AI.Personalizer.csproj @@ -32,6 +32,7 @@ + diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs index c7f68973615d..4f6aedf30a54 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Azure.Core; using Azure.Core.Pipeline; +using Rl.Net; namespace Azure.AI.Personalizer { @@ -18,6 +19,7 @@ public class PersonalizerClient private readonly ClientDiagnostics _clientDiagnostics; private readonly HttpPipeline _pipeline; private readonly bool _isLocalInference; + internal RankRestClient RankRestClient { get; set; } internal EventsRestClient EventsRestClient { get; set; } internal MultiSlotRestClient MultiSlotRestClient { get; set; } @@ -67,15 +69,19 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, bool isLocal this(endpoint, credential, options) { _isLocalInference = isLocalInference; - LiveModel liveModel = new LiveModel(configuration); - liveModel.Init(); - _rankProcessor = new RankProcessor(liveModel); + if (isLocalInference) + { + //Intialize liveModel and call Rank processor + /*LiveModel liveModel = new LiveModel(GetClientConfigurationForLiveModel()); + liveModel.Init(); + _rankProcessor = new RankProcessor(liveModel);*/ + } } /// Initializes a new instance of PersonalizerClient. /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. - public PersonalizerClient(Uri endpoint, TokenCredential credential) : this(endpoint, credential, null){ } + public PersonalizerClient(Uri endpoint, TokenCredential credential) : this(endpoint, credential, null) { } /// Initializes a new instance of PersonalizerClient. /// Supported Cognitive Services endpoint. @@ -114,16 +120,17 @@ public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, bool isLo _isLocalInference = isLocalInference; if (isLocalInference) { - LiveModel liveModel = new LiveModel(GetClientConfigurationForLiveModel()); + //Intialize liveModel and Rankprocessor + /*LiveModel liveModel = new LiveModel(GetClientConfigurationForLiveModel()); liveModel.Init(); - _rankProcessor = new RankProcessor(liveModel); + _rankProcessor = new RankProcessor(liveModel);*/ } } /// Initializes a new instance of PersonalizerClient. /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. - public PersonalizerClient(Uri endpoint, AzureKeyCredential credential): this(endpoint, credential, null) { } + public PersonalizerClient(Uri endpoint, AzureKeyCredential credential) : this(endpoint, credential, null) { } /// Initializes a new instance of MultiSlotEventsClient. /// The handler for diagnostic messaging in the client. @@ -150,7 +157,15 @@ public virtual async Task> 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) { @@ -195,11 +210,12 @@ public virtual Response Rank(PersonalizerRankOptions opt { if (_isLocalInference) { - return _rankProcessor.Rank(options, cancellationToken); + //return RankProcessor result here + return null; } else { - return RankRestClient.Rank(options, cancellationToken); + return RankRestClient.Rank(options, cancellationToken); } } catch (Exception e) @@ -510,15 +526,13 @@ public virtual Response ActivateMultiSlot(string eventId, CancellationToken canc internal Configuration GetClientConfigurationForLiveModel() { result = new Lazy(() => 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["model.blob.uri"] = result.Value.ModelBlobUri; + config["vw.commandline"] = 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 diff --git a/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs b/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs index da4d4e85482e..d2604d28806f 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/tests/Infrastructure/PersonalizerTestBase.cs @@ -11,14 +11,14 @@ public abstract class PersonalizerTestBase : RecordedTestBase GetPersonalizerClientAsync(bool isSingleSlot = false) + protected async Task GetPersonalizerClientAsync(bool isSingleSlot = false, bool isLocalInference = false) { string endpoint = isSingleSlot ? TestEnvironment.SingleSlotEndpoint : TestEnvironment.MultiSlotEndpoint; string apiKey = isSingleSlot ? TestEnvironment.SingleSlotApiKey : TestEnvironment.MultiSlotApiKey; From 6b7569565cd7f64d754c81255f2b408283ba3afe Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Tue, 18 Jan 2022 08:02:07 -0800 Subject: [PATCH 16/22] generating client configuration using autorest --- .../Generated/ClientConfigurationClient.cs | 117 ++++++++++++++++++ .../ClientConfigurationRestClient.cs | 34 ++--- .../ClientConfiguration.Serialization.cs | 86 +++++++++++++ .../Generated/Models/ClientConfiguration.cs | 56 +++++++++ .../Generated/Models/PersonalizerErrorCode.cs | 16 ++- .../Models/PersonalizerEvaluationJobStatus.cs | 2 +- .../Models/PersonalizerEvaluationType.cs | 2 +- .../Models/PersonalizerLearningMode.cs | 2 +- .../Models/PersonalizerPolicySource.cs | 2 +- .../src/Generated/Models/ServiceStatus.cs | 3 + .../src/Generated/MultiSlotClient.cs | 23 ---- .../Generated/PersonalizerClientOptions.cs | 2 - .../src/Generated/PersonalizerModelFactory.cs | 15 +++ .../src/Generated/RankClient.cs | 23 ---- .../src/Models/ClientConfiguration.cs | 9 ++ .../src/Models/PersonalizerClient.cs | 30 ++--- ...sonalizerClientProperties.Serialization.cs | 50 -------- .../Models/PersonalizerClientProperties.cs | 60 --------- 18 files changed, 337 insertions(+), 195 deletions(-) create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Generated/ClientConfigurationClient.cs rename sdk/personalizer/Azure.AI.Personalizer/src/{Models => Generated}/ClientConfigurationRestClient.cs (72%) create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.Serialization.cs create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.cs create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfiguration.cs delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/ClientConfigurationClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/ClientConfigurationClient.cs new file mode 100644 index 000000000000..dbd0b9bd3f8d --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/ClientConfigurationClient.cs @@ -0,0 +1,117 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Threading; +using System.Threading.Tasks; +using Azure; +using Azure.Core; +using Azure.Core.Pipeline; + +namespace Azure.AI.Personalizer +{ + /// The ClientConfiguration service client. + public partial class ClientConfigurationClient + { + private readonly ClientDiagnostics _clientDiagnostics; + private readonly HttpPipeline _pipeline; + internal ClientConfigurationRestClient RestClient { get; } + + /// Initializes a new instance of ClientConfigurationClient for mocking. + protected ClientConfigurationClient() + { + } + + /// Initializes a new instance of ClientConfigurationClient. + /// Supported Cognitive Services endpoint. + /// A credential used to authenticate to an Azure Service. + /// The options for configuring the client. + public ClientConfigurationClient(string endpoint, TokenCredential credential, PersonalizerClientOptions options = null) + { + if (endpoint == null) + { + throw new ArgumentNullException(nameof(endpoint)); + } + if (credential == null) + { + throw new ArgumentNullException(nameof(credential)); + } + + options ??= new PersonalizerClientOptions(); + _clientDiagnostics = new ClientDiagnostics(options); + string[] scopes = { "https://cognitiveservices.azure.com/.default" }; + _pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, scopes)); + RestClient = new ClientConfigurationRestClient(_clientDiagnostics, _pipeline, endpoint); + } + + /// Initializes a new instance of ClientConfigurationClient. + /// Supported Cognitive Services endpoint. + /// A credential used to authenticate to an Azure Service. + /// The options for configuring the client. + public ClientConfigurationClient(string endpoint, AzureKeyCredential credential, PersonalizerClientOptions options = null) + { + if (endpoint == null) + { + throw new ArgumentNullException(nameof(endpoint)); + } + if (credential == null) + { + throw new ArgumentNullException(nameof(credential)); + } + + options ??= new PersonalizerClientOptions(); + _clientDiagnostics = new ClientDiagnostics(options); + _pipeline = HttpPipelineBuilder.Build(options, new AzureKeyCredentialPolicy(credential, "Ocp-Apim-Subscription-Key")); + RestClient = new ClientConfigurationRestClient(_clientDiagnostics, _pipeline, endpoint); + } + + /// Initializes a new instance of ClientConfigurationClient. + /// The handler for diagnostic messaging in the client. + /// The HTTP pipeline for sending and receiving REST requests and responses. + /// Supported Cognitive Services endpoint. + internal ClientConfigurationClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string endpoint) + { + RestClient = new ClientConfigurationRestClient(clientDiagnostics, pipeline, endpoint); + _clientDiagnostics = clientDiagnostics; + _pipeline = pipeline; + } + + /// Get configuration settings used in distributed Personalizer deployments. + /// The cancellation token to use. + public virtual async Task> ListAsync(CancellationToken cancellationToken = default) + { + using var scope = _clientDiagnostics.CreateScope("ClientConfigurationClient.List"); + scope.Start(); + try + { + return await RestClient.ListAsync(cancellationToken).ConfigureAwait(false); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } + } + + /// Get configuration settings used in distributed Personalizer deployments. + /// The cancellation token to use. + public virtual Response List(CancellationToken cancellationToken = default) + { + using var scope = _clientDiagnostics.CreateScope("ClientConfigurationClient.List"); + scope.Start(); + try + { + return RestClient.List(cancellationToken); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } + } + } +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfigurationRestClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/ClientConfigurationRestClient.cs similarity index 72% rename from sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfigurationRestClient.cs rename to sdk/personalizer/Azure.AI.Personalizer/src/Generated/ClientConfigurationRestClient.cs index b169533f85ce..46dfc94ba4eb 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfigurationRestClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/ClientConfigurationRestClient.cs @@ -1,16 +1,21 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +// + +#nullable disable + using System; using System.Text.Json; using System.Threading; using System.Threading.Tasks; +using Azure; using Azure.Core; using Azure.Core.Pipeline; -namespace Azure.AI.Personalizer.Models +namespace Azure.AI.Personalizer { - internal class ClientConfigurationRestClient + internal partial class ClientConfigurationRestClient { private string endpoint; private ClientDiagnostics _clientDiagnostics; @@ -27,7 +32,8 @@ public ClientConfigurationRestClient(ClientDiagnostics clientDiagnostics, HttpPi _clientDiagnostics = clientDiagnostics; _pipeline = pipeline; } - internal HttpMessage CreatePostRequest() + + internal HttpMessage CreateListRequest() { var message = _pipeline.CreateMessage(); var request = message.Request; @@ -41,19 +47,19 @@ internal HttpMessage CreatePostRequest() return message; } - /// Get the Personalizer service configuration. + /// Get configuration settings used in distributed Personalizer deployments. /// The cancellation token to use. - public async Task> PostAsync(CancellationToken cancellationToken = default) + public async Task> ListAsync(CancellationToken cancellationToken = default) { - using var message = CreatePostRequest(); + using var message = CreateListRequest(); await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); switch (message.Response.Status) { case 200: { - PersonalizerClientProperties value = default; + ClientConfiguration value = default; using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false); - value = PersonalizerClientProperties.DeserializePersonalizerServiceProperties(document.RootElement); + value = ClientConfiguration.DeserializeClientConfiguration(document.RootElement); return Response.FromValue(value, message.Response); } default: @@ -61,19 +67,19 @@ public async Task> PostAsync(Cancellation } } - /// Get the Personalizer service configuration. + /// Get configuration settings used in distributed Personalizer deployments. /// The cancellation token to use. - public Response Post(CancellationToken cancellationToken = default) + public Response List(CancellationToken cancellationToken = default) { - using var message = CreatePostRequest(); + using var message = CreateListRequest(); _pipeline.Send(message, cancellationToken); switch (message.Response.Status) { case 200: { - PersonalizerClientProperties value = default; + ClientConfiguration value = default; using var document = JsonDocument.Parse(message.Response.ContentStream); - value = PersonalizerClientProperties.DeserializePersonalizerServiceProperties(document.RootElement); + value = ClientConfiguration.DeserializeClientConfiguration(document.RootElement); return Response.FromValue(value, message.Response); } default: diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.Serialization.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.Serialization.cs new file mode 100644 index 000000000000..d362f4da7c49 --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.Serialization.cs @@ -0,0 +1,86 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Personalizer +{ + public partial class ClientConfiguration + { + internal static ClientConfiguration DeserializeClientConfiguration(JsonElement element) + { + Optional applicationID = default; + Optional eventHubInteractionConnectionString = default; + Optional eventHubObservationConnectionString = default; + Optional modelBlobUri = default; + Optional initialExplorationEpsilon = default; + Optional learningMode = default; + Optional initialCommandLine = default; + Optional protocolVersion = default; + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("applicationID")) + { + applicationID = property.Value.GetString(); + continue; + } + if (property.NameEquals("eventHubInteractionConnectionString")) + { + eventHubInteractionConnectionString = property.Value.GetString(); + continue; + } + if (property.NameEquals("eventHubObservationConnectionString")) + { + eventHubObservationConnectionString = property.Value.GetString(); + continue; + } + if (property.NameEquals("modelBlobUri")) + { + modelBlobUri = property.Value.GetString(); + continue; + } + if (property.NameEquals("initialExplorationEpsilon")) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + property.ThrowNonNullablePropertyIsNull(); + continue; + } + initialExplorationEpsilon = property.Value.GetSingle(); + continue; + } + if (property.NameEquals("learningMode")) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + property.ThrowNonNullablePropertyIsNull(); + continue; + } + learningMode = new PersonalizerLearningMode(property.Value.GetString()); + continue; + } + if (property.NameEquals("initialCommandLine")) + { + initialCommandLine = property.Value.GetString(); + continue; + } + if (property.NameEquals("protocolVersion")) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + property.ThrowNonNullablePropertyIsNull(); + continue; + } + protocolVersion = property.Value.GetInt32(); + continue; + } + } + return new ClientConfiguration(applicationID.Value, eventHubInteractionConnectionString.Value, eventHubObservationConnectionString.Value, modelBlobUri.Value, Optional.ToNullable(initialExplorationEpsilon), Optional.ToNullable(learningMode), initialCommandLine.Value, Optional.ToNullable(protocolVersion)); + } + } +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.cs new file mode 100644 index 000000000000..1f0bad1d0e6a --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.cs @@ -0,0 +1,56 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +namespace Azure.AI.Personalizer +{ + /// The ClientConfiguration. + public partial class ClientConfiguration + { + /// Initializes a new instance of ClientConfiguration. + internal ClientConfiguration() + { + } + + /// Initializes a new instance of ClientConfiguration. + /// Unique identifier for this Personalizer instance. + /// Event hub connection string for sending interactions. + /// Event hub connection string for sending observations. + /// SAS Uri for the inference model. + /// Exploration value used before downloading model in CB. + /// Learning Modes for Personalizer. + /// Command line used for prediction before downloading model. + /// Version used by reinforcement learning client. + internal ClientConfiguration(string applicationID, string eventHubInteractionConnectionString, string eventHubObservationConnectionString, string modelBlobUri, float? initialExplorationEpsilon, PersonalizerLearningMode? learningMode, string initialCommandLine, int? protocolVersion) + { + ApplicationID = applicationID; + EventHubInteractionConnectionString = eventHubInteractionConnectionString; + EventHubObservationConnectionString = eventHubObservationConnectionString; + ModelBlobUri = modelBlobUri; + InitialExplorationEpsilon = initialExplorationEpsilon; + LearningMode = learningMode; + InitialCommandLine = initialCommandLine; + ProtocolVersion = protocolVersion; + } + + /// Unique identifier for this Personalizer instance. + public string ApplicationID { get; } + /// Event hub connection string for sending interactions. + public string EventHubInteractionConnectionString { get; } + /// Event hub connection string for sending observations. + public string EventHubObservationConnectionString { get; } + /// SAS Uri for the inference model. + public string ModelBlobUri { get; } + /// Exploration value used before downloading model in CB. + public float? InitialExplorationEpsilon { get; } + /// Learning Modes for Personalizer. + public PersonalizerLearningMode? LearningMode { get; } + /// Command line used for prediction before downloading model. + public string InitialCommandLine { get; } + /// Version used by reinforcement learning client. + public int? ProtocolVersion { get; } + } +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerErrorCode.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerErrorCode.cs index 8fa9cdc7fb79..9efb336cc0fd 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerErrorCode.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerErrorCode.cs @@ -15,7 +15,7 @@ namespace Azure.AI.Personalizer.Models { private readonly string _value; - /// Determines if two values are the same. + /// Initializes a new instance of . /// is null. public PersonalizerErrorCode(string value) { @@ -35,12 +35,16 @@ public PersonalizerErrorCode(string value) private const string InvalidEventIdToActivateValue = "InvalidEventIdToActivate"; private const string InvalidRankRequestValue = "InvalidRankRequest"; private const string InvalidExportLogsRequestValue = "InvalidExportLogsRequest"; + private const string InvalidRequestValue = "InvalidRequest"; private const string InvalidContainerValue = "InvalidContainer"; private const string InvalidModelMetadataValue = "InvalidModelMetadata"; private const string ApprenticeModeNeverTurnedOnValue = "ApprenticeModeNeverTurnedOn"; private const string MissingAppIdValue = "MissingAppId"; private const string InvalidRewardWaitTimeValue = "InvalidRewardWaitTime"; + private const string InvalidLogRetentionDaysValue = "InvalidLogRetentionDays"; private const string InvalidMultiSlotApiAccessValue = "InvalidMultiSlotApiAccess"; + private const string PayloadSizeExceededValue = "PayloadSizeExceeded"; + private const string InvalidApiAccessValue = "InvalidApiAccess"; private const string ModelFileAccessDeniedValue = "ModelFileAccessDenied"; private const string ProblemTypeIncompatibleWithAutoOptimizationValue = "ProblemTypeIncompatibleWithAutoOptimization"; private const string ResourceNotFoundValue = "ResourceNotFound"; @@ -84,6 +88,8 @@ public PersonalizerErrorCode(string value) public static PersonalizerErrorCode InvalidRankRequest { get; } = new PersonalizerErrorCode(InvalidRankRequestValue); /// Invalid request. public static PersonalizerErrorCode InvalidExportLogsRequest { get; } = new PersonalizerErrorCode(InvalidExportLogsRequestValue); + /// Invalid request. + public static PersonalizerErrorCode InvalidRequest { get; } = new PersonalizerErrorCode(InvalidRequestValue); /// SAS Uri must be the Uri to a container that has write permissions. public static PersonalizerErrorCode InvalidContainer { get; } = new PersonalizerErrorCode(InvalidContainerValue); /// Invalid model metadata. @@ -92,10 +98,16 @@ public PersonalizerErrorCode(string value) public static PersonalizerErrorCode ApprenticeModeNeverTurnedOn { get; } = new PersonalizerErrorCode(ApprenticeModeNeverTurnedOnValue); /// AppId is missing in the header. public static PersonalizerErrorCode MissingAppId { get; } = new PersonalizerErrorCode(MissingAppIdValue); - /// Reward wait time should be between 5 seconds and 2 days. + /// Reward wait time should be either 10 minutes or 4 hours or 12 hours or 24 hours. public static PersonalizerErrorCode InvalidRewardWaitTime { get; } = new PersonalizerErrorCode(InvalidRewardWaitTimeValue); + /// Log Rention Days must be -1 to store indefinitely or must be at least reward wait time plus 1 day (rounded up). + public static PersonalizerErrorCode InvalidLogRetentionDays { get; } = new PersonalizerErrorCode(InvalidLogRetentionDaysValue); /// Multi-slot feature is currently disabled. Please follow multi-slot Personalizer documentation to update your loop settings to enable multi-slot functionality. public static PersonalizerErrorCode InvalidMultiSlotApiAccess { get; } = new PersonalizerErrorCode(InvalidMultiSlotApiAccessValue); + /// Exceeds maximum allowed payload size. + public static PersonalizerErrorCode PayloadSizeExceeded { get; } = new PersonalizerErrorCode(PayloadSizeExceededValue); + /// Api is currently disabled for the instance. + public static PersonalizerErrorCode InvalidApiAccess { get; } = new PersonalizerErrorCode(InvalidApiAccessValue); /// Key vault Key used for customer managed key cannot be accessed. public static PersonalizerErrorCode ModelFileAccessDenied { get; } = new PersonalizerErrorCode(ModelFileAccessDeniedValue); /// Auto-optimization is not compatible with multi-slot personalization. diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationJobStatus.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationJobStatus.cs index 085e5f329912..389201f0f3dd 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationJobStatus.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationJobStatus.cs @@ -15,7 +15,7 @@ namespace Azure.AI.Personalizer { private readonly string _value; - /// Determines if two values are the same. + /// Initializes a new instance of . /// is null. public PersonalizerEvaluationJobStatus(string value) { diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationType.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationType.cs index 9a313a1f08e8..96785da12f5e 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationType.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationType.cs @@ -15,7 +15,7 @@ namespace Azure.AI.Personalizer { private readonly string _value; - /// Determines if two values are the same. + /// Initializes a new instance of . /// is null. public PersonalizerEvaluationType(string value) { diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerLearningMode.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerLearningMode.cs index 8a4b6c377704..94137b19b95c 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerLearningMode.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerLearningMode.cs @@ -15,7 +15,7 @@ namespace Azure.AI.Personalizer { private readonly string _value; - /// Determines if two values are the same. + /// Initializes a new instance of . /// is null. public PersonalizerLearningMode(string value) { diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerPolicySource.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerPolicySource.cs index 0c7d1f64f6f7..905b6286b041 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerPolicySource.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerPolicySource.cs @@ -15,7 +15,7 @@ namespace Azure.AI.Personalizer { private readonly string _value; - /// Determines if two values are the same. + /// Initializes a new instance of . /// is null. public PersonalizerPolicySource(string value) { diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ServiceStatus.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ServiceStatus.cs index 2e47a345afb4..bec504a098b8 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ServiceStatus.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ServiceStatus.cs @@ -15,8 +15,11 @@ internal ServiceStatus() { } + /// Gets the service. public string Service { get; } + /// Gets the api status. public string ApiStatus { get; } + /// Gets the api status message. public string ApiStatusMessage { get; } } } diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs index 5691244d0075..87b0f06f7775 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs @@ -19,7 +19,6 @@ internal partial class MultiSlotClient { private readonly ClientDiagnostics _clientDiagnostics; private readonly HttpPipeline _pipeline; - private readonly bool _isLocalInference; internal MultiSlotRestClient RestClient { get; } /// Initializes a new instance of MultiSlotClient for mocking. @@ -49,17 +48,6 @@ public MultiSlotClient(string endpoint, TokenCredential credential, Personalizer RestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, endpoint); } - /// Initializes a new instance of PersonalizerClient. - /// Supported Cognitive Services endpoint. - /// A credential used to authenticate to an Azure Service. - /// A flag to determine whether to use local inference. - /// The options for configuring the client. - public MultiSlotClient(string endpoint, TokenCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) : - this(endpoint, credential, options) - { - _isLocalInference = isLocalInference; - } - /// Initializes a new instance of MultiSlotClient. /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. @@ -81,17 +69,6 @@ public MultiSlotClient(string endpoint, AzureKeyCredential credential, Personali RestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, endpoint); } - /// Initializes a new instance of PersonalizerClient. - /// Supported Cognitive Services endpoint. - /// A credential used to authenticate to an Azure Service. - /// A flag to determine whether to use local inference. - /// The options for configuring the client. - public MultiSlotClient(string endpoint, AzureKeyCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) : - this(endpoint, credential, options) - { - _isLocalInference = isLocalInference; - } - /// Initializes a new instance of MultiSlotClient. /// The handler for diagnostic messaging in the client. /// The HTTP pipeline for sending and receiving REST requests and responses. diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerClientOptions.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerClientOptions.cs index 643a4e1a15c8..2dfd7fc85d90 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerClientOptions.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerClientOptions.cs @@ -32,8 +32,6 @@ public PersonalizerClientOptions(ServiceVersion version = LatestVersion) ServiceVersion.V1_1_preview_1 => "1.1-preview.1", _ => throw new NotSupportedException() }; - - Retry.NetworkTimeout = TimeSpan.FromMinutes(8); } } } diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerModelFactory.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerModelFactory.cs index 3ea622c6496e..45bed2bb653e 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerModelFactory.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerModelFactory.cs @@ -14,6 +14,21 @@ namespace Azure.AI.Personalizer /// Model factory for read-only models. public static partial class PersonalizerModelFactory { + /// Initializes a new instance of ClientConfiguration. + /// Unique identifier for this Personalizer instance. + /// Event hub connection string for sending interactions. + /// Event hub connection string for sending observations. + /// SAS Uri for the inference model. + /// Exploration value used before downloading model in CB. + /// Learning Modes for Personalizer. + /// Command line used for prediction before downloading model. + /// Version used by reinforcement learning client. + /// A new instance for mocking. + public static ClientConfiguration ClientConfiguration(string applicationID = null, string eventHubInteractionConnectionString = null, string eventHubObservationConnectionString = null, string modelBlobUri = null, float? initialExplorationEpsilon = null, PersonalizerLearningMode? learningMode = null, string initialCommandLine = null, int? protocolVersion = null) + { + return new ClientConfiguration(applicationID, eventHubInteractionConnectionString, eventHubObservationConnectionString, modelBlobUri, initialExplorationEpsilon, learningMode, initialCommandLine, protocolVersion); + } + /// Initializes a new instance of PersonalizerEvaluation. /// The ID of the evaluation. /// The name of the evaluation. diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs index d0473d9cf981..956b7ff668de 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs @@ -19,7 +19,6 @@ internal partial class RankClient { private readonly ClientDiagnostics _clientDiagnostics; private readonly HttpPipeline _pipeline; - private readonly bool _isLocalInference; internal RankRestClient RestClient { get; } /// Initializes a new instance of RankClient for mocking. @@ -49,17 +48,6 @@ public RankClient(string endpoint, TokenCredential credential, PersonalizerClien RestClient = new RankRestClient(_clientDiagnostics, _pipeline, endpoint); } - /// Initializes a new instance of PersonalizerClient. - /// Supported Cognitive Services endpoint. - /// A credential used to authenticate to an Azure Service. - /// A flag to determine whether to use local inference. - /// The options for configuring the client. - public RankClient(string endpoint, TokenCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) : - this(endpoint, credential, options) - { - _isLocalInference = isLocalInference; - } - /// Initializes a new instance of RankClient. /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. @@ -81,17 +69,6 @@ public RankClient(string endpoint, AzureKeyCredential credential, PersonalizerCl RestClient = new RankRestClient(_clientDiagnostics, _pipeline, endpoint); } - /// Initializes a new instance of PersonalizerClient. - /// Supported Cognitive Services endpoint. - /// A credential used to authenticate to an Azure Service. - /// A flag to determine whether to use local reference. - /// The options for configuring the client. - public RankClient(string endpoint, AzureKeyCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) : - this(endpoint, credential, options) - { - _isLocalInference = isLocalInference; - } - /// Initializes a new instance of RankClient. /// The handler for diagnostic messaging in the client. /// The HTTP pipeline for sending and receiving REST requests and responses. diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfiguration.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfiguration.cs new file mode 100644 index 000000000000..972a4e92dabc --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfiguration.cs @@ -0,0 +1,9 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +using Azure.Core; + +namespace Azure.AI.Personalizer +{ + [CodeGenModel("ClientConfiguration")] + public partial class ClientConfiguration { } +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs index 4f6aedf30a54..f6fb6c356dec 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs @@ -25,8 +25,8 @@ public class PersonalizerClient internal MultiSlotRestClient MultiSlotRestClient { get; set; } internal MultiSlotEventsRestClient MultiSlotEventsRestClient { get; set; } - internal Models.ClientConfigurationRestClient ClientConfigurationRestClient { get; set; } - internal Lazy result { get; set; } + internal ClientConfigurationRestClient ClientConfigurationRestClient { get; set; } + internal ClientConfiguration result { get; set; } /// Initializes a new instance of Personalizer Client for mocking. protected PersonalizerClient() @@ -57,7 +57,7 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, Personalizer EventsRestClient = new EventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotRestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotEventsRestClient = new MultiSlotEventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); - ClientConfigurationRestClient = new Models.ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); + ClientConfigurationRestClient = new ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); } /// Initializes a new instance of PersonalizerClient. @@ -72,9 +72,7 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, bool isLocal if (isLocalInference) { //Intialize liveModel and call Rank processor - /*LiveModel liveModel = new LiveModel(GetClientConfigurationForLiveModel()); - liveModel.Init(); - _rankProcessor = new RankProcessor(liveModel);*/ + Configuration config = GetClientConfigurationForLiveModel(); } } @@ -106,7 +104,7 @@ public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, Personali EventsRestClient = new EventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotRestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotEventsRestClient = new MultiSlotEventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); - ClientConfigurationRestClient = new Models.ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); + ClientConfigurationRestClient = new ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); } /// Initializes a new instance of PersonalizerClient. @@ -121,9 +119,7 @@ public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, bool isLo if (isLocalInference) { //Intialize liveModel and Rankprocessor - /*LiveModel liveModel = new LiveModel(GetClientConfigurationForLiveModel()); - liveModel.Init(); - _rankProcessor = new RankProcessor(liveModel);*/ + Configuration config = GetClientConfigurationForLiveModel(); } } @@ -143,7 +139,7 @@ internal PersonalizerClient(ClientDiagnostics clientDiagnostics, HttpPipeline pi EventsRestClient = new EventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotRestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotEventsRestClient = new MultiSlotEventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); - ClientConfigurationRestClient = new Models.ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); + ClientConfigurationRestClient = new ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); _clientDiagnostics = clientDiagnostics; _pipeline = pipeline; } @@ -525,16 +521,16 @@ public virtual Response ActivateMultiSlot(string eventId, CancellationToken canc /// Gets the configuration details for the live model to use internal Configuration GetClientConfigurationForLiveModel() { - result = new Lazy(() => ClientConfigurationRestClient.Post()); + result = ClientConfigurationRestClient.List(); Configuration config = new Configuration(); // configure the personalizer loop - config["appid"] = result.Value.ApplicationID; + config["appid"] = result.ApplicationID; // set up the model - config["model.blob.uri"] = result.Value.ModelBlobUri; - config["vw.commandline"] = 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); + config["model.blob.uri"] = result.ModelBlobUri; + config["vw.commandline"] = result.InitialCommandLine; + config["initial_exploration.epsilon"] = Convert.ToString(result.InitialExplorationEpsilon, CultureInfo.InvariantCulture); + config["rank.learning.mode"] = Convert.ToString(result.LearningMode, CultureInfo.InvariantCulture); //return the config model return config; } diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs deleted file mode 100644 index 7b58d4c6a78e..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Text.Json; -using Azure.Core; - -namespace Azure.AI.Personalizer.Models -{ - internal partial class PersonalizerClientProperties - { - internal static PersonalizerClientProperties DeserializePersonalizerServiceProperties(JsonElement element) - { - string applicationID = default; - string modelBlobUri = default; - float initialExplorationEpsilon = default; - PersonalizerLearningMode learningMode = default; - string initialCommandLine = default; - foreach (var property in element.EnumerateObject()) - { - if (property.NameEquals("applicationID")) - { - applicationID = property.Value.GetString(); - continue; - } - if (property.NameEquals("modelBlobUri")) - { - modelBlobUri = property.Value.GetString(); - continue; - } - if (property.NameEquals("initialExplorationEpsilon")) - { - initialExplorationEpsilon = property.Value.GetSingle(); - continue; - } - if (property.NameEquals("learningMode")) - { - learningMode = new PersonalizerLearningMode(property.Value.GetString()); - continue; - } - if (property.NameEquals("initialCommandLine")) - { - initialCommandLine = property.Value.GetString(); - continue; - } - } - return new PersonalizerClientProperties(applicationID, modelBlobUri, initialExplorationEpsilon, learningMode, initialCommandLine); - } - } -} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs deleted file mode 100644 index 3571c63adcd7..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Text; - -namespace Azure.AI.Personalizer.Models -{ - internal partial class PersonalizerClientProperties - { - internal PersonalizerClientProperties(string applicationID, string modelBlobUri, float initialExplorationEpsilon, PersonalizerLearningMode learningMode, string initialCommandLine) - { - ApplicationID = applicationID; - ModelBlobUri = modelBlobUri; - InitialExplorationEpsilon = initialExplorationEpsilon; - LearningMode = learningMode; - InitialCommandLine = initialCommandLine; - } - /// - /// Unique identifier for this Personalizer instance. - /// - public string ApplicationID { get; } - - /// - /// Event hub connection string for sending interactions. - /// - public string EventHubInteractionConnectionString { get;} - - /// - /// Event hub connection string for sending observations. - /// - public string EventHubObservationConnectionString { get; } - - /// - /// SAS Uri for the inference model. - /// - public string ModelBlobUri { get; } - - /// - /// Exploration value used before downloading model in CB. - /// - public float InitialExplorationEpsilon { get; } - - /// - /// Learning Mode setting. - /// - public PersonalizerLearningMode LearningMode { get; } - - /// - /// Command line used for prediction before downloading model. - /// - public string InitialCommandLine { get; } - - /// - /// Version used by reinforcement learning client. - /// - public int ProtocolVersion { get; } - } -} From 1f0a1618047d14fd3441b37c2c00e3091bc33584 Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Thu, 20 Jan 2022 08:04:40 -0800 Subject: [PATCH 17/22] Revert "generating client configuration using autorest" This reverts commit 6b7569565cd7f64d754c81255f2b408283ba3afe. --- .../Generated/ClientConfigurationClient.cs | 117 ------------------ .../ClientConfiguration.Serialization.cs | 86 ------------- .../Generated/Models/ClientConfiguration.cs | 56 --------- .../Generated/Models/PersonalizerErrorCode.cs | 16 +-- .../Models/PersonalizerEvaluationJobStatus.cs | 2 +- .../Models/PersonalizerEvaluationType.cs | 2 +- .../Models/PersonalizerLearningMode.cs | 2 +- .../Models/PersonalizerPolicySource.cs | 2 +- .../src/Generated/Models/ServiceStatus.cs | 3 - .../src/Generated/MultiSlotClient.cs | 23 ++++ .../Generated/PersonalizerClientOptions.cs | 2 + .../src/Generated/PersonalizerModelFactory.cs | 15 --- .../src/Generated/RankClient.cs | 23 ++++ .../src/Models/ClientConfiguration.cs | 9 -- .../ClientConfigurationRestClient.cs | 34 +++-- .../src/Models/PersonalizerClient.cs | 30 +++-- ...sonalizerClientProperties.Serialization.cs | 50 ++++++++ .../Models/PersonalizerClientProperties.cs | 60 +++++++++ 18 files changed, 195 insertions(+), 337 deletions(-) delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Generated/ClientConfigurationClient.cs delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.Serialization.cs delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.cs delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfiguration.cs rename sdk/personalizer/Azure.AI.Personalizer/src/{Generated => Models}/ClientConfigurationRestClient.cs (72%) create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs create mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/ClientConfigurationClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/ClientConfigurationClient.cs deleted file mode 100644 index dbd0b9bd3f8d..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/ClientConfigurationClient.cs +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.Threading; -using System.Threading.Tasks; -using Azure; -using Azure.Core; -using Azure.Core.Pipeline; - -namespace Azure.AI.Personalizer -{ - /// The ClientConfiguration service client. - public partial class ClientConfigurationClient - { - private readonly ClientDiagnostics _clientDiagnostics; - private readonly HttpPipeline _pipeline; - internal ClientConfigurationRestClient RestClient { get; } - - /// Initializes a new instance of ClientConfigurationClient for mocking. - protected ClientConfigurationClient() - { - } - - /// Initializes a new instance of ClientConfigurationClient. - /// Supported Cognitive Services endpoint. - /// A credential used to authenticate to an Azure Service. - /// The options for configuring the client. - public ClientConfigurationClient(string endpoint, TokenCredential credential, PersonalizerClientOptions options = null) - { - if (endpoint == null) - { - throw new ArgumentNullException(nameof(endpoint)); - } - if (credential == null) - { - throw new ArgumentNullException(nameof(credential)); - } - - options ??= new PersonalizerClientOptions(); - _clientDiagnostics = new ClientDiagnostics(options); - string[] scopes = { "https://cognitiveservices.azure.com/.default" }; - _pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, scopes)); - RestClient = new ClientConfigurationRestClient(_clientDiagnostics, _pipeline, endpoint); - } - - /// Initializes a new instance of ClientConfigurationClient. - /// Supported Cognitive Services endpoint. - /// A credential used to authenticate to an Azure Service. - /// The options for configuring the client. - public ClientConfigurationClient(string endpoint, AzureKeyCredential credential, PersonalizerClientOptions options = null) - { - if (endpoint == null) - { - throw new ArgumentNullException(nameof(endpoint)); - } - if (credential == null) - { - throw new ArgumentNullException(nameof(credential)); - } - - options ??= new PersonalizerClientOptions(); - _clientDiagnostics = new ClientDiagnostics(options); - _pipeline = HttpPipelineBuilder.Build(options, new AzureKeyCredentialPolicy(credential, "Ocp-Apim-Subscription-Key")); - RestClient = new ClientConfigurationRestClient(_clientDiagnostics, _pipeline, endpoint); - } - - /// Initializes a new instance of ClientConfigurationClient. - /// The handler for diagnostic messaging in the client. - /// The HTTP pipeline for sending and receiving REST requests and responses. - /// Supported Cognitive Services endpoint. - internal ClientConfigurationClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string endpoint) - { - RestClient = new ClientConfigurationRestClient(clientDiagnostics, pipeline, endpoint); - _clientDiagnostics = clientDiagnostics; - _pipeline = pipeline; - } - - /// Get configuration settings used in distributed Personalizer deployments. - /// The cancellation token to use. - public virtual async Task> ListAsync(CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("ClientConfigurationClient.List"); - scope.Start(); - try - { - return await RestClient.ListAsync(cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Get configuration settings used in distributed Personalizer deployments. - /// The cancellation token to use. - public virtual Response List(CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("ClientConfigurationClient.List"); - scope.Start(); - try - { - return RestClient.List(cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - } -} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.Serialization.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.Serialization.cs deleted file mode 100644 index d362f4da7c49..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.Serialization.cs +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System.Text.Json; -using Azure.Core; - -namespace Azure.AI.Personalizer -{ - public partial class ClientConfiguration - { - internal static ClientConfiguration DeserializeClientConfiguration(JsonElement element) - { - Optional applicationID = default; - Optional eventHubInteractionConnectionString = default; - Optional eventHubObservationConnectionString = default; - Optional modelBlobUri = default; - Optional initialExplorationEpsilon = default; - Optional learningMode = default; - Optional initialCommandLine = default; - Optional protocolVersion = default; - foreach (var property in element.EnumerateObject()) - { - if (property.NameEquals("applicationID")) - { - applicationID = property.Value.GetString(); - continue; - } - if (property.NameEquals("eventHubInteractionConnectionString")) - { - eventHubInteractionConnectionString = property.Value.GetString(); - continue; - } - if (property.NameEquals("eventHubObservationConnectionString")) - { - eventHubObservationConnectionString = property.Value.GetString(); - continue; - } - if (property.NameEquals("modelBlobUri")) - { - modelBlobUri = property.Value.GetString(); - continue; - } - if (property.NameEquals("initialExplorationEpsilon")) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - property.ThrowNonNullablePropertyIsNull(); - continue; - } - initialExplorationEpsilon = property.Value.GetSingle(); - continue; - } - if (property.NameEquals("learningMode")) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - property.ThrowNonNullablePropertyIsNull(); - continue; - } - learningMode = new PersonalizerLearningMode(property.Value.GetString()); - continue; - } - if (property.NameEquals("initialCommandLine")) - { - initialCommandLine = property.Value.GetString(); - continue; - } - if (property.NameEquals("protocolVersion")) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - property.ThrowNonNullablePropertyIsNull(); - continue; - } - protocolVersion = property.Value.GetInt32(); - continue; - } - } - return new ClientConfiguration(applicationID.Value, eventHubInteractionConnectionString.Value, eventHubObservationConnectionString.Value, modelBlobUri.Value, Optional.ToNullable(initialExplorationEpsilon), Optional.ToNullable(learningMode), initialCommandLine.Value, Optional.ToNullable(protocolVersion)); - } - } -} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.cs deleted file mode 100644 index 1f0bad1d0e6a..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ClientConfiguration.cs +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -namespace Azure.AI.Personalizer -{ - /// The ClientConfiguration. - public partial class ClientConfiguration - { - /// Initializes a new instance of ClientConfiguration. - internal ClientConfiguration() - { - } - - /// Initializes a new instance of ClientConfiguration. - /// Unique identifier for this Personalizer instance. - /// Event hub connection string for sending interactions. - /// Event hub connection string for sending observations. - /// SAS Uri for the inference model. - /// Exploration value used before downloading model in CB. - /// Learning Modes for Personalizer. - /// Command line used for prediction before downloading model. - /// Version used by reinforcement learning client. - internal ClientConfiguration(string applicationID, string eventHubInteractionConnectionString, string eventHubObservationConnectionString, string modelBlobUri, float? initialExplorationEpsilon, PersonalizerLearningMode? learningMode, string initialCommandLine, int? protocolVersion) - { - ApplicationID = applicationID; - EventHubInteractionConnectionString = eventHubInteractionConnectionString; - EventHubObservationConnectionString = eventHubObservationConnectionString; - ModelBlobUri = modelBlobUri; - InitialExplorationEpsilon = initialExplorationEpsilon; - LearningMode = learningMode; - InitialCommandLine = initialCommandLine; - ProtocolVersion = protocolVersion; - } - - /// Unique identifier for this Personalizer instance. - public string ApplicationID { get; } - /// Event hub connection string for sending interactions. - public string EventHubInteractionConnectionString { get; } - /// Event hub connection string for sending observations. - public string EventHubObservationConnectionString { get; } - /// SAS Uri for the inference model. - public string ModelBlobUri { get; } - /// Exploration value used before downloading model in CB. - public float? InitialExplorationEpsilon { get; } - /// Learning Modes for Personalizer. - public PersonalizerLearningMode? LearningMode { get; } - /// Command line used for prediction before downloading model. - public string InitialCommandLine { get; } - /// Version used by reinforcement learning client. - public int? ProtocolVersion { get; } - } -} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerErrorCode.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerErrorCode.cs index 9efb336cc0fd..8fa9cdc7fb79 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerErrorCode.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerErrorCode.cs @@ -15,7 +15,7 @@ namespace Azure.AI.Personalizer.Models { private readonly string _value; - /// Initializes a new instance of . + /// Determines if two values are the same. /// is null. public PersonalizerErrorCode(string value) { @@ -35,16 +35,12 @@ public PersonalizerErrorCode(string value) private const string InvalidEventIdToActivateValue = "InvalidEventIdToActivate"; private const string InvalidRankRequestValue = "InvalidRankRequest"; private const string InvalidExportLogsRequestValue = "InvalidExportLogsRequest"; - private const string InvalidRequestValue = "InvalidRequest"; private const string InvalidContainerValue = "InvalidContainer"; private const string InvalidModelMetadataValue = "InvalidModelMetadata"; private const string ApprenticeModeNeverTurnedOnValue = "ApprenticeModeNeverTurnedOn"; private const string MissingAppIdValue = "MissingAppId"; private const string InvalidRewardWaitTimeValue = "InvalidRewardWaitTime"; - private const string InvalidLogRetentionDaysValue = "InvalidLogRetentionDays"; private const string InvalidMultiSlotApiAccessValue = "InvalidMultiSlotApiAccess"; - private const string PayloadSizeExceededValue = "PayloadSizeExceeded"; - private const string InvalidApiAccessValue = "InvalidApiAccess"; private const string ModelFileAccessDeniedValue = "ModelFileAccessDenied"; private const string ProblemTypeIncompatibleWithAutoOptimizationValue = "ProblemTypeIncompatibleWithAutoOptimization"; private const string ResourceNotFoundValue = "ResourceNotFound"; @@ -88,8 +84,6 @@ public PersonalizerErrorCode(string value) public static PersonalizerErrorCode InvalidRankRequest { get; } = new PersonalizerErrorCode(InvalidRankRequestValue); /// Invalid request. public static PersonalizerErrorCode InvalidExportLogsRequest { get; } = new PersonalizerErrorCode(InvalidExportLogsRequestValue); - /// Invalid request. - public static PersonalizerErrorCode InvalidRequest { get; } = new PersonalizerErrorCode(InvalidRequestValue); /// SAS Uri must be the Uri to a container that has write permissions. public static PersonalizerErrorCode InvalidContainer { get; } = new PersonalizerErrorCode(InvalidContainerValue); /// Invalid model metadata. @@ -98,16 +92,10 @@ public PersonalizerErrorCode(string value) public static PersonalizerErrorCode ApprenticeModeNeverTurnedOn { get; } = new PersonalizerErrorCode(ApprenticeModeNeverTurnedOnValue); /// AppId is missing in the header. public static PersonalizerErrorCode MissingAppId { get; } = new PersonalizerErrorCode(MissingAppIdValue); - /// Reward wait time should be either 10 minutes or 4 hours or 12 hours or 24 hours. + /// Reward wait time should be between 5 seconds and 2 days. public static PersonalizerErrorCode InvalidRewardWaitTime { get; } = new PersonalizerErrorCode(InvalidRewardWaitTimeValue); - /// Log Rention Days must be -1 to store indefinitely or must be at least reward wait time plus 1 day (rounded up). - public static PersonalizerErrorCode InvalidLogRetentionDays { get; } = new PersonalizerErrorCode(InvalidLogRetentionDaysValue); /// Multi-slot feature is currently disabled. Please follow multi-slot Personalizer documentation to update your loop settings to enable multi-slot functionality. public static PersonalizerErrorCode InvalidMultiSlotApiAccess { get; } = new PersonalizerErrorCode(InvalidMultiSlotApiAccessValue); - /// Exceeds maximum allowed payload size. - public static PersonalizerErrorCode PayloadSizeExceeded { get; } = new PersonalizerErrorCode(PayloadSizeExceededValue); - /// Api is currently disabled for the instance. - public static PersonalizerErrorCode InvalidApiAccess { get; } = new PersonalizerErrorCode(InvalidApiAccessValue); /// Key vault Key used for customer managed key cannot be accessed. public static PersonalizerErrorCode ModelFileAccessDenied { get; } = new PersonalizerErrorCode(ModelFileAccessDeniedValue); /// Auto-optimization is not compatible with multi-slot personalization. diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationJobStatus.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationJobStatus.cs index 389201f0f3dd..085e5f329912 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationJobStatus.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationJobStatus.cs @@ -15,7 +15,7 @@ namespace Azure.AI.Personalizer { private readonly string _value; - /// Initializes a new instance of . + /// Determines if two values are the same. /// is null. public PersonalizerEvaluationJobStatus(string value) { diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationType.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationType.cs index 96785da12f5e..9a313a1f08e8 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationType.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerEvaluationType.cs @@ -15,7 +15,7 @@ namespace Azure.AI.Personalizer { private readonly string _value; - /// Initializes a new instance of . + /// Determines if two values are the same. /// is null. public PersonalizerEvaluationType(string value) { diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerLearningMode.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerLearningMode.cs index 94137b19b95c..8a4b6c377704 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerLearningMode.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerLearningMode.cs @@ -15,7 +15,7 @@ namespace Azure.AI.Personalizer { private readonly string _value; - /// Initializes a new instance of . + /// Determines if two values are the same. /// is null. public PersonalizerLearningMode(string value) { diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerPolicySource.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerPolicySource.cs index 905b6286b041..0c7d1f64f6f7 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerPolicySource.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/PersonalizerPolicySource.cs @@ -15,7 +15,7 @@ namespace Azure.AI.Personalizer { private readonly string _value; - /// Initializes a new instance of . + /// Determines if two values are the same. /// is null. public PersonalizerPolicySource(string value) { diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ServiceStatus.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ServiceStatus.cs index bec504a098b8..2e47a345afb4 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ServiceStatus.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/Models/ServiceStatus.cs @@ -15,11 +15,8 @@ internal ServiceStatus() { } - /// Gets the service. public string Service { get; } - /// Gets the api status. public string ApiStatus { get; } - /// Gets the api status message. public string ApiStatusMessage { get; } } } diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs index 87b0f06f7775..5691244d0075 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/MultiSlotClient.cs @@ -19,6 +19,7 @@ internal partial class MultiSlotClient { private readonly ClientDiagnostics _clientDiagnostics; private readonly HttpPipeline _pipeline; + private readonly bool _isLocalInference; internal MultiSlotRestClient RestClient { get; } /// Initializes a new instance of MultiSlotClient for mocking. @@ -48,6 +49,17 @@ public MultiSlotClient(string endpoint, TokenCredential credential, Personalizer RestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, endpoint); } + /// Initializes a new instance of PersonalizerClient. + /// Supported Cognitive Services endpoint. + /// A credential used to authenticate to an Azure Service. + /// A flag to determine whether to use local inference. + /// The options for configuring the client. + public MultiSlotClient(string endpoint, TokenCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) : + this(endpoint, credential, options) + { + _isLocalInference = isLocalInference; + } + /// Initializes a new instance of MultiSlotClient. /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. @@ -69,6 +81,17 @@ public MultiSlotClient(string endpoint, AzureKeyCredential credential, Personali RestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, endpoint); } + /// Initializes a new instance of PersonalizerClient. + /// Supported Cognitive Services endpoint. + /// A credential used to authenticate to an Azure Service. + /// A flag to determine whether to use local inference. + /// The options for configuring the client. + public MultiSlotClient(string endpoint, AzureKeyCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) : + this(endpoint, credential, options) + { + _isLocalInference = isLocalInference; + } + /// Initializes a new instance of MultiSlotClient. /// The handler for diagnostic messaging in the client. /// The HTTP pipeline for sending and receiving REST requests and responses. diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerClientOptions.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerClientOptions.cs index 2dfd7fc85d90..643a4e1a15c8 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerClientOptions.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerClientOptions.cs @@ -32,6 +32,8 @@ public PersonalizerClientOptions(ServiceVersion version = LatestVersion) ServiceVersion.V1_1_preview_1 => "1.1-preview.1", _ => throw new NotSupportedException() }; + + Retry.NetworkTimeout = TimeSpan.FromMinutes(8); } } } diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerModelFactory.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerModelFactory.cs index 45bed2bb653e..3ea622c6496e 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerModelFactory.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/PersonalizerModelFactory.cs @@ -14,21 +14,6 @@ namespace Azure.AI.Personalizer /// Model factory for read-only models. public static partial class PersonalizerModelFactory { - /// Initializes a new instance of ClientConfiguration. - /// Unique identifier for this Personalizer instance. - /// Event hub connection string for sending interactions. - /// Event hub connection string for sending observations. - /// SAS Uri for the inference model. - /// Exploration value used before downloading model in CB. - /// Learning Modes for Personalizer. - /// Command line used for prediction before downloading model. - /// Version used by reinforcement learning client. - /// A new instance for mocking. - public static ClientConfiguration ClientConfiguration(string applicationID = null, string eventHubInteractionConnectionString = null, string eventHubObservationConnectionString = null, string modelBlobUri = null, float? initialExplorationEpsilon = null, PersonalizerLearningMode? learningMode = null, string initialCommandLine = null, int? protocolVersion = null) - { - return new ClientConfiguration(applicationID, eventHubInteractionConnectionString, eventHubObservationConnectionString, modelBlobUri, initialExplorationEpsilon, learningMode, initialCommandLine, protocolVersion); - } - /// Initializes a new instance of PersonalizerEvaluation. /// The ID of the evaluation. /// The name of the evaluation. diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs index 956b7ff668de..d0473d9cf981 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Generated/RankClient.cs @@ -19,6 +19,7 @@ internal partial class RankClient { private readonly ClientDiagnostics _clientDiagnostics; private readonly HttpPipeline _pipeline; + private readonly bool _isLocalInference; internal RankRestClient RestClient { get; } /// Initializes a new instance of RankClient for mocking. @@ -48,6 +49,17 @@ public RankClient(string endpoint, TokenCredential credential, PersonalizerClien RestClient = new RankRestClient(_clientDiagnostics, _pipeline, endpoint); } + /// Initializes a new instance of PersonalizerClient. + /// Supported Cognitive Services endpoint. + /// A credential used to authenticate to an Azure Service. + /// A flag to determine whether to use local inference. + /// The options for configuring the client. + public RankClient(string endpoint, TokenCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) : + this(endpoint, credential, options) + { + _isLocalInference = isLocalInference; + } + /// Initializes a new instance of RankClient. /// Supported Cognitive Services endpoint. /// A credential used to authenticate to an Azure Service. @@ -69,6 +81,17 @@ public RankClient(string endpoint, AzureKeyCredential credential, PersonalizerCl RestClient = new RankRestClient(_clientDiagnostics, _pipeline, endpoint); } + /// Initializes a new instance of PersonalizerClient. + /// Supported Cognitive Services endpoint. + /// A credential used to authenticate to an Azure Service. + /// A flag to determine whether to use local reference. + /// The options for configuring the client. + public RankClient(string endpoint, AzureKeyCredential credential, bool isLocalInference, PersonalizerClientOptions options = null) : + this(endpoint, credential, options) + { + _isLocalInference = isLocalInference; + } + /// Initializes a new instance of RankClient. /// The handler for diagnostic messaging in the client. /// The HTTP pipeline for sending and receiving REST requests and responses. diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfiguration.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfiguration.cs deleted file mode 100644 index 972a4e92dabc..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfiguration.cs +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. -using Azure.Core; - -namespace Azure.AI.Personalizer -{ - [CodeGenModel("ClientConfiguration")] - public partial class ClientConfiguration { } -} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/ClientConfigurationRestClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfigurationRestClient.cs similarity index 72% rename from sdk/personalizer/Azure.AI.Personalizer/src/Generated/ClientConfigurationRestClient.cs rename to sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfigurationRestClient.cs index 46dfc94ba4eb..b169533f85ce 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Generated/ClientConfigurationRestClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfigurationRestClient.cs @@ -1,21 +1,16 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -// - -#nullable disable - using System; using System.Text.Json; using System.Threading; using System.Threading.Tasks; -using Azure; using Azure.Core; using Azure.Core.Pipeline; -namespace Azure.AI.Personalizer +namespace Azure.AI.Personalizer.Models { - internal partial class ClientConfigurationRestClient + internal class ClientConfigurationRestClient { private string endpoint; private ClientDiagnostics _clientDiagnostics; @@ -32,8 +27,7 @@ public ClientConfigurationRestClient(ClientDiagnostics clientDiagnostics, HttpPi _clientDiagnostics = clientDiagnostics; _pipeline = pipeline; } - - internal HttpMessage CreateListRequest() + internal HttpMessage CreatePostRequest() { var message = _pipeline.CreateMessage(); var request = message.Request; @@ -47,19 +41,19 @@ internal HttpMessage CreateListRequest() return message; } - /// Get configuration settings used in distributed Personalizer deployments. + /// Get the Personalizer service configuration. /// The cancellation token to use. - public async Task> ListAsync(CancellationToken cancellationToken = default) + public async Task> PostAsync(CancellationToken cancellationToken = default) { - using var message = CreateListRequest(); + using var message = CreatePostRequest(); await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); switch (message.Response.Status) { case 200: { - ClientConfiguration value = default; + PersonalizerClientProperties value = default; using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false); - value = ClientConfiguration.DeserializeClientConfiguration(document.RootElement); + value = PersonalizerClientProperties.DeserializePersonalizerServiceProperties(document.RootElement); return Response.FromValue(value, message.Response); } default: @@ -67,19 +61,19 @@ public async Task> ListAsync(CancellationToken can } } - /// Get configuration settings used in distributed Personalizer deployments. + /// Get the Personalizer service configuration. /// The cancellation token to use. - public Response List(CancellationToken cancellationToken = default) + public Response Post(CancellationToken cancellationToken = default) { - using var message = CreateListRequest(); + using var message = CreatePostRequest(); _pipeline.Send(message, cancellationToken); switch (message.Response.Status) { case 200: { - ClientConfiguration value = default; + PersonalizerClientProperties value = default; using var document = JsonDocument.Parse(message.Response.ContentStream); - value = ClientConfiguration.DeserializeClientConfiguration(document.RootElement); + value = PersonalizerClientProperties.DeserializePersonalizerServiceProperties(document.RootElement); return Response.FromValue(value, message.Response); } default: diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs index f6fb6c356dec..4f6aedf30a54 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs @@ -25,8 +25,8 @@ public class PersonalizerClient internal MultiSlotRestClient MultiSlotRestClient { get; set; } internal MultiSlotEventsRestClient MultiSlotEventsRestClient { get; set; } - internal ClientConfigurationRestClient ClientConfigurationRestClient { get; set; } - internal ClientConfiguration result { get; set; } + internal Models.ClientConfigurationRestClient ClientConfigurationRestClient { get; set; } + internal Lazy result { get; set; } /// Initializes a new instance of Personalizer Client for mocking. protected PersonalizerClient() @@ -57,7 +57,7 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, Personalizer EventsRestClient = new EventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotRestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotEventsRestClient = new MultiSlotEventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); - ClientConfigurationRestClient = new ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); + ClientConfigurationRestClient = new Models.ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); } /// Initializes a new instance of PersonalizerClient. @@ -72,7 +72,9 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, bool isLocal if (isLocalInference) { //Intialize liveModel and call Rank processor - Configuration config = GetClientConfigurationForLiveModel(); + /*LiveModel liveModel = new LiveModel(GetClientConfigurationForLiveModel()); + liveModel.Init(); + _rankProcessor = new RankProcessor(liveModel);*/ } } @@ -104,7 +106,7 @@ public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, Personali EventsRestClient = new EventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotRestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotEventsRestClient = new MultiSlotEventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); - ClientConfigurationRestClient = new ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); + ClientConfigurationRestClient = new Models.ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); } /// Initializes a new instance of PersonalizerClient. @@ -119,7 +121,9 @@ public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, bool isLo if (isLocalInference) { //Intialize liveModel and Rankprocessor - Configuration config = GetClientConfigurationForLiveModel(); + /*LiveModel liveModel = new LiveModel(GetClientConfigurationForLiveModel()); + liveModel.Init(); + _rankProcessor = new RankProcessor(liveModel);*/ } } @@ -139,7 +143,7 @@ internal PersonalizerClient(ClientDiagnostics clientDiagnostics, HttpPipeline pi EventsRestClient = new EventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotRestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotEventsRestClient = new MultiSlotEventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); - ClientConfigurationRestClient = new ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); + ClientConfigurationRestClient = new Models.ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); _clientDiagnostics = clientDiagnostics; _pipeline = pipeline; } @@ -521,16 +525,16 @@ public virtual Response ActivateMultiSlot(string eventId, CancellationToken canc /// Gets the configuration details for the live model to use internal Configuration GetClientConfigurationForLiveModel() { - result = ClientConfigurationRestClient.List(); + result = new Lazy(() => ClientConfigurationRestClient.Post()); Configuration config = new Configuration(); // configure the personalizer loop - config["appid"] = result.ApplicationID; + config["appid"] = result.Value.ApplicationID; // set up the model - config["model.blob.uri"] = result.ModelBlobUri; - config["vw.commandline"] = result.InitialCommandLine; - config["initial_exploration.epsilon"] = Convert.ToString(result.InitialExplorationEpsilon, CultureInfo.InvariantCulture); - config["rank.learning.mode"] = Convert.ToString(result.LearningMode, CultureInfo.InvariantCulture); + config["model.blob.uri"] = result.Value.ModelBlobUri; + config["vw.commandline"] = 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; } diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs new file mode 100644 index 000000000000..7b58d4c6a78e --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs @@ -0,0 +1,50 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Personalizer.Models +{ + internal partial class PersonalizerClientProperties + { + internal static PersonalizerClientProperties DeserializePersonalizerServiceProperties(JsonElement element) + { + string applicationID = default; + string modelBlobUri = default; + float initialExplorationEpsilon = default; + PersonalizerLearningMode learningMode = default; + string initialCommandLine = default; + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("applicationID")) + { + applicationID = property.Value.GetString(); + continue; + } + if (property.NameEquals("modelBlobUri")) + { + modelBlobUri = property.Value.GetString(); + continue; + } + if (property.NameEquals("initialExplorationEpsilon")) + { + initialExplorationEpsilon = property.Value.GetSingle(); + continue; + } + if (property.NameEquals("learningMode")) + { + learningMode = new PersonalizerLearningMode(property.Value.GetString()); + continue; + } + if (property.NameEquals("initialCommandLine")) + { + initialCommandLine = property.Value.GetString(); + continue; + } + } + return new PersonalizerClientProperties(applicationID, modelBlobUri, initialExplorationEpsilon, learningMode, initialCommandLine); + } + } +} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs new file mode 100644 index 000000000000..3571c63adcd7 --- /dev/null +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs @@ -0,0 +1,60 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.Text; + +namespace Azure.AI.Personalizer.Models +{ + internal partial class PersonalizerClientProperties + { + internal PersonalizerClientProperties(string applicationID, string modelBlobUri, float initialExplorationEpsilon, PersonalizerLearningMode learningMode, string initialCommandLine) + { + ApplicationID = applicationID; + ModelBlobUri = modelBlobUri; + InitialExplorationEpsilon = initialExplorationEpsilon; + LearningMode = learningMode; + InitialCommandLine = initialCommandLine; + } + /// + /// Unique identifier for this Personalizer instance. + /// + public string ApplicationID { get; } + + /// + /// Event hub connection string for sending interactions. + /// + public string EventHubInteractionConnectionString { get;} + + /// + /// Event hub connection string for sending observations. + /// + public string EventHubObservationConnectionString { get; } + + /// + /// SAS Uri for the inference model. + /// + public string ModelBlobUri { get; } + + /// + /// Exploration value used before downloading model in CB. + /// + public float InitialExplorationEpsilon { get; } + + /// + /// Learning Mode setting. + /// + public PersonalizerLearningMode LearningMode { get; } + + /// + /// Command line used for prediction before downloading model. + /// + public string InitialCommandLine { get; } + + /// + /// Version used by reinforcement learning client. + /// + public int ProtocolVersion { get; } + } +} From 59e364edc3c954ea8b7a4db217faa07e588763cc Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Thu, 20 Jan 2022 12:47:29 -0800 Subject: [PATCH 18/22] Configuration details for livemodel --- .../Models/ClientConfigurationRestClient.cs | 84 ------------------- .../src/Models/PersonalizerClient.cs | 60 +++++++------ ...sonalizerClientProperties.Serialization.cs | 50 ----------- .../Models/PersonalizerClientProperties.cs | 60 ------------- 4 files changed, 36 insertions(+), 218 deletions(-) delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfigurationRestClient.cs delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs delete mode 100644 sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfigurationRestClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfigurationRestClient.cs deleted file mode 100644 index b169533f85ce..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/ClientConfigurationRestClient.cs +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; -using Azure.Core; -using Azure.Core.Pipeline; - -namespace Azure.AI.Personalizer.Models -{ - internal class ClientConfigurationRestClient - { - private string endpoint; - private ClientDiagnostics _clientDiagnostics; - private HttpPipeline _pipeline; - - /// Initializes a new instance of ClientConfigurationRestClient. - /// The handler for diagnostic messaging in the client. - /// The HTTP pipeline for sending and receiving REST requests and responses. - /// Supported Cognitive Services endpoint. - /// is null. - public ClientConfigurationRestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string endpoint) - { - this.endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint)); - _clientDiagnostics = clientDiagnostics; - _pipeline = pipeline; - } - internal HttpMessage CreatePostRequest() - { - 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("/configurations/client", false); - request.Uri = uri; - request.Headers.Add("Accept", "application/json"); - return message; - } - - /// Get the Personalizer service configuration. - /// The cancellation token to use. - public async Task> PostAsync(CancellationToken cancellationToken = default) - { - using var message = CreatePostRequest(); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - { - PersonalizerClientProperties value = default; - using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false); - value = PersonalizerClientProperties.DeserializePersonalizerServiceProperties(document.RootElement); - return Response.FromValue(value, message.Response); - } - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Get the Personalizer service configuration. - /// The cancellation token to use. - public Response Post(CancellationToken cancellationToken = default) - { - using var message = CreatePostRequest(); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - { - PersonalizerClientProperties value = default; - using var document = JsonDocument.Parse(message.Response.ContentStream); - value = PersonalizerClientProperties.DeserializePersonalizerServiceProperties(document.RootElement); - return Response.FromValue(value, message.Response); - } - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - } -} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs index 4f6aedf30a54..4237e90c7764 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs @@ -4,6 +4,8 @@ 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; @@ -19,14 +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 Models.ClientConfigurationRestClient ClientConfigurationRestClient { get; set; } - internal Lazy result { get; set; } + internal ServiceConfigurationRestClient ServiceConfigurationRestClient { get; set; } + internal PolicyRestClient PolicyRestClient { get; set; } + internal PersonalizerServiceProperties _personalizerServiceProperties { get; set; } + internal PersonalizerPolicy _personalizerPolicy { get; set; } /// Initializes a new instance of Personalizer Client for mocking. protected PersonalizerClient() @@ -52,12 +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); - ClientConfigurationRestClient = new Models.ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); + ServiceConfigurationRestClient = new ServiceConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); + PolicyRestClient = new PolicyRestClient(_clientDiagnostics, _pipeline, stringEndpoint); } /// Initializes a new instance of PersonalizerClient. @@ -72,9 +78,7 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, bool isLocal if (isLocalInference) { //Intialize liveModel and call Rank processor - /*LiveModel liveModel = new LiveModel(GetClientConfigurationForLiveModel()); - liveModel.Init(); - _rankProcessor = new RankProcessor(liveModel);*/ + Configuration config = GetConfigurationForLiveModel("Token", "token"); } } @@ -97,16 +101,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); - ClientConfigurationRestClient = new Models.ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); + ServiceConfigurationRestClient = new ServiceConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); + PolicyRestClient = new PolicyRestClient(_clientDiagnostics, _pipeline, stringEndpoint); } /// Initializes a new instance of PersonalizerClient. @@ -121,9 +126,7 @@ public PersonalizerClient(Uri endpoint, AzureKeyCredential credential, bool isLo if (isLocalInference) { //Intialize liveModel and Rankprocessor - /*LiveModel liveModel = new LiveModel(GetClientConfigurationForLiveModel()); - liveModel.Init(); - _rankProcessor = new RankProcessor(liveModel);*/ + Configuration config = GetConfigurationForLiveModel("apiKey", apiKey); } } @@ -143,7 +146,6 @@ internal PersonalizerClient(ClientDiagnostics clientDiagnostics, HttpPipeline pi EventsRestClient = new EventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotRestClient = new MultiSlotRestClient(_clientDiagnostics, _pipeline, stringEndpoint); MultiSlotEventsRestClient = new MultiSlotEventsRestClient(_clientDiagnostics, _pipeline, stringEndpoint); - ClientConfigurationRestClient = new Models.ClientConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint); _clientDiagnostics = clientDiagnostics; _pipeline = pipeline; } @@ -523,18 +525,28 @@ public virtual Response ActivateMultiSlot(string eventId, CancellationToken canc } /// Gets the configuration details for the live model to use - internal Configuration GetClientConfigurationForLiveModel() + internal Configuration GetConfigurationForLiveModel(string authType, string authValue) { - result = new Lazy(() => ClientConfigurationRestClient.Post()); + _personalizerServiceProperties = ServiceConfigurationRestClient.Get(); + _personalizerPolicy = PolicyRestClient.Get(); Configuration config = new Configuration(); - // configure the personalizer loop - config["appid"] = result.Value.ApplicationID; - // set up the model - config["model.blob.uri"] = result.Value.ModelBlobUri; - config["vw.commandline"] = 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); + if (authType == "apiKey") + { + config["http.api.key"] = authValue; + } + else + { + //ToDo: Working on changes to support token authentication in RLClient + config["http.tokent.key"] = authValue; + } + config["interaction.http.api.host"] = stringEndpoint+"personalizer/v1.1-preview.2/logs/interactions"; + config["observation.http.api.host"] = stringEndpoint+"personalizer/v1.1-preview.2/logs/observations"; + 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; } diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs deleted file mode 100644 index 7b58d4c6a78e..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.Serialization.cs +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Text.Json; -using Azure.Core; - -namespace Azure.AI.Personalizer.Models -{ - internal partial class PersonalizerClientProperties - { - internal static PersonalizerClientProperties DeserializePersonalizerServiceProperties(JsonElement element) - { - string applicationID = default; - string modelBlobUri = default; - float initialExplorationEpsilon = default; - PersonalizerLearningMode learningMode = default; - string initialCommandLine = default; - foreach (var property in element.EnumerateObject()) - { - if (property.NameEquals("applicationID")) - { - applicationID = property.Value.GetString(); - continue; - } - if (property.NameEquals("modelBlobUri")) - { - modelBlobUri = property.Value.GetString(); - continue; - } - if (property.NameEquals("initialExplorationEpsilon")) - { - initialExplorationEpsilon = property.Value.GetSingle(); - continue; - } - if (property.NameEquals("learningMode")) - { - learningMode = new PersonalizerLearningMode(property.Value.GetString()); - continue; - } - if (property.NameEquals("initialCommandLine")) - { - initialCommandLine = property.Value.GetString(); - continue; - } - } - return new PersonalizerClientProperties(applicationID, modelBlobUri, initialExplorationEpsilon, learningMode, initialCommandLine); - } - } -} diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs deleted file mode 100644 index 3571c63adcd7..000000000000 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClientProperties.cs +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Text; - -namespace Azure.AI.Personalizer.Models -{ - internal partial class PersonalizerClientProperties - { - internal PersonalizerClientProperties(string applicationID, string modelBlobUri, float initialExplorationEpsilon, PersonalizerLearningMode learningMode, string initialCommandLine) - { - ApplicationID = applicationID; - ModelBlobUri = modelBlobUri; - InitialExplorationEpsilon = initialExplorationEpsilon; - LearningMode = learningMode; - InitialCommandLine = initialCommandLine; - } - /// - /// Unique identifier for this Personalizer instance. - /// - public string ApplicationID { get; } - - /// - /// Event hub connection string for sending interactions. - /// - public string EventHubInteractionConnectionString { get;} - - /// - /// Event hub connection string for sending observations. - /// - public string EventHubObservationConnectionString { get; } - - /// - /// SAS Uri for the inference model. - /// - public string ModelBlobUri { get; } - - /// - /// Exploration value used before downloading model in CB. - /// - public float InitialExplorationEpsilon { get; } - - /// - /// Learning Mode setting. - /// - public PersonalizerLearningMode LearningMode { get; } - - /// - /// Command line used for prediction before downloading model. - /// - public string InitialCommandLine { get; } - - /// - /// Version used by reinforcement learning client. - /// - public int ProtocolVersion { get; } - } -} From 92f6ba31a9be803f14c1a41d1ad09b0a1b256054 Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Thu, 20 Jan 2022 12:51:46 -0800 Subject: [PATCH 19/22] cleanup --- .../Azure.AI.Personalizer/src/Models/PersonalizerClient.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs index 4237e90c7764..89ecb43a2755 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs @@ -78,6 +78,7 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, bool isLocal if (isLocalInference) { //Intialize liveModel and call Rank processor + //ToDo: Working on changes to support token authentication in RLClient Configuration config = GetConfigurationForLiveModel("Token", "token"); } } From 2c7e79cc9c162e161fbf8e6736d6a4675c4a785b Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Thu, 20 Jan 2022 12:55:14 -0800 Subject: [PATCH 20/22] cleanup --- .../Azure.AI.Personalizer/src/Models/PersonalizerClient.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs index 89ecb43a2755..2b2fe10a611a 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs @@ -79,7 +79,7 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, bool isLocal { //Intialize liveModel and call Rank processor //ToDo: Working on changes to support token authentication in RLClient - Configuration config = GetConfigurationForLiveModel("Token", "token"); + //Configuration config = GetConfigurationForLiveModel("Token", "token"); } } @@ -539,7 +539,7 @@ internal Configuration GetConfigurationForLiveModel(string authType, string auth else { //ToDo: Working on changes to support token authentication in RLClient - config["http.tokent.key"] = authValue; + //config["http.token.key"] = authValue; } config["interaction.http.api.host"] = stringEndpoint+"personalizer/v1.1-preview.2/logs/interactions"; config["observation.http.api.host"] = stringEndpoint+"personalizer/v1.1-preview.2/logs/observations"; From 23d1c934c5e0c14d1b496387eb92be599ecc129e Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Thu, 20 Jan 2022 14:36:35 -0800 Subject: [PATCH 21/22] Added ToDo comments --- .../Azure.AI.Personalizer/src/Models/PersonalizerClient.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs index 2b2fe10a611a..0e5a269a61ca 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs @@ -78,7 +78,7 @@ public PersonalizerClient(Uri endpoint, TokenCredential credential, bool isLocal if (isLocalInference) { //Intialize liveModel and call Rank processor - //ToDo: Working on changes to support token authentication in RLClient + //ToDo:TASK 13057958: Working on changes to support token authentication in RLClient //Configuration config = GetConfigurationForLiveModel("Token", "token"); } } @@ -538,11 +538,12 @@ internal Configuration GetConfigurationForLiveModel(string authType, string auth } else { - //ToDo: Working on changes to support token authentication in RLClient + //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.2/logs/interactions"; config["observation.http.api.host"] = stringEndpoint+"personalizer/v1.1-preview.2/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"; From a914445efba02e1a70fe2c269eb748a2a7765045 Mon Sep 17 00:00:00 2001 From: Tejaswi Paruchuri Date: Fri, 21 Jan 2022 10:13:51 -0800 Subject: [PATCH 22/22] correct the version in endpoints --- .../Azure.AI.Personalizer/src/Models/PersonalizerClient.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs index 0e5a269a61ca..f729f7305b83 100644 --- a/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs +++ b/sdk/personalizer/Azure.AI.Personalizer/src/Models/PersonalizerClient.cs @@ -541,8 +541,8 @@ internal Configuration GetConfigurationForLiveModel(string authType, string auth //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.2/logs/interactions"; - config["observation.http.api.host"] = stringEndpoint+"personalizer/v1.1-preview.2/logs/observations"; + 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;