Extract insights from freeform values in a column in Snowflake using Snowflake Cortex AI

Ever wanted to extract insights from freeform values in a table?

Snowflake AI might be able to assist with that. Snowflake Cortex is a part of the AI features in Snowflake.

Snowflake Cortex according to the documentation is…

a suite of AI features that use large language models (LLMs) to understand unstructured data, answer freeform questions, and provide intelligent assistance.”

Several interesting functions are present in a notebook that has been created by Snowflake as a demo-notebook:

SNOWFLAKE.CORTEX.SENTIMENT( FREE_FORM_COLUMN )

SNOWFLAKE.CORTEX.SUMMARIZE( FREE_FORM_COLUMN )

SNOWFLAKE.CORTEX.AI_CLASSIFY( FREE_FORM_COLUMN, [‘category1’, ‘category2’] )

See the screenshot below for an example of how these functions work.

The demo notebook is called “Analyze sentiment in unstructured data” and again, I did not create it. Snowflake did, and it should be available to you in your account. Are you using these functions and if so, for what purpose?

SQL
SELECT 
  REVIEW_ID,
  REVIEW,
  LEN(REVIEW) AS LENTH_REVIEW, -- Length of the review.
  SNOWFLAKE.CORTEX.SUMMARIZE(REVIEW) AS SUMMARY, -- Summary of the review.
  LEN(SUMMARY) AS LEN_SUMMARY, -- Length of the summary of the review. Still quite long, apparently this function is not a a fan of one-liners. Should take lessons from Arnold. 
  SNOWFLAKE.CORTEX.SENTIMENT(REVIEW) AS SENTIMENT, -- Extracts the sentiment of this review
  SNOWFLAKE.CORTEX.AI_CLASSIFY(REVIEW, ['negative', 'positive']) AS CATEGORY_RAW, -- Classify a review with either the category 'negative' or 'positive'. Raw value from the function.
  REPLACE(SNOWFLAKE.CORTEX.AI_CLASSIFY(REVIEW, ['negative', 'positive']):labels[0], '"') AS CATEGORY -- Classify a review with either the category 'negative' or 'positive'.
FROM TRUCK_REVIEWS
LIMIT 15;
SQL