dialogflow-esdialogflow-cxdialogflow-cx-dashboard

Dialogflow CX import entities as CSV


Task:

I want to create a custom entity type to hold a list of 100 product names.

Solution in Dialogflow ES (v2)

In Dialogflow ES, it was easy upload entity or switch to raw mode while creating entity as you can see in images below:

enter image description here enter image description here

Problem in Dialogflow CX (v3)

In Dialogflow CX, I don’t see any of such options (as you can see in images below), and having to do this manually for every custom entity is not only difficult but also time consuming specially when you have it in hundreds.

enter image description here enter image description here

What I am looking for in answer:

I know there is way to upload entity using dialogflow v3 rest api, but I am looking for a way to upload entities from something that is UI based, I also understand that I can make my own UI to upload entities using dialogflow v3 rest apis but that would be re invention of wheels I would say, so now if there is any option that is hidden somewhere to in cx dashboard please let me know, or if someone have contacts in dialogflow dev team please let them know we are having hard time with this


Solution

  • Check the Official Dialogflow CX REST API documentation for this. https://cloud.google.com/dialogflow/cx/docs/reference/rest/v3/projects.locations.agents.entityTypes/create

    Below is a sample code that reads the column City in the sample.csv using pandas and creates an entity. Python

    import requests
    import pandas as pd
    from google.oauth2 import service_account
    from google.cloud.dialogflowcx_v3beta1.types import entity_type
    from google.auth.transport.requests import AuthorizedSession
    
    # sample csv data
    # Name,City
    # John,New York City
    # Alice,Philadelphia
    
    data = pd.read_csv("sample.csv") 
    cities = data['City'].tolist()
    entity_json = []
    for each in cities:
        each_entity_value = {}
        each_entity_value['value'] = each
        each_entity_value['synonyms'] = [each]
        entity_json.append(each_entity_value)
    print(entity_json)
    print('***************************************')
    
    # download the service account json with the required permissions to call the cx agent
    credentials = service_account.Credentials.from_service_account_file(
        'credentials.json')
    scoped_credentials = credentials.with_scopes(
        ['https://www.googleapis.com/auth/cloud-platform'])
    authed_session = AuthorizedSession(scoped_credentials)
    kind = entity_type.EntityType.Kind.KIND_MAP
    
    # configure these variables before running the script
    project_id = #YOUR-PROJECT-ID
    agent_id = #YOUR-CX-AGENT-ID
    location = #AGENT-LOCATION-ID
    response = authed_session.post('https://dialogflow.googleapis.com/v3/projects/'+ project_id + '/locations/' + location + '/agents/' + agent_id + '/entityTypes',
                                    json={
                                            "kind": kind,
                                            "displayName": "city-names",
                                            "entities": entity_json
                                        }
                                    )                        
    
    response_txt = response.text
    print(response_txt)
    

    Here are the requirements.

    # requirements.txt
    # google-cloud-dialogflow-cx==0.5.0
    # pandas==1.1.5
    # requests==2.21.0
    # requests-oauthlib==1.3.0
    

    You can always try it on the API explorer first by providing the required parameters.

    parent: projects/<project-id>/locations/<location>/agents/<agent-id>
    languageCode: en
    Request body: {
      "displayName": "test",
      "entities": [
        {
          "value": "string",
          "synonyms": [
            "string",
            "str"
          ]
        }
      ],
      "kind": "KIND_MAP"
    }
    

    Enable the credentials and execute it. You will get a 200 success response and a new entity will be created in the CX agent console.