Actioning Buyer Critiques at Scale with Databricks SQL AI Capabilities


Each morning Susan walks straight right into a storm of messages, and does not know the place to begin! Susan is a buyer success specialist at a world retailer, and her main goal is to make sure prospects are pleased and obtain personalised service each time they encounter points.

In a single day the corporate receives a whole bunch of evaluations and suggestions throughout a number of channels together with web sites, apps, social media posts, and e mail. Susan begins her day by logging into every of those techniques and choosing up the messages not but collected by her colleagues. Subsequent, she has to make sense of those messages, determine what must be responded to, and formulate a response for the client. It is not simple as a result of these messages are sometimes in several codecs and each buyer expresses their opinions in their very own distinctive fashion.

This is a pattern of what she has to cope with (for the needs of this text, we’re utilising extracts from Amazon’s buyer evaluate dataset)

Actioning Customer Reviews at Scale with Databricks SQL AI Functions

Susan feels uneasy as a result of she is aware of she is not all the time deciphering, categorizing, and responding to those messages in a constant method. Her greatest concern is that she could inadvertently miss responding to a buyer as a result of she did not correctly interpret their message. Susan is not alone. Lots of her colleagues really feel this fashion, as do most fellow customer support representatives on the market!

The problem for retailers is how do they mixture, analyse, and motion this freeform suggestions in a well timed method? An excellent first step is leveraging the Lakehouse to seamlessly collate all these messages throughout all these techniques into one place. However then what?

Enter LLMs

Giant language fashions (LLMs) are good for this state of affairs. As their identify implies, they’re extremely able to making sense of advanced unstructured textual content. They’re additionally adept at summarizing key matters mentioned, figuring out sentiment, and even producing responses. Nonetheless, not each group has the assets or experience to develop and preserve its personal LLM fashions.

Fortunately, in as we speak’s world, we now have LLMs we will leverage as a service, akin to Azure OpenAI’s GPT fashions. The query then turns into: how can we apply these fashions to our information within the Lakehouse?

On this walkthrough, we’ll present you how one can apply Azure OpenAI’s GPT fashions to unstructured information that’s residing in your Databricks Lakehouse and find yourself with well-structured queryable information. We are going to take buyer evaluations, determine matters mentioned, their sentiment, and decide whether or not the suggestions requires a response from our buyer success crew. We’ll even pre-generate a message for them!

The issues that have to be solved for Susan’s firm embrace:

  • Using a available LLM that additionally has enterprise assist and governance
  • Generate constant which means towards freeform suggestions
  • Figuring out if a subsequent motion is required
  • Most significantly, permit analysts to work together with the LLM utilizing acquainted SQL abilities

Walkthrough: Databricks SQL AI Capabilities

AI Capabilities simplifies the daunting activity of deriving which means from unstructured information. On this walkthrough, we’ll leverage a deployment of an Azure OpenAI mannequin to use conversational logic to freeform buyer evaluations.

Actioning Customer Reviews at Scale with Databricks SQL AI Functions

Pre-requisites

We’d like the next to get began

  • Join for the SQL AI Capabilities public preview
  • An Azure OpenAI key
  • Retailer the important thing in Databricks Secrets and techniques (documentation: AWS, Azure, GCP)
  • A Databricks SQL Professional or Serverless warehouse

Immediate Design

To get one of the best out of a generative mannequin, we want a well-formed immediate (i.e. the query we ask the mannequin) that gives us with a significant reply. Moreover, we want the response in a format that may be simply loaded right into a Delta desk. Fortuitously, we will inform the mannequin to return its evaluation within the format of a JSON object.

Right here is the immediate we use for figuring out entity sentiment and whether or not the evaluate requires a follow-up:


A buyer left a evaluate. We comply with up with anybody who seems sad.
Extract all entities talked about. For every entity:
- classify sentiment as ["POSITIVE", "NEUTRAL", "NEGATIVE"]
- whether or not buyer requires a comply with-up: Y or N
- cause for requiring followup

Return JSON ONLY. No different textual content exterior the JSON. JSON format:
{
entities: [{
   "entity_name": <entity name>,
   "entity_type": <entity type>,
   "entity_sentiment": <entity sentiment>,
   "followup": <Y or N for follow up>,
   "followup_reason": <reason for followup>
}]
}

Evaluate:
Like different customers, whose evaluations I want I'd paid extra consideration to, the 
constant efficiency of those k-cups is disappointing. Drawn to the 
product by its cut price worth, I'm reminded you typically get what you purchase. Whereas 
the espresso tastes OK, it's no higher than most different manufacturers I've bought. 
This is the ONLY model I've bought, although, that has a defect about 50% 
of the time. Espresso goes into the cup and sprays into the cup holder which 
ruins the beverage. With solely about half of the cups working correctly that 
successfully doubles the associated fee making it something however a cut price. I can't 
buy once more and, if requested, will advocate towards purchases.

Operating this by itself offers us a response like


{
    "entities": [
      	{
            "entity_name": "k-cups",
            "entity_type": "product",
            "entity_sentiment": "NEGATIVE",
            "followup": "Y",
            "followup_reason": "Defect in 50% of the cups"
        },
        {
            "entity_name": "coffee",
            "entity_type": "product",
            "entity_sentiment": "NEUTRAL",
            "followup": "N",
            "followup_reason": ""
        },
        {
            "entity_name": "price",
            "entity_type": "attribute",
            "entity_sentiment": "NEGATIVE",
            "followup": "N",
            "followup_reason": ""
        }
    ]
}

Equally, for producing a response again to the client, we use a immediate like


A buyer of ours was sad about <product identify> particularly 
about <entity> on account of <cause>. Present an empathetic message I can 
ship to my buyer together with the supply to have a name with the related 
product supervisor to go away suggestions. I need to win again their favour and 
I do not need the client to churn

AI Capabilities

We’ll use Databricks SQL AI Capabilities as our interface for interacting with Azure OpenAI. Utilising SQL supplies us with three key advantages:

  • Comfort: we forego the necessity to implement customized code to interface with Azure OpenAI’s APIs
  • Finish-users: Analysts can use these features of their SQL queries when working with Databricks SQL and their BI instruments of selection
  • Pocket book builders: can use these features in SQL cells and spark.sql() instructions

We first create a operate to deal with our prompts. We have saved the Azure OpenAI API key in a Databricks Secret, and reference it with the SECRET() operate. We additionally move it the Azure OpenAI useful resource identify (resourceName) and the mannequin’s deployment identify (deploymentName). We even have the flexibility to set the mannequin’s temperature, which controls the extent of randomness and creativity within the generated output. We explicitly set the temperature to 0 to minimise randomness and maximise repeatability


-- Wrapper operate to deal with all our calls to Azure OpenAI
-- Analysts who need to use arbitrary prompts can use this handler
CREATE OR REPLACE FUNCTION PROMPT_HANDLER(immediate STRING)
RETURNS STRING
RETURN AI_GENERATE_TEXT(immediate,
  "azure_openai/gpt-35-turbo",
  "apiKey", SECRET("tokens", "azure-openai"),
  "temperature", CAST(0.0 AS DOUBLE),
  "deploymentName", "llmbricks",
  "apiVersion", "2023-03-15-preview",  
  "resourceName", "llmbricks"
);

Now we create our first operate to annotate our evaluate with entities (i.e. matters mentioned), entity sentiments, whether or not a follow-up is required and why. Because the immediate will return a well-formed JSON illustration, we will instruct the operate to return a STRUCT sort that may simply be inserted right into a Delta desk


-- Extracts entities, entity sentiment, and whether or not follow-up is required from a buyer evaluate
-- Since we're receiving a well-formed JSON, we will parse it and return a STRUCT information sort for simpler querying downstream
CREATE OR REPLACE FUNCTION ANNOTATE_REVIEW(evaluate STRING)
RETURNS STRUCT<entities: ARRAY<STRUCT<entity_name: STRING, entity_type: STRING, entity_sentiment: STRING, followup: STRING, followup_reason: STRING>>>
RETURN FROM_JSON(
  PROMPT_HANDLER(CONCAT(
    'A buyer left a evaluate. We comply with up with anybody who seems sad.
     Extract all entities talked about. For every entity:
      - classify sentiment as ["POSITIVE","NEUTRAL","NEGATIVE"]
      - whether or not buyer requires a follow-up: Y or N
      - cause for requiring followup

    Return JSON ONLY. No different textual content exterior the JSON. JSON format:
    {
        entities: [{
            "entity_name": <entity name>,
            "entity_type": <entity type>,
            "entity_sentiment": <entity sentiment>,
            "followup": <Y or N for follow up>,
            "followup_reason": <reason for followup>
        }]
    }

    Evaluate:
    ', evaluate)),
  "STRUCT<entities: ARRAY<STRUCT<entity_name: STRING, entity_type: STRING, entity_sentiment: STRING, followup: STRING, followup_reason: STRING>>>"
);

We create an identical operate for producing a response to complaints, together with recommending different merchandise to strive


-- Generate a response to a buyer primarily based on their criticism
CREATE OR REPLACE FUNCTION GENERATE_RESPONSE(product STRING, entity STRING, cause STRING)
RETURNS STRING
COMMENT "Generate a response to a buyer primarily based on their criticism"
RETURN PROMPT_HANDLER(
 CONCAT("A buyer of ours was sad about ", product, 
"particularly about ", entity, " on account of ", cause, ". Present an empathetic 
message I can ship to my buyer together with the supply to have a name with 
the related product supervisor to go away suggestions. I need to win again their 
favour and I don't need the client to churn"));

We may wrap up all of the above logic right into a single immediate to minimise API calls and latency. Nonetheless, we advocate decomposing your questions into granular SQL features in order that they are often reused for different eventualities inside your organisation.

Analysing buyer evaluate information

Now let’s put our features to the check!


SELECT review_body, ANNOTATE_REVIEW(review_body) AS annotations
FROM customer_reviews

The LLM operate returns well-structured information that we will now simply question!

Actioning Customer Reviews at Scale with Databricks SQL AI Functions

Subsequent we’ll construction the information in a format that’s extra simply queried by BI instruments:


CREATE OR REPLACE TABLE silver_reviews_processed
AS
WITH exploded AS (
  SELECT * EXCEPT(annotations),
    EXPLODE(annotations.entities) AS entity_details
  FROM silver_reviews_annotated
)
SELECT * EXCEPT(entity_details),
  entity_details.entity_name AS entity_name,
  LOWER(entity_details.entity_type) AS entity_type,
  entity_details.entity_sentiment AS entity_sentiment,
  entity_details.followup AS followup_required,
  entity_details.followup_reason AS followup_reason
FROM exploded

Now we now have a number of rows per evaluate, with every row representing the evaluation of an entity (matter) mentioned within the textual content

Actioning Customer Reviews at Scale with Databricks SQL AI Functions

Creating response messages for our buyer success crew

Let’s now create a dataset for our buyer success crew the place they will determine who requires a response, the explanation for the response, and even a pattern message to begin them off


-- Generate a response to a buyer primarily based on their criticism
CREATE OR REPLACE TABLE gold_customer_followups_required
AS
SELECT *, GENERATE_RESPONSE(product_title, entity_name, followup_reason) AS followup_response
FROM silver_reviews_processed
WHERE followup_required = "Y"

The ensuing information appears to be like like

Actioning Customer Reviews at Scale with Databricks SQL AI Functions

As buyer evaluations and suggestions stream into the Lakehouse, Susan and her crew foregoes the labour-intensive and error-prone activity of manually assessing each bit of suggestions. As an alternative, they now spend extra time on the high-value activity of delighting their prospects!

Supporting ad-hoc queries

Analysts also can create ad-hoc queries utilizing the PROMPT_HANDLER() operate we created earlier than. For instance, an analyst may be all in favour of understanding whether or not a evaluate discusses drinks:


SELECT review_id,
  PROMPT_HANDLER(CONCAT("Does this evaluate talk about drinks? 
Reply Y or N solely, no explanations or notes. Evaluate: ", review_body)) 
    AS discusses_beverages,
  review_body
FROM gold_customer_reviews
Actioning Customer Reviews at Scale with Databricks SQL AI Functions

From unstructured information to analysed information in minutes!

Now when Susan arrives at work within the morning, she’s greeted with a dashboard that factors her to which prospects she needs to be spending time with and why. She’s even supplied with starter messages to construct upon!

Actioning Customer Reviews at Scale with Databricks SQL AI Functions

To lots of Susan’s colleagues, this looks like magic! Each magic trick has a secret, and the key right here is AI_GENERATE_TEXT() and the way simple it makes making use of LLMs to your Lakehouse. The Lakehouse has been working behind the scenes to centralise evaluations from a number of information sources, assigning which means to the information, and recommending subsequent greatest actions

Actioning Customer Reviews at Scale with Databricks SQL AI Functions

Let’s recap the important thing advantages for Susan’s enterprise:

  • They’re instantly capable of apply AI to their information with out the weeks required to coach, construct, and operationalise a mannequin
  • Analysts and builders can work together with this mannequin by utilizing acquainted SQL abilities

You’ll be able to apply these SQL features to the whole thing of your Lakehouse akin to:

  • Classifying information in real-time with Delta Stay Tables
  • Construct and distribute real-time SQL Alerts to warn on elevated detrimental sentiment exercise for a model
  • Capturing product sentiment in Characteristic Retailer tables that again their real-time serving fashions

Areas for consideration

Whereas this workflow brings speedy worth to our information with out the necessity to prepare and preserve our personal fashions, we have to be cognizant of some issues:

  • The important thing to an correct response from an LLM is a well-constructed and detailed immediate. For instance, generally the ordering of your guidelines and statements issues. Make sure you periodically fine-tune your prompts. You might spend extra time engineering your prompts than writing your SQL logic!
  • LLM responses will be non-deterministic. Setting the temperature to 0 will make the responses extra deterministic, nevertheless it’s by no means a assure. Subsequently, if you’re reprocessing information, the output for beforehand processed information may differ. You need to use Delta Lake’s time journey and change information feed options to determine altered responses and tackle them accordingly
  • Along with integrating LLM providers, Databricks additionally makes it simple to construct and operationalise LLMs that you simply personal and are fine-tuned in your information. For instance, find out how we constructed Dolly. You need to use these at the side of AI Capabilities to create insights really distinctive to your small business

What subsequent?

Day by day the neighborhood is showcasing new artistic makes use of of prompts. What artistic makes use of are you able to apply to the information in your Databricks Lakehouse?

  • Join the Public Preview of AI Capabilities right here
  • Learn the docs right here
  • Comply with together with our demo at dbdemos.ai
  • Take a look at our Webinar masking the best way to construct your individual LLM like Dolly right here!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles