internal HttpMessage CreateQueryRequest(string projectName, KnowledgebaseQueryOptions knowledgebaseQueryParameters, 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(knowledgebaseQueryParameters); request.Content = content; return(message); }
public async Task ChatAsync() { QuestionAnsweringClient client = Client; KnowledgebaseAnswer previousAnswer = QuestionAnsweringModelFactory.KnowledgebaseAnswer(id: 27); #region Snippet:QuestionAnsweringClient_ChatAsync #if SNIPPET // Answers are ordered by their ConfidenceScore so assume the user choose the first answer below: KnowledgebaseAnswer previousAnswer = answers.Answers.First(); #endif KnowledgebaseQueryOptions options = new KnowledgebaseQueryOptions("How long should charging take?") { Context = new KnowledgebaseAnswerRequestContext(previousAnswer.Id.Value) }; #if SNIPPET Response <KnowledgebaseAnswers> response = await client.QueryKnowledgebaseAsync("FAQ", options); #else Response <KnowledgebaseAnswers> response = await client.QueryKnowledgebaseAsync(TestEnvironment.ProjectName, options, TestEnvironment.DeploymentName); #endif foreach (KnowledgebaseAnswer answer in response.Value.Answers) { Console.WriteLine($"({answer.ConfidenceScore:P2}) {answer.Answer}"); Console.WriteLine($"Source: {answer.Source}"); Console.WriteLine(); } #endregion Assert.That(response.GetRawResponse().Status, Is.EqualTo(200)); }
public async Task QueryKnowledgebaseAsync() { QuestionAnsweringClient client = Client; #region Snippet:QuestionAnsweringClient_QueryKnowledgebaseAsync #if SNIPPET string projectName = "FAQ"; #endif KnowledgebaseQueryOptions options = new KnowledgebaseQueryOptions("How long should my Surface battery last?"); #if SNIPPET Response <KnowledgebaseAnswers> response = await client.QueryKnowledgebaseAsync(projectName, options); #else Response <KnowledgebaseAnswers> response = await client.QueryKnowledgebaseAsync(TestEnvironment.ProjectName, options, TestEnvironment.DeploymentName); #endif foreach (KnowledgebaseAnswer answer in response.Value.Answers) { Console.WriteLine($"({answer.ConfidenceScore:P2}) {answer.Answer}"); Console.WriteLine($"Source: {answer.Source}"); Console.WriteLine(); } #endregion Assert.That(response.GetRawResponse().Status, Is.EqualTo(200)); }
public void BadArgument() { QuestionAnsweringClient client = Client; KnowledgebaseQueryOptions options = new KnowledgebaseQueryOptions("Does this knowledge base exist?"); #region Snippet:QuestionAnsweringClient_BadRequest try { Response <KnowledgebaseAnswers> response = client.QueryKnowledgebase("invalid-knowledgebase", options); } catch (RequestFailedException ex) { Console.WriteLine(ex.ToString()); } #endregion }
/// <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, KnowledgebaseQueryOptions 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; } }
public async Task <Response <KnowledgebaseAnswers> > QueryAsync(string projectName, KnowledgebaseQueryOptions knowledgebaseQueryParameters, string deploymentName = null, CancellationToken cancellationToken = default) { if (projectName == null) { throw new ArgumentNullException(nameof(projectName)); } if (knowledgebaseQueryParameters == null) { throw new ArgumentNullException(nameof(knowledgebaseQueryParameters)); } using var message = CreateQueryRequest(projectName, knowledgebaseQueryParameters, 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)); }