Analyze customer reviews using AI Functions

Preview

This feature is in Public Preview.

This article illustrates how to use AI Functions to examine customer reviews and determine if a response needs to be generated. The AI Functions used in this example are built-in Databricks SQL functions, powered by generative AI models made available by Databricks Foundation Model APIs. See AI Functions on Databricks.

This example performs the following on a test dataset called reviews using AI Functions:

  • Determines the sentiment of a review.

  • For negative reviews, extracts information from the review to classify the cause.

  • Identifies whether a response is required back to the customer.

  • Generates a response mentioning alternative products that may satisfy the customer.

Requirements

  • A workspace in a Foundation Model APIs pay-per-token supported region.

  • These functions are not available on Databricks SQL Classic.

  • During the preview, these functions have restrictions on their performance. Reach out to your Databricks account team if you require a higher quota for your use cases.

Analyze sentiment of reviews

You can use the ai_analyze_sentiment() to help you understand how customers feel from their reviews. In the following example, the sentiment can be positive, negative, neutral, or mixed.

SELECT
  review,
  ai_analyze_sentiment(review) AS sentiment
FROM
  product_reviews;

From the following results, you see that the function returns the sentiment for each review without any prompt engineering or parsing results.

Results for ai_sentiment function

Classify reviews

In this example, after identifying negative reviews you can use ai_classify() to gain more insights into customer reviews, like whether the negative review is due to poor logistics, product quality, or other factors.

SELECT
  review,
  ai_classify(
    review,
    ARRAY(
      "Arrives too late",
      "Wrong size",
      "Wrong color",
      "Dislike the style"
    )
  ) AS reason
FROM
  product_reviews
WHERE
  ai_analyze_sentiment(review) = "negative"

In this case, ai_classify() is able to correctly categorize the negative reviews based on custom labels to allow for further analysis.

Results for ai_classify function

Extract information from reviews

You might want to improve your product description based on the reasons customers had for their negative reviews. You can find key information from a blob of text using ai_extract(). The following example extracts information and classifies if the negative review was based on sizing issues with the product:

SELECT
  review,
  ai_extract(review, array("usual size")) AS usual_size,
  ai_classify(review, array("Size is wrong", "Size is right")) AS fit
FROM
  product_reviews

The following are a sample of results:

Results for ai_extract function

Generate responses with recommendations

After reviewing the customer responses, you can use the ai_gen() function to generate a response to a customer based on their complaint and strengthen customer relationships with prompt replies to their feedback.

SELECT
  review,
  ai_gen(
    "Generate a reply in 60 words to address the customer's review.
    Mention their opinions are valued and a 30% discount coupon code has been sent to their email.
    Customer's review: " || review
  ) AS reply
FROM
  product_reviews
WHERE
  ai_analyze_sentiment(review) = "negative"

The following are a sample of results:

Results for ai_gen_results function