0

I am using the DialogFlow CX python library for an agent integration. Once I send a request through detect_intent, I want to be able to check in the response for a flag that determines if the interaction with the agent has ended.

For this, I found a property within the ResponseMessage data type response called end_interaction. I tested this with one of my agents, and effectively once the interaction ends, I can see the end_interaction within the array of response messages:

# inspecting query_result.response_messages
[
  text {
    text: "Goodbye."
  }, 
  end_interaction {
  }
]

Now, the problem is that I want to trigger some actions whenever we receive the signal, but since the end_interaction field is being sent empty whenever I try to check it returns me False:

# Both response messages from the example above return False under a bool
bool(query_result.response_messages[0].end_interaction) == False # text message response
bool(query_result.response_messages[1].end_interaction) == False # end_interaction signal

I have tried many things, such as checking with isinstance and hasattr, but they return True for all scenarios since the ResponseMessage data type always has an end_interaction attr.

Any help for finding how to detect and check for this signal is highly appreciated! Thanks!

Carlos
  • 23
  • 1
  • 1
  • 2

1 Answers1

0

To detect and check for the end_interaction field, you can use the following in your detect_intent:

any("end_interaction" in d for d in response.query_result.response_messages)

Please note that this will return True if the end_interaction field is found in the response_messages list and False if not. You can use this to determine if the interaction with the agent has ended.

Here is a python sample code of the detect_intent with the end_interaction field for your reference:

def detect_intent_texts(agent, session_id, texts, language_code):
session_path = f'{agent}/sessions/{session_id}'
print(f"Session path: {session_path}\n")
client_options = None
agent_components = AgentsClient.parse_agent_path(agent)
location_id = agent_components["location"]
if location_id != "global":
    api_endpoint = f"{location_id}-dialogflow.googleapis.com:443"
    print(f"API Endpoint: {api_endpoint}\n")
    client_options = {"api_endpoint": api_endpoint}
session_client = SessionsClient(client_options=client_options)

for text in texts:
    text_input = session.TextInput(text=text)
    query_input = session.QueryInput(text=text_input, language_code=language_code)
    request = session.DetectIntentRequest(
        session=session_path, query_input=query_input
    )
    response = session_client.detect_intent(request=request)

    print("=" * 20)
    print(f"Query text: {response.query_result.text}")
    response_messages = [
        " ".join(msg.text.text) for msg in response.query_result.response_messages
    ]
    print(f"Response Messages:\n {response.query_result.response_messages}")
    print(f'End of interaction: {any("end_interaction" in d for d in response.query_result.response_messages)}')

Here is the result:

Result

Leri
  • 185
  • 5