Working with Dialogflow CX via Python Series — Part 1 — Agent Operations

Anjali Chimnani
Google Cloud - Community
3 min readAug 19, 2023

Dialogflow is a Natural Language platform enabling creation of Virtual agents that provide different ways to the end users to engage with your business. Virtual Agents thus, provide your business a 24X7 connect with your end users for any requirement — it would be for customer support, queries, know-how, payments, and anything that you could think of for better continued uninterrupted engagement. The agents are a Lifelike conversational AI interface using which the end users can interact with in ways they are most comfortable with. Thus, the agents can be created for different interfaces as chat, voice, web, etc. and can integrate with different components in your ecosystem both upstream and downstream.

Dialogflow provides 2 services for agent creation depending on the sophistication required:

A Sample Virtual Agent conversation
A Sample Virtual Agent Conversation

Developing or interacting with Dialogflow CX or ES can be done using Dialogflow console, REST APIs and client libraries.

This is the first part of our Series to work with Dialogflow CX using Python Client Libraries. Here, we will create and work on our Virtual agent in Dialogflow CX using the library. There are many components in Dialogflow CX such as Agents, Flows, Pages, Statehandlers, Webhooks etc. We will be working with all of these components and Agents are the beginning.

Agents are the Natural Language Understanding (NLU) modules that transform user requests into actionable data. We create Agents in our Google Cloud projects to encapsulate all the business functionality we want the agent to communicate with our user. Agents could be in different languages, time zones, and could be 1 or many depending upon the organization required.

Since we are working with client libraries, there are 2 versions that exist currently — dialogflowcx_v3 (stable) and dialogflowcx_v3beta1(with beta features). We will be using the former version as it is stable and can be used in applications that can be productionized.

Lets get Started.

First, we will set up our Python environment for working with Dialogflow CX.

  • Install a supported version of Python i.e any of the current active and maintenance versions of Python. I am using Python 3.9.6 version.
  • Install virtualenv as:
pip install virtualenv
  • Create Virtual environment to isolate the dependencies as:
virtualenv venv

venv/bin/activate
  • Install the Dialogflow libraries

Add the following in the requirements.txt file

google-cloud-dialogflow-cx==1.25.0

Install the dependencies:

pip install -r requirements.txt

Once our environment setup is completed, let us create the Dialogflow CX Agent in our Google Cloud Project.

In case you do not have Google Cloud Project yet, it can be created easily using the documentation

Agents package in the DF CX library helps us with all the operations that can be performed on the Agent from creating them, getting/listing its different aspects, exporting to deleting them. We will use a few methods in our code that are most commonly used. For this purpose we will create a file as agent_operations.py and add all the different operations

touch agent_operations.py
Python code for Agent Operations

We will create a Dialogflow CX agent with name cai_agent_library in our Google Cloud Project (to be replaced in the code below at <>) in global location in English language in Asia/Calcutta time zone. There are different locations, languages and time zones supported.

touch dialogflow.py
Python main file performing operations on Agents invoking respective methods

This code helps to create a Dialogflow CX agent from within Python program, list all the agents present in our Google Cloud Project in the given region/location, export it and then delete it. In case we want to persist the agent, comment the last line calling the delete_agent method. The print statements within the code are optional, they are just to show the responses returned.

Voila! We did it. We created a Python program to interact with Dialogflow CX agents using its libraries. We are going to do furthermore in our next expedition.

--

--