forked from BotBuilderCommunity/botbuilder-community-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuisModelAttribute.cs
More file actions
242 lines (214 loc) · 7.52 KB
/
LuisModelAttribute.cs
File metadata and controls
242 lines (214 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
namespace Bot.Builder.Community.Dialogs.Luis
{
using System;
/// <summary>
/// The LUIS model information.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method, AllowMultiple = true)]
[Serializable]
public class LuisModelAttribute : Attribute, ILuisModel, ILuisOptions, IEquatable<ILuisModel>
{
private string _modelId;
private double _threshold;
private string _subscriptionKey;
private string _domain;
private LuisApiVersion _apiVersion;
private Uri _uriBase;
/// <summary>
/// Construct the LUIS model information.
/// </summary>
/// <param name="modelID">The LUIS model ID.</param>
/// <param name="subscriptionKey">The LUIS subscription key.</param>
/// <param name="apiVersion">The LUIS API version.</param>
/// <param name="domain">Domain where LUIS model is located.</param>
/// <param name="threshold">Threshold for the top scoring intent.</param>
public LuisModelAttribute(
string modelID,
string subscriptionKey,
LuisApiVersion apiVersion = LuisApiVersion.V2,
string domain = null,
double threshold = 0.0d)
{
SetField.NotNull(out this._modelId, nameof(modelID), modelID);
SetField.NotNull(out this._subscriptionKey, nameof(subscriptionKey), subscriptionKey);
this._apiVersion = apiVersion;
this._domain = domain;
this._uriBase = UriFor(apiVersion, domain);
this._threshold = threshold;
this.Log = true;
}
/// <summary>
/// Gets or sets the GUID for the LUIS model.
/// </summary>
/// <value>
/// The GUID for the LUIS model.
/// </value>
public string ModelID
{
get => _modelId;
set => _modelId = value;
}
/// <summary>
/// Gets or sets the subscription key for LUIS.
/// </summary>
/// <value>
/// The subscription key for LUIS.
/// </value>
public string SubscriptionKey
{
get => _subscriptionKey;
set => _subscriptionKey = value;
}
/// <summary>
/// Gets or sets the domain where LUIS application is located.
/// </summary>
/// <remarks>Null means default which is api.projectoxford.ai for V1 API and westus.api.cognitive.microsoft.com for V2 api.</remarks>
/// <value>
/// The domain where LUIS application is located.
/// </value>
public string Domain
{
get => _domain;
set => _domain = value;
}
/// <summary>
/// Gets or sets the base URI for LUIS calls.
/// </summary>
/// <value>
/// The base URI for LUIS calls.
/// </value>
public Uri UriBase
{
get => _uriBase;
set => _uriBase = value;
}
/// <summary>
/// Gets or sets the version of query API to call.
/// </summary>
/// <value>
/// The version of query API to call.
/// </value>
public LuisApiVersion ApiVersion
{
get => _apiVersion;
set => _apiVersion = value;
}
/// <summary>
/// Gets or sets the Threshold for top scoring intent
/// </summary>
/// <value>
/// The Threshold for top scoring intent
/// </value>
public double Threshold
{
get => _threshold;
set => _threshold = value;
}
/// <summary>
/// Gets or sets a value indicating whether logging of queries to LUIS is allowed.
/// </summary>
/// <value>
/// A flag indicating if logging of queries to LUIS is allowed.
/// </value>
public bool Log
{
get => Options.Log ?? default(bool);
set => Options.Log = value;
}
/// <summary>
/// Gets or sets a value indicating whether spell checking is on or not.
/// </summary>
/// <value>
/// A flag indicating if spell checking is on.
/// </value>
public bool SpellCheck
{
get => Options.SpellCheck ?? default(bool);
set => Options.SpellCheck = value;
}
/// <summary>
/// Gets or sets a value indicating whether the staging endpoint should be used.
/// </summary>
/// <value>
/// A flag indicating if the staging endpoint should be used.
/// </value>
public bool Staging
{
get => Options.Staging ?? default(bool);
set => Options.Staging = value;
}
/// <summary>
/// Gets or sets the time zone offset.
/// </summary>
/// <value>
/// The time zone offset.
/// </value>
public double TimezoneOffset
{
get => Options.TimezoneOffset ?? default(double);
set => Options.TimezoneOffset = value;
}
/// <summary>
/// Gets or sets a value indicating whether verbose should be used.
/// </summary>
/// <value>
/// The verbose flag.
/// </value>
public bool Verbose
{
get => Options.Verbose ?? default(bool);
set => Options.Verbose = value;
}
/// <summary>
/// Gets or sets the Bing Spell Check subscription key.
/// </summary>
/// <value>
/// The Bing Spell Check subscription key.
/// </value>
public string BingSpellCheckSubscriptionKey
{
get => Options.BingSpellCheckSubscriptionKey;
set => Options.BingSpellCheckSubscriptionKey = value;
}
bool? ILuisOptions.Log { get; set; }
bool? ILuisOptions.SpellCheck { get; set; }
bool? ILuisOptions.Staging { get; set; }
double? ILuisOptions.TimezoneOffset { get; set; }
bool? ILuisOptions.Verbose { get; set; }
string ILuisOptions.BingSpellCheckSubscriptionKey { get; set; }
private ILuisOptions Options => (ILuisOptions)this;
public static Uri UriFor(LuisApiVersion apiVersion, string domain = null)
{
if (domain == null)
{
domain = apiVersion == LuisApiVersion.V2 ? "westus.api.cognitive.microsoft.com" : "api.projectoxford.ai/luis/v1/application";
}
return new Uri(apiVersion == LuisApiVersion.V2 ? $"https://{domain}/luis/v2.0/apps/" : $"https://api.projectoxford.ai/luis/v1/application");
}
public bool Equals(ILuisModel other)
{
return other != null
&& object.Equals(this.ModelID, other.ModelID)
&& object.Equals(this.SubscriptionKey, other.SubscriptionKey)
&& object.Equals(this.ApiVersion, other.ApiVersion)
&& object.Equals(this.UriBase, other.UriBase)
;
}
public override bool Equals(object other)
{
return this.Equals(other as ILuisModel);
}
public override int GetHashCode()
{
return ModelID.GetHashCode()
^ SubscriptionKey.GetHashCode()
^ UriBase.GetHashCode()
^ ApiVersion.GetHashCode();
}
public LuisRequest ModifyRequest(LuisRequest request)
{
Options.Apply(request);
return request;
}
}
}