def create_kb(client):
    print ("Creating knowledge base...")

    qna1 = QnADTO(
        answer="Yes, You can use our [REST APIs](https://docs.microsoft.com/rest/api/cognitiveservices/qnamaker/knowledgebase) to manage your knowledge base.",
        questions=["How do I manage my knowledgebase?"],
        metadata=[
            MetadataDTO(name="Category", value="api"),
            MetadataDTO(name="Language", value="REST"),
        ]
    )

    qna2 = QnADTO(
        answer="Yes, You can use our [Python SDK](https://pypi.org/project/azure-cognitiveservices-knowledge-qnamaker/) with the [Python Reference Docs](https://docs.microsoft.com/python/api/azure-cognitiveservices-knowledge-qnamaker/azure.cognitiveservices.knowledge.qnamaker?view=azure-python) to manage your knowledge base.",
        questions=["Can I program with Python?"],
        metadata=[
            MetadataDTO(name="Category", value="api"),
            MetadataDTO(name="Language", value="Python"),
        ]
    )

    urls = []
    files = [
        FileDTO(
            file_name = "structured.docx",
            file_uri = "https://github.com/Azure-Samples/cognitive-services-sample-data-files/raw/master/qna-maker/data-source-formats/structured.docx"
        )]


    create_kb_dto = CreateKbDTO(
        name="QnA Maker Python SDK Quickstart",
        qna_list=[
            qna1,
            qna2
        ],
        urls=urls,
        files=[],
        enable_hierarchical_extraction=True,
        default_answer_used_for_extraction="No answer found.",
        language="English"
    )
    create_op = client.knowledgebase.create(create_kb_payload=create_kb_dto)

    create_op_monitor = _monitor_operation(client=client, operation=create_op)

    # Get knowledge base ID from resourceLocation HTTP header
    knowledge_base_ID = create_op_monitor.resource_location.replace("/knowledgebases/", "")
    print("Created KB with ID: {}".format(knowledge_base_ID))

    return knowledge_base_ID
Ejemplo n.º 2
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)
Ejemplo n.º 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.")
Ejemplo n.º 4
0
def create_kb(client):

    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/", "")
Ejemplo n.º 5
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")
            ]
        )
Ejemplo n.º 6
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)