GCP – Create a self-escalating chatbot in Conversational Agents using Webhook and Generators
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
- <ListValue: [StructValue([(‘title’, ‘$300 in free credit to try Google Cloud developer tools’), (‘body’, <wagtail.rich_text.RichText object at 0x3e683c538df0>), (‘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.rnSummarize 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.rnAlso, specify the conversation messages below the summary which are present in the conversation. The conversation is as follows: $conversationrnThe feedback of the user about the issue is $last-user-utterance.’), (‘language’, ”), (‘caption’, <wagtail.rich_text.RichText object at 0x3e683c538f10>)])]>
-
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 rnimport http.client rnimport smtplib, sslrnfrom flask import Flask, jsonifyrnfrom email.message import EmailMessagernrn# Cloud Run Functions to handle webhook requests from Conversational Agents (Dialogflow CX)rn@functions_framework.http rndef handle_webhook(request):rn “””rn Handles webhook requests from Conversational Agents (Dialogflow CX) and sends an email rn summarizing the conversation to a human agent.rnrn Args:rn request: The incoming HTTP request containing the webhook payload.rnrn Returns:rn A JSON response indicating the status of the email sending process.rn “””rnrn port = 465 # For SSLrn smtp_server = “smtp.gmail.com”rn rn sender_email = ‘<sender_agent_mail_id>’rn receiver_email = ‘<receiver_agent_mail_id>’rn password = ‘<sender_agent_password>’rn cc_email = ‘the logged-in user mail id’ # Include the user’s emailrnrn # Extract the conversation summary from the webhook payloadrn req = request.get_json()rn message = req[‘sessionInfo’][‘parameters’][‘$request.generative.output’]rn rn # Create and send the emailrn msg = EmailMessage()rn msg.set_content(message)rnrn msg[‘Subject’] = ‘Action Needed: Customer Escalation’rn msg[‘From’] = sender_emailrn msg[‘To’] = receiver_emailrn msg[‘Cc’] = cc_email # Add the user to the email for transparencyrnrn try:rn # Establish a secure connection to the SMTP serverrn server = smtplib.SMTP_SSL(smtp_server, port)rn server.login(sender_email, password) rn server.send_message(msg) rn server.quit()rnrn # Return a success message to Conversational Agents (Dialogflow CX)rn return jsonify(rn {rn ‘fulfillment_response’: {rn ‘messages’: [rn {rn ‘text’: {rn ‘text’: [‘The mail is successfully sent!’]rn }rn }rn ]rn }rn }rn )rnrn except Exception as e:rn # Handle potential errors during email sendingrn print(f”Error sending email: {e}”)rn return jsonify(rn {rn ‘fulfillment_response’: {rn ‘messages’: [rn {rn ‘text’: {rn ‘text’: [‘There was an error sending the email. Please try again later.’]rn }rn }rn ]rn }rn }rn )’), (‘language’, ‘lang-py’), (‘caption’, <wagtail.rich_text.RichText object at 0x3e683c538a00>)])]>
-
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’, ‘{rn “richContent”: [rn [rn {rn “options”: [rn {rn “text”: “Yes”rn },rn {rn “text”: “No”rn }rn ],rn “type”: “chips”rn }rn ]rn ]rn}’), (‘language’, ‘lang-py’), (‘caption’, <wagtail.rich_text.RichText object at 0x3e683c538f70>)])]>
-
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’, ‘{rn “richContent”: [rn [rn {rn “options”: [rn {rn “text”: “Yes”rn },rn {rn “text”: “No”rn }rn ],rn “type”: “chips”rn }rn ]rn ]rn}’), (‘language’, ”), (‘caption’, <wagtail.rich_text.RichText object at 0x3e683c538d90>)])]>
-
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
Read More for the details.