snowflake.snowpark.functions.ai_classify¶

snowflake.snowpark.functions.ai_classify(expr: Union[Column, str], list_of_categories: Union[Column, List[str]]) → Column[source]¶

Classifies text or images into categories that you specify.

Parameters:
  • expr – The string, image, or a SQL object from prompt() that you’re classifying. If you’re classifying text, the input string is case sensitive. You might get different results if you use different capitalization.

  • list_of_categories – An array of strings that represents the different categories. Categories are case-sensitive. The array must contain at least 2 and no more than 100 categories. If the requirements aren’t met, the function returns an error.

Returns:

A serialized object. The object’s label field is a string that specifies the category to which the input belongs. If you specify invalid values for the arguments, an error is returned.

Examples:

>>> # for text
>>> session.range(1).select(ai_classify('One day I will see the world', ['travel', 'cooking']).alias("answer")).show()
-----------------------
|"ANSWER"             |
-----------------------
|{                    |
|  "label": "travel"  |
|}                    |
-----------------------

>>> df = session.create_dataframe([
...     ['France', ['North America', 'Europe', 'Asia']],
...     ['Singapore', ['North America', 'Europe', 'Asia']],
...     ['one day I will see the world', ['travel', 'cooking', 'dancing']],
...     ['my lobster bisque is second to none', ['travel', 'cooking', 'dancing']]
... ], schema=["data", "category"])
>>> df.select(ai_classify(col("data"), col("category"))["label"].alias("class")).show()
-------------
|"CLASS"    |
-------------
|"Europe"   |
|"Asia"     |
|"travel"   |
|"cooking"  |
-------------

>>> # for image
>>> _ = session.sql("CREATE OR REPLACE TEMP STAGE mystage ENCRYPTION = (TYPE = 'SNOWFLAKE_SSE')").collect()
>>> _ = session.file.put("tests/resources/dog.jpg", "@mystage", auto_compress=False)
>>> df = session.range(1).select(
...     ai_classify(
...         prompt("Please help me classify the dog within this image {0}", to_file("@mystage/dog.jpg")),
...         ["French Bulldog", "Golden Retriever", "Bichon", "Cavapoo", "Beagle"]
...     ).alias("classes")
... )
>>> df.show()
------------------------
|"CLASSES"             |
------------------------
|{                     |
|  "label": "Cavapoo"  |
|}                     |
------------------------




This function or method is in private preview since 1.31.0.
Copy