Dialogflow ES

Here we implement DialogflowEsConnector, an implementation of Connector that allows Agents to operate on Dialogflow ES.

class CustomDialogflowEsIntent[source]

This just gives a name to the content of custom intent files. These intents can be optionally imported in the Agent, but cannot be used within the Intents framework

class DialogflowEsConnector(google_credentials, agent_cls, default_session=None, default_language=None, rich_platforms=('telegram'), webhook_configuration=None, custom_intents=None)[source]

This is an implementation of Connector that enables Agents to work as Dialogflow projects.

An Agent can be connected to Dialogflow by providing its Agent class and service account credentials for the the Google Cloud project that hosts the Dialogflow ES agent:

from example_agent import ExampleAgent
from intents.connectors import DialogflowEsConnector
df = DialogflowEsConnector('/path/to/your/service-account-credentials.json', ExampleAgent)

The Connector can now be used, mainly to

A notable init parameter is custom_intents. This is a path to a folder that contains raw Dialogflow JSON intent files. They will uploaded, but their fulfillment will be ignored, and won’t return an Intent class when predicted. They are useful to include placeholders for features that are not yet modelled in Intents, such as Location entities, or Google Assistant helper intent triggers. Note that this is experimental and may be changed in the future.

Parameters
  • google_credentials (Union[str, Credentials]) – Path to service account JSON credentials, or a Credentials object

  • agent_cls (Type[Agent]) – The Agent to connect

  • default_session (Optional[str]) – An arbitrary string to identify the conversation during predictions. If None, Connector will generate a random string

  • default_language (Union[LanguageCode, str, None]) – Default language to use during predictions. If None, Connector will use the Agent’s firs defined language.

  • rich_platforms (Iterable[str]) – Platforms to include when exporting Rich response messages

  • webhook_configuration (Optional[WebhookConfiguration]) – Webhook connection parameters

  • custom_intents (Optional[str]) – Path to a folder containing custom Dialogflow intents JSON files.

export(destination)[source]

Export the connected Agent in a format that can be read and imported natively by the Prediction service. For instance, the Dialogflow service will produce a ZIP export that can be imported from the Dialogflow console.

Note that you can also directly upload the Agent with upload().

Parameters

destination (str) – destination path of the exported Agent

fulfill(fulfillment_request)[source]

This method is responsible for handling requests coming from a fulfillment interface. We are at that point in the flow when an intent was triggered/predicted, and Service is calling the webhook service for fulfillment. Refer to intents.fulfillment for a more detailed explanation.

In this method, Connector interprets the body of the request, builds a FulfillmentContext object, builds the Intent object that is references in the request, and calls its fulfill() method.

This will produce a FulfillmentResult object, that Connector will translate into a Service-compatible response (a dictionary) and return to caller.

Parameters

fulfillment_request (FulfillmentRequest) – A raw fulfillment request, as it is sent by the Prediction service (typically via a standard REST webhook call)

Return type

dict

Returns

An object containing a response that Service can read

property gcp_project_id

Return the Google Cloud Project ID that is associated with the current Connection

Return type

str

predict(message, session=None, language=None)[source]

Predict the given User message in the given session using the given language. When session or language are None, predict will use the default values that are specified in __init__().

predict will return an instance of Prediction, representing the service response.

>>> from intents.connectors import DialogflowEsConnector
>>> from example_agent import ExampleAgent
>>> df = DialogflowEsConnector('/path/to/service-account.json', ExampleAgent)
>>> prediction = df.predict("Hi, my name is Guido")
>>> prediction.intent
UserNameGive(user_name='Guido')
>>> prediction.intent.user_name
"Guido"
>>> prediction.fulfillment_text
"Hi Guido, I'm Bot"
>>> prediction.confidence
0.86
Parameters
  • message (str) – The User message to predict

  • session (Optional[str]) – Any string identifying a conversation

  • language (Union[LanguageCode, str, None]) – A LanguageCode object, or a ISO 639-1 string (e.g. “en”)

Return type

DialogflowPrediction

trigger(intent, session=None, language=None)[source]

Trigger the given Intent in the given session using the given language. When session or language are None, predict will use the default values that are specified in __init__().

>>> from intents.connectors import DialogflowEsConnector
>>> from example_agent import ExampleAgent, smalltalk
>>> df = DialogflowEsConnector('/path/to/service-account.json', ExampleAgent)
>>> prediction = df.trigger(smalltalk.AgentNameGive(agent_name='Alice'))
>>> prediction.intent
AgentNameGive(agent_name='Alice')
>>> prediction.fulfillment_text
"Howdy Human, I'm Alice"
>>> prediction.confidence
1.0
Parameters
  • intent (Intent) – The Intent instance to trigger

  • session (Optional[str]) – Any string identifying a conversation

  • language (Union[LanguageCode, str, None]) – A LanguageCode object, or a ISO 639-1 string (e.g. “en”)

Return type

DialogflowPrediction

upload()[source]

Upload the connected Agent to the Prediction Service.

class DialogflowPrediction(intent, confidence, fulfillment_messages, fulfillment_text=None, df_response=False)[source]

This is an implementation of Prediction that comes from Dialogflow. It adds a df_response field, through which the full Dialogflow prediction payload can be accessed. Note that this is a tool for debugging: relying on Dialogflow data in your business logic is likely to make it harder to connect your Agent to different platforms.

DialogflowPredictions are produced internally by DialogflowEsConnector, and are returned by its predict() and trigger() methods.

Parameters
  • intent (Intent) – An instance of the predicted Intent

  • confidence (float) – Dialogflow’s confidence on its prediction

  • fulfillment_messages (IntentResponseDict) – A map of Intent Responses, as they were returned by the Service

  • fulfillment_text (Optional[str]) – A plain-text version of the response

  • df_response (DetectIntentBody) – Raw Dialogflow response data. It is advisable not to rely on this is production, if you want to keep cross-service compatibility