def test_query_knowledgebase_with_dictparams(self, qna_account, qna_key,
                                                 qna_project):
        client = QuestionAnsweringClient(qna_account,
                                         AzureKeyCredential(qna_key))
        query_params = {
            "question": "How long should my Surface battery last?",
            "top": 3,
            "userId": "sd53lsY=",
            "confidenceScoreThreshold": 0.2,
            "answerSpanRequest": {
                "enable": True,
                "confidenceScoreThreshold": 0.2,
                "topAnswersWithSpan": 1
            },
            "includeUnstructuredSources": True
        }

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

        assert len(output.answers) == 2
        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"
    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
Exemplo n.º 3
0
def sample_query_knowledgebase():
    # [START query_knowledgebase]
    import os
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.language.questionanswering import QuestionAnsweringClient
    from azure.ai.language.questionanswering import models as qna

    endpoint = os.environ["AZURE_QUESTIONANSWERING_ENDPOINT"]
    key = os.environ["AZURE_QUESTIONANSWERING_KEY"]
    knowledge_base_project = os.environ["AZURE_QUESTIONANSWERING_PROJECT"]

    client = QuestionAnsweringClient(endpoint, AzureKeyCredential(key))
    with client:
        question = "How long should my Surface battery last?"
        output = client.get_answers(
            question=question,
            top=3,
            confidence_threshold=0.2,
            include_unstructured_sources=True,
            short_answer_options=qna.ShortAnswerOptions(
                confidence_threshold=0.2, top=1),
            project_name=knowledge_base_project,
            deployment_name="test")
        best_candidate = [a for a in output.answers if a.confidence > 0.9][0]
        print("Q: {}".format(question))
        print("A: {}".format(best_candidate.answer))
    def test_query_knowledgebase_with_followup(self, qna_account, qna_key,
                                               qna_project):
        client = QuestionAnsweringClient(qna_account,
                                         AzureKeyCredential(qna_key))
        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 = 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 = 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"
Exemplo n.º 5
0
def sample_chit_chat():
    # [START chit_chat]
    import os
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.language.questionanswering import QuestionAnsweringClient
    from azure.ai.language.questionanswering import models as qna

    endpoint = os.environ["AZURE_QUESTIONANSWERING_ENDPOINT"]
    key = os.environ["AZURE_QUESTIONANSWERING_KEY"]
    knowledge_base_project = os.environ["AZURE_QUESTIONANSWERING_PROJECT"]

    client = QuestionAnsweringClient(endpoint, AzureKeyCredential(key))
    with client:
        first_question = "How long should my Surface battery last?"

        output = client.get_answers(
            question=first_question,
            top=3,
            confidence_threshold=0.2,
            include_unstructured_sources=True,
            short_answer_options=qna.ShortAnswerOptions(
                confidence_threshold=0.2, top=1),
            project_name=knowledge_base_project,
            deployment_name="test")
        best_candidate = [a for a in output.answers if a.confidence > 0.9][0]
        print(u"Q: {}".format(first_question))
        print(u"A: {}".format(best_candidate.answer))

        followup_question = "How long it takes to charge Surface?"

        output = client.get_answers(
            question=followup_question,
            top=3,
            confidence_threshold=0.2,
            answer_context=qna.KnowledgeBaseAnswerContext(
                previous_question=first_question,
                previous_qna_id=best_candidate.qna_id),
            short_answer_options=qna.ShortAnswerOptions(
                confidence_threshold=0.2, top=1),
            include_unstructured_sources=True,
            project_name=knowledge_base_project,
            deployment_name="test")
        print(u"Q: {}".format(followup_question))
        print(u"A: {}".format(output.answers[0].answer))
    def test_query_knowledgebase_python_dict(self, qna_account, qna_key,
                                             qna_project):
        client = QuestionAnsweringClient(qna_account,
                                         AzureKeyCredential(qna_key))
        with client:
            query_params = {"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_overload(self, qna_account, qna_key,
                                          qna_project):
        client = QuestionAnsweringClient(qna_account,
                                         AzureKeyCredential(qna_key))
        with client:
            output = client.get_answers(
                project_name=qna_project,
                deployment_name='test',
                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)

        assert len(output.answers) == 2
        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"