Beispiel #1
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 = qna.KnowledgeBaseQueryOptions(
            question="How long should my Surface battery last?",
            top=3,
            confidence_score_threshold=0.2,
            include_unstructured_sources=True,
            answer_span_request=qna.AnswerSpanRequest(
                enable=True,
                confidence_score_threshold=0.2,
                top_answers_with_span=1),
        )

        output = client.query_knowledge_base(
            first_question,
            project_name=knowledge_base_project,
            deployment_name="test")
        best_candidate = [
            a for a in output.answers if a.confidence_score > 0.9
        ][0]
        print("Q: {}".format(first_question.question))
        print("A: {}".format(best_candidate.answer))

        followup_question = qna.KnowledgeBaseQueryOptions(
            question="How long it takes to charge Surface?",
            top=3,
            confidence_score_threshold=0.2,
            context=qna.KnowledgeBaseAnswerRequestContext(
                previous_user_query="How long should my Surface battery last?",
                previous_qna_id=best_candidate.id),
            answer_span_request=qna.AnswerSpanRequest(
                enable=True,
                confidence_score_threshold=0.2,
                top_answers_with_span=1),
            include_unstructured_sources=True)

        output = client.query_knowledge_base(
            followup_question,
            project_name=knowledge_base_project,
            deployment_name="test")
        print("Q: {}".format(followup_question.question))
        print("A: {}".format(output.answers[0].answer))
Beispiel #2
0
    def test_query_knowledgebase_with_followup(self, qna_account, qna_key,
                                               qna_project):
        client = QuestionAnsweringClient(qna_account,
                                         AzureKeyCredential(qna_key))
        with client:
            query_params = KnowledgeBaseQueryOptions(
                question="How long should my Surface battery last?",
                top=3,
                user_id="sd53lsY=",
                confidence_score_threshold=0.2,
                answer_span_request=AnswerSpanRequest(
                    enable=True,
                    confidence_score_threshold=0.2,
                    top_answers_with_span=1),
                include_unstructured_sources=True)

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

            query_params = KnowledgeBaseQueryOptions(
                question="How long it takes to charge Surface?",
                top=3,
                user_id="sd53lsY=",
                confidence_score_threshold=0.2,
                context=KnowledgeBaseAnswerRequestContext(
                    previous_user_query=
                    "How long should my Surface battery last?",
                    previous_qna_id=confident_answers[0].id),
                answer_span_request=AnswerSpanRequest(
                    enable=True,
                    confidence_score_threshold=0.2,
                    top_answers_with_span=1),
                include_unstructured_sources=True)
            output = client.query_knowledge_base(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_score > 0.6
            ]
            assert len(confident_answers) == 1
            assert confident_answers[0].answer_span.text == "two to four hours"
Beispiel #3
0
    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.query_knowledge_base(query_params,
                                                 project_name=qna_project,
                                                 deployment_name='test')

        assert len(output.answers) == 3
        confident_answers = [
            a for a in output.answers if a.confidence_score > 0.9
        ]
        assert len(confident_answers) == 1
        assert confident_answers[0].source == "surface-pro-4-user-guide-EN.pdf"
Beispiel #4
0
    def test_query_knowledgebase(self, qna_account, qna_key, qna_project):
        client = QuestionAnsweringClient(qna_account,
                                         AzureKeyCredential(qna_key))
        query_params = KnowledgeBaseQueryOptions(
            question="Ports and connectors",
            top=3,
            context=KnowledgeBaseAnswerRequestContext(
                previous_user_query="Meet Surface Pro 4", previous_qna_id=4))

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

        assert output.answers
        for answer in output.answers:
            assert answer.answer
            assert answer.confidence_score
            assert answer.id
            assert answer.source
            assert answer.metadata is not None
            assert not answer.answer_span

            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
Beispiel #5
0
    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.query_knowledge_base(query_params,
                                                 project_name=qna_project,
                                                 deployment_name='test')

            assert len(output.answers) == 1
Beispiel #6
0
    def test_query_knowledgebase_only_id(self, qna_account, qna_key,
                                         qna_project):
        client = QuestionAnsweringClient(qna_account,
                                         AzureKeyCredential(qna_key))
        with client:
            query_params = KnowledgeBaseQueryOptions(qna_id=19)

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

            assert len(output.answers) == 1
Beispiel #7
0
    def test_query_knowledgebase_overload(self, qna_account, qna_key,
                                          qna_project):
        client = QuestionAnsweringClient(qna_account,
                                         AzureKeyCredential(qna_key))
        with client:
            output = client.query_knowledge_base(
                project_name=qna_project,
                deployment_name='test',
                question="How long should my Surface battery last?",
                top=3,
                user_id="sd53lsY=",
                confidence_score_threshold=0.2,
                answer_span_request=AnswerSpanRequest(
                    enable=True,
                    confidence_score_threshold=0.2,
                    top_answers_with_span=1),
                include_unstructured_sources=True)

        assert len(output.answers) == 3
        confident_answers = [
            a for a in output.answers if a.confidence_score > 0.9
        ]
        assert len(confident_answers) == 1
        assert confident_answers[0].source == "surface-pro-4-user-guide-EN.pdf"