snowflake.snowpark.functions.ai_filter¶

snowflake.snowpark.functions.ai_filter(predicate: Union[Column, str], file: Optional[Union[Column, str]] = None) → Column[source]¶

Classifies free-form prompt inputs into a boolean. Currently supports both text and image filtering.

Parameters:
  • predicate – If you’re specifying an input string, it is string containing the text to be classified; If you’re filtering on one file, it is a string containing the instructions to classify the file input as either TRUE or FALSE.

  • file – The column that the file is classified by based on the instructions specified in predicate. You can use IMAGE FILE as an input to the AI_FILTER function.

Note

For more complicated prompts, especially with multiple file columns, you can use the prompt() function to help with creating an input, which supports formatting across both strings and FILE datatypes.

Examples:

>>> # for text
>>> session.range(1).select(ai_filter('Is Canada in North America?').alias("answer")).show()
------------
|"ANSWER"  |
------------
|True      |
------------

>>> # use prompt function
>>> df = session.create_dataframe(["Switzerland", "Korea"], schema=["country"])
>>> df.select(
...     ai_filter(prompt("Is {0} in Asia?", col("country"))).as_("asia"),
...     ai_filter(prompt("Is {0} in Europe?", col("country"))).as_("europe"),
...     ai_filter(prompt("Is {0} in North America?", col("country"))).as_("north_america"),
...     ai_filter(prompt("Is {0} in Central America?", col("country"))).as_("central_america"),
... ).show()
-----------------------------------------------------------
|"ASIA"  |"EUROPE"  |"NORTH_AMERICA"  |"CENTRAL_AMERICA"  |
-----------------------------------------------------------
|False   |True      |False            |False              |
|True    |False     |False            |False              |
-----------------------------------------------------------

>>> # 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_filter("is it a dog picture?", to_file("@mystage/dog.jpg")).alias("is_dog"))
>>> df.show()
------------
|"IS_DOG"  |
------------
|True      |
------------



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