Cloud Blog: Create a self-escalating chatbot in Conversational Agents using Webhook and Generators

Source URL: https://cloud.google.com/blog/topics/developers-practitioners/create-a-self-escalating-chatbot-in-conversational-agents/
Source: Cloud Blog
Title: Create a self-escalating chatbot in Conversational Agents using Webhook and Generators

Feedly Summary: As conversational AI becomes a core part of the user experience, it’s crucial for application developers to build chatbots that not only provide accurate information, but also know the right time to escalate to a human agent. 
This blog post will show you how to create a self-escalating chatbot using Google Cloud’s generative AI solutions such as Vertex AI, Conversational Agents (Dialogflow CX), and others. The solution offers several benefits:

Improved user experience: Users receive accurate information and timely assistance, even for complex questions.
Reduced agent workload: Agents receive concise summaries of previous interactions, allowing them to address issues efficiently.
Enhanced chatbot capabilities: The chatbot can learn from escalated queries, continuously improving its ability to handle future interactions.
Increased scalability and security: Cloud Run Functions (CRF) provides a scalable and secure platform for running the webhook function.

Let’s get started.

aside_block
), (‘btn_text’, ‘Start building for free’), (‘href’, ‘http://console.cloud.google.com/freetrial?redirectPath=/welcome’), (‘image’, None)])]>

1. Build the knowledge base
Let’s say we want a chatbot to answer questions related to tourism in India. We’ll start by leveraging Vertex AI Agent Builder and Conversational Agents (Dialogflow CX) to create it.

Unstructured datastore: We index an article on “Tourism in India" as an unstructured datastore within Vertex AI. This allows the chatbot to access and retrieve relevant information from the article in real time, providing comprehensive answers to user queries.
Conversational Agents (Dialogflow CX): We design the conversational flow using Conversational Agents (Dialogflow CX), enabling the chatbot to understand user intent and respond appropriately.

2. Gauge user satisfaction
To ensure user satisfaction, we incorporate a crucial step in the conversation flow: asking the user if they are satisfied with the chatbot’s response. This is done using "yes" and "no" chips specified as part of custom payload, providing a clear and intuitive way for users to express their feedback.
3. Escalate with generative AI
If the user expresses dissatisfaction by clicking "no," the chatbot initiates the escalation process. This is where the power of generative AI comes in.

Generators: We create a generator in Conversational Agents (Dialogflow CX) named "Summarize_mail" that utilizes a zero-shot prompt (direct prompt with no examples) to summarize the conversation. This summary is then used to generate the content of an email, providing context to the human agent.

Here’s the zero-shot prompt we use:

code_block
<ListValue: [StructValue([(‘code’, ‘You are an English expert in summarizing the text in form of a very short mail.\r\nSummarize the conversation and write it in form of a concise e-mail which will be forwarded to an agent as a ticket. The mail should be on point, properly formatted and written in formal tone with a polite closure. Keep the text as less as possible.\r\nAlso, specify the conversation messages below the summary which are present in the conversation. The conversation is as follows: $conversation\r\nThe feedback of the user about the issue is $last-user-utterance.’), (‘language’, ”), (‘caption’, <wagtail.rich_text.RichText object at 0x3e713ff23a90>)])]>

Model configuration: This generator utilizes the Gemini-1.5-flash model with a temperature of 0.3, helping to ensure coherent and informative summaries.

4. Trigger the email with Cloud Run Functions (CRF)
To send the email, we use a webhook to connect our Conversational Agents (Dialogflow CX) agent to a serverless function deployed on Cloud Run Functions. This function handles the email sending logic.
We utilize the Cloud Run Functions console inline editor to write and deploy the following Python code:

code_block
<ListValue: [StructValue([(‘code’, ‘import functions_framework \r\nimport http.client \r\nimport smtplib, ssl\r\nfrom flask import Flask, jsonify\r\nfrom email.message import EmailMessage\r\n\r\n# Cloud Run Functions to handle webhook requests from Conversational Agents (Dialogflow CX)\r\n@functions_framework.http \r\ndef handle_webhook(request):\r\n """\r\n Handles webhook requests from Conversational Agents (Dialogflow CX) and sends an email \r\n summarizing the conversation to a human agent.\r\n\r\n Args:\r\n request: The incoming HTTP request containing the webhook payload.\r\n\r\n Returns:\r\n A JSON response indicating the status of the email sending process.\r\n """\r\n\r\n port = 465 # For SSL\r\n smtp_server = "smtp.gmail.com"\r\n \r\n sender_email = \'<sender_agent_mail_id>\’\r\n receiver_email = \'<receiver_agent_mail_id>\’\r\n password = \'<sender_agent_password>\’\r\n cc_email = \’the logged-in user mail id\’ # Include the user\’s email\r\n\r\n # Extract the conversation summary from the webhook payload\r\n req = request.get_json()\r\n message = req[\’sessionInfo\’][\’parameters\’][\’$request.generative.output\’]\r\n \r\n # Create and send the email\r\n msg = EmailMessage()\r\n msg.set_content(message)\r\n\r\n msg[\’Subject\’] = \’Action Needed: Customer Escalation\’\r\n msg[\’From\’] = sender_email\r\n msg[\’To\’] = receiver_email\r\n msg[\’Cc\’] = cc_email # Add the user to the email for transparency\r\n\r\n try:\r\n # Establish a secure connection to the SMTP server\r\n server = smtplib.SMTP_SSL(smtp_server, port)\r\n server.login(sender_email, password) \r\n server.send_message(msg) \r\n server.quit()\r\n\r\n # Return a success message to Conversational Agents (Dialogflow CX)\r\n return jsonify(\r\n {\r\n \’fulfillment_response\’: {\r\n \’messages\’: [\r\n {\r\n \’text\’: {\r\n \’text\’: [\’The mail is successfully sent!\’]\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n )\r\n\r\n except Exception as e:\r\n # Handle potential errors during email sending\r\n print(f"Error sending email: {e}")\r\n return jsonify(\r\n {\r\n \’fulfillment_response\’: {\r\n \’messages\’: [\r\n {\r\n \’text\’: {\r\n \’text\’: [\’There was an error sending the email. Please try again later.\’]\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n )’), (‘language’, ‘lang-py’), (‘caption’, <wagtail.rich_text.RichText object at 0x3e713ff23490>)])]>

Cloud Run Functions (CRF) configuration: We configure the CRF with the following settings:

Runtime: Python 3.12

Environment: 2nd Gen 

Entry point: handle_webhook

Trigger type: HTTPS

Memory allocated: 256 MiB

Ingress settings: Allow all traffic

Requirements (requirements.txt): functions-framework==3.*

Not sure which CRF version to choose for Dialogflow CX Webhooks? Here’s a quick way to decide:

1st Gen: Simpler to set up and deploy, suitable for basic webhooks with less demanding performance requirements.

2nd Gen: Offers more flexibility, control, and scalability, making it a better choice for complex webhooks or high-traffic scenarios.

More details here
5. Connect the pieces
Specify the URL of our deployed CRF as the webhook URL in Conversational Agents (Dialogflow CX), ensuring that the escalation process is triggered correctly when needed.
Here’s how it all comes together
This breakdown provides a comprehensive understanding of the Conversational Agents (Dialogflow CX) flow design, emphasizing the escalation process and the role of the CRF. Remember to adapt the flow, messages, and email content to suit your specific needs and branding.
Flow Name: (e.g., "Customer Support Flow")
Pages: There are two pages, ‘Start Page’ and ‘Escalate’.
1. Start Page

Purpose: Initiates the conversation with the welcome intent greeting the user and gauges user satisfaction.

Greeting entry fulfillment:

Agent says: "Hi name! Please let us know how we can help you."

Datastore response entry fulfillment:

Agent says: "Are you satisfied with the response?"

Custom payload: (This creates the "Yes" and "No" chips)JSON

code_block
<ListValue: [StructValue([(‘code’, ‘{\r\n "richContent": [\r\n [\r\n {\r\n "options": [\r\n {\r\n "text": "Yes"\r\n },\r\n {\r\n "text": "No"\r\n }\r\n ],\r\n "type": "chips"\r\n }\r\n ]\r\n ]\r\n}’), (‘language’, ‘lang-py’), (‘caption’, <wagtail.rich_text.RichText object at 0x3e713ff23040>)])]>

Routes:

Condition: "Yes" chip selected

Transition: Start page (Marks the conversation as successful)

Agent says: "Thank you! Is there anything else we can help with?”

Condition: "No" chip selected

Transition: "Escalate" Page

Agent says: "Sorry to hear that!" (Acknowledges user dissatisfaction)

2. Escalate

Purpose: Offers the user the option to escalate to a human agent.

Entry fulfillment:

Agent says: "Would you like to escalate?"

Custom payload: (Same "Yes" and "No" chips as the "Start Page")

code_block
<ListValue: [StructValue([(‘code’, ‘{\r\n "richContent": [\r\n [\r\n {\r\n "options": [\r\n {\r\n "text": "Yes"\r\n },\r\n {\r\n "text": "No"\r\n }\r\n ],\r\n "type": "chips"\r\n }\r\n ]\r\n ]\r\n}’), (‘language’, ”), (‘caption’, <wagtail.rich_text.RichText object at 0x3e713ff23760>)])]>

Routes:

Condition: "No" chip selected

Transition: "Start Page"

Agent says: "Sure, Thank you!" (Allows the user to continue with the bot)

Condition: "Yes" chip selected

Transition: "Escalation Webhook"

Webhook: Cloud Run Functions (Triggers the escalation process)

Intents:

We describe the two intents ‘confirm.yes’ and ‘confirm.no’ with training phrases described as ‘yes’ and ‘no’. This corresponds to the user clicking on ‘yes’ and ‘no’ chips or simply writing the phrases or the similar ones. 

To learn more about Intents, please click here.

Cloud Run Functions (Escalation Webhook)

Trigger: HTTPS Eventarc trigger (Activated when the "Escalate" page transitions to the webhook)

Functionality:

Gather conversation history: Retrieve and process the complete conversation history from the Conversational Agents (Dialogflow CX) session using the $conversation in the generator prompt which captures the conversation between the agent and the user, excluding the very last user utterance and the agent’s utterances thereafter.

Summarize conversation: Generate a concise summary of the conversation ($conversation), highlighting key user requests or issues.

Extract user information: Obtain the user’s email address (and any other relevant details) from the Conversational Agents (Dialogflow CX) session or your user database.

Compose email: Create an email with:

Subject: (e.g., "Escalated Conversation from [User Email]")

Recipient: The support agent’s email address

CC: The user’s email address

Body:

User information (e.g., name, ID)

Conversation summary

Link to the full conversation in Conversational Agents (Dialogflow CX) (if available)

Send email: Use an email sending library (e.g., sendgrid, mailgun,smtp) or your email provider’s API to send the email.

Return response (optional): Send a response back to Conversational Agents (Dialogflow CX) to inform the user that their request has been escalated (e.g., "The mail is successfully sent!”, "Your request has been escalated. An agent will contact you shortly.").

Chatbot testing and results
After completing the above steps, you can click on ‘Publish’ and then ‘Try it now’ to test the chatbot.

These are a few example user journeys:
1. The user is not satisfied with the response and does not want to escalate as well.

2. The user is not satisfied with the response and escalates as well. The sample triggered mail is shown below in the right image.

This approach shows how you can combine various Google Cloud technologies, including Vertex AI, to build intelligent and user-friendly chatbots. As conversational AI continues to evolve, we can expect even more innovative solutions that enhance customer experiences. To learn more and contribute feedback, visit our Google Cloud community on Medium. 
References

Webhooks | Dialogflow CX | Google Cloud

Generators | Dialogflow CX | Google Cloud

Conversational Agents (Dialogflow CX) documentation | Google Cloud

Intents | Dialogflow CX | Google Cloud

Pages | Dialogflow CX | Google Cloud

State handlers | Routes | Dialogflow CX | Google Cloud

Flows | Dialogflow CX | Google Cloud

Eventarc triggers | Cloud Run functions Documentation

Experiment with parameter values | Generative AI on Vertex AI | Google Cloud

Prompt Engineering for Generative AI | Machine Learning | Google for Developers

Gemini models | Gemini API | Google AI for Developers

Cloud Run functions version comparison

AI Summary and Description: Yes

Summary: The text outlines the development of a self-escalating chatbot using Google Cloud’s generative AI solutions, specifically targeting improved user experience and agent efficiency in customer service applications. This integration leverages various cloud services to enhance the capabilities of chatbots, particularly with the escalation process, which is crucial for maintaining customer satisfaction.

Detailed Description:
The article discusses the creation of a self-escalating chatbot utilizing Google Cloud’s tools, including Vertex AI and Dialogflow CX, highlighting its advantages for both users and support agents. The integration aims to streamline customer service interactions by employing artificial intelligence to handle inquiries and manage escalations effectively.

Key Points Include:

– **User Experience Enhancement:**
– The chatbot is designed to deliver accurate information swiftly, enhancing user satisfaction.
– Users can escalate issues to human agents if the chatbot fails to meet their needs, ensuring they receive the assistance necessary.

– **Agent Efficiency Improvement:**
– The system summarizes interactions before escalation, allowing agents to address issues more efficiently.
– Reduced workload for agents as they receive detailed yet concise summaries from the chatbot, improving response times and effectiveness.

– **Scalability and Security:**
– The implementation utilizes Google Cloud Run Functions for scalability and secured operation, necessary for high-traffic scenarios.

– **Technical Implementation Details:**
– The chatbot’s knowledge base is built using Vertex AI to index relevant content, with Conversational Agents used to design interactive flows.
– User satisfaction checks are incorporated, allowing users to provide immediate feedback which can trigger escalations.
– Generative AI is used for summarizing conversations and composing emails to human agents, maintaining a professional tone.

– **Trigger Configuration:**
– The process is streamlined by connecting the Dialogflow CX with Cloud Run Functions, which handle requests and automate the escalation workflow.
– Uses best practices for configuration management in the Cloud Run Functions to optimize performance under varying loads.

– **User Journey Examples:**
– Illustrates potential paths users might take—ranging from not escalating issues to requesting direct human assistance, showing the chatbot’s flexibility.

The author’s focus on integrating generative AI technologies into the customer service landscape represents a significant trend—a movement towards smart interaction solutions that prioritize user satisfaction while optimizing support staff workload. This convergence of AI with cloud computing highlights crucial implications for security and compliance, especially in handling user data and interactions securely.