コード例 #1
0
 def update(self):
     client = QnAMakerClient(endpoint=self.credential.app_host,
                             credentials=CognitiveServicesCredentials(
                                 self.credential.app_key))
     update_kb_operation_dto = UpdateKbOperationDTO(
         add=UpdateKbOperationDTOAdd(qna_list=[
             QnADTO(questions=self.questions, answer=self.answer)
         ]))
     client.knowledgebase.update(kb_id=self.credential.app_id,
                                 update_kb=update_kb_operation_dto)
     time.sleep(5)
コード例 #2
0
def update_kb(client, kb_id, blob_url):
    logging.info("Updating knowledge base...")

    update_kb_operation_dto = UpdateKbOperationDTO(add=UpdateKbOperationDTOAdd(
        qna_list=[], urls=[blob_url], files=[]),
                                                   delete=None,
                                                   update=None)

    logging.info(update_kb_operation_dto)

    update_op = client.knowledgebase.update(kb_id=kb_id,
                                            update_kb=update_kb_operation_dto)

    #_monitor_operation(client=client, operation=update_op)

    logging.info("Updated knowledge base.")
コード例 #3
0
def update_kb(client, kb_id):
    print("Updating knowledge base...")

    qna3 = QnADTO(answer="goodbye",
                  questions=["bye", "end", "stop", "quit", "done"],
                  metadata=[
                      MetadataDTO(name="Category", value="Chitchat"),
                      MetadataDTO(name="Chitchat", value="end"),
                  ])

    qna4 = QnADTO(
        answer=
        "Hello, please select from the list of questions or enter a new question to continue.",
        questions=["hello", "hi", "start"],
        metadata=[
            MetadataDTO(name="Category", value="Chitchat"),
            MetadataDTO(name="Chitchat", value="begin"),
        ],
        context=QnADTOContext(is_context_only=False,
                              prompts=[
                                  PromptDTO(display_order=1,
                                            display_text="Use REST",
                                            qna_id=1),
                                  PromptDTO(
                                      display_order=2,
                                      display_text="Use .NET NuGet package",
                                      qna_id=2),
                              ]))

    urls = [
        "https://docs.microsoft.com/azure/cognitive-services/QnAMaker/troubleshooting"
    ]

    update_kb_operation_dto = UpdateKbOperationDTO(add=UpdateKbOperationDTOAdd(
        qna_list=[qna3, qna4], urls=urls, files=[]),
                                                   delete=None,
                                                   update=None)
    update_op = client.knowledgebase.update(kb_id=kb_id,
                                            update_kb=update_kb_operation_dto)
    _monitor_operation(client=client, operation=update_op)
    print("Updated knowledge base.")
コード例 #4
0
def knowledge_based_crud_sample(subscription_key):
    """KnowledgeBasedCRUDSample.

    This will create, update, publish, download, then delete a knowledge base.
    """
    def _create_sample_kb(client):
        """Helper function for knowledge_based_crud_sample.

        This helper function takes in a QnAMakerClient and returns an operation of a created knowledge base.
        """
        qna = QnADTO(
            answer="You can use our REST APIs to manage your knowledge base.",
            questions=["How do I manage my knowledgebase?"],
            metadata=[MetadataDTO(name="Category", value="api")]
        )
        urls = [
            "https://docs.microsoft.com/en-in/azure/cognitive-services/qnamaker/faqs"]
        create_kb_dto = CreateKbDTO(
            name="QnA Maker FAQ from quickstart",
            qna_list=[qna],
            urls=urls
        )
        create_op = client.knowledgebase.create(
            create_kb_payload=create_kb_dto)
        create_op = _monitor_operation(client=client, operation=create_op)
        return create_op.resource_location.replace("/knowledgebases/", "")

    def _monitor_operation(client, operation):
        """Helper function for knowledge_based_crud_sample.

        This helper function takes in a QnAMakerClient and an operation, and loops until the operation has either succeeded
        or failed and returns the operation.
        """
        for i in range(20):
            if operation.operation_state in [OperationStateType.not_started, OperationStateType.running]:
                print("Waiting for operation: {} to complete.".format(
                    operation.operation_id))
                time.sleep(5)
                operation = client.operations.get_details(
                    operation_id=operation.operation_id)
            else:
                break
        if operation.operation_state != OperationStateType.succeeded:
            raise Exception("Operation {} failed to complete.".format(
                operation.operation_id))
        return operation

    client = QnAMakerClient(endpoint=QNA_ENDPOINT, credentials=CognitiveServicesCredentials(subscription_key))

    # Create a KB
    print("Creating KB...")
    kb_id = _create_sample_kb(client=client)
    print("Created KB with ID: {}".format(kb_id))

    # Update the KB
    print("Updating KB...")
    update_kb_operation_dto = UpdateKbOperationDTO(
        add=UpdateKbOperationDTOAdd(
            qna_list=[
                QnADTO(questions=["bye"], answer="goodbye")
            ]
        )
    )
    update_op = client.knowledgebase.update(
        kb_id=kb_id, update_kb=update_kb_operation_dto)
    _monitor_operation(client=client, operation=update_op)

    # Publish the KB
    print("Publishing KB...")
    client.knowledgebase.publish(kb_id=kb_id)
    print("KB Published.")

    # Download the KB
    print("Downloading KB...")
    kb_data = client.knowledgebase.download(kb_id=kb_id, environment="Prod")
    print("KB Downloaded. It has {} QnAs.".format(len(kb_data.qna_documents)))

    # Delete the KB
    print("Deleting KB...")
    client.knowledgebase.delete(kb_id=kb_id)
    print("KB Deleted.")
コード例 #5
0
def update_kb(client, kb_id):
    update_kb_operation_dto = UpdateKbOperationDTO(add=UpdateKbOperationDTOAdd(
        qna_list=[QnADTO(questions=["bye"], answer="goodbye")]))
    update_op = client.knowledgebase.update(kb_id=kb_id,
                                            update_kb=update_kb_operation_dto)
    _monitor_operation(client=client, operation=update_op)