Пример #1
0
        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
            QueryKnowledgeBaseOptions options = new QueryKnowledgeBaseOptions("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));
        }
Пример #2
0
        public async Task QueryKnowledgeBaseAsync()
        {
            QuestionAnsweringClient client = Client;

            #region Snippet:QuestionAnsweringClient_QueryKnowledgeBaseAsync
#if SNIPPET
            string projectName = "FAQ";
#endif
            QueryKnowledgeBaseOptions options = new QueryKnowledgeBaseOptions("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));
        }
        /* uncomment when we have authoring
         * [Ignore("Used only for the migration guide")]
         * private async Task MigrationGuide_Authoring()
         * {
         #region Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_CreateClient
         *  IQnAMakerClient client = new QnAMakerClient(new ApiKeyServiceClientCredentials("<QnAMakerSubscriptionKey>"), new HttpClient(), false)
         *  {
         *      Endpoint = "https://westus.api.cognitive.microsoft.com"
         *  };
         #endregion Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_CreateClient
         *
         #region Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_CreateKnowledgeBase
         *  var createOp = await client.Knowledgebase.CreateAsync(new CreateKbDTO
         *  {
         *      Name = "testqna", QnaList = new List<QnADTO>
         *      {
         *          new QnADTO
         *          {
         *              Questions = new List<string>
         *              {
         *                  "hi"
         *              },
         *              Answer = "hello"
         *          }
         *      }
         *  });
         #endregion Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_CreateKnowledgeBase
         *
         #region Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_UpdateKnowledeBase
         *  var updateOp = await client.Knowledgebase.UpdateAsync("<KnowledgeBaseID>",
         *      new UpdateKbOperationDTO
         *      {
         *          Add = new UpdateKbOperationDTOAdd
         *          {
         *              QnaList = new List<QnADTO>
         *              {
         *                  new QnADTO
         *                  {
         *                      Questions = new List<string>
         *                      {
         *                          "bye"
         *                      },
         *                      Answer = "goodbye"
         *                  }
         *              }
         *          }
         *      });
         #endregion Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_UpdateKnowledeBase
         *
         #region Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_DownloadKnowledeBase
         *  var kbdata = await client.Knowledgebase.DownloadAsync("<KnowledgeBaseID>", EnvironmentType.Test);
         #endregion Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_UpdateKnowledeBase
         *
         #region Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_DeleteKnowledeBase
         *  await client.Knowledgebase.DeleteAsync("<KnowledgeBaseID>");
         #endregion Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_DeleteKnowledeBase
         *
         * }*/
        private async Task Language_QnA_MigrationGuide_Runtime()
        {
            #region Snippet:Language_QnA_Maker_Snippets_MigrationGuide_CreateRuntimeClient
            var endpoint   = new Uri("{endpoint}");
            var credential = new AzureKeyCredential("{api-key}");

            var client = new QuestionAnsweringClient(endpoint, credential);
            #endregion

            #region Snippet:Language_QnA_Maker_Snippets_MigrationGuide_QueryKnowledgeBase
            var response = await client.QueryKnowledgeBaseAsync(
                "{project-name}",
                "{deployment-name}",
                "{question}");

            #endregion

            #region Snippet:Language_QnA_Maker_Snippets_MigrationGuide_Chat

            var options = new QueryKnowledgeBaseOptions(
                "{project-name}",
                "{deployment-name}",
                "{question}");
            options.Context = new KnowledgeBaseAnswerRequestContext(1);

            var responseFollowUp = await client.QueryKnowledgeBaseAsync(options);

            #endregion
        }