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 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 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 list_entity_types(project_id): entity_types_client = dialogflow.EntityTypesClient() parent = entity_types_client.project_agent_path(project_id) entity_types = entity_types_client.list_entity_types(parent) for entity_type in entity_types: print('Entity type name: {}'.format(entity_type.name)) print('Entity type display name: {}'.format(entity_type.display_name)) print('Number of entities: {}\n'.format(len(entity_type.entities)))
def create_entity_type(project_id, display_name, kind=dialogflow.enums.EntityType.Kind.KIND_MAP): 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) logger.debug('Entity type created: \n{}'.format(response))
def add_entity_type(self, name): entity_types_client = dialogflow.EntityTypesClient() parent = entity_types_client.project_agent_path(self.project_id) entity_type = dialogflow.types.EntityType( display_name=name, kind=dialogflow.enums.EntityType.Kind.KIND_LIST) for entity in self.get_entities(): if entity.display_name == name: return entity_types_client.create_entity_type(parent, entity_type)
def add_entity(self, entity_name, entity_value): entity_types_client = dialogflow.EntityTypesClient() entity_type_id = self.get_entity_type_id(entity_name) entity_type_path = entity_types_client.entity_type_path( self.project_id, entity_type_id) synonyms = [entity_value] entity = dialogflow.types.EntityType.Entity() entity.value = entity_value entity.synonyms.extend(synonyms) entity_types_client.batch_create_entities( entity_type_path, [entity], DialogFlowService.LANGUAGE_CODE)
def _get_entity_type_ids(project_id, display_name): entity_types_client = dialogflow.EntityTypesClient() parent = entity_types_client.project_agent_path(project_id) entity_types = entity_types_client.list_entity_types(parent) entity_type_names = [ entity_type.name for entity_type in entity_types if entity_type.display_name == display_name ] return [ entity_type_name.split('/')[-1] for entity_type_name in entity_type_names ]
def __init__(self): if not utils.has_credentials(): utils.print_missing_credential_notification() sys.exit(-1) self._project_id = utils.get_project_id_from_credentials() self._intents_client = dialogflow.IntentsClient() self._entity_types_client = dialogflow.EntityTypesClient() self._project_parent = self._intents_client.project_agent_path( self._project_id) log.info('Project Name: {}'.format(self._project_id)) self.__load_intents__() self.__load_entities__() self._input_contexts = []
def update_dialogflow_entity(city="", entity=""): entity_type_client = dialogflow.EntityTypesClient() if city: entity_parent = entity_type_client.entity_type_path(PROJECT_ID, LOCATION_ENTITY_UUID) entity_to_update = dialogflow.types.EntityType.Entity( value = city, synonyms = [city] ) response = entity_type_client.batch_update_entities(entity_parent, [entity_to_update]) else: entity_parent = entity_type_client.entity_type_path(PROJECT_ID, MEDREQ_ENTITY_UUID) entity_to_update = dialogflow.types.EntityType.Entity( value = entity, synonyms = [entity] ) response = entity_type_client.batch_update_entities(entity_parent, [entity_to_update])
def add_entities(self, entity_name, entity_values): entity_types_client = dialogflow.EntityTypesClient() entity_type_id = self.get_entity_type_id(entity_name) entity_type_path = entity_types_client.entity_type_path( self.project_id, entity_type_id) entity_list = [] for value in entity_values: synonyms = [value] entity = dialogflow.types.EntityType.Entity() entity.value = value entity.synonyms.extend(synonyms) entity_list.append(entity) entity_types_client.batch_create_entities( entity_type_path, entity_list, DialogFlowService.LANGUAGE_CODE)
def create_entity(project_id, entity_type_id, entity_value, synonyms): """Create an entity of the given entity type.""" import dialogflow_v2 as dialogflow 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 create_or_update_entities(project_id, entity_type_id, entities_json): entity_types_client = dialogflow.EntityTypesClient() entity_type_path = entity_types_client.entity_type_path( project_id, entity_type_id) entities = [] for entity_json in entities_json: # Note: synonyms must be exactly [entity_value] if the # entity_type's kind is KIND_LIST synonyms = entity_json['synonyms'] or [entity_json['value']] entity = dialogflow.types.EntityType.Entity() entity.value = entity_json['value'] entity.synonyms.extend(synonyms) entities.append(entity) response = entity_types_client.batch_update_entities( entity_type_path, entities) logger.debug('Entities batch created or updated: \n{}'.format(response))
def get_entities(self): entity_types_client = dialogflow.EntityTypesClient() return entity_types_client.list_entity_types( self.parent, DialogFlowService.LANGUAGE_CODE)
# Imports the Google Cloud client library import os import dialogflow import management.entity_type_management as entity_types_mng import management.entity_management as entity_mng # use GCP credentials and specify dialogflow project os.environ[ "GOOGLE_APPLICATION_CREDENTIALS"] = "C:/Users/jack/Desktop/work/CLOUDIFAI/mycare-patients-6d3e767f97e4.json" projectID = "mycare-patients" client = dialogflow.EntityTypesClient() parent = client.project_agent_path(projectID) session_client = dialogflow.SessionsClient() days_entities = [] for day in range(1, 30): days_entities.append({"value": str(day), "synonyms": [str(day)]}) # day of the month entity_id = entity_types_mng.create_entity_type(projectID, "day_of_the_month", 1) parent_entity_type = parent + "/entityTypes/{}".format(entity_id.name[-36:]) client.batch_create_entities(parent_entity_type, days_entities) # hour of the day entity_id = entity_types_mng.create_entity_type(projectID, "hour_of_the_day", 1) hour_entities = [] for hour in [8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7]: hour_entities.append({"value": str(hour), "synonyms": [str(hour)]})
def handle(self, *args, **options): if os.environ.get('GOOGLE_APPLICATION_CREDENTIALS') is None: raise CommandError( 'The environment variable "GOOGLE_APPLICATION_CREDENTIALS" is not ' 'set. Could not authenticate.') client = dialogflow.EntityTypesClient() contest_type, ticket_type = get_entity_types( client, settings.DIALOGFLOW_PROJECT_ID) current_contests = [c.value for c in contest_type.entities] current_tickets = [t.value for t in ticket_type.entities] contests_to_create, contests_to_delete = [], current_contests.copy() tickets_to_create, tickets_to_delete = [], current_tickets.copy() # Compute entities to create & delete for c in Contest.objects.get_active_contests(): if str(c.id) in current_contests: # entity already exists, don't delete it contests_to_delete.remove(str(c.id)) else: # new entity contests_to_create.append({ "value": str(c.id), "synonyms": [c.name] }) if c.regex in current_tickets: # entity already exists, don't delete it tickets_to_delete.remove(c.regex) else: # new entity tickets_to_create.append({"value": c.regex}) if not (contests_to_create or contests_to_delete or tickets_to_create or tickets_to_delete): self.stdout.write( self.style.SUCCESS( 'All entities up to date. No changes made.')) else: # Create new entities if contests_to_create: client.batch_create_entities(contest_type.name, contests_to_create) self.stdout.write('Created the following contests:\n') for c in contests_to_create: self.stdout.write(f' - {c["value"]}\n') if tickets_to_create: client.batch_create_entities(ticket_type.name, tickets_to_create) self.stdout.write('Created the following tickets:\n') for t in tickets_to_create: self.stdout.write(f' - {t["value"]}\n') # Delete outdated entities if contests_to_delete: client.batch_delete_entities(contest_type.name, contests_to_delete) self.stdout.write('Deleted the following contests:\n') for c in contests_to_delete: self.stdout.write(f' - {c}\n') if tickets_to_delete: client.batch_delete_entities(ticket_type.name, tickets_to_delete) self.stdout.write('Deleted the following tickets:\n') for t in tickets_to_delete: self.stdout.write(f' - {t}\n') self.stdout.write( self.style.SUCCESS('Entities updated successfully'))