/// <summary>Answers the specified question using the provided text in the body.</summary> /// <param name="options">The question to answer.</param> /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param> /// <returns><see cref="TextAnswers"/> containing answers to the <see cref="QueryTextOptions.Question"/>.</returns> /// <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 <TextAnswers> QueryText(QueryTextOptions options, CancellationToken cancellationToken = default) { Argument.AssertNotNull(options, nameof(options)); using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(QuestionAnsweringClient)}.{nameof(QueryText)}"); scope.Start(); try { return(_textRestClient.Query(options, cancellationToken)); } catch (Exception ex) { scope.Failed(ex); throw; } }
/// <summary>Answers the specified question using the provided text in the body.</summary> /// <param name="options">The question to answer.</param> /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param> /// <returns><see cref="TextAnswers"/> containing answers to the <see cref="QueryTextOptions.Question"/>.</returns> /// <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 async Task <Response <TextAnswers> > QueryTextAsync(QueryTextOptions options, CancellationToken cancellationToken = default) { Argument.AssertNotNull(options, nameof(options)); using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(QuestionAnsweringClient)}.{nameof(QueryText)}"); scope.Start(); try { options = options.Clone(Options.DefaultLanguage); return(await _textRestClient.QueryAsync(options, cancellationToken).ConfigureAwait(false)); } catch (Exception ex) { scope.Failed(ex); throw; } }
public async Task <Response <TextAnswers> > QueryAsync(QueryTextOptions textQueryOptions, CancellationToken cancellationToken = default) { if (textQueryOptions == null) { throw new ArgumentNullException(nameof(textQueryOptions)); } using var message = CreateQueryRequest(textQueryOptions); await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); switch (message.Response.Status) { case 200: { TextAnswers value = default; using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false); value = TextAnswers.DeserializeTextAnswers(document.RootElement); return(Response.FromValue(value, message.Response)); }
internal HttpMessage CreateQueryRequest(QueryTextOptions textQueryOptions) { 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-text", false); 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(textQueryOptions); request.Content = content; return(message); }
/// <summary>Answers the specified question using the text <paramref name="records"/>.</summary> /// <param name="question">The question to answer.</param> /// <param name="records">A collection of <see cref="TextRecord"/> to query.</param> /// <param name="language"> /// The language of the text records. This is the BCP-47 representation of a language. /// For example, use "en" for English, "es" for Spanish, etc. /// If not set, uses <see cref="QuestionAnsweringClientOptions.DefaultLanguage"/> as the default. /// If <see cref="QuestionAnsweringClientOptions.DefaultLanguage"/> is not set, the service default, "en" for English, is used. /// </param> /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param> /// <returns><see cref="TextAnswers"/> containing answers to the <paramref name="question"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="question"/> or <paramref name="records"/> is null.</exception> /// <exception cref="RequestFailedException">The service returned an error. The exception contains details of the service error.</exception> public virtual Response <TextAnswers> QueryText(string question, IEnumerable <TextRecord> records, string language = default, CancellationToken cancellationToken = default) => QueryText(QueryTextOptions.From(question, records, language ?? Options.DefaultLanguage), cancellationToken);
/// <summary>Answers the specified question using the text <paramref name="records"/>.</summary> /// <param name="question">The question to answer.</param> /// <param name="records">The text records to query.</param> /// <param name="language">Optional language of the text <paramref name="records"/>.</param> /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param> /// <returns><see cref="TextAnswers"/> containing answers to the <paramref name="question"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="question"/> or <paramref name="records"/> is null.</exception> /// <exception cref="RequestFailedException">The service returned an error. The exception contains details of the service error.</exception> public virtual Task <Response <TextAnswers> > QueryTextAsync(string question, IEnumerable <string> records, string language = default, CancellationToken cancellationToken = default) => QueryTextAsync(QueryTextOptions.From(question, records, language), cancellationToken);