Search code examples
pythongoogle-cloud-platformdialogflow-esapi-designagent

Adding new utterances to specific Intents in Dialogflow Agent


The questions is quite simple, I have developed an agent in Dialogflow-es, it has been trained with N intents and M utterances by intent. Now, I want to automate the training process, or part of it, using a python script or a notebook. I want to add new utterances to the N intents of the agent using the Dialogflow API and training again after that, but the documentation is not clear at all. I have seen the method batchUpdate but it's not clear for me whether this method is intendet to do what I want neither how to implement it.

Here is the documentation --> GCP-intent batchUpdate

Somebody has done something similar? Or is there an API method to do that?

Thanks in advance.


Solution

  • Finally I have figure it out. it is achieved by following two steps:

    1. Retrieve the current intents and training phrases
    2. Add new intents and training phrases to previous one.

    Although there is a GCP library to do this, I wanted to implement the two steps using requests to keep the code as agnostic as possible.

    here is the code of step one and two.

    Step one:

    url = "https://ValidURL/agent/intents"
    headers = {
        "Authorization": token,
        "Content-Type": "application/json",
        "x-goog-user-project": "valid-project-name"
    }
    
    params = {'intentView': "INTENT_VIEW_FULL"}
    
    while has_next_page:
        if next_page_token:
            params['pageToken'] = next_page_token
            
        response = requests.get(url, headers=headers, params=params)
        response_json = response.json()
    
        if response.status_code == 200:
            intents = response_json.get('intents', [])
            all_display_names.extend([intent['displayName'] for intent in intents])
            all_responses.extend([intent for intent in intents])
            
            next_page_token = response_json.get('nextPageToken')  
            has_next_page = bool(next_page_token)
        else:
            print("Error", response.status_code)
            break
    

    Step two:

    url = "https://validUrL/agents/intents:batchUpdate"
    headers = {
        "Authorization": token,
        "x-goog-user-project": "project_name"
    }
    
    # Building body part
    body = {
        "updateMask":{"paths": ["training_phrases"]},
        "intentView": "INTENT_VIEW_FULL",
        "intentBatchInline": {
            "intents": [
                {
                    "name": f"Project_name/agent/intents/{intent_id}",
                    "display_name": display_name,
                    "trainingPhrases": new_training_phrases,
                    "languageCode":"es"
                }
            ]
        }
    }
    
    # post the new training phrases
    response = requests.post(url, headers=headers, json=body)
    
    
    if response.status_code == 200:
        print("Success.")
    else:
        print("Error:", response.status_code)
        print(response.text)