/// <summary>
 /// Return results of the analysis (Suggested actions and intents).
 /// </summary>
 /// <typeparam name="T">The recognition result type.</typeparam>
 /// <param name="turnContext">Context object containing information for a single turn of conversation with a user.</param>
 /// <param name="recognizerOptions">A <see cref="LuisRecognizerOptions"/> instance to be used by the call.
 /// This parameter overrides the default <see cref="LuisRecognizerOptions"/> passed in the constructor.</param>
 /// <param name="telemetryProperties">Additional properties to be logged to telemetry with the LuisResult event.</param>
 /// <param name="telemetryMetrics">Additional metrics to be logged to telemetry with the LuisResult event.</param>
 /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
 /// <returns>The LUIS results of the analysis of the current message text in the current turn's context activity.</returns>
 public virtual async Task<T> RecognizeAsync<T>(ITurnContext turnContext, LuisRecognizerOptions recognizerOptions, Dictionary<string, string> telemetryProperties, Dictionary<string, double> telemetryMetrics = null, CancellationToken cancellationToken = default(CancellationToken))
     where T : IRecognizerConvert, new()
 {
     var result = new T();
     result.Convert(await RecognizeInternalAsync(turnContext, recognizerOptions, telemetryProperties, telemetryMetrics, cancellationToken).ConfigureAwait(false));
     return result;
 }
 /// <summary>
 /// Runs an utterance through a recognizer and returns a strongly-typed recognizer result.
 /// </summary>
 /// <typeparam name="T">The recognition result type.</typeparam>
 /// <param name="turnContext">Turn context.</param>
 /// <param name="recognizerOptions">A <see cref="LuisRecognizerOptions"/> instance to be used by the call.
 /// This parameter overrides the default <see cref="LuisRecognizerOptions"/> passed in the constructor.</param>
 /// <param name="cancellationToken">Cancellation token.</param>
 /// <returns>Analysis of utterance.</returns>
 public virtual async Task<T> RecognizeAsync<T>(ITurnContext turnContext, LuisRecognizerOptions recognizerOptions, CancellationToken cancellationToken)
     where T : IRecognizerConvert, new()
 {
     var result = new T();
     result.Convert(await RecognizeInternalAsync(turnContext, recognizerOptions, null, null, cancellationToken).ConfigureAwait(false));
     return result;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="LuisRecognizer"/> class.
        /// </summary>
        /// <param name="recognizerOptions"> The LUIS recognizer version options.</param>
        /// <param name="clientHandler">(Optional) Custom handler for LUIS API calls to allow mocking.</param>
        public LuisRecognizer(LuisRecognizerOptions recognizerOptions, HttpClientHandler clientHandler = null)
        {
            _luisRecognizerOptions = recognizerOptions;

            TelemetryClient        = recognizerOptions.TelemetryClient;
            LogPersonalInformation = recognizerOptions.LogPersonalInformation;

            var delegatingHandler = new LuisDelegatingHandler();
            var httpClientHandler = clientHandler ?? CreateRootHandler();
            var currentHandler    = CreateHttpHandlerPipeline(httpClientHandler, delegatingHandler);

            DefaultHttpClient = new HttpClient(currentHandler, false)
            {
                Timeout = TimeSpan.FromMilliseconds(recognizerOptions.Timeout),
            };
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LuisRecognizer"/> class.
        /// </summary>
        /// <param name="application">The LUIS application to use to recognize text.</param>
        /// <param name="recognizerOptions">(Optional) Options for the created recognizer.</param>
        /// <param name="predictionOptions">(Optional) The default LUIS prediction options to use.</param>
        public LuisRecognizer(LuisApplication application, LuisRecognizerOptions recognizerOptions = null, LuisPredictionOptions predictionOptions = null)
        {
            recognizerOptions  = recognizerOptions ?? new LuisRecognizerOptions();
            _application       = application ?? throw new ArgumentNullException(nameof(application));
            _predictionOptions = predictionOptions ?? new LuisPredictionOptions();

            TelemetryClient        = recognizerOptions.TelemetryClient;
            LogPersonalInformation = recognizerOptions.LogPersonalInformation;

            var delegatingHandler = new LuisDelegatingHandler();
            var httpClientHandler = recognizerOptions.HttpClient ?? CreateRootHandler();
            var currentHandler    = CreateHttpHandlerPipeline(httpClientHandler, delegatingHandler);

            DefaultHttpClient = new HttpClient(currentHandler, false)
            {
                Timeout = recognizerOptions.Timeout,
            };

            DefaultHttpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _application.EndpointKey);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LuisRecognizer"/> class.
        /// </summary>
        /// <param name="recognizerOptions"> The LUIS recognizer version options.</param>
        /// <param name="clientHandler">(Optional) Custom handler for LUIS API calls to allow mocking.</param>
        public LuisRecognizer(LuisRecognizerOptions recognizerOptions, HttpClientHandler clientHandler = null)
        {
            _luisRecognizerOptions = recognizerOptions;

            TelemetryClient        = recognizerOptions.TelemetryClient;
            LogPersonalInformation = recognizerOptions.LogPersonalInformation;

            var delegatingHandler = new LuisDelegatingHandler();
            var httpClientHandler = clientHandler ?? CreateRootHandler();

#pragma warning disable CA2000 // Dispose objects before losing scope (suppressing this warning, for now! we will address this once we implement HttpClientFactory in a future release)
            var currentHandler = CreateHttpHandlerPipeline(httpClientHandler, delegatingHandler);
#pragma warning restore CA2000 // Dispose objects before losing scope

            HttpClient = new HttpClient(currentHandler, false)
            {
                Timeout = TimeSpan.FromMilliseconds(recognizerOptions.Timeout),
            };

#pragma warning disable 618 // Reference to obsolete property, this is here only for backward compat and should be removed when DefaultHttpClient is removed.
            DefaultHttpClient = HttpClient;
#pragma warning restore 618
        }
        /// <summary>
        /// Returns a RecognizerResult object.
        /// </summary>
        /// <param name="turnContext">Dialog turn Context.</param>
        /// <param name="predictionOptions">LuisRecognizerOptions implementation to override current properties.</param>
        /// <param name="telemetryProperties"> Additional properties to be logged to telemetry with the LuisResult event.</param>
        /// <param name="telemetryMetrics">Additional metrics to be logged to telemetry with the LuisResult event.</param>
        /// <returns>RecognizerResult object.</returns>
        private async Task <RecognizerResult> RecognizeInternalAsync(ITurnContext turnContext, LuisRecognizerOptions predictionOptions, Dictionary <string, string> telemetryProperties, Dictionary <string, double> telemetryMetrics, CancellationToken cancellationToken)
        {
            var recognizer = predictionOptions ?? _luisRecognizerOptions;
            var result     = await recognizer.RecognizeInternalAsync(turnContext, DefaultHttpClient, cancellationToken).ConfigureAwait(false);

            await OnRecognizerResultAsync(result, turnContext, telemetryProperties, telemetryMetrics, cancellationToken).ConfigureAwait(false);

            return(result);
        }
 /// <summary>
 /// Return results of the analysis (Suggested actions and intents).
 /// </summary>
 /// <param name="turnContext">Context object containing information for a single turn of conversation with a user.</param>
 /// <param name="recognizerOptions">A <see cref="LuisRecognizerOptions"/> instance to be used by the call.
 /// This parameter overrides the default <see cref="LuisRecognizerOptions"/> passed in the constructor.</param>
 /// <param name="telemetryProperties">Additional properties to be logged to telemetry with the LuisResult event.</param>
 /// <param name="telemetryMetrics">Additional metrics to be logged to telemetry with the LuisResult event.</param>
 /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
 /// <returns>The LUIS results of the analysis of the current message text in the current turn's context activity.</returns>
 public virtual async Task <RecognizerResult> RecognizeAsync(ITurnContext turnContext, LuisRecognizerOptions recognizerOptions, Dictionary <string, string> telemetryProperties, Dictionary <string, double> telemetryMetrics = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(await RecognizeInternalAsync(turnContext, recognizerOptions, telemetryProperties, telemetryMetrics, cancellationToken).ConfigureAwait(false));
 }
 /// <summary>
 /// Runs an utterance through a recognizer and returns a generic recognizer result.
 /// </summary>
 /// <param name="turnContext">Turn context.</param>
 /// <param name="recognizerOptions">A <see cref="LuisRecognizerOptions"/> instance to be used by the call.
 /// This parameter overrides the default <see cref="LuisRecognizerOptions"/> passed in the constructor.</param>
 /// <param name="cancellationToken">Cancellation token.</param>
 /// <returns>Analysis of utterance.</returns>
 public virtual async Task <RecognizerResult> RecognizeAsync(ITurnContext turnContext, LuisRecognizerOptions recognizerOptions, CancellationToken cancellationToken)
 {
     return(await RecognizeInternalAsync(turnContext, recognizerOptions, null, null, cancellationToken).ConfigureAwait(false));
 }
        /// <summary>
        /// Runs an utterance through a recognizer and returns a strongly-typed recognizer result.
        /// </summary>
        /// <typeparam name="T">The recognition result type.</typeparam>
        /// <param name="dialogContext">dialog context.</param>
        /// <param name="activity">activity to recognize.</param>
        /// <param name="recognizerOptions">A <see cref="LuisRecognizerOptions"/> instance to be used by the call.
        /// This parameter overrides the default <see cref="LuisRecognizerOptions"/> passed in the constructor.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>Analysis of utterance.</returns>
        public virtual async Task <T> RecognizeAsync <T>(DialogContext dialogContext, Activity activity, LuisRecognizerOptions recognizerOptions, CancellationToken cancellationToken)
            where T : IRecognizerConvert, new()
        {
            var result = new T();

            result.Convert(await RecognizeInternalAsync(dialogContext, activity, recognizerOptions, null, null, cancellationToken).ConfigureAwait(false));
            return(result);
        }
 /// <summary>
 /// Runs an utterance through a recognizer and returns a generic recognizer result.
 /// </summary>
 /// <param name="dialogContext">dialog context.</param>
 /// <param name="activity">activity to recognize.</param>
 /// <param name="recognizerOptions">A <see cref="LuisRecognizerOptions"/> instance to be used by the call.
 /// This parameter overrides the default <see cref="LuisRecognizerOptions"/> passed in the constructor.</param>
 /// <param name="cancellationToken">Cancellation token.</param>
 /// <returns>Analysis of utterance.</returns>
 public virtual async Task <RecognizerResult> RecognizeAsync(DialogContext dialogContext, Activity activity, LuisRecognizerOptions recognizerOptions, CancellationToken cancellationToken)
 {
     return(await RecognizeInternalAsync(dialogContext, activity, recognizerOptions, null, null, cancellationToken).ConfigureAwait(false));
 }
Exemplo n.º 11
0
        /// <summary>
        /// Returns a RecognizerResult object.
        /// </summary>
        /// <param name="utterance">utterance to recognize.</param>
        /// <param name="predictionOptions">LuisRecognizerOptions implementation to override current properties.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>RecognizerResult object.</returns>
        private async Task <RecognizerResult> RecognizeInternalAsync(string utterance, LuisRecognizerOptions predictionOptions, CancellationToken cancellationToken)
        {
            var recognizer = predictionOptions ?? _luisRecognizerOptions;
            var result     = await recognizer.RecognizeInternalAsync(utterance, HttpClient, cancellationToken).ConfigureAwait(false);

            return(result);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Return results of the analysis (Suggested actions and intents).
 /// </summary>
 /// <remarks>No telemetry is provided when using this method.</remarks>
 /// <param name="utterance">utterance to recognize.</param>
 /// <param name="recognizerOptions">A <see cref="LuisRecognizerOptions"/> instance to be used by the call.
 /// This parameter overrides the default <see cref="LuisRecognizerOptions"/> passed in the constructor.</param>
 /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
 /// <returns>The LUIS results of the analysis of the current message text in the current turn's context activity.</returns>
 public virtual async Task <RecognizerResult> RecognizeAsync(string utterance, LuisRecognizerOptions recognizerOptions = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     recognizerOptions ??= _luisRecognizerOptions;
     return(await RecognizeInternalAsync(utterance, recognizerOptions, cancellationToken).ConfigureAwait(false));
 }