public override async Task <DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            var endpoint = new QnAMakerEndpoint
            {
                EndpointKey                       = endpointkey.TryEvaluate(dc.GetState()).error == null?endpointkey.TryEvaluate(dc.GetState()).value.ToString() : this.EndpointKey,
                                             Host = hostname.TryEvaluate(dc.GetState()).error == null?hostname.TryEvaluate(dc.GetState()).value.ToString() : this.HostName,
                                                        KnowledgeBaseId = knowledgebaseId.TryEvaluate(dc.GetState()).error == null?knowledgebaseId.TryEvaluate(dc.GetState()).value.ToString() : this.KnowledgeBaseId
            };

            var qnamakerOptions = new QnAMakerOptions
            {
                ScoreThreshold = this.Threshold,
                StrictFilters  = this.StrictFilters
            };

            if (this.QnaMakerClient == null)
            {
                this.QnaMakerClient = new QnAMaker(endpoint, qnamakerOptions, httpClient);
            }

            if (dc.Context?.Activity?.Type != ActivityTypes.Message)
            {
                return(EndOfTurn);
            }

            return(await ExecuteAdaptiveQnAMakerDialog(dc, this.QnaMakerClient, qnamakerOptions, cancellationToken).ConfigureAwait(false));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CustomQuestionAnswering"/> class.
        /// </summary>
        /// <param name="endpoint">The <see cref="QnAMakerEndpoint"/> of the knowledge base to query.</param>
        /// <param name="options">The <see cref="QnAMakerOptions"/> for the Custom Question Answering Knowledge Base.</param>
        /// <param name="httpClient">An alternate client with which to talk to Language Service.
        /// If null, a default client is used for this instance.</param>
        /// <param name="telemetryClient">The IBotTelemetryClient used for logging telemetry events.</param>
        /// <param name="logPersonalInformation">Set to true to include personally identifiable information in telemetry events.</param>
        public CustomQuestionAnswering(QnAMakerEndpoint endpoint, QnAMakerOptions options, HttpClient httpClient, IBotTelemetryClient telemetryClient, bool logPersonalInformation = false)
        {
            _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));

            if (string.IsNullOrEmpty(endpoint.KnowledgeBaseId))
            {
                throw new ArgumentException(nameof(endpoint.KnowledgeBaseId));
            }

            if (string.IsNullOrEmpty(endpoint.Host))
            {
                throw new ArgumentException(nameof(endpoint.Host));
            }

            if (string.IsNullOrEmpty(endpoint.EndpointKey))
            {
                throw new ArgumentException(nameof(endpoint.EndpointKey));
            }

            if (_endpoint.Host.EndsWith("v2.0", StringComparison.Ordinal) || _endpoint.Host.EndsWith("v3.0", StringComparison.Ordinal))
            {
                throw new NotSupportedException("v2.0 and v3.0 of QnA Maker service is no longer supported in the QnA Maker.");
            }

            _httpClient = httpClient ?? DefaultHttpClient;

            TelemetryClient        = telemetryClient ?? new NullBotTelemetryClient();
            LogPersonalInformation = logPersonalInformation;

            _languageServiceHelper = new LanguageServiceUtils(TelemetryClient, _httpClient, endpoint, options);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="QnAMaker"/> class.
        /// </summary>
        /// <param name="endpoint">The endpoint of the knowledge base to query.</param>
        /// <param name="options">The options for the QnA Maker knowledge base.</param>
        /// <param name="httpClient">An alternate client with which to talk to QnAMaker.
        /// If null, a default client is used for this instance.</param>
        public QnAMaker(QnAMakerEndpoint endpoint, QnAMakerOptions options = null, HttpClient httpClient = null)
        {
            _httpClient = httpClient ?? DefaultHttpClient;

            _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));

            if (string.IsNullOrEmpty(endpoint.KnowledgeBaseId))
            {
                throw new ArgumentException(nameof(endpoint.KnowledgeBaseId));
            }

            if (string.IsNullOrEmpty(endpoint.Host))
            {
                throw new ArgumentException(nameof(endpoint.Host));
            }

            if (string.IsNullOrEmpty(endpoint.EndpointKey))
            {
                throw new ArgumentException(nameof(endpoint.EndpointKey));
            }

            if (_endpoint.Host.EndsWith("v2.0"))
            {
                throw new NotSupportedException("v2.0 of QnA Maker service is no longer supported in the Bot Framework. Please upgrade your QnA Maker service at www.qnamaker.ai.");
            }

            _isLegacyProtocol = _endpoint.Host.EndsWith("v3.0");

            _options = options ?? new QnAMakerOptions();

            ValidateOptions(_options);
        }
 private static void SetHeaders(HttpRequestMessage request, QnAMakerEndpoint endpoint)
 {
     request.Headers.Add("Authorization", $"EndpointKey {endpoint.EndpointKey}");
     request.Headers.Add("Ocp-Apim-Subscription-Key", endpoint.EndpointKey);
     request.Headers.UserAgent.Add(botBuilderInfo);
     request.Headers.UserAgent.Add(platformInfo);
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="GenerateAnswerHelper"/> class.
        /// </summary>
        /// <param name="telemetryClient">Telemetry client.</param>
        /// <param name="endpoint">QnA Maker endpoint details.</param>
        /// <param name="options">QnA Maker options.</param>
        /// <param name="httpClient">Http client.</param>
        /// <param name="logPersonalInformation">Log personal Information.</param>
        public GenerateAnswerHelper(IBotTelemetryClient telemetryClient, QnAMakerEndpoint endpoint, QnAMakerOptions options, HttpClient httpClient)
        {
            this.telemetryClient = telemetryClient;
            this._endpoint       = endpoint;

            this.Options = options ?? new QnAMakerOptions();
            ValidateOptions(this.Options);
            this.httpClient = httpClient;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GenerateAnswerUtils"/> class.
        /// </summary>
        /// <param name="telemetryClient">Telemetry client.</param>
        /// <param name="endpoint">QnA Maker endpoint details.</param>
        /// <param name="options">QnA Maker options.</param>
        /// <param name="httpClient">Http client.</param>
        public GenerateAnswerUtils(IBotTelemetryClient telemetryClient, QnAMakerEndpoint endpoint, QnAMakerOptions options, HttpClient httpClient)
        {
            _telemetryClient = telemetryClient;
            _endpoint        = endpoint;

            Options = options ?? new QnAMakerOptions();
            ValidateOptions(Options);
            _httpClient = httpClient;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="QnAMaker"/> class.
        /// </summary>
        /// <param name="endpoint">The endpoint of the knowledge base to query.</param>
        /// <param name="options">The options for the QnA Maker knowledge base.</param>
        /// <param name="httpClient">An alternate client with which to talk to QnAMaker.
        /// If null, a default client is used for this instance.</param>
        public QnAMaker(QnAMakerEndpoint endpoint, QnAMakerOptions options = null, HttpClient httpClient = null)
        {
            _httpClient = httpClient ?? DefaultHttpClient;

            _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));

            if (string.IsNullOrEmpty(endpoint.KnowledgeBaseId))
            {
                throw new ArgumentException(nameof(endpoint.KnowledgeBaseId));
            }

            if (string.IsNullOrEmpty(endpoint.Host))
            {
                throw new ArgumentException(nameof(endpoint.Host));
            }

            if (string.IsNullOrEmpty(endpoint.EndpointKey))
            {
                throw new ArgumentException(nameof(endpoint.EndpointKey));
            }

            _options = options ?? new QnAMakerOptions();

            if (_options.ScoreThreshold == 0)
            {
                _options.ScoreThreshold = 0.3F;
            }

            if (_options.Top == 0)
            {
                _options.Top = 1;
            }

            if (_options.ScoreThreshold < 0 || _options.ScoreThreshold > 1)
            {
                throw new ArgumentOutOfRangeException(nameof(_options.ScoreThreshold), "Score threshold should be a value between 0 and 1");
            }

            if (_options.Top < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(_options.Top), "Top should be an integer greater than 0");
            }

            if (_options.StrictFilters == null)
            {
                _options.StrictFilters = new Metadata[] { };
            }

            if (_options.MetadataBoost == null)
            {
                _options.MetadataBoost = new Metadata[] { };
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="QnAMaker"/> class.
        /// </summary>
        /// <param name="endpoint">The endpoint of the knowledge base to query.</param>
        /// <param name="options">The options for the QnA Maker knowledge base.</param>
        /// <param name="httpClient">An alternate client with which to talk to QnAMaker.
        /// If null, a default client is used for this instance.</param>
        /// <param name="telemetryClient">The IBotTelemetryClient used for logging telemetry events.</param>
        /// <param name="logPersonalInformation">Set to true to include personally indentifiable information in telemetry events.</param>
        public QnAMaker(QnAMakerEndpoint endpoint, QnAMakerOptions options, HttpClient httpClient, IBotTelemetryClient telemetryClient, bool logPersonalInformation = false)
        {
            _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));

            if (string.IsNullOrEmpty(endpoint.KnowledgeBaseId))
            {
                throw new ArgumentException(nameof(endpoint.KnowledgeBaseId));
            }

            if (string.IsNullOrEmpty(endpoint.Host))
            {
                throw new ArgumentException(nameof(endpoint.Host));
            }

            if (string.IsNullOrEmpty(endpoint.EndpointKey))
            {
                throw new ArgumentException(nameof(endpoint.EndpointKey));
            }

            if (_endpoint.Host.EndsWith("v2.0"))
            {
                throw new NotSupportedException("v2.0 of QnA Maker service is no longer supported in the Bot Framework. Please upgrade your QnA Maker service at www.qnamaker.ai.");
            }

            _options = options ?? new QnAMakerOptions();
            ValidateOptions(_options);

            if (httpClient == null)
            {
                // assign DefaultHttpClient to _httpClient to expose Timeout in unit tests
                // and keep HttpClient usage as a singleton by default
                DefaultHttpClient.Timeout = TimeSpan.FromMilliseconds(_options.Timeout);
                _httpClient = DefaultHttpClient;
            }
            else
            {
                _httpClient = httpClient;
            }

            _isLegacyProtocol = _endpoint.Host.EndsWith("v3.0");

            TelemetryClient        = telemetryClient ?? new NullBotTelemetryClient();
            LogPersonalInformation = logPersonalInformation;
        }
Exemplo n.º 9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="QnAMaker"/> class.
        /// </summary>
        /// <param name="endpoint">The endpoint of the knowledge base to query.</param>
        /// <param name="options">The options for the QnA Maker knowledge base.</param>
        /// <param name="httpClient">An alternate client with which to talk to QnAMaker.
        /// If null, a default client is used for this instance.</param>
        /// <param name="telemetryClient">The IBotTelemetryClient used for logging telemetry events.</param>
        /// <param name="logPersonalInformation">Set to true to include personally identifiable information in telemetry events.</param>
        public QnAMaker(QnAMakerEndpoint endpoint, QnAMakerOptions options, HttpClient httpClient, IBotTelemetryClient telemetryClient, bool logPersonalInformation = false)
        {
            _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));

            if (string.IsNullOrEmpty(endpoint.KnowledgeBaseId))
            {
                throw new ArgumentException(nameof(endpoint.KnowledgeBaseId));
            }

            if (string.IsNullOrEmpty(endpoint.Host))
            {
                throw new ArgumentException(nameof(endpoint.Host));
            }

            if (string.IsNullOrEmpty(endpoint.EndpointKey))
            {
                throw new ArgumentException(nameof(endpoint.EndpointKey));
            }

            if (_endpoint.Host.EndsWith("v2.0") || _endpoint.Host.EndsWith("v3.0"))
            {
                throw new NotSupportedException("v2.0 and v3.0 of QnA Maker service is no longer supported in the QnA Maker.");
            }

            if (httpClient == null)
            {
                _httpClient = DefaultHttpClient;
            }
            else
            {
                _httpClient = httpClient;
            }

            TelemetryClient        = telemetryClient ?? new NullBotTelemetryClient();
            LogPersonalInformation = logPersonalInformation;

            this.generateAnswerHelper      = new GenerateAnswerUtils(TelemetryClient, _endpoint, options, _httpClient);
            this.activeLearningTrainHelper = new TrainUtils(_endpoint, _httpClient);
        }
Exemplo n.º 10
0
 private void SetHeaders(HttpRequestMessage request, QnAMakerEndpoint endpoint)
 {
     request.Headers.Add("Authorization", $"EndpointKey {endpoint.EndpointKey}");
     request.Headers.UserAgent.Add(BotBuilderInfo);
     request.Headers.UserAgent.Add(PlatformInfo);
 }
Exemplo n.º 11
0
        /// <summary>
        /// Execute Http request.
        /// </summary>
        /// <param name="requestUrl">Http request url.</param>
        /// <param name="payloadBody">Http request body.</param>
        /// <param name="endpoint">QnA Maker endpoint details.</param>
        /// <returns>Returns http response object.</returns>
        public async Task <HttpResponseMessage> ExecuteHttpRequest(string requestUrl, string payloadBody, QnAMakerEndpoint endpoint)
        {
            if (requestUrl == null)
            {
                throw new ArgumentNullException(nameof(requestUrl), "Request url can not be null.");
            }

            if (payloadBody == null)
            {
                throw new ArgumentNullException(nameof(payloadBody), "Payload body can not be null.");
            }

            if (endpoint == null)
            {
                throw new ArgumentNullException(nameof(endpoint));
            }

            var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);

            request.Content = new StringContent(payloadBody, Encoding.UTF8, "application/json");

            SetHeaders(request, endpoint);

            var response = await this._httpClient.SendAsync(request).ConfigureAwait(false);

            response.EnsureSuccessStatusCode();

            return(response);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="QnAMaker"/> class.
 /// </summary>
 /// <param name="endpoint">The endpoint of the knowledge base to query.</param>
 /// <param name="options">The options for the QnA Maker knowledge base.</param>
 /// <param name="httpClient">An alternate client with which to talk to QnAMaker.
 /// If null, a default client is used for this instance.</param>
 public QnAMaker(QnAMakerEndpoint endpoint, QnAMakerOptions options = null, HttpClient httpClient = null)
     : this(endpoint, options, httpClient, null)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TrainUtils"/> class.
 /// </summary>
 /// <param name="endpoint">QnA Maker endpoint details.</param>
 /// <param name="httpClient">Http client.</param>
 public TrainUtils(QnAMakerEndpoint endpoint, HttpClient httpClient)
 {
     this._endpoint  = endpoint;
     this.httpClient = httpClient;
 }
Exemplo n.º 14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CustomQuestionAnswering"/> class.
 /// </summary>
 /// <param name="endpoint">The <see cref="QnAMakerEndpoint"/> of the knowledge base to query.</param>
 /// <param name="options">The <see cref="QnAMakerOptions"/> for the Custom Question Answering Knowledge Base.</param>
 /// <param name="httpClient">An alternate client with which to talk to Language Service.
 /// If null, a default client is used for this instance.</param>
 public CustomQuestionAnswering(QnAMakerEndpoint endpoint, QnAMakerOptions options = null, HttpClient httpClient = null)
     : this(endpoint, options, httpClient, null)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TrainUtils"/> class.
 /// </summary>
 /// <param name="endpoint">QnA Maker endpoint details.</param>
 /// <param name="httpClient">Http client.</param>
 public TrainUtils(QnAMakerEndpoint endpoint, HttpClient httpClient)
 {
     _endpoint   = endpoint;
     _httpClient = httpClient;
 }