Skip to content

Commit

Permalink
SNOW-1690711: support for cortex sentiment, classify_text with apply
Browse files Browse the repository at this point in the history
Signed-off-by: Labanya Mukhopadhyay <labanya.mukhopadhyay@snowflake.com>
  • Loading branch information
sfc-gh-lmukhopadhyay committed Dec 7, 2024
1 parent 7da2b12 commit 13d2e81
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/snowflake/snowpark/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10149,3 +10149,35 @@ def snowflake_cortex_summarize(text: ColumnOrLiteralStr):
sql_func_name = "snowflake.cortex.summarize"
text_col = _to_col_if_lit(text, sql_func_name)
return builtin(sql_func_name)(text_col)


def snowflake_cortex_classify_text(input: ColumnOrLiteralStr, list_of_categories):
"""
Classifies free-form text into categories that you provide.
Args:
input: A string containing the English text from which a summary should be generated.
list_of_categories: Array that represents the categories. Must contain at least two and at most 100 unique
categories. Categories are case sensitive. If these requirements are not met, the function returns an error.
Returns:
Returns a string that contains a JSON object. The JSON object contains the category that the input prompt was
classified as. If invalid arguments are given, an error is returned.
"""
sql_func_name = "snowflake.cortex.classify_text"
input_col = _to_col_if_lit(input, sql_func_name)
return builtin(sql_func_name)(input_col, list_of_categories)


def snowflake_cortex_sentiment(text: ColumnOrLiteralStr):
"""
A string containing the text for which a sentiment score should be calculated.
Args:
text: A string containing the English text from which a summary should be generated.
Returns:
A floating-point number from -1 to 1 (inclusive) indicating the level of negative or positive sentiment in the
text. Values around 0 indicate neutral sentiment.
"""
sql_func_name = "snowflake.cortex.sentiment"
text_col = _to_col_if_lit(text, sql_func_name)
return builtin(sql_func_name)(text_col)
4 changes: 4 additions & 0 deletions src/snowflake/snowpark/modin/plugin/_internal/apply_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
_log2,
_log10,
sin,
snowflake_cortex_sentiment,
snowflake_cortex_summarize,
snowflake_cortex_classify_text,
udf,
to_variant,
when,
Expand Down Expand Up @@ -110,7 +112,9 @@
floor,
trunc,
sqrt,
snowflake_cortex_sentiment,
snowflake_cortex_summarize,
snowflake_cortex_classify_text,
}


Expand Down
10 changes: 10 additions & 0 deletions tests/integ/modin/test_apply_snowpark_python_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,13 @@ def test_apply_snowflake_cortex_summarize():
summary = s.apply(snowflake_cortex_summarize).iloc[0]
# this length check is to get around the fact that this function may not be deterministic
assert 0 < len(summary) < len(content)


@sql_count_checker(query_count=1)
def test_apply_snowflake_cortex_sentiment():
from snowflake.snowpark.functions import snowflake_cortex_sentiment

content = "A very very bad review!"
s = pd.Series([content])
sentiment = s.apply(snowflake_cortex_sentiment).iloc[0]
assert -1 <= sentiment <= 1

0 comments on commit 13d2e81

Please sign in to comment.