Example #1
0
        internal HttpMessage CreateQueryRequest(string projectName, QueryKnowledgeBaseOptions knowledgeBaseQueryOptions, string deploymentName)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;

            request.Method = RequestMethod.Post;
            var uri = new RawRequestUriBuilder();

            uri.Reset(endpoint);
            uri.AppendRaw("/language", false);
            uri.AppendPath("/:query-knowledgebases", false);
            uri.AppendQuery("projectName", projectName, true);
            if (deploymentName != null)
            {
                uri.AppendQuery("deploymentName", deploymentName, true);
            }
            uri.AppendQuery("api-version", apiVersion, true);
            request.Uri = uri;
            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("Content-Type", "application/json");
            var content = new Utf8JsonRequestContent();

            content.JsonWriter.WriteObjectValue(knowledgeBaseQueryOptions);
            request.Content = content;
            return(message);
        }
        /// <summary>Answers the specified question using your knowledge base.</summary>
        /// <param name="options">
        /// An <see cref="QueryKnowledgeBaseOptions"/> containing the <see cref="QueryKnowledgeBaseOptions.ProjectName"/>,
        /// <see cref="QueryKnowledgeBaseOptions.DeploymentName"/>, <see cref="QueryKnowledgeBaseOptions.Question"/>, and other options to answer a question.</param>
        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param>
        /// <exception cref="ArgumentNullException"><paramref name="options"/> is null.</exception>
        /// <exception cref="RequestFailedException">The service returned an error. The exception contains details of the service error.</exception>
        public virtual Response <KnowledgeBaseAnswers> QueryKnowledgeBase(QueryKnowledgeBaseOptions options, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(options, nameof(options));

            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(QuestionAnsweringClient)}.{nameof(QueryKnowledgeBase)}");
            scope.AddAttribute("projectName", options.ProjectName);
            scope.AddAttribute("deploymentName", options.DeploymentName);
            scope.Start();

            try
            {
                return(_knowledgebaseRestClient.Query(options.ProjectName, options.DeploymentName, options, cancellationToken));
            }
            catch (Exception ex)
            {
                scope.Failed(ex);
                throw;
            }
        }
Example #3
0
        /// <summary>Answers the specified question using your knowledge base.</summary>
        /// <param name="projectName">The name of the project to use.</param>
        /// <param name="options">The question to ask along with other options to query for answers.</param>
        /// <param name="deploymentName">The optional deployment name of the project to use, such as "test" or "prod". If not specified, the "prod" knowledge base will be queried.</param>
        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param>
        /// <exception cref="ArgumentNullException"><paramref name="projectName"/> or <paramref name="options"/> is null.</exception>
        /// <exception cref="RequestFailedException">The service returned an error. The exception contains details of the service error.</exception>
        public virtual async Task <Response <KnowledgeBaseAnswers> > QueryKnowledgeBaseAsync(string projectName, QueryKnowledgeBaseOptions options, string deploymentName = null, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(projectName, nameof(projectName));
            Argument.AssertNotNull(options, nameof(options));

            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(QuestionAnsweringClient)}.{nameof(QueryKnowledgeBase)}");
            scope.AddAttribute("project", projectName);
            scope.Start();

            try
            {
                return(await _knowledgebaseRestClient.QueryAsync(projectName, options, deploymentName, cancellationToken).ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                scope.Failed(ex);
                throw;
            }
        }
Example #4
0
        public async Task <Response <KnowledgeBaseAnswers> > QueryAsync(string projectName, QueryKnowledgeBaseOptions knowledgeBaseQueryOptions, string deploymentName = null, CancellationToken cancellationToken = default)
        {
            if (projectName == null)
            {
                throw new ArgumentNullException(nameof(projectName));
            }
            if (knowledgeBaseQueryOptions == null)
            {
                throw new ArgumentNullException(nameof(knowledgeBaseQueryOptions));
            }

            using var message = CreateQueryRequest(projectName, knowledgeBaseQueryOptions, deploymentName);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            switch (message.Response.Status)
            {
            case 200:
            {
                KnowledgeBaseAnswers value = default;
                using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                value = KnowledgeBaseAnswers.DeserializeKnowledgeBaseAnswers(document.RootElement);
                return(Response.FromValue(value, message.Response));
            }