3
3
4
4
import json
5
5
from typing import Dict , List , Tuple , Union
6
-
7
- from azure .cognitiveservices .language .luis .runtime import LUISRuntimeClient
8
- from azure .cognitiveservices .language .luis .runtime .models import LuisResult
9
- from msrest .authentication import CognitiveServicesCredentials
10
-
11
6
from botbuilder .core import (
12
7
BotAssert ,
13
8
IntentScore ,
16
11
TurnContext ,
17
12
)
18
13
from botbuilder .schema import ActivityTypes
19
-
20
14
from . import LuisApplication , LuisPredictionOptions , LuisTelemetryConstants
21
- from .activity_util import ActivityUtil
22
- from .luis_util import LuisUtil
15
+ from .luis_recognizer_v3 import LuisRecognizerV3
16
+ from .luis_recognizer_v2 import LuisRecognizerV2
17
+ from .luis_recognizer_options_v2 import LuisRecognizerOptionsV2
18
+ from .luis_recognizer_options_v3 import LuisRecognizerOptionsV3
23
19
24
20
25
21
class LuisRecognizer (Recognizer ):
@@ -36,7 +32,9 @@ class LuisRecognizer(Recognizer):
36
32
def __init__ (
37
33
self ,
38
34
application : Union [LuisApplication , str ],
39
- prediction_options : LuisPredictionOptions = None ,
35
+ prediction_options : Union [
36
+ LuisRecognizerOptionsV2 , LuisRecognizerOptionsV3 , LuisPredictionOptions
37
+ ] = None ,
40
38
include_api_results : bool = False ,
41
39
):
42
40
"""Initializes a new instance of the <see cref="LuisRecognizer"/> class.
@@ -59,17 +57,17 @@ def __init__(
59
57
)
60
58
61
59
self ._options = prediction_options or LuisPredictionOptions ()
62
-
63
- self ._include_api_results = include_api_results
60
+ self ._include_api_results = include_api_results or (
61
+ prediction_options .include_api_results
62
+ if isinstance (
63
+ prediction_options , (LuisRecognizerOptionsV3 , LuisRecognizerOptionsV2 )
64
+ )
65
+ else False
66
+ )
64
67
65
68
self .telemetry_client = self ._options .telemetry_client
66
69
self .log_personal_information = self ._options .log_personal_information
67
70
68
- credentials = CognitiveServicesCredentials (self ._application .endpoint_key )
69
- self ._runtime = LUISRuntimeClient (self ._application .endpoint , credentials )
70
- self ._runtime .config .add_user_agent (LuisUtil .get_user_agent ())
71
- self ._runtime .config .connection .timeout = self ._options .timeout // 1000
72
-
73
71
@staticmethod
74
72
def top_intent (
75
73
results : RecognizerResult , default_intent : str = "None" , min_score : float = 0.0
@@ -249,7 +247,9 @@ async def _recognize_internal(
249
247
turn_context : TurnContext ,
250
248
telemetry_properties : Dict [str , str ],
251
249
telemetry_metrics : Dict [str , float ],
252
- luis_prediction_options : LuisPredictionOptions = None ,
250
+ luis_prediction_options : Union [
251
+ LuisPredictionOptions , LuisRecognizerOptionsV2 , LuisRecognizerOptionsV3
252
+ ] = None ,
253
253
) -> RecognizerResult :
254
254
255
255
BotAssert .context_not_none (turn_context )
@@ -259,10 +259,9 @@ async def _recognize_internal(
259
259
260
260
utterance : str = turn_context .activity .text if turn_context .activity is not None else None
261
261
recognizer_result : RecognizerResult = None
262
- luis_result : LuisResult = None
263
262
264
263
if luis_prediction_options :
265
- options = self . _merge_options ( luis_prediction_options )
264
+ options = luis_prediction_options
266
265
else :
267
266
options = self ._options
268
267
@@ -271,71 +270,49 @@ async def _recognize_internal(
271
270
text = utterance , intents = {"" : IntentScore (score = 1.0 )}, entities = {}
272
271
)
273
272
else :
274
- luis_result = self ._runtime .prediction .resolve (
275
- self ._application .application_id ,
276
- utterance ,
277
- timezone_offset = options .timezone_offset ,
278
- verbose = options .include_all_intents ,
279
- staging = options .staging ,
280
- spell_check = options .spell_check ,
281
- bing_spell_check_subscription_key = options .bing_spell_check_subscription_key ,
282
- log = options .log if options .log is not None else True ,
283
- )
284
273
285
- recognizer_result = RecognizerResult (
286
- text = utterance ,
287
- altered_text = luis_result .altered_query ,
288
- intents = LuisUtil .get_intents (luis_result ),
289
- entities = LuisUtil .extract_entities_and_metadata (
290
- luis_result .entities ,
291
- luis_result .composite_entities ,
292
- options .include_instance_data
293
- if options .include_instance_data is not None
294
- else True ,
295
- ),
296
- )
297
- LuisUtil .add_properties (luis_result , recognizer_result )
298
- if self ._include_api_results :
299
- recognizer_result .properties ["luisResult" ] = luis_result
274
+ luis_recognizer = self ._build_recognizer (options )
275
+ recognizer_result = await luis_recognizer .recognizer_internal (turn_context )
300
276
301
277
# Log telemetry
302
278
self .on_recognizer_result (
303
279
recognizer_result , turn_context , telemetry_properties , telemetry_metrics
304
280
)
305
281
306
- await self ._emit_trace_info (
307
- turn_context , luis_result , recognizer_result , options
308
- )
309
-
310
282
return recognizer_result
311
283
312
- async def _emit_trace_info (
313
- self ,
314
- turn_context : TurnContext ,
315
- luis_result : LuisResult ,
316
- recognizer_result : RecognizerResult ,
317
- options : LuisPredictionOptions ,
318
- ) -> None :
319
- trace_info : Dict [str , object ] = {
320
- "recognizerResult" : LuisUtil .recognizer_result_as_dict (recognizer_result ),
321
- "luisModel" : {"ModelID" : self ._application .application_id },
322
- "luisOptions" : {"Staging" : options .staging },
323
- "luisResult" : LuisUtil .luis_result_as_dict (luis_result ),
324
- }
325
-
326
- trace_activity = ActivityUtil .create_trace (
327
- turn_context .activity ,
328
- "LuisRecognizer" ,
329
- trace_info ,
330
- LuisRecognizer .luis_trace_type ,
331
- LuisRecognizer .luis_trace_label ,
332
- )
333
-
334
- await turn_context .send_activity (trace_activity )
335
-
336
284
def _merge_options (
337
- self , user_defined_options : LuisPredictionOptions
285
+ self ,
286
+ user_defined_options : Union [
287
+ LuisRecognizerOptionsV3 , LuisRecognizerOptionsV2 , LuisPredictionOptions
288
+ ],
338
289
) -> LuisPredictionOptions :
339
290
merged_options = LuisPredictionOptions ()
340
291
merged_options .__dict__ .update (user_defined_options .__dict__ )
341
292
return merged_options
293
+
294
+ def _build_recognizer (
295
+ self ,
296
+ luis_prediction_options : Union [
297
+ LuisRecognizerOptionsV3 , LuisRecognizerOptionsV2 , LuisPredictionOptions
298
+ ],
299
+ ):
300
+ if isinstance (luis_prediction_options , LuisRecognizerOptionsV3 ):
301
+ return LuisRecognizerV3 (self ._application , luis_prediction_options )
302
+ if isinstance (luis_prediction_options , LuisRecognizerOptionsV2 ):
303
+ return LuisRecognizerV3 (self ._application , luis_prediction_options )
304
+
305
+ recognizer_options = LuisRecognizerOptionsV2 (
306
+ luis_prediction_options .bing_spell_check_subscription_key ,
307
+ luis_prediction_options .include_all_intents ,
308
+ luis_prediction_options .include_instance_data ,
309
+ luis_prediction_options .log ,
310
+ luis_prediction_options .spell_check ,
311
+ luis_prediction_options .staging ,
312
+ luis_prediction_options .timeout ,
313
+ luis_prediction_options .timezone_offset ,
314
+ self ._include_api_results ,
315
+ luis_prediction_options .telemetry_client ,
316
+ luis_prediction_options .log_personal_information ,
317
+ )
318
+ return LuisRecognizerV2 (self ._application , recognizer_options )
0 commit comments