1
0

DatabricksのAI Functionsを活用した顧客レビューの分析

Last updated at Posted at 2024-03-06

こちらのチュートリアルをウォークスルーします。

データの準備

最初に、分析に使うデータを準備します。DatabricksワークスペースにはAmazonレビューのデータセットがあるのでこちらを使います。

df = spark.read.format("parquet").option("header", True).load("dbfs:/databricks-datasets/amazon/data20K/")
display(df)

Screenshot 2024-03-06 at 10.39.54.png

テーブルとして永続化します。

df.write.saveAsTable("takaakiyayoi_catalog.default.reviews")

AI Functionsの活用

注意
AI Functionsを使うには、ペイパートークンがサポートされているリージョンのDatabricksワークスペースでサーバレスのSQLウェアハウスを使う必要があります。

レビューの感情分析

ai_sentimentでレビューの感情分析を行います。

%sql
SELECT
  review,
  ai_analyze_sentiment(review) AS sentiment
FROM
  takaakiyayoi_catalog.default.reviews LIMIT 1;

Screenshot 2024-03-06 at 10.43.52.png

レビューの分類

ai_classifyを使ってレビューを分類します。

%sql
SELECT
  review,
  ai_classify(
    review,
    ARRAY(
      "Arrives too late",
      "Wrong size",
      "Wrong color",
      "Dislike the style"
    )
  ) AS reason
FROM
  takaakiyayoi_catalog.default.reviews
WHERE
  ai_analyze_sentiment(review) = "negative" LIMIT 10

Screenshot 2024-03-06 at 10.45.33.png

なお、このチュートリアルで触れていませんが、ai_translateを使うことでレビューを日本語にすることもできます、

%sql
SELECT
  ai_translate(review, "ja"),
  ai_classify(
    review,
    ARRAY(
      "Arrives too late",
      "Wrong size",
      "Wrong color",
      "Dislike the style"
    )
  ) AS reason
FROM
  takaakiyayoi_catalog.default.reviews
WHERE
  ai_analyze_sentiment(review) = "negative" LIMIT 10

Screenshot 2024-03-06 at 10.46.36.png

レビューからの情報抽出

ai_extractを使って、適正なサイズに関する情報を抽出します。

%sql
SELECT
  review,
  ai_extract(review, array("usual size")) AS usual_size,
  ai_classify(review, array("Size is wrong", "Size is right")) AS fit
FROM
  takaakiyayoi_catalog.default.reviews LIMIT 10

Screenshot 2024-03-06 at 10.48.41.png

レビューに基づいてレスポンスを生成

ai_gen

%sql
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
  takaakiyayoi_catalog.default.reviews
WHERE
  ai_analyze_sentiment(review) = "negative" LIMIT 10

Screenshot 2024-03-06 at 10.50.23.png

こちらも翻訳が可能です。

%sql
SELECT
  ai_translate(review, "ja"),
  ai_translate(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
  ), "ja") AS reply
FROM
  takaakiyayoi_catalog.default.reviews
WHERE
  ai_analyze_sentiment(review) = "negative" LIMIT 10

Screenshot 2024-03-06 at 10.54.51.png

はじめてのDatabricks

はじめてのDatabricks

Databricks無料トライアル

Databricks無料トライアル

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0