8000 Merge branch 'work-in-progress' of https://github.com/Microsoft/botbu… · rsliang/botbuilder-python@5a6e3fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a6e3fe

Browse files
committed
Merge branch 'work-in-progress' of https://github.com/Microsoft/botbuilder-python into work-in-progress
Merge.
2 parents 1a448d8 + 39cd559 commit 5a6e3fe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+5270
-102
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
from .about import __title__, __version__
5+
6+
__all__ = ["__title__", "__version__"]

libraries/botbuilder-ai/botbuilder/ai/luis/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
from .luis_application import LuisApplication
66
from .luis_prediction_options import LuisPredictionOptions
77
from .luis_telemetry_constants import LuisTelemetryConstants
8-
from .recognizer_result import RecognizerResult
8+
from .recognizer_result import RecognizerResult, TopIntent
9+
from .luis_recognizer import LuisRecognizer
910

1011
__all__ = [
1112
"IntentScore",
1213
"LuisApplication",
1314
"LuisPredictionOptions",
15+
"LuisRecognizer",
1416
"LuisTelemetryConstants",
1517
"RecognizerResult",
18+
"TopIntent",
1619
]

libraries/botbuilder-ai/botbuilder/ai/luis/luis_prediction_options.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ class LuisPredictionOptions(object):
99
Optional parameters for a LUIS prediction request.
1010
"""
1111

12-
def __init__(self):
12+
def __init__(self, timeout: float = 100000):
1313
self._bing_spell_check_subscription_key: str = None
1414
self._include_all_intents: bool = None
1515
self._include_instance_data: bool = None
1616
self._log: bool = None
1717
self._spell_check: bool = None
1818
self._staging: bool = None
19-
self._timeout: float = 100000
19+
self._timeout: float = timeout
2020
self._timezone_offset: float = None
2121
self._telemetry_client: BotTelemetryClient = NullTelemetryClient()
2222
self._log_personal_information: bool = False
2323

2424
@property
2525
def bing_spell_check_subscription_key(self) -> str:
26-
"""Gets or sets the Bing Spell Check subscription key.
26+
"""Gets the Bing Spell Check subscription key.
2727
2828
:return: The Bing Spell Check subscription key.
2929
:rtype: str
@@ -33,7 +33,7 @@ def bing_spell_check_subscription_key(self) -> str:
3333

3434
@bing_spell_check_subscription_key.setter
3535
def bing_spell_check_subscription_key(self, value: str) -> None:
36-
"""Gets or sets the Bing Spell Check subscription key.
36+
"""Sets the Bing Spell Check subscription key.
3737
3838
:param value: The Bing Spell Check subscription key.
3939
:type value: str
@@ -45,7 +45,7 @@ def bing_spell_check_subscription_key(self, value: str) -> None:
4545

4646
@property
4747
def include_all_intents(self) -> bool:
48-
"""Gets or sets whether all intents come back or only the top one.
48+
"""Gets whether all intents come back or only the top one.
4949
5050
:return: True for returning all intents.
5151
:rtype: bool
@@ -55,7 +55,7 @@ def include_all_intents(self) -> bool:
5555

5656
@include_all_intents.setter
5757
def include_all_intents(self, value: bool) -> None:
58-
"""Gets or sets whether all intents come back or only the top one.
58+
"""Sets whether all intents come back or only the top one.
5959
6060
:param value: True for returning all intents.
6161
:type value: bool
@@ -67,7 +67,7 @@ def include_all_intents(self, value: bool) -> None:
6767

6868
@property
6969
def include_instance_data(self) -> bool:
70-
"""Gets or sets a value indicating whether or not instance data should be included in response.
70+
"""Gets a value indicating whether or not instance data should be included in response.
7171
7272
:return: A value indicating whether or not instance data should be included in response.
7373
:rtype: bool
@@ -77,7 +77,7 @@ def include_instance_data(self) -> bool:
7777

7878
@include_instance_data.setter
7979
def include_instance_data(self, value: bool) -> None:
80-
"""Gets or sets a value indicating whether or not instance data should be included in response.
80+
"""Sets a value indicating whether or not instance data should be included in response.
8181
8282
:param value: A value indicating whether or not instance data should be included in response.
8383
:type value: bool
@@ -89,7 +89,7 @@ def include_instance_data(self, value: bool) -> None:
8989

9090
@property
9191
def log(self) -> bool:
92-
"""Gets or sets if queries should be logged in LUIS.
92+
"""Gets if queries should be logged in LUIS.
9393
9494
:return: If queries should be logged in LUIS.
9595
:rtype: bool
@@ -99,7 +99,7 @@ def log(self) -> bool:
9999

100100
@log.setter
101101
def log(self, value: bool) -> None:
102-
"""Gets or sets if queries should be logged in LUIS.
102+
"""Sets if queries should be logged in LUIS.
103103
104104
:param value: If queries should be logged in LUIS.
105105
:type value: bool
@@ -111,7 +111,7 @@ def log(self, value: bool) -> None:
111111

112112
@property
113113
def spell_check(self) -> bool:
114-
"""Gets or sets whether to spell check queries.
114+
"""Gets whether to spell check queries.
115115
116116
:return: Whether to spell check queries.
117117
:rtype: bool
@@ -121,7 +121,7 @@ def spell_check(self) -> bool:
121121

122122
@spell_check.setter
123123
def spell_check(self, value: bool) -> None:
124-
"""Gets or sets whether to spell check queries.
124+
"""Sets whether to spell check queries.
125125
126126
:param value: Whether to spell check queries.
127127
:type value: bool
@@ -133,7 +133,7 @@ def spell_check(self, value: bool) -> None:
133133

134134
@property
135135
def staging(self) -> bool:
136-
"""Gets or sets whether to use the staging endpoint.
136+
"""Gets whether to use the staging endpoint.
137137
138138
:return: Whether to use the staging endpoint.
139139
:rtype: bool
@@ -143,7 +143,7 @@ def staging(self) -> bool:
143143

144144
@staging.setter
145145
def staging(self, value: bool) -> None:
146-
"""Gets or sets whether to use the staging endpoint.
146+
"""Sets whether to use the staging endpoint.
147147
148148
149149
:param value: Whether to use the staging endpoint.
@@ -156,7 +156,7 @@ def staging(self, value: bool) -> None:
156156

157157
@property
158158
def timeout(self) -> float:
159-
"""Gets or sets the time in milliseconds to wait before the request times out.
159+
"""Gets the time in milliseconds to wait before the request times out.
160160
161161
:return: The time in milliseconds to wait before the request times out. Default is 100000 milliseconds.
162162
:rtype: float
@@ -166,7 +166,7 @@ def timeout(self) -> float:
166166

167167
@timeout.setter
168168
def timeout(self, value: float) -> None:
169-
"""Gets or sets the time in milliseconds to wait before the request times out.
169+
"""Sets the time in milliseconds to wait before the request times out.
170170
171171
:param value: The time in milliseconds to wait before the request times out. Default is 100000 milliseconds.
172172
:type value: float
@@ -178,7 +178,7 @@ def timeout(self, value: float) -> None:
178178

179179
@property
180180
def timezone_offset(self) -> float:
181-
"""Gets or sets the time zone offset.
181+
"""Gets the time zone offset.
182182
183183
:return: The time zone offset.
184184
:rtype: float
@@ -188,7 +188,7 @@ def timezone_offset(self) -> float:
188188

189189
@timezone_offset.setter
190190
def timezone_offset(self, value: float) -> None:
191-
"""Gets or sets the time zone offset.
191+
"""Sets the time zone offset.
192192
193193
:param value: The time zone offset.
194194
:type value: float
@@ -200,7 +200,7 @@ def timezone_offset(self, value: float) -> None:
200200

201201
@property
202202
def telemetry_client(self) -> BotTelemetryClient:
203-
"""Gets or sets the IBotTelemetryClient used to log the LuisResult event.
203+
"""Gets the BotTelemetryClient used to log the LuisResult event.
204204
205205
:return: The client used to log telemetry events.
206206
:rtype: BotTelemetryClient
@@ -210,7 +210,7 @@ def telemetry_client(self) -> BotTelemetryClient:
210210

211211
@telemetry_client.setter
212212
def telemetry_client(self, value: BotTelemetryClient) -> None:
213-
"""Gets or sets the IBotTelemetryClient used to log the LuisResult event.
213+
"""Sets the BotTelemetryClient used to log the LuisResult event.
214214
215215
:param value: The client used to log telemetry events.
216216
:type value: BotTelemetryClient
@@ -222,7 +222,7 @@ def telemetry_client(self, value: BotTelemetryClient) -> None:
222222

223223
@property
224224
def log_personal_information(self) -> bool:
225-
"""Gets or sets a value indicating whether to log personal information that came from the user to telemetry.
225+
"""Gets a value indicating whether to log personal information that came from the user to telemetry.
226226
227227
:return: If true, personal information is logged to Telemetry; otherwise the properties will be filtered.
228228
:rtype: bool
@@ -232,7 +232,7 @@ def log_personal_information(self) -> bool:
232232

233233
@log_personal_information.setter
234234
def log_personal_information(self, value: bool) -> None:
235-
"""Gets or sets a value indicating whether to log personal information that came from the user to telemetry.
235+
"""Sets a value indicating whether to log personal information that came from the user to telemetry.
236236
237237
:param value: If true, personal information is logged to Telemetry; otherwise the properties will be filtered.
238238
:type value: bool

libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer.py

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the MIT License.
33

44
import json
5-
from typing import Dict, List, Tuple
5+
from typing import Dict, List, Tuple, Union
66

77
from azure.cognitiveservices.language.luis.runtime import LUISRuntimeClient
88
from azure.cognitiveservices.language.luis.runtime.models import LuisResult
@@ -39,7 +39,7 @@ class LuisRecognizer(object):
3939

4040
def __init__(
4141
self,
42-
application: LuisApplication,
42+
application: Union[LuisApplication, str],
4343
prediction_options: LuisPredictionOptions = None,
4444
include_api_results: bool = False,
4545
):
@@ -54,19 +54,26 @@ def __init__(
5454
:raises TypeError:
5555
"""
5656

57-
if application is None:
58-
raise TypeError("LuisRecognizer.__init__(): application cannot be None.")
59-
self._application = application
57+
if isinstance(application, LuisApplication):
58+
self._application = application
59+
elif isinstance(application, str):
60+
self._application = LuisApplication.from_application_endpoint(application)
61+
else:
62+
raise TypeError(
63+
"LuisRecognizer.__init__(): application is not an instance of LuisApplication or str."
64+
)
6065

6166
self._options = prediction_options or LuisPredictionOptions()
6267

6368
self._include_api_results = include_api_results
6469

65-
self._telemetry_client = self._options.TelemetryClient
66-
self._log_personal_information = self._options.LogPersonalInformation
70+
self._telemetry_client = self._options.telemetry_client
71+
self._log_personal_information = self._options.log_personal_information
6772

68-
credentials = CognitiveServicesCredentials(application.EndpointKey)
69-
self._runtime = LUISRuntimeClient(application.endpoint, credentials)
73+
credentials = CognitiveServicesCredentials(self._application.endpoint_key)
74+
self._runtime = LUISRuntimeClient(self._application.endpoint, credentials)
75+
self._runtime.config.add_user_agent(LuisUtil.get_user_agent())
76+
self._runtime.config.connection.timeout = self._options.timeout // 1000
7077

7178
@property
7279
def log_personal_information(self) -> bool:
@@ -110,11 +117,9 @@ def telemetry_client(self, value: BotTelemetryClient):
110117

111118
self._telemetry_client = value
112119

120+
@staticmethod
113121
def top_intent(
114-
self,
115-
results: RecognizerResult,
116-
default_intent: str = "None",
117-
min_score: float = 0.0,
122+
results: RecognizerResult, default_intent: str = "None", min_score: float = 0.0
118123
) -> str:
119124
"""Returns the name of the top scoring intent from a set of LUIS results.
120125
@@ -135,15 +140,15 @@ def top_intent(
135140
top_intent: str = None
136141
top_score: float = -1.0
137142
if results.intents:
138-
for intent, intent_score in results.intents.items():
139-
score = float(intent_score)
143+
for intent_name, intent_score in results.intents.items():
144+
score = intent_score.score
140145
if score > top_score and score >= min_score:
141-
top_intent = intent
146+
top_intent = intent_name
142147
top_score = score
143148

144149
return top_intent or default_intent
145150

146-
async def recognize(
151+
def recognize(
147152
self,
148153
turn_context: TurnContext,
149154
telemetry_properties: Dict[str, str] = None,
@@ -161,11 +166,11 @@ async def recognize(
161166
:rtype: RecognizerResult
162167
"""
163168

164-
return await self._recognize_internal(
169+
return self._recognize_internal(
165170
turn_context, telemetry_properties, telemetry_metrics
166171
)
167172

168-
async def on_recognizer_result(
173+
def on_recognizer_result(
169174
self,
170175
recognizer_result: RecognizerResult,
171176
turn_context: TurnContext,
@@ -184,7 +189,7 @@ async def on_recognizer_result(
184189
:param telemetry_metrics: Dict[str, float], optional
185190
"""
186191

187-
properties = await self.fill_luis_event_properties(
192+
properties = self.fill_luis_event_properties(
188193
recognizer_result, turn_context, telemetry_properties
189194
)
190195

@@ -202,7 +207,7 @@ def _get_top_k_intent_score(
202207
if intent_names:
203208
intent_name = intent_names[0]
204209
if intents[intent_name] is not None:
205-
intent_score = "{:.2f}".format(intents[intent_name])
210+
intent_score = "{:.2f}".format(intents[intent_name].score)
206211

207212
return intent_name, intent_score
208213

@@ -241,12 +246,12 @@ def fill_luis_event_properties(
241246

242247
# Add the intent score and conversation id properties
243248
properties: Dict[str, str] = {
244-
LuisTelemetryConstants.application_id_property: self._application.ApplicationId,
249+
LuisTelemetryConstants.application_id_property: self._application.application_id,
245250
LuisTelemetryConstants.intent_property: intent_name,
246251
LuisTelemetryConstants.intent_score_property: intent_score,
247252
LuisTelemetryConstants.intent2_property: intent2_name,
248253
LuisTelemetryConstants.intent_score2_property: intent2_score,
249-
LuisTelemetryConstants.from_id_property: turn_context.Activity.From.Id,
254+
LuisTelemetryConstants.from_id_property: turn_context.activity.from_property.id,
250255
}
251256

252257
sentiment = recognizer_result.properties.get("sentiment")
@@ -277,7 +282,7 @@ def fill_luis_event_properties(
277282

278283
return properties
279284

280-
async def _recognize_internal(
285+
def _recognize_internal(
281286
self,
282287
turn_context: TurnContext,
283288
telemetry_properties: Dict[str, str],
@@ -298,7 +303,7 @@ async def _recognize_internal(
298303
text=utterance, intents={"": IntentScore(score=1.0)}, entities={}
299304
)
300305
else:
301-
luis_result = await self._runtime.prediction.resolve(
306+
luis_result = self._runtime.prediction.resolve(
302307
self._application.application_id,
303308
utterance,
304309
timezoneOffset=self._options.timezone_offset,
@@ -326,21 +331,8 @@ async def _recognize_internal(
326331
recognizer_result.properties["luisResult"] = luis_result
327332

328333
# Log telemetry
329-
await self.on_recognizer_result(
334+
self.on_recognizer_result(
330335
recognizer_result, turn_context, telemetry_properties, telemetry_metrics
331336
)
332337

333-
trace_info = {
334-
"recognizerResult": recognizer_result,
335-
"luisModel": {"ModelID": self._application.application_id},
336-
"luisOptions": self._options,
337-
"luisResult": luis_result,
338-
}
339-
340-
await turn_context.trace_activity_async(
341-
"LuisRecognizer",
342-
trace_info,
343-
LuisRecognizer.luis_trace_type,
344-
LuisRecognizer.luis_trace_label,
345-
)
346338
return recognizer_result

0 commit comments

Comments
 (0)
0