def test_delete_document(self): # Setup Expected Response expected_response = {} expected_response = empty_pb2.Empty(**expected_response) operation = operations_pb2.Operation( name="operations/test_delete_document", done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.DocumentsClient() # Setup Request name = client.document_path("[PROJECT]", "[KNOWLEDGE_BASE]", "[DOCUMENT]") response = client.delete_document(name) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = document_pb2.DeleteDocumentRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_document(self): # Setup Expected Response name_2 = "name2-1052831874" display_name = "displayName1615086568" mime_type = "mimeType-196041627" content_uri = "contentUri-388807514" enable_auto_reload = False expected_response = { "name": name_2, "display_name": display_name, "mime_type": mime_type, "content_uri": content_uri, "enable_auto_reload": enable_auto_reload, } expected_response = document_pb2.Document(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.DocumentsClient() # Setup Request name = client.document_path("[PROJECT]", "[KNOWLEDGE_BASE]", "[DOCUMENT]") response = client.get_document(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = document_pb2.GetDocumentRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_documents(self): # Setup Expected Response next_page_token = '' documents_element = {} documents = [documents_element] expected_response = { 'next_page_token': next_page_token, 'documents': documents } expected_response = document_pb2.ListDocumentsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = dialogflow_v2beta1.DocumentsClient(channel=channel) # Setup Request parent = client.knowledge_base_path('[PROJECT]', '[KNOWLEDGE_BASE]') paged_list_response = client.list_documents(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.documents[0] == resources[0] assert len(channel.requests) == 1 expected_request = document_pb2.ListDocumentsRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_documents(self): # Setup Expected Response next_page_token = "" documents_element = {} documents = [documents_element] expected_response = { "next_page_token": next_page_token, "documents": documents } expected_response = document_pb2.ListDocumentsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.DocumentsClient() # Setup Request parent = client.knowledge_base_path("[PROJECT]", "[KNOWLEDGE_BASE]") paged_list_response = client.list_documents(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.documents[0] == resources[0] assert len(channel.requests) == 1 expected_request = document_pb2.ListDocumentsRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def setup_teardown(): # Create a knowledge base to use in document management client = dialogflow.KnowledgeBasesClient() project_path = client.project_path(PROJECT_ID) knowledge_base = dialogflow.types.KnowledgeBase( display_name=KNOWLEDGE_BASE_NAME) response = client.create_knowledge_base(project_path, knowledge_base) pytest.KNOWLEDGE_BASE_ID = response.name.split( '/knowledgeBases/')[1].split('\n')[0] # Create a document to delete knowledge_base_path = client.knowledge_base_path( PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) document = dialogflow.types.Document( display_name=DOCUMENT_DISPLAY_NAME, mime_type='text/html', content_uri='https://cloud.google.com/storage/docs/faq') document.knowledge_types.append( dialogflow.types.Document.KnowledgeType.Value('FAQ')) documents_client = dialogflow.DocumentsClient() response = documents_client.create_document(knowledge_base_path, document) document = response.result(timeout=90) pytest.DOCUMENT_ID = document.name.split('/documents/')[1].split('\n')[0] yield # Delete the created knowledge base client.delete_knowledge_base(knowledge_base_path, force=True)
def test_create_document(self): # Setup Expected Response name = 'name3373707' display_name = 'displayName1615086568' mime_type = 'mimeType-196041627' content_uri = 'contentUri-388807514' expected_response = { 'name': name, 'display_name': display_name, 'mime_type': mime_type, 'content_uri': content_uri } expected_response = document_pb2.Document(**expected_response) operation = operations_pb2.Operation( name='operations/test_create_document', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = dialogflow_v2beta1.DocumentsClient(channel=channel) # Setup Request parent = client.knowledge_base_path('[PROJECT]', '[KNOWLEDGE_BASE]') document = {} response = client.create_document(parent, document) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = document_pb2.CreateDocumentRequest( parent=parent, document=document) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_document(self): # Setup Expected Response name_2 = 'name2-1052831874' display_name = 'displayName1615086568' mime_type = 'mimeType-196041627' content_uri = 'contentUri-388807514' expected_response = { 'name': name_2, 'display_name': display_name, 'mime_type': mime_type, 'content_uri': content_uri } expected_response = document_pb2.Document(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.DocumentsClient() # Setup Request name = client.document_path('[PROJECT]', '[KNOWLEDGE_BASE]', '[DOCUMENT]') response = client.get_document(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = document_pb2.GetDocumentRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_documents_exception(self): channel = ChannelStub(responses=[CustomException()]) client = dialogflow_v2beta1.DocumentsClient(channel=channel) # Setup request parent = client.knowledge_base_path('[PROJECT]', '[KNOWLEDGE_BASE]') paged_list_response = client.list_documents(parent) with pytest.raises(CustomException): list(paged_list_response)
def test_reload_document_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.DocumentsClient() with pytest.raises(CustomException): client.reload_document()
def test_get_document_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = dialogflow_v2beta1.DocumentsClient(channel=channel) # Setup request name = client.document_path('[PROJECT]', '[KNOWLEDGE_BASE]', '[DOCUMENT]') with pytest.raises(CustomException): client.get_document(name)
def test_get_document_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.DocumentsClient() # Setup request name = client.document_path("[PROJECT]", "[KNOWLEDGE_BASE]", "[DOCUMENT]") with pytest.raises(CustomException): client.get_document(name)
def test_list_documents_exception(self): channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.DocumentsClient() # Setup request parent = client.knowledge_base_path("[PROJECT]", "[KNOWLEDGE_BASE]") paged_list_response = client.list_documents(parent) with pytest.raises(CustomException): list(paged_list_response)
def create_document(project_id, knowledge_base_id, display_name, mime_type, knowledge_type, content_uri): """Creates a Document. Args: project_id: The GCP project linked with the agent. knowledge_base_id: Id of the Knowledge base. display_name: The display name of the Document. mime_type: The mime_type of the Document. e.g. text/csv, text/html, text/plain, text/pdf etc. knowledge_type: The Knowledge type of the Document. e.g. FAQ, EXTRACTIVE_QA. content_uri: Uri of the document, e.g. gs://path/mydoc.csv, http://mypage.com/faq.html.""" client = dialogflow.DocumentsClient() knowledge_base_path = client.knowledge_base_path(project_id, knowledge_base_id) document = dialogflow.types.Document(display_name=display_name, mime_type=mime_type, content_uri=content_uri) document.knowledge_types.append( dialogflow.types.Document.KnowledgeType.Value(knowledge_type)) response = client.create_document(knowledge_base_path, document) print('Waiting for results...') document = response.result(timeout=120) print('Created Document:') print(' - Display Name: {}'.format(document.display_name)) print(' - Knowledge ID: {}'.format(document.name)) print(' - MIME Type: {}'.format(document.mime_type)) print(' - Knowledge Types:') # for knowledge_type in document.knowledge_types: # print(' - {}'.format(KNOWLEDGE_TYPES[knowledge_type])) print(' - Source: {}\n'.format(document.content_uri)) document = {} response = client.update_document(document) def callback(operation_future): # Handle result. result = operation_future.result() response.add_done_callback(callback) # Handle metadata. metadata = response.metadata()
def delete_document(project_id, knowledge_base_id, document_id): """Deletes a Document. Args: project_id: The GCP project linked with the agent. knowledge_base_id: Id of the Knowledge base. document_id: Id of the Document.""" import dialogflow_v2beta1 as dialogflow client = dialogflow.DocumentsClient() document_path = client.document_path(project_id, knowledge_base_id, document_id) response = client.delete_document(document_path) print('operation running:\n {}'.format(response.operation)) print('Waiting for results...') print('Done.\n {}'.format(response.result()))
def delete_document(PROJECT_ID, KID, DID): """ Deletes a Document. called by DELETE /knowledge_base/{KID}/document/{DID} :parameter PROJECT_ID: The GCP project linked with the agent. :parameter KID: Id of the Knowledge base. :parameter DID: Id of the Document. """ client = dialogflow.DocumentsClient() document_path = client.document_path(PROJECT_ID, KID, DID) response = client.delete_document(document_path) print('operation running:\n {}'.format(response.operation)) print('Waiting for results...') print('Done.\n {}'.format(response.result()))
def list_documents(PROJECT_ID, KID): """ List all Documents in a Knowledge base by its KID. indirectly called by GET /knowledge_base/{KID}. directly called by get_knowledge_base Function format returned: "documents": [ { "document-id": "searched from knowledge base management", "document-name": "stackoverflow", "MIME-type": "text/csv", "Knowledge-type": "FAQ", "content_uri": "gs://test/test01.csv" } :parameter PROJECT_ID: The GCP project linked with the agent. :parameter KID: ID to Knowledge base by. """ client = dialogflow.DocumentsClient() knowledge_base_path = client.knowledge_base_path(PROJECT_ID, KID) #testing stout print('Documents for Knowledge Id: {}'.format(KID)) for document in client.list_documents(knowledge_base_path): print(' - Display Name: {}'.format(document.display_name)) print(' - document ID: {}'.format(document.name.split("/")[-1])) print(' - MIME Type: {}'.format(document.mime_type)) print(' - Knowledge Types:') for knowledge_type in document.knowledge_types: print(' - {}'.format(KNOWLEDGE_TYPES[knowledge_type])) print(' - Source: {}\n'.format(document.content_uri)) print(' - Detail Location: {}'.format(document.name)) #content returned document_list = [] for document in client.list_documents(knowledge_base_path): document_dict = dict() document_dict["document-name"] = str(document.display_name) document_dict["document-id"] = str(document.name.split("/")[-1]) document_dict["MIME-type"] = str(document.mime_type) knowledge_type_list = [ KNOWLEDGE_TYPES[i] for i in document.knowledge_types ] document_dict["Knowledge-type"] = ",".join(knowledge_type_list) document_dict["content_uri"] = document.content_uri document_list.append(document_dict) #returned only document list, need embeded into a dict of kb return document_dict
def get_document(PROJECT_ID, KID, DID): """ Gets a Document in a specific knowledge base by kid called by GET /knowledge_base/{KID}/document/{DID} format returned: { "knowledge-base-id": "searched from knowledge base management", "document-id": "searched from knowledge base management", "display-name": "stackoverflow", "MIME-type": "text/csv", "Knowledge-type": "FAQ", "content_uri": "gs://test/test01.csv" } :parameter PROJECT_ID: The GCP project linked with the agent. :parameter KID: Id of the Knowledge base. :parameter DID: Id of the Document. """ client = dialogflow.DocumentsClient() document_path = client.document_path(PROJECT_ID, KID, DID) response = client.get_document(document_path) # testing stout print('Got Document:') print(' - Display Name: {}'.format(response.display_name)) print(' - Knowledge ID: {}'.format(response.name.split("/")[-1])) print(' - MIME Type: {}'.format(response.mime_type)) print(' - Knowledge Types:') for knowledge_type in response.knowledge_types: print(' - {}'.format(KNOWLEDGE_TYPES[knowledge_type])) print(' - Source: {}\n'.format(response.content_uri)) print(' - Detail Location: {}'.format(response.name)) # content returned document_dict = dict() document_dict["knowledge-base-id"] = str(KID) document_dict["document-name"] = str(response.display_name) document_dict["document-id"] = str(response.name.split("/")[-1]) document_dict["MIME-type"] = str(response.mime_type) knowledge_type_list = [ KNOWLEDGE_TYPES[i] for i in response.knowledge_types ] document_dict["Knowledge-type"] = ",".join(knowledge_type_list) document_dict["content_uri"] = response.content_uri # returned done, directly use as response return document_dict
def test_create_document_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_create_document_exception', done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) client = dialogflow_v2beta1.DocumentsClient(channel=channel) # Setup Request parent = client.knowledge_base_path('[PROJECT]', '[KNOWLEDGE_BASE]') document = {} response = client.create_document(parent, document) exception = response.exception() assert exception.errors[0] == error
def test_delete_document_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_delete_document_exception', done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) client = dialogflow_v2beta1.DocumentsClient(channel=channel) # Setup Request name = client.document_path('[PROJECT]', '[KNOWLEDGE_BASE]', '[DOCUMENT]') response = client.delete_document(name) exception = response.exception() assert exception.errors[0] == error
def counter(PROJECT_ID, KID): """ count the number of documents in a specific knowledgement base :parameter PROJECT_ID: The GCP project linked with the agent. :parameter KID: ID to Knowledge base by. :return num: the number of documents """ client = dialogflow.DocumentsClient() knowledge_base_path = client.knowledge_base_path(PROJECT_ID, KID) # testing stout print('Documents for Knowledge Id: {}'.format(KID)) iterator = client.list_documents(knowledge_base_path) count = 0 for i in iterator: count += 0 return count
def test_update_document_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_update_document_exception", done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.DocumentsClient() # Setup Request document = {} response = client.update_document(document) exception = response.exception() assert exception.errors[0] == error
def list_documents(project_id, knowledge_base_id): """Lists the Documents belonging to a Knowledge base. Args: project_id: The GCP project linked with the agent. knowledge_base_id: Id of the Knowledge base.""" import dialogflow_v2beta1 as dialogflow client = dialogflow.DocumentsClient() knowledge_base_path = client.knowledge_base_path(project_id, knowledge_base_id) print('Documents for Knowledge Id: {}'.format(knowledge_base_id)) for document in client.list_documents(knowledge_base_path): print(' - Display Name: {}'.format(document.display_name)) print(' - Knowledge ID: {}'.format(document.name)) print(' - MIME Type: {}'.format(document.mime_type)) print(' - Knowledge Types:') for knowledge_type in document.knowledge_types: print(' - {}'.format(KNOWLEDGE_TYPES[knowledge_type])) print(' - Source: {}\n'.format(document.content_uri))
def get_document(project_id, knowledge_base_id, document_id): """Gets a Document. Args: project_id: The GCP project linked with the agent. knowledge_base_id: Id of the Knowledge base. document_id: Id of the Document.""" import dialogflow_v2beta1 as dialogflow client = dialogflow.DocumentsClient() document_path = client.document_path(project_id, knowledge_base_id, document_id) response = client.get_document(document_path) print('Got Document:') print(' - Display Name: {}'.format(response.display_name)) print(' - Knowledge ID: {}'.format(response.name)) print(' - MIME Type: {}'.format(response.mime_type)) print(' - Knowledge Types:') for knowledge_type in response.knowledge_types: print(' - {}'.format(KNOWLEDGE_TYPES[knowledge_type])) print(' - Source: {}\n'.format(response.content_uri))
def test_reload_document(self): # Setup Expected Response name = "name3373707" done = True expected_response = {"name": name, "done": done} expected_response = operations_pb2.Operation(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.DocumentsClient() response = client.reload_document() assert expected_response == response assert len(channel.requests) == 1 expected_request = document_pb2.ReloadDocumentRequest() actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_reload_document_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_reload_document_exception", done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.DocumentsClient() # Setup Request name = client.document_path("[PROJECT]", "[KNOWLEDGE_BASE]", "[DOCUMENT]") response = client.reload_document(name) exception = response.exception() assert exception.errors[0] == error
def test_create_document_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_create_document_exception', done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.DocumentsClient() # Setup Request parent = client.knowledge_base_path('[PROJECT]', '[KNOWLEDGE_BASE]') document = {} response = client.create_document(parent, document) exception = response.exception() assert exception.errors[0] == error
def test_create_document(self): # Setup Expected Response name = "name3373707" display_name = "displayName1615086568" mime_type = "mimeType-196041627" content_uri = "contentUri-388807514" enable_auto_reload = False expected_response = { "name": name, "display_name": display_name, "mime_type": mime_type, "content_uri": content_uri, "enable_auto_reload": enable_auto_reload, } expected_response = document_pb2.Document(**expected_response) operation = operations_pb2.Operation( name="operations/test_create_document", done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.DocumentsClient() # Setup Request parent = client.knowledge_base_path("[PROJECT]", "[KNOWLEDGE_BASE]") document = {} response = client.create_document(parent, document) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = document_pb2.CreateDocumentRequest( parent=parent, document=document) actual_request = channel.requests[0][1] assert expected_request == actual_request
def create_document(project_id, knowledge_base_id, display_name, mime_type, knowledge_type, content_uri): """Creates a Document. Args: project_id: The GCP project linked with the agent. knowledge_base_id: Id of the Knowledge base. display_name: The display name of the Document. mime_type: The mime_type of the Document. e.g. text/csv, text/html, text/plain, text/pdf etc. knowledge_type: The Knowledge type of the Document. e.g. FAQ, EXTRACTIVE_QA. content_uri: Uri of the document, e.g. gs://path/mydoc.csv, http://mypage.com/faq.html.""" import dialogflow_v2beta1 as dialogflow client = dialogflow.DocumentsClient() knowledge_base_path = client.knowledge_base_path(project_id, knowledge_base_id) document = dialogflow.types.Document(display_name=display_name, mime_type=mime_type, content_uri=content_uri) document.knowledge_types.append( dialogflow.types.Document.KnowledgeType.Value(knowledge_type)) response = client.create_document(knowledge_base_path, document) print('Waiting for results...') document = response.result(timeout=90) print('Created Document:') print(' - Display Name: {}'.format(document.display_name)) print(' - Knowledge ID: {}'.format(document.name)) print(' - MIME Type: {}'.format(document.mime_type)) print(' - Knowledge Types:') for knowledge_type in document.knowledge_types: print(' - {}'.format(KNOWLEDGE_TYPES[knowledge_type])) print(' - Source: {}\n'.format(document.content_uri))
def test_update_document(self): # Setup Expected Response name = "name3373707" display_name = "displayName1615086568" mime_type = "mimeType-196041627" content_uri = "contentUri-388807514" expected_response = { "name": name, "display_name": display_name, "mime_type": mime_type, "content_uri": content_uri, } expected_response = document_pb2.Document(**expected_response) operation = operations_pb2.Operation( name="operations/test_update_document", done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.DocumentsClient() # Setup Request document = {} response = client.update_document(document) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = document_pb2.UpdateDocumentRequest( document=document) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_delete_document(self): # Setup Expected Response expected_response = {} expected_response = empty_pb2.Empty(**expected_response) operation = operations_pb2.Operation( name='operations/test_delete_document', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = dialogflow_v2beta1.DocumentsClient(channel=channel) # Setup Request name = client.document_path('[PROJECT]', '[KNOWLEDGE_BASE]', '[DOCUMENT]') response = client.delete_document(name) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = document_pb2.DeleteDocumentRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request