Snowflake Cortex によるAI分析【入門】
1. はじめに
以下の Snowflake Quick starts を活用し、実際に Snowflake Cortex に触れてみました。この記事では、ここで学んだ内容を紹介します。
2. Snowflake Cortex とは
Snowflake 社が提供するデータクラウドプラットフォームの一部で、特に機械学習(ML)や人工知能(AI)に関連する機能を強化するためのツールやサービスを指します。Snowflake Cortex の機能には以下が含まれており、今回は LLM 関数を扱いました。
- LLM 関数: SQL と Python 関数で、大規模言語モデル(LLMs)を活用し、自由形式のテキストを理解、クエリ、翻訳、要約、および生成します。
- ML ベースの関数: 機械学習を使用して予測分析を実行し、構造化データについてのインサイトを得て、日常的な分析を加速するのに役立つ SQL 関数です。
3. 実施した内容
Snowflake Notebook 内の Snowflake Cortex を使用して、架空のフードトラック会社 Tasty Bytes が顧客体験の不足部分を特定する方法を説明します。Tasty Bytes は、複数のソースと言語から顧客レビューを収集し、それによって改善点を把握して顧客満足度とロイヤルティを向上させることができます。
Snowflake オブジェクト (ウェアハウス、データベース、スキーマ、生のテーブル) を作成する
S3 から生のテーブルにデータを取り込む
レビュービューを作成する
Snowflake ノートブックを作成し、以下のパッケージを追加
・ seaborn
・ matplotlib
・ snowflake-snowpark-python
・ snowflake-ml-python
Snowflake Cortex - Translate を活用することで、多言語への翻訳が可能です。以下では、多言語のレビューを英語に翻訳し、分析を容易にします。
# Conditionally translate reviews that are not english using Cortex Translate
reviews_df = reviews_df.withColumn('TRANSLATED_REVIEW',when(F.col('LANGUAGE') !=\
F.lit("en"), cortex.Translate(F.col('REVIEW'), F.col('LANGUAGE'), "en")) \
.otherwise(F.col('REVIEW')))
reviews_df.filter(F.col('LANGUAGE') != F.lit("en")) \
.select(["REVIEW","LANGUAGE","TRANSLATED_REVIEW"]).show(3)
結果は以下の通りです。
"REVIEW" | "LANGUAGE" | "TRANSLATED_REVIEW" |
---|---|---|
The Chicken Pot Pie Crepe and Crepe Suzette fro... | fr | The Chicken Pot Pie Crepe and Crepe Suzette fro... |
¡Hola a todos los amantes de la comida! Quiero... | es | Hello to all food lovers! I want to share my re... |
"Freezing Point" i Stockholm var en stor besvi... | sv | "Freezing Point" in Stockholm was a big disappo... |
Snowflake Cortex - Complete では、プロンプトを与えると、サポートされている言語モデルの中から選択したものを使用して応答を生成します。以下では、特定の顧客のレビューに基づいて、その顧客がフードトラックでの体験をどのような評価で評価する可能性があるかを大規模に理解します。以下プロンプトは、タスクの説明と、ワンショット学習とも呼ばれる完了したタスクのサンプル例で構成されています。
# Prompt to get a rating based on a customer review
# We provide one shot incontext learning to better the answers we get from LLM
prompt = """[INST]
###
You are tasked with rating cutsomer reviews for global food truck network called tasty bytes. \
Rating can be one of the following - awful, poor, okay, good, excellent such that awful is the worst \
possible rating and excellent is the best possible rating. Include only the rating in the output \
without any additional text. \
Rate the following review:
The "Freezing Point" ice cream truck in Seoul offered a mix of classic and unique \
options. The Sugar Cone and Waffle Cone were both fresh and crisp, providing a satisfying crunch. The \
Bottled Water and Ice Tea were standard, as expected. The standout, however, was the Mango Sticky Rice \
- a delightful blend of sweet and tangy, it was a refreshing twist to the usual ice cream flavors. The \
service was efficient, but lacked a personal touch. Overall, it\'s a decent choice for a quick, cool \
treat in the city.
Rating : good
Rate the following review:
###"""
# Ask cortex complete and create a new column
review_df = reviews_df.withColumn('RATING', cortex.Complete('mixtral-8x7b', \
F.concat(F.lit(prompt), \
F.col('REVIEW'), \
F.lit("""[/INST]"""))))\
.withColumn('CLEAN_RATING', when(F.contains(F.lower(F.col('RATING')), F.lit('awful')), \
F.lit('awful')) \
.when(F.contains(F.lower(F.col('RATING')), F.lit('poor' )), \
F.lit('poor')) \
.when(F.contains(F.lower(F.col('RATING')), F.lit('okay')), \
F.lit('okay')) \
.when(F.contains(F.lower(F.col('RATING')), F.lit('good')), \
F.lit('good')) \
.when(F.contains(F.lower(F.col('RATING')), F.lit('excellent')), \
F.lit('excellent')) \
.otherwise(F.lit('unsure')))
review_df.select(["REVIEW","CLEAN_RATING"]).show(3)
結果は以下の通りです。
"REVIEW" | "CLEAN_RATING" |
---|---|
The food at Tasty Tibs was simply not satisfact... | awful |
The Kitakata Ramen Bar in Delhi offered a satis... | good |
"Agar aap Tasty Tibs ke through Ethiopian food... | awful |
Snowflake Cortex - Sentiment は、入力テキストに対する感情スコア(-1 から 1 の間の値)を返します。-1 は最も否定的で、1 は最も肯定的です。以下では、顧客が提供したレビューに基づいて顧客の口調を理解するために使用されます。
# Understand the sentiment of customer review using Cortex Sentiment
reviews_df = reviews_df.withColumn('SENTIMENT', cortex.Sentiment(F.col('REVIEW')))
reviews_df.select(["REVIEW","SENTIMENT"]).show(3)
結果は以下の通りです。
"REVIEW" | "SENTIMENT" |
---|---|
A vegetarian's delight discovered at Plant Pala... | 0.8669128 |
The Pastrami, Hot Ham & Cheese, Italian sandwic... | -0.55079645 |
Experience at Le Coin des Crêpes was satisfacto... | 0.057502847 |
レビューの全体的な感情だけでなく、食品の品質、サービス、価格設定などのさまざまな側面について顧客がどう思っているかを理解します。これは、Snowflake cortex - Completeとワンショット例を含むプロンプトを組み合わせて活用することで実現できます。
# Prompt to understand sentiment for different categories mentioned in the customer review
# We employ one shot incontext learning to inform LLM
prompt = """[INST]
###
You are analyzing food-truck customer reviews to undertsand what a given review says about different relevant categories like \
food quality, menu options, staff, overall experience, price, ambience, customer support, \
hygiene standards etc and if sentiment is negative,positive or neutral for that category. \
Only answer in a single valid JSON containing "category", "sentiment" and "details". \
Make sure there is no additional text and not mention categories in answer which are not \
talked in the review. \
Get category based sentiment for the follwoing customer review:
"This food truck offers a disappointing experience. \
The menu lacks healthy options and the food quality is subpar. Finding a parking spot near the \
truck can also be a frustrating ordeal. Additionally, the value for money is not worth it. To top \
it all off, the service provided at this food truck is less than pleasant, adding to the overall \
negative dining experience. Tried reaching out the customer support but was unable to get through."
Answer : [{ "category": "food quality", "sentiment": "negative", "details": "subpar quality" }, { "category": "menu options", "sentiment": "negative", "details": "lacks healthy options" }, { "category": "staff", "sentiment": "negative", "details": "unpleasant" }, { "category": "price", "sentiment": "negative", "details": "not worth the money" }, { "category": "experience", "sentiment": "negative", "details": "regrettable dining experience" }, { "category": "customer support", "sentiment": "negative", "details": "unable to get through" } ].
Get category based sentiment for the follwoing customer review:
###"""
# Ask Cortex Complete and create a new column
review_df = reviews_df.withColumn('CATEGORY_SENTIMENT', cortex.Complete('mixtral-8x7b', \
F.concat(F.lit(prompt), \
F.col('REVIEW'), \
F.lit("""Answer:[/INST]"""))))
review_df.select(["REVIEW","CATEGORY_SENTIMENT"]).show(2)
結果は以下の通りです。
"REVIEW" | "CATEGORY_SENTIMENT" |
---|---|
"King Combo and Greek Salad served by Cheeky Gr... | { "category": "food quality", "sentiment": "negative", "details": "dry and overcooked meat, wilted and... }, { "category": "menu options", "sentiment": "neutral", "details": "King Combo and Greek Salad" }, { "category": "staff", "sentiment": "neutral", "details": "not mentioned" }, ... ] |
The Cheeky Greek truck in Madrid served up an ... | [ { "category": "food quality", "sentiment": "positive", "details": "tender, flavorful meat, fresh toppi... }, { "category": "menu options", "sentiment": "positive", "details": "Gyro Plate and Greek Salad, authent... }, ... ] |
4. まとめ
この Quick starts を通じて、Snowflake Cortex の LLM 関数を用いた翻訳、感情処理などのデータ分析について学びました。Snowflake Cortex を活用することで、これらの分析をわずか数分で行うことができます。手軽な AI 分析が可能なため、他のデータセットでもどんどん試していきたいです。
Discussion