def test_get_entity_type(self): # Setup Expected Response name_2 = "name2-1052831874" display_name = "displayName1615086568" enable_fuzzy_extraction = True expected_response = { "name": name_2, "display_name": display_name, "enable_fuzzy_extraction": enable_fuzzy_extraction, } expected_response = entity_type_pb2.EntityType(**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_v2.EntityTypesClient() # Setup Request name = client.entity_type_path("[PROJECT]", "[ENTITY_TYPE]") response = client.get_entity_type(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = entity_type_pb2.GetEntityTypeRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_entity_types(self): # Setup Expected Response next_page_token = '' entity_types_element = {} entity_types = [entity_types_element] expected_response = { 'next_page_token': next_page_token, 'entity_types': entity_types } expected_response = entity_type_pb2.ListEntityTypesResponse( **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_v2.EntityTypesClient() # Setup Request parent = client.project_agent_path('[PROJECT]') paged_list_response = client.list_entity_types(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.entity_types[0] == resources[0] assert len(channel.requests) == 1 expected_request = entity_type_pb2.ListEntityTypesRequest( parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_entity_type(self): # Setup Expected Response name = 'name3373707' display_name = 'displayName1615086568' expected_response = {'name': name, 'display_name': display_name} expected_response = entity_type_pb2.EntityType(**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_v2.EntityTypesClient() # Setup Request entity_type = {} response = client.update_entity_type(entity_type) assert expected_response == response assert len(channel.requests) == 1 expected_request = entity_type_pb2.UpdateEntityTypeRequest( entity_type=entity_type) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_batch_update_entity_types(self): # Setup Expected Response expected_response = {} expected_response = entity_type_pb2.BatchUpdateEntityTypesResponse( **expected_response) operation = operations_pb2.Operation( name="operations/test_batch_update_entity_types", 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_v2.EntityTypesClient() # Setup Request parent = client.project_agent_path("[PROJECT]") response = client.batch_update_entity_types(parent) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = entity_type_pb2.BatchUpdateEntityTypesRequest( parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_batch_delete_entities(self): # Setup Expected Response expected_response = {} expected_response = empty_pb2.Empty(**expected_response) operation = operations_pb2.Operation( name='operations/test_batch_delete_entities', 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_v2.EntityTypesClient() # Setup Request parent = client.entity_type_path('[PROJECT]', '[ENTITY_TYPE]') entity_values = [] response = client.batch_delete_entities(parent, entity_values) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = entity_type_pb2.BatchDeleteEntitiesRequest( parent=parent, entity_values=entity_values) actual_request = channel.requests[0][1] assert expected_request == actual_request
def batch_update_entities(project_id, entity_type, entities): """Create an entity type with the given display name.""" import dialogflow_v2 as dialogflow client = dialogflow.EntityTypesClient() parent = client.entity_type_path(project_id, entity_type) response = client.batch_create_entities(parent, entities) print("Entities Updated: \n{}".format(response))
def setup_teardown(): # Create an entity type entity_types_client = dialogflow.EntityTypesClient() parent = entity_types_client.project_agent_path(PROJECT_ID) entity_type = dialogflow.types.EntityType( display_name=DISPLAY_NAME, kind=dialogflow.enums.EntityType.Kind.KIND_MAP, ) response = entity_types_client.create_entity_type(parent, entity_type) pytest.ENTITY_TYPE_ID = response.name.split("agent/entityTypes/")[1] # Create an entity inside the entity type entity_type_path = entity_types_client.entity_type_path( PROJECT_ID, pytest.ENTITY_TYPE_ID ) entity = dialogflow.types.EntityType.Entity( value=ENTITY_VALUE_1, synonyms=[ENTITY_VALUE_1] ) response = entity_types_client.batch_create_entities( entity_type_path, [entity] ) response.result() yield # Delete the created entity type and its entities entity_types_client.delete_entity_type(entity_type_path)
def create_entity(PROJECT_ID, entity_type_id, entity_value, synonyms): """ :param PROJECT_ID: project id of the agent :param entity_type_id: a specific entity type id in gent :param entity_value: the entity value :param synonyms: the synonyms corresponding entity value :return: """ entity_types_client = dialogflow.EntityTypesClient() # Note: synonyms must be exactly [entity_value] if the # entity_type's kind is KIND_LIST synonyms = synonyms or [entity_value] entity_type_path = entity_types_client.entity_type_path( PROJECT_ID, entity_type_id) entity = dialogflow.types.EntityType.Entity() entity.value = entity_value entity.synonyms.extend(synonyms) response = entity_types_client.batch_create_entities( entity_type_path, [entity]) print('Entity created: {}'.format(response))
def list_entity_types(self): """ List all entity types """ if not self.entity_types_cache: client = dialogflow.EntityTypesClient(credentials=self.credentials) parent = client.project_agent_path(self.project_id) self.entity_types_cache = list(client.list_entity_types(parent)) return self.entity_types_cache
def test_list_entity_types(self): # Setup Expected Response next_page_token = '' entity_types_element = {} entity_types = [entity_types_element] expected_response = { 'next_page_token': next_page_token, 'entity_types': entity_types } expected_response = entity_type_pb2.ListEntityTypesResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = dialogflow_v2.EntityTypesClient(channel=channel) # Setup Request parent = client.project_agent_path('[PROJECT]') paged_list_response = client.list_entity_types(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.entity_types[0] == resources[0] assert len(channel.requests) == 1 expected_request = entity_type_pb2.ListEntityTypesRequest( parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_batch_update_entities(self): # Setup Expected Response expected_response = {} expected_response = empty_pb2.Empty(**expected_response) operation = operations_pb2.Operation( name='operations/test_batch_update_entities', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = dialogflow_v2.EntityTypesClient(channel=channel) # Setup Request parent = client.entity_type_path('[PROJECT]', '[ENTITY_TYPE]') entities = [] response = client.batch_update_entities(parent, entities) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = entity_type_pb2.BatchUpdateEntitiesRequest( parent=parent, entities=entities) actual_request = channel.requests[0][1] assert expected_request == actual_request
def updateEventoKeyWordEntities(sender, instance, created, **kwargs): assunto = instance.assunto # instancia o modelo de nlp nlp = pt_core_news_sm.load() doc = nlp(assunto) # Separação de tokens tokens = pre_processing(doc) # Requisição do dialogflow para obter as entities client = dialogflow_v2.EntityTypesClient() parent = client.project_agent_path(os.environ['PROJECT_ID']) list_entity_types_response = list(client.list_entity_types(parent)) # cria uma nova instância com as novas entities processadas list_entity_types_response = list(client.list_entity_types(parent)) entity_type = list_entity_types_response[2] entries = [] entities = list(entity_type.entities) for token in tokens: entities.append({'value': token.lemma_, 'synonyms': [token.text]}) #realiza o submit das entities ao dialogflow response = client.batch_update_entities(entity_type.name, entities) response.done() # treina o modelo do client = dialogflow_v2.AgentsClient() project_parent = client.project_path(os.environ['PROJECT_ID']) client.train_agent(project_parent)
def test_update_entity_type(self): # Setup Expected Response name = "name3373707" display_name = "displayName1615086568" enable_fuzzy_extraction = True expected_response = { "name": name, "display_name": display_name, "enable_fuzzy_extraction": enable_fuzzy_extraction, } expected_response = entity_type_pb2.EntityType(**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_v2.EntityTypesClient() # Setup Request entity_type = {} response = client.update_entity_type(entity_type) assert expected_response == response assert len(channel.requests) == 1 expected_request = entity_type_pb2.UpdateEntityTypeRequest( entity_type=entity_type) actual_request = channel.requests[0][1] assert expected_request == actual_request
def delete_entity_type(PROJECT_ID, entity_type_id): """Delete entity type with the given entity type name.""" entity_types_client = dialogflow.EntityTypesClient() entity_type_path = entity_types_client.entity_type_path( PROJECT_ID, entity_type_id) entity_types_client.delete_entity_type(entity_type_path)
def get_entity_list(): entity_type_client = dialogflow.EntityTypesClient() parent = entity_type_client.project_agent_path('kobaksa-1b59d') entity_types = entity_type_client.list_entity_types(parent) for entity_type in entity_types: print(entity_type)
def delete_entity(entity, entity_type): print(f'Deleting Entity "{entity}" from {entity_type} in {DIALOGFLOW_AGENT}') uuid = get_entity_type_uuid(entity_type) entity_types_client = dialogflow.EntityTypesClient() parent = entity_types_client.entity_type_path(DIALOGFLOW_AGENT, uuid) return entity_types_client.batch_delete_entities(parent, [entity])
def get_entity_type_uuid(entity_type): entity_types_client = dialogflow.EntityTypesClient() parent = entity_types_client.project_agent_path(DIALOGFLOW_AGENT) entity_types = entity_types_client.list_entity_types(parent) for e in entity_types: if e.display_name == entity_type.value: uuid = e.name.split("/")[-1] return uuid
def delete_entity(project_id, entity_type_id, entity_value): """Delete entity with the given entity type and entity value.""" import dialogflow_v2 as dialogflow entity_types_client = dialogflow.EntityTypesClient() entity_type_path = entity_types_client.entity_type_path( project_id, entity_type_id) entity_types_client.batch_delete_entities(entity_type_path, [entity_value])
def teardown(): yield # Delete the created entity type entity_types_client = dialogflow.EntityTypesClient() assert pytest.ENTITY_TYPE_ID is not None entity_type_path = entity_types_client.entity_type_path( PROJECT_ID, pytest.ENTITY_TYPE_ID) entity_types_client.delete_entity_type(entity_type_path)
def get_entity_id(project_id: str, entity_type: str) -> Optional[str]: """Returns the ID of the specified entity.""" client = dialogflow.EntityTypesClient() parent = client.project_agent_path(project_id) for element in client.list_entity_types(parent): if element.display_name == entity_type: return element.name.split('/')[-1]
def delete_entity_type(project_id, entity_type_id): """Delete entity type with the given entity type name.""" import dialogflow_v2 as dialogflow entity_types_client = dialogflow.EntityTypesClient() entity_type_path = entity_types_client.entity_type_path( project_id, entity_type_id) entity_types_client.delete_entity_type(entity_type_path)
def create_entity_type(PROJECT_ID, display_name, kind): """Create an entity type with the given display name.""" entity_types_client = dialogflow.EntityTypesClient() parent = entity_types_client.project_agent_path(PROJECT_ID) entity_type = dialogflow.types.EntityType(display_name=display_name, kind=kind) response = entity_types_client.create_entity_type(parent, entity_type) print('Entity type created: \n{}'.format(response))
def test_update_entity_type_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = dialogflow_v2.EntityTypesClient(channel=channel) # Setup request entity_type = {} with pytest.raises(CustomException): client.update_entity_type(entity_type)
def test_delete_entity_type_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = dialogflow_v2.EntityTypesClient(channel=channel) # Setup request name = client.entity_type_path('[PROJECT]', '[ENTITY_TYPE]') with pytest.raises(CustomException): client.delete_entity_type(name)
def test_list_entity_types_exception(self): channel = ChannelStub(responses=[CustomException()]) client = dialogflow_v2.EntityTypesClient(channel=channel) # Setup request parent = client.project_agent_path('[PROJECT]') paged_list_response = client.list_entity_types(parent) with pytest.raises(CustomException): list(paged_list_response)
def create_entity_type(self, display_name, kind): """Create an entity type with the given display name.""" client = dialogflow.EntityTypesClient() parent = client.project_agent_path(self.project_id) entity_type = dialogflow.types.EntityType(display_name=display_name, kind=kind) response = client.create_entity_type(parent, entity_type) print "Entity type created:\n{}".format(response)
def list_entities(project_id, entity_type_id): import dialogflow_v2 as dialogflow entity_types_client = dialogflow.EntityTypesClient() parent = entity_types_client.entity_type_path(project_id, entity_type_id) entities = entity_types_client.get_entity_type(parent).entities for entity in entities: print('Entity value: {}'.format(entity.value)) print('Entity synonyms: {}\n'.format(entity.synonyms))
def setup(): # Create an entity type for deletion entity_types_client = dialogflow.EntityTypesClient() parent = entity_types_client.project_agent_path(PROJECT_ID) entity_type = dialogflow.types.EntityType( display_name=DISPLAY_NAME, kind=dialogflow.enums.EntityType.Kind.KIND_MAP, ) response = entity_types_client.create_entity_type(parent, entity_type) pytest.ENTITY_TYPE_ID = response.name.split("agent/entityTypes/")[1]
def test_create_entity_type_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = dialogflow_v2.EntityTypesClient(channel=channel) # Setup request parent = client.project_agent_path('[PROJECT]') entity_type = {} with pytest.raises(CustomException): client.create_entity_type(parent, entity_type)
def add_entity(entity, entity_type): print(f'Adding Entity "{entity}" to {entity_type} in {DIALOGFLOW_AGENT}') uuid = get_entity_type_uuid(entity_type) entity_types_client = dialogflow.EntityTypesClient() parent = entity_types_client.entity_type_path(DIALOGFLOW_AGENT, uuid) new_entity = dialogflow.types.EntityType.Entity() new_entity.value = entity new_entity.synonyms.extend([entity]) return entity_types_client.batch_create_entities(parent, [new_entity])