/// <summary>
        /// Gets an instance of <see cref="IQnAMakerClient"/>.
        /// </summary>
        /// <param name="dc">The <see cref="DialogContext"/> used to access state.</param>
        /// <returns>An instance of <see cref="IQnAMakerClient"/>.</returns>
        protected virtual Task <IQnAMakerClient> GetQnAMakerClientAsync(DialogContext dc)
        {
            var qnaClient = dc.Context.TurnState.Get <IQnAMakerClient>();

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

            var(epKey, error)            = EndpointKey.TryGetValue(dc.State);
            var(hn, error2)              = HostName.TryGetValue(dc.State);
            var(kbId, error3)            = KnowledgeBaseId.TryGetValue(dc.State);
            var(logPersonalInfo, error4) = LogPersonalInformation.TryGetValue(dc.State);

            var endpoint = new QnAMakerEndpoint
            {
                EndpointKey = epKey ?? throw new InvalidOperationException($"Unable to get a value for {nameof(EndpointKey)} from state. {error}"),
                                    Host = hn ?? throw new InvalidOperationException($"Unable to a get value for {nameof(HostName)} from state. {error2}"),
                                                 KnowledgeBaseId = kbId ?? throw new InvalidOperationException($"Unable to get a value for {nameof(KnowledgeBaseId)} from state. {error3}")
            };

            return(Task.FromResult <IQnAMakerClient>(new QnAMaker(endpoint, new QnAMakerOptions(), HttpClient, TelemetryClient, logPersonalInfo)));
        }
    }
Example #2
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)));
        }