Exemple #1
0
        /// <summary>
        /// Gets an <see cref="IQnAMakerClient"/> to use to access the QnA Maker knowledge base.
        /// </summary>
        /// <param name="dc">The <see cref="DialogContext"/> for the current turn of conversation.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        /// <remarks>If the task is successful, the result contains the QnA Maker client to use.</remarks>
        protected virtual async Task <IQnAMakerClient> GetQnAMakerClientAsync(DialogContext dc)
        {
            var qnaClient = dc.Context.TurnState.Get <IQnAMakerClient>();

            if (qnaClient != null)
            {
                // return mock client
                return(qnaClient);
            }

            var httpClient = dc.Context.TurnState.Get <HttpClient>() ?? HttpClient;

            var endpoint = new QnAMakerEndpoint
            {
                EndpointKey     = this.EndpointKey.GetValue(dc.State),
                Host            = this.HostName.GetValue(dc.State),
                KnowledgeBaseId = KnowledgeBaseId.GetValue(dc.State),
                QnAServiceType  = QnAServiceType.GetValue(dc.State)
            };

            var options = await GetQnAMakerOptionsAsync(dc).ConfigureAwait(false);

            if (endpoint.QnAServiceType == ServiceType.Language)
            {
                return(new CustomQuestionAnswering(endpoint, options, httpClient, TelemetryClient, LogPersonalInformation.GetValue(dc.State)));
            }

            return(new QnAMaker(endpoint, options, httpClient, TelemetryClient, LogPersonalInformation.GetValue(dc.State)));
        }
        /// <summary>
        /// Uses the RecognizerResult to create a list of properties to be included when tracking the result in telemetry.
        /// </summary>
        /// <param name="recognizerResult">Recognizer Result.</param>
        /// <param name="telemetryProperties">A list of properties to append or override the properties created using the RecognizerResult.</param>
        /// <param name="dialogContext">Dialog Context.</param>
        /// <returns>A dictionary that can be included when calling the TrackEvent method on the TelemetryClient.</returns>
        protected override Dictionary<string, string> FillRecognizerResultTelemetryProperties(RecognizerResult recognizerResult, Dictionary<string, string> telemetryProperties, DialogContext dialogContext = null)
        {
            if (dialogContext == null)
            {
                throw new ArgumentNullException(nameof(dialogContext), "DialogContext needed for state in AdaptiveRecognizer.FillRecognizerResultTelemetryProperties method.");
            }

            var orderedIntents = recognizerResult.Intents.Any() ? recognizerResult.Intents.OrderByDescending(key => key.Value.Score) : null;
            var properties = new Dictionary<string, string>
            {
                { "TopIntent", recognizerResult.Intents.Any() ? orderedIntents.First().Key : null },
                { "TopIntentScore", recognizerResult.Intents.Any() ? orderedIntents.First().Value?.Score?.ToString("N1", CultureInfo.InvariantCulture) : null },
                { "NextIntent", recognizerResult.Intents.Count > 1 ? orderedIntents.ElementAtOrDefault(1).Key : null },
                { "NextIntentScore", recognizerResult.Intents.Count > 1 ? orderedIntents.ElementAtOrDefault(1).Value?.Score?.ToString("N1", CultureInfo.InvariantCulture) : null },
                { "Intents", recognizerResult.Intents.Any() ? JsonConvert.SerializeObject(recognizerResult.Intents) : null },
                { "Entities", recognizerResult.Entities?.ToString() },
                { "AdditionalProperties", recognizerResult.Properties.Any() ? JsonConvert.SerializeObject(recognizerResult.Properties) : null },
            };

            var (logPersonalInfo, error) = LogPersonalInformation.TryGetValue(dialogContext.State);

            if (logPersonalInfo && !string.IsNullOrEmpty(recognizerResult.Text))
            {
                properties.Add("Text", recognizerResult.Text);
                properties.Add("AlteredText", recognizerResult.AlteredText);
            }

            // Additional Properties can override "stock" properties.
            if (telemetryProperties != null)
            {
                return telemetryProperties.Concat(properties)
                    .GroupBy(kv => kv.Key)
                    .ToDictionary(g => g.Key, g => g.First().Value);
            }

            return properties;
        }