banner

Welcome to the Powered by Tasty Bytes - Customer Support Email App!

This application streamlines the workload of call center agents by automating responses to customer emails using a knowledge base. When an email arrives, a Cortex LLM determines if it can address the customer query using the knowledge base. If it can, an automatic response is sent; if not, the email is directed to an agent. Agents have the choice to reply to customer emails, with a Cortex LLM offering suggested responses.

Additionally, agents can contribute responses to the knowledge base, enabling the LLM to address similar queries automatically in the future. Sentiment scores are generated from both agent and auto-responses for further analysis. Moreover, there's an option to simulate a new email for demo purposes.

What You'll Learn

What You'll Need

What You'll Build

Step 1 - Accessing hosted Setup SQL in GitHub

setup.sql

Step 2 - Run the contents of setup.sql

Open a new Snowsight worksheet and run all commands from setup.sql.

Step 3 - Load Streamlit Files

stage

pages-folder

streamlit-files

Step 4 - Create the Streamlit App

Run the code below in a Snowsight worksheet to build the Streamlit app.

USE ROLE sysadmin;

CREATE OR REPLACE STREAMLIT tasty_bytes_customer_support_email.app.customer_support_email_app
ROOT_LOCATION = '@tasty_bytes_customer_support_email.app.customer_support_email_app'
MAIN_FILE = '01_Queue.py'
QUERY_WAREHOUSE = 'tasty_bytes_customer_support_email_wh';

GRANT USAGE ON STREAMLIT tasty_bytes_customer_support_email.app.customer_support_email_app TO ROLE customer_support_email_role;

Step 5 - Open the Streamlit App

streamlit-app

This application is divided into three sections.

Let's simulate a scenario where we first act as a customer, sending an email that the LLM cannot answer. Then, we will act as a Customer Support Agent to respond, adding the summarized response to our knowledge base or corpus, and resend a similar email to trigger an auto-response from LLM.

To do this, please follow the steps below:

simulate-email

auto-response

knowledge-base

If you prefer not to find something new to add to the knowledge base for each demo, you can simply delete the ones you added by executing the commands below:

delete from tasty_bytes_customer_support_email.harmonized.chunk_text
where source = 'EMAIL';

delete from tasty_bytes_customer_support_email.harmonized.vector_store
where source = 'EMAIL';

The following features are used in the customer support email app.

Auto Response with Cortex Complete

Cortex complete generates a response (completion) to a prompt using your choice of supported language model. In the application, we pass the prompt and model (mistral-large) to Cortex complete to generate a response to the customer's email. You can read more documentation here.

We add additional information to the prompt to ensure the email is answered accurately.

def generate_email_response(email):
  prompt = f"""Generate an email response to send to {email['sender']} 
                who is a customer of A Tasty Bytes Food Truck. Do not inlude Subject and use Tasty Bytes Support Team for Best Regards. Provide response to this customer question sent to our support agent 
                at Tasty Bytes Food Truck Company. Use the background information and provided context taken from the 
                most relevant corporate documents or previous support chat logs with other customers. Be concise and only answer 
                the latest question.
                Email Subject from Customer: <Subject> {email['subject']} </Subject>.
                Customer Email Address: <Email_Address> {email['sender']} </Email_Address>.
                Email body is: <EMAIL_BODY> {email['body']} </EMAIL_BODY>.
                Context: <context> {get_context(email['body'], DOC_TABLE)} </context>.
                Background Info: <background_info> {st.session_state.background_info} </background_info>."""
  response = Complete('mistral-large', prompt.replace("'", "''"))
  return response.strip()

Summarize Emails with Cortex Complete

We again leverage Cortex Complete to generate a summary of emails manually sent by agents. The email summaries are then added to our knowlege base so agents don't need to answer the question again.

prompt = f"""Generate a summary to add to the call center knowledge base. If the bot can't answer a customer's 
          question, it should be transferred to an agent. The agent answers the question, and if it's a 
          recurring query, the summary should be stored in the knowledge base. This way, the bot can utilize 
          the knowledge base for future similar inquiries. Create a summary that includes the question and 
          its corresponding answer for storage in the knowledge base. Question is: 
          {st.session_state.current_record['body']} and answer is:
          {st.session_state.email_response}"""
response = Complete('mistral-large', prompt.replace("'", "''"))

Cortex Sentiment

Cortex Sentiment Analysis is used to determine the sentiment of customer emails. You can use SNOWFLAKE.CORTEX.SENTIMENT in a standard query to determine the sentiment of text. In the code below, the single line is added to our query returning emails to collect the sentiment.

agent_history = session.sql("""SELECT 
                                  es.email_id,
                                  es.email_object AS Email_Request,
                                  er.email_response AS Email_Response,
                                  er.sent_ts,
                                  SNOWFLAKE.CORTEX.SENTIMENT(CONCAT('Email from Customer is: ', TO_VARCHAR(es.email_object), ', Response from Company is: ', TO_VARCHAR(er.email_response))) AS sentiment_score
                                FROM 
                                  raw_support.email_status_app es
                                JOIN 
                                  raw_support.email_response_app er ON es.email_id = er.email_id
                                WHERE 
                                  1=1
                                  AND es.responded_flag = TRUE
                                  AND es.email_status_code = 3
                                  and er.email_response is not null
                                ORDER BY 
                                er.sent_ts DESC;""").to_pandas()

RAG Chatbot

The app has an embedded RAG chatbot for agents to ask more questions of the knowlege base. Check out the Tasty Bytes - RAG Chatbot Using Cortex and Streamlit quickstart for more details on building RAG chatbots.

Congratulations! You have built a customer support email application in Streamlit using Cortex LLMs. This powerful application helps Tasty Bytes customer support agents quickly and accurately answer customer emails, including automatic responses.

Check out several the Tasty Bytes - RAG Chatbot Using Cortex and Streamlit and Tasty Bytes - Enhance Customer Experience Streamlit App to learn more about how Streamlit and Cortex helps Tasty Bytes improve customer experience.

What You Learned

Related Resources