33
44import json
55from 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-
116from botbuilder .core import (
127 BotAssert ,
138 IntentScore ,
1611 TurnContext ,
1712)
1813from botbuilder .schema import ActivityTypes
19-
2014from . 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
2319
2420
2521class LuisRecognizer (Recognizer ):
@@ -36,7 +32,9 @@ class LuisRecognizer(Recognizer):
3632 def __init__ (
3733 self ,
3834 application : Union [LuisApplication , str ],
39- prediction_options : LuisPredictionOptions = None ,
35+ prediction_options : Union [
36+ LuisRecognizerOptionsV2 , LuisRecognizerOptionsV3 , LuisPredictionOptions
37+ ] = None ,
4038 include_api_results : bool = False ,
4139 ):
4240 """Initializes a new instance of the <see cref="LuisRecognizer"/> class.
@@ -59,17 +57,17 @@ def __init__(
5957 )
6058
6159 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+ )
6467
6568 self .telemetry_client = self ._options .telemetry_client
6669 self .log_personal_information = self ._options .log_personal_information
6770
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-
7371 @staticmethod
7472 def top_intent (
7573 results : RecognizerResult , default_intent : str = "None" , min_score : float = 0.0
@@ -249,7 +247,9 @@ async def _recognize_internal(
249247 turn_context : TurnContext ,
250248 telemetry_properties : Dict [str , str ],
251249 telemetry_metrics : Dict [str , float ],
252- luis_prediction_options : LuisPredictionOptions = None ,
250+ luis_prediction_options : Union [
251+ LuisPredictionOptions , LuisRecognizerOptionsV2 , LuisRecognizerOptionsV3
252+ ] = None ,
253253 ) -> RecognizerResult :
254254
255255 BotAssert .context_not_none (turn_context )
@@ -259,10 +259,9 @@ async def _recognize_internal(
259259
260260 utterance : str = turn_context .activity .text if turn_context .activity is not None else None
261261 recognizer_result : RecognizerResult = None
262- luis_result : LuisResult = None
263262
264263 if luis_prediction_options :
265- options = self . _merge_options ( luis_prediction_options )
264+ options = luis_prediction_options
266265 else :
267266 options = self ._options
268267
@@ -271,71 +270,49 @@ async def _recognize_internal(
271270 text = utterance , intents = {"" : IntentScore (score = 1.0 )}, entities = {}
272271 )
273272 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- )
284273
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 )
300276
301277 # Log telemetry
302278 self .on_recognizer_result (
303279 recognizer_result , turn_context , telemetry_properties , telemetry_metrics
304280 )
305281
306- await self ._emit_trace_info (
307- turn_context , luis_result , recognizer_result , options
308- )
309-
310282 return recognizer_result
311283
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-
336284 def _merge_options (
337- self , user_defined_options : LuisPredictionOptions
285+ self ,
286+ user_defined_options : Union [
287+ LuisRecognizerOptionsV3 , LuisRecognizerOptionsV2 , LuisPredictionOptions
288+ ],
338289 ) -> LuisPredictionOptions :
339290 merged_options = LuisPredictionOptions ()
340291 merged_options .__dict__ .update (user_defined_options .__dict__ )
341292 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