如何使用V2 API将实体及其同义词上载到DialogFlow?

时间:2019-03-19 04:34:57

标签: python dialogflow

我正在尝试上载实体及其与DialogFlow的同义词。据我研究,我们只能创建JSON / CSV格式的实体并直接上传。 original question 或直接从Web应用程序上传。

是否可以通过python使用V2 API上传?

1 个答案:

答案 0 :(得分:1)

您可以使用Python SDK for Dialogflow创建实体类型和实体。

这是基本代码,您可以根据需要进行更改:

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))

您将需要创建entity_type,可以使用以下代码进行创建:

def create_entity_type(project_id, display_name, kind):
    """Create an entity type with the given display name."""
    import dialogflow_v2 as dialogflow
    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))

希望有帮助。