Exemplo n.º 1
0
    async def test_query_knowledgebase_with_followup(self, qna_account, qna_key, qna_project):
        client = QuestionAnsweringClient(qna_account, AzureKeyCredential(qna_key))
        async with client:
            query_params = AnswersOptions(
                question="How long should my Surface battery last?",
                top=3,
                user_id="sd53lsY=",
                confidence_threshold=0.2,
                short_answer_options=ShortAnswerOptions(
                    confidence_threshold=0.2,
                    top=1
                ),
                include_unstructured_sources=True
            )

            output = await client.get_answers(
                query_params,
                project_name=qna_project,
                deployment_name='test'
            )
            confident_answers = [a for a in output.answers if a.confidence > 0.9]
            assert len(confident_answers) == 1
            assert confident_answers[0].source == "surface-pro-4-user-guide-EN.pdf"

            query_params = AnswersOptions(
                question="How long it takes to charge Surface?",
                top=3,
                user_id="sd53lsY=",
                confidence_threshold=0.2,
                answer_context=KnowledgeBaseAnswerContext(
                    previous_question="How long should my Surface battery last?",
                    previous_qna_id=confident_answers[0].qna_id
                ),
                short_answer_options=ShortAnswerOptions(
                    confidence_threshold=0.2,
                    top=1
                ),
                include_unstructured_sources=True
            )
            output = await client.get_answers(
                query_params,
                project_name=qna_project,
                deployment_name='test'
            )

            assert output.answers
            confident_answers = [a for a in output.answers if a.confidence > 0.5]
            assert len(confident_answers) == 1
            assert confident_answers[0].short_answer.text == " two to four hours"
    def test_query_knowledgebase(self, qna_account, qna_key, qna_project):
        client = QuestionAnsweringClient(qna_account,
                                         AzureKeyCredential(qna_key))
        query_params = AnswersOptions(
            question="Ports and connectors",
            top=3,
            answer_context=KnowledgeBaseAnswerContext(
                previous_question="Meet Surface Pro 4", previous_qna_id=4))

        with client:
            output = client.get_answers(query_params,
                                        project_name=qna_project,
                                        deployment_name='test')

        assert output.answers
        for answer in output.answers:
            assert answer.answer
            assert answer.confidence
            assert answer.qna_id
            assert answer.source
            assert answer.metadata is not None
            assert not answer.short_answer

            assert answer.questions
            for question in answer.questions:
                assert question

            assert answer.dialog
            assert answer.dialog.is_context_only is not None
            assert answer.dialog.prompts is not None
            if answer.dialog.prompts:
                for prompt in answer.dialog.prompts:
                    assert prompt.display_order is not None
                    assert prompt.qna_id
                    assert prompt.display_text
    def test_query_knowledgebase_only_id(self, qna_account, qna_key,
                                         qna_project):
        client = QuestionAnsweringClient(qna_account,
                                         AzureKeyCredential(qna_key))
        with client:
            query_params = AnswersOptions(qna_id=19)

            output = client.get_answers(query_params,
                                        project_name=qna_project,
                                        deployment_name='test')

            assert len(output.answers) == 1
    def test_query_knowledgebase_question_or_qna_id(self):
        with QuestionAnsweringClient("http://fake.com",
                                     AzureKeyCredential("123")) as client:

            options = AnswersOptions()
            with pytest.raises(TypeError):
                client.get_answers(options,
                                   project_name="hello",
                                   deployment_name='test')

            with pytest.raises(TypeError):
                client.get_answers(project_name="hello",
                                   deployment_name='test')