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)
def main(myblob: func.InputStream): logging.info(f"Python blob trigger function processed blob \n" f"Name: {myblob.name}\n" f"URI: {myblob.uri}\n" f"Blob Size: {myblob.length} bytes") subscription_key = os.environ["subscription_key"] endpoint = os.environ["endpoint"] kb_id = os.environ["kb_id"] client = QnAMakerClient( endpoint=endpoint, credentials=CognitiveServicesCredentials(subscription_key)) update_kb(client=client, kb_id=kb_id, blob_url=myblob.uri) publish_kb(client=client, kb_id=kb_id)
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") ] )
def publish(self): client = QnAMakerClient(endpoint=self.credential.app_host, credentials=CognitiveServicesCredentials( self.credential.app_key)) client.knowledgebase.publish(kb_id=self.credential.app_id)
listSearchResults = client.knowledgebase.generate_answer( kb_id, QueryDTO(question="How do I manage my knowledgebase?")) for i in listSearchResults.answers: print(f"Answer ID: {i.id}.") print(f"Answer: {i.answer}.") print(f"Answer score: {i.score}.") # </GenerateAnswerPreview> # <Main> # <AuthorizationAuthor> client = QnAMakerClient( endpoint=endpoint, credentials=CognitiveServicesCredentials(subscription_key)) # </AuthorizationAuthor> kb_id = create_kb(client=client) update_kb(client=client, kb_id=kb_id) publish_kb(client=client, kb_id=kb_id) download_kb(client=client, kb_id=kb_id) queryRuntimeKey = getEndpointKeys_kb(client=client) if (try_managed_preview): generate_answer_preview(client=client, kb_id=kb_id) else: # <AuthorizationQuery> runtimeClient = QnAMakerRuntimeClient(