Ejemplo n.º 1
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_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"
Ejemplo n.º 3
0
def sample_query_text():
    # [START query_text]
    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"]

    client = QuestionAnsweringClient(endpoint, AzureKeyCredential(key))
    with client:
        input = qna.TextQueryOptions(
            question="How long it takes to charge surface?",
            records=[
                qna.TextRecord(
                    text=
                    "Power and charging. It takes two to four hours to charge the Surface Pro 4 battery fully from an empty state. "
                    +
                    "It can take longer if you’re using your Surface for power-intensive activities like gaming or video streaming while you’re charging it.",
                    id="doc1"),
                qna.TextRecord(
                    text=
                    "You can use the USB port on your Surface Pro 4 power supply to charge other devices, like a phone, while your Surface charges. "
                    +
                    "The USB port on the power supply is only for charging, not for data transfer. If you want to use a USB device, plug it into the USB port on your Surface.",
                    id="doc2")
            ])

        output = client.query_text(input)

        best_answer = [a for a in output.answers
                       if a.confidence_score > 0.9][0]
        print("Q: {}".format(input.question))
        print("A: {}".format(best_answer.answer))
Ejemplo n.º 4
0
    def test_query_text_overload(self, qna_account, qna_key):
        client = QuestionAnsweringClient(qna_account, AzureKeyCredential(qna_key))

        with client:
            with pytest.raises(TypeError):
                client.query_text(
                    question="How long it takes to charge surface?",
                    records=[
                        "Power and charging. It takes two to four hours to charge the Surface Pro 4 battery fully from an empty state. " +
                        "It can take longer if you’re using your Surface for power-intensive activities like gaming or video streaming while you’re charging it.",
                        {
                            "text": "You can use the USB port on your Surface Pro 4 power supply to charge other devices, like a phone, while your Surface charges. "+
                                    "The USB port on the power supply is only for charging, not for data transfer. If you want to use a USB device, plug it into the USB port on your Surface.",
                            "id": "2"
                        }
                    ]
                )
            output = client.query_text(
                question="How long it takes to charge surface?",
                records=[
                    "Power and charging. It takes two to four hours to charge the Surface Pro 4 battery fully from an empty state. " +
                    "It can take longer if you’re using your Surface for power-intensive activities like gaming or video streaming while you’re charging it.",
                    "You can use the USB port on your Surface Pro 4 power supply to charge other devices, like a phone, while your Surface charges. "+
                    "The USB port on the power supply is only for charging, not for data transfer. If you want to use a USB device, plug it into the USB port on your Surface.",
                ]
            )
            assert len(output.answers) == 3
            confident_answers = [a for a in output.answers if a.confidence_score > 0.9]
            assert len(confident_answers) == 2
            assert confident_answers[0].answer_span.text == "two to four hours"
Ejemplo n.º 5
0
    def test_query_text_with_dictparams(self, qna_account, qna_key):
        client = QuestionAnsweringClient(qna_account,
                                         AzureKeyCredential(qna_key))
        params = {
            "question":
            "How long it takes to charge surface?",
            "records": [{
                "text":
                "Power and charging. It takes two to four hours to charge the Surface Pro 4 battery fully from an empty state. "
                +
                "It can take longer if you’re using your Surface for power-intensive activities like gaming or video streaming while you’re charging it.",
                "id":
                "1"
            }, {
                "text":
                "You can use the USB port on your Surface Pro 4 power supply to charge other devices, like a phone, while your Surface charges. "
                +
                "The USB port on the power supply is only for charging, not for data transfer. If you want to use a USB device, plug it into the USB port on your Surface.",
                "id":
                "2"
            }],
            "language":
            "en"
        }

        with client:
            output = client.get_answers_from_text(params)
            assert len(output.answers) == 3
            confident_answers = [
                a for a in output.answers if a.confidence > 0.9
            ]
            assert len(confident_answers) == 2
            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
Ejemplo n.º 7
0
    def test_query_text(self, qna_account, qna_key):
        client = QuestionAnsweringClient(qna_account,
                                         AzureKeyCredential(qna_key))
        params = AnswersFromTextOptions(
            question="What is the meaning of life?",
            text_documents=[
                TextDocument(
                    text="abc Graphics  Surprise, surprise -- our 4K  ",
                    id="doc1"),
                TextDocument(
                    text=
                    "e graphics card. While the Nvidia GeForce MX250 GPU isn't meant for demanding gaming, it is a step up from integrated graphics as proven by comparing it to the UHD 620 GPU in the FHD model.   The MX250-equipped Envy 13 scored a 116,575 on the Ice Storm Unlimited benchmark while the base model scored a 82,270. Upgrading to the discrete graphics gives the Envy 13 better performance than the Notebook 9 Pro (61,662; UHD 620), Surface Laptop 2 (71,647; UHD 620) and the premium laptop average (86,937).   While the Nvidia GeForce MX250 GPU isn't meant for demanding gaming, it is a step up from integrated graphics as proven by comparing it to the UHD 620 GPU in the FHD model.   We played the racing game Dirt 3 at 92 frames per second on  ",
                    id="doc2"),
                TextDocument(
                    text=
                    "Graphics  Surprise, surprise -- our 4K Envy 13 came with a discrete graphics card. While the Nvidia GeForce MX250 GPU isn't meant for demanding gaming, it is a step up from integrated graphics as proven by comparing it to the UHD 620 GPU in the FHD model.   The MX250-equipped Envy 13 scored a 116,575 on the Ice Storm Unlimited benchmark while the base model scored a 82,270. Upgrading to the discrete graphics gives the Envy 13 better performance than the Notebook 9 Pro (61,662; UHD 620), Surface Laptop 2 (71,647; UHD 620) and the premium laptop average (86,937).   While the Nvidia GeForce MX250 GPU isn't meant for demanding gaming, it is a step up from integrated graphics as proven by comparing it to the UHD 620 GPU in the FHD model.   We played the racing game Dirt 3 at 92 frames per second on the MX250 model, which is well above our 30-fps playability, the category average (69 fps) and what the Surface Laptop 2 (82 fps) achieved. The ZenBook S UX391UA (45 fps) fell flat on this real-world test but ran better than the base model Envy 13 (31 fps).  Audio  I had a good ol' time groovin' to the sound of the Envy 13's crisp speakers. HP went all out with the Envy, placing dual speakers on the underside of the chassis along with a third, top-firing driver above the keyboard. Devon Gilfillian's funky jam \"Here and Now\" boomed smooth, soulful tunes throughout my small apartment. The twang of the electric guitar played nicely with the thudding percussion but never overshadowed Gilfillian or the female backup vocals.    Bang & Olufsen software comes preinstalled on the Envy 13, with equalizer controls so you can adjust the bass, midrange and treble to your liking. But even out of the box, you'll enjoy great sound without having to bust out your headphones.  Battery Life  Get an Envy 13 with the 1080p non-touch display if battery life is important to you.   The FHD model endured for 11 hours and 11 minutes whereas the 4K model lasted only 4 hours and 36 minutes on our battery test, which involves continuous web browsing over Wi-Fi at 150 nits of brightness.   MORE: Laptops with Best Battery Life - Longest Lasting Laptop Batteries  Competing laptops like the ZenBook S UX391UA (7:05), Surface Laptop 2 (9:22) and Notebook 9 Pro (8:53) outstayed the 4K Envy 13 but powered down long before the 1080p version.  Webcam  The 720p webcam on the Envy 13 is nothing to write home about. A selfie I snapped in my dimly lit room was covered in a haze of visual noise. My beard and hair were unkempt blobs, while my eyes looked like they were drawn on by a pointillist painter. If there's one positive, it's that the lens captures natural colors and even extracted the different shades of gray in my T-shirt.   On the right edge of the Envy 13 is a physical kill switch that cuts the power to the webcam so you can feel reassured that nobody is snooping on you.   Heat  Leave the lapdesk at home - you don't have to worry about the Envy 13 overheating.    After I played a 15-minute, full-HD video in full screen, the touchpad on the HP Envy 13 with a Core i7 CPU rose to only 83 degrees Fahrenheit while the keyboard (87 degrees) and underside (90 degrees) also remained well below our 95-degree comfort threshold. Even the toastiest part of the machine, the lower-left edge on the underside, topped out at 94 degrees.   Software and Warranty  It's a shame that a laptop with such beautiful hardware ships with such ugly software. Pre-installed on this machine are entirely too many programs that could either be packaged together or omitted altogether.   HP provides an app called Audio Switch, which simply lets you switch your audio input/output between the internal speakers and headphones. As the same implies, HP's Command Center is where you can get information about your Envy 13 but also switch the thermal profiles between comfort and performance. Along with support documentation, HP also bundles in a setup program called JumpStart, a program for connecting printers and a redundant system-info app called Event Utility.   Also installed on the Envy 13's Windows 10 Home OS are several Microsoft apps, including Simple Solitaire, Candy Crush Friends and Your Phone. Other third-party apps include Booking.com, Netflix and McAfee Security.   HP ships the Envy 13 with a one-year warranty. See how HP did on our Tech Support Showdown and Best and Worst Brands ranking.   Bottom Line  The Envy 13 has cemented its standing as the ultimate laptop for college students or travelers. Along with 11-plus hours of battery life (on the FHD model), the Envy 13 has a sleek, ultraportable chassis, fast performance, and powerful speakers. Best of all, the Envy 13 starts at a reasonable $799, which is hundreds less than the competition. In many ways, the Envy 13 is what we wanted the new MacBook Air to be.   The new HP Envy 13 is everything I was hoping the new MacBook Air would be: fast, attractive and affordable.  Just be sure to buy the right model. We strongly recommend the 1080p version over the 4K model because it lasts several hours longer on a charge and costs less. In fact, if we were reviewing the 4K model separately, we'd only give it a 3.5 rating. You should also consider the Envy 13 with a 10th Gen CPU, although we haven't gotten the chance to review it yet.   If you absolutely need a high-res display, the 4K Envy 13 is one of many good options. We also recommend the Samsung Notebook 9 Pro, which has a similarly premium design but much better battery life than the 4K Envy. The Microsoft Surface Laptop 2 is another recommended alternative, though you might want to wait a few months for the rumored Surface Laptop 3.   Overall, the HP Envy 13 is a fantastic laptop that checks all the right boxes --- as long as you buy the 1080p model.   Credit: Laptop Mag  HP Envy 13 (2019) Specs BluetoothBluetooth 5.0 BrandHP CPUIntel Core i7-8565U Card SlotsmicroSD Company Websitehttps://www8.hp.com/us/en/home.html Display Size13.3 Graphics CardNvidia GeForce MX250 Hard Drive Size512GB Hard Drive TypePCIe NVMe M.2 Highest Available Resolution3840 x 2160 Native Resolution3840 x 2160 Operating SystemWindows 10 Home Ports (excluding USB)USB 3.1 with Type-C, USB 3.1 Always-On, USB 3.1, Headphone/Mic, microSD RAM16GB RAM Upgradable to16GB Size12.1 x 8.3 x .57 inches Touchpad Size4.3 x 2.2 inches USB Ports3 Video Memory2GB Warranty/Supportone-year warranty. Weight2.8 pounds Wi-Fi802.11ac Wi-Fi ModelIntel Wireless-AC 9560 ",
                    id="doc3")
            ],
            language="en")

        output = client.get_answers_from_text(params)
        assert output.answers
        for answer in output.answers:
            assert answer.answer
            assert answer.confidence
            assert answer.id
            assert answer.offset
            assert answer.length
            assert answer.short_answer
            assert answer.short_answer.text
            assert answer.short_answer.confidence
            assert answer.short_answer.offset is not None
            assert answer.short_answer.length
    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
Ejemplo n.º 9
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
Ejemplo n.º 10
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))
    def test_query_knowledgebase_llc_with_answerspan(self, qna_account,
                                                     qna_key, qna_project):
        client = QuestionAnsweringClient(qna_account,
                                         AzureKeyCredential(qna_key))
        json_content = {
            "question": "Ports and connectors",
            "top": 3,
            "context": {
                "previousUserQuery": "Meet Surface Pro 4",
                "previousQnAId": 4
            },
            "answerSpanRequest": {
                "enable": True,
                "confidenceScoreThreshold": 0.1,
                "topAnswersWithSpan": 1
            }
        }
        request = build_get_answers_request(json=json_content,
                                            project_name=qna_project,
                                            deployment_name='test')
        with client:
            response = client.send_request(request)
            assert response.status_code == 200

        output = response.json()
        assert output
        assert output.get('answers')
        for answer in output['answers']:
            assert answer.get('answer')
            assert answer.get('confidenceScore')
            assert answer.get('id')
            assert answer.get('source')
            assert answer.get('metadata') is not None

            if answer.get('answerSpan'):
                assert answer['answerSpan'].get('text')
                assert answer['answerSpan'].get('confidenceScore')
                assert answer['answerSpan'].get('offset') is not None
                assert answer['answerSpan'].get('length')

            assert answer.get('questions')
            for question in answer['questions']:
                assert question

            assert answer.get('dialog')
            assert answer['dialog'].get('isContextOnly') is not None
            assert answer['dialog'].get('prompts') is not None
            if answer['dialog'].get('prompts'):
                for prompt in answer['dialog']['prompts']:
                    assert prompt.get('displayOrder') is not None
                    assert prompt.get('qnaId')
                    assert prompt.get('displayText')
Ejemplo n.º 12
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"
 def test_query_knowledgebase_filter_dict_params(self, qna_account, qna_key,
                                                 qna_project):
     filters = {
         "metadataFilter": {
             "metadata":
             [("explicitlytaggedheading", "check the battery level"),
              ("explicitlytaggedheading", "make your battery last")],
             "logicalOperation":
             "or"
         },
     }
     with QuestionAnsweringClient(qna_account,
                                  AzureKeyCredential(qna_key)) as client:
         response = client.get_answers(
             project_name=qna_project,
             deployment_name='test',
             question="Battery life",
             filters=filters,
             top=3,
         )
         assert len(response.answers) == 2
         assert any([
             a for a in response.answers if a.metadata.get(
                 'explicitlytaggedheading') == "check the battery level"
         ])
         assert any([
             a for a in response.answers if a.metadata.get(
                 'explicitlytaggedheading') == "make your battery last"
         ])
Ejemplo n.º 14
0
    def test_query_text_overload_positional_and_kwarg(self):
        with QuestionAnsweringClient("http://fake.com",
                                     AzureKeyCredential("123")) as client:
            with pytest.raises(TypeError):
                client.get_answers_from_text("positional_one",
                                             "positional_two")
            with pytest.raises(TypeError):
                client.get_answers_from_text("positional_options_bag",
                                             options="options bag by name")

            params = AnswersFromTextOptions(
                question="What is the meaning of life?",
                text_documents=[
                    TextDocument(text="foo", id="doc1"),
                    TextDocument(text="bar", id="doc2")
                ],
                language="en")
            with pytest.raises(TypeError):
                client.get_answers_from_text(options=params)

            with pytest.raises(TypeError):
                client.get_answers_from_text(question="why?",
                                             text_documents=["foo", "bar"],
                                             options=params)

            with pytest.raises(TypeError):
                client.get_answers_from_text(params, question="Why?")
 def test_query_knowledgebase_filter(self, qna_account, qna_key,
                                     qna_project):
     """Thanks to @heaths for this test!"""
     filters = QueryFilters(metadata_filter=MetadataFilter(
         metadata=[("explicitlytaggedheading", "check the battery level"),
                   ("explicitlytaggedheading", "make your battery last")],
         logical_operation="OR"), )
     with QuestionAnsweringClient(qna_account,
                                  AzureKeyCredential(qna_key)) as client:
         response = client.get_answers(
             project_name=qna_project,
             deployment_name='test',
             question="Battery life",
             filters=filters,
             top=3,
         )
         assert len(response.answers) == 2
         assert any([
             a for a in response.answers if a.metadata.get(
                 'explicitlytaggedheading') == "check the battery level"
         ])
         assert any([
             a for a in response.answers if a.metadata.get(
                 'explicitlytaggedheading') == "make your battery last"
         ])
    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"
Ejemplo n.º 17
0
    def test_query_knowledgebase_with_answerspan(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),
            answer_span_request=AnswerSpanRequest(
                enable=True,
                confidence_score_threshold=0.1,
                top_answers_with_span=2))

        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

            if answer.answer_span:
                assert answer.answer_span.text
                assert answer.answer_span.confidence_score
                assert answer.answer_span.offset is not None
                assert answer.answer_span.length

            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_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"
    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')
 def test_query_knowledgebase_overload_positional_and_kwarg(self):
     with QuestionAnsweringClient("http://fake.com",
                                  AzureKeyCredential("123")) as client:
         with pytest.raises(TypeError):
             client.get_answers("positional_one", "positional_two")
         with pytest.raises(TypeError):
             client.get_answers("positional_options_bag",
                                options="options bag by name")
         with pytest.raises(TypeError):
             client.get_answers(options={'qnaId': 15},
                                project_name="hello",
                                deployment_name='test')
         with pytest.raises(TypeError):
             client.get_answers({'qnaId': 15},
                                question='Why?',
                                project_name="hello",
                                deployment_name='test')