- Categories:
String & binary functions (AI Functions)
AI_FILTERΒΆ
Classifies free-form prompt inputs into a boolean. Currently supports both text and image filtering.
Region availabilityΒΆ
The following table shows the regions where you can use the AI_FILTER function for both text and images:
Data type
|
AWS US West 2
(Oregon)
|
AWS US East 1
(N. Virginia)
|
AWS Europe Central 1
(Frankfurt)
|
AWS Europe West 1
(Ireland)
|
AWS AP Southeast 2
(Sydney)
|
AWS AP Northeast 1
(Tokyo)
|
Azure East US 2
(Virginia)
|
Azure West Europe
(Netherlands)
|
AWS
(Cross-Region)
|
---|---|---|---|---|---|---|---|---|---|
TEXT
|
β |
β |
β |
β |
β |
β |
β |
β |
β |
IMAGE
|
β |
β |
β |
β |
SyntaxΒΆ
Applying AI_FILTER to an input string:
AI_FILTER( <input> )
Applying AI_FILTER to single image:
AI_FILTER( <predicate> , <input> )
Applying AI_FILTER to multiple columns with both text and images, leveraging the PROMPT function:
AI_FILTER( PROMPT('<template_string>', <col_1>, β¦ ) )
ArgumentsΒΆ
Required:
If youβre specifying an input string:
input
A string containing the text to be classified.
If youβre filtering on one file:
predicate
A string containing the instructions to classify the file input as either
TRUE
orFALSE
.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.
If youβre using the PROMPT() function to format the inputs:
For more complicated prompts, especially with multiple file columns, you can use the PROMPT function to help with creating an input
.
The PROMPT() function supports formatting across both strings and FILE datatypes. For detailed usage, see Examples.
ReturnsΒΆ
Returns a Boolean value that indicates whether the statement evaluates to TRUE or FALSE for the specified text.
Access control requirementsΒΆ
Users must use a role that has been granted the SNOWFLAKE.CORTEX_USER database role. See Required privileges for more information on this privilege.
Usage notesΒΆ
For optimal performance, follow these guidelines:
Make sure the columns sent into AI_FILTER donβt contain NULL values.
Use plain text in English for the input string or for PROMPT() arguments.
Provide details for the input text instruction. For example, instead of a statement like βsounds satisfiedβ, use βIn the following support transcript, the customer sounds satisfiedβ.
Consider phrasing the input in the form of a question. For example, βIn the following support transcript, does the customer sound satisfied?β
ExamplesΒΆ
AI_FILTER: TextΒΆ
Can be called as a simple scalar Boolean function on string constants.
SELECT AI_FILTER('Is Canada in North America?');
TRUE
You can CONCAT , || instructions with text columns to use this function:
WITH reviews AS (
SELECT 'Wow... Loved this place.' AS review
UNION ALL SELECT 'The pizza is not good.'
)
SELECT * FROM reviews
WHERE AI_FILTER(CONCAT('The reviewer enjoyed the restaurant: ', review));
For easier templated formatting across multiple columns, Snowflake provides PROMPT function; for example:
WITH reviews AS (
SELECT 'Wow... Loved this place.' AS review
UNION ALL SELECT 'The pizza is not good.'
)
SELECT * FROM reviews
WHERE AI_FILTER(PROMPT('The reviewer enjoyed the restaurant: {0}', review));
+--------------------------+
| REVIEW |
|--------------------------+
| Wow... Loved this place. |
+--------------------------+
While evaluating the quality of AI_FILTER, it can be helpful to compare candidate predicates across columns.
WITH country AS (
SELECT 'Switzerland' AS country,
UNION ALL SELECT 'Korea'
),
region AS (
SELECT 'Asia' AS region,
UNION ALL SELECT 'Europe'
)
SELECT country,
region,
AI_FILTER(PROMPT('{0} is in {1}', country, region)) AS result
FROM country CROSS JOIN region ;
+-------------+-------+--------+
| COUNTRY |REGION | RESULT |
|-------------+-------+--------+
| Switzerland |Europe | TRUE |
|-------------+-------+--------+
| Switzerland | Asia | FALSE |
|-------------+-------+--------+
| Korea |Europe | FALSE |
+-------------+-------+--------+
| Korea | Asia | TRUE |
+-------------+-------+--------+
Using AI_FILTER with a JOINΒΆ
You can use AI_FILTER with a JOIN to express linking two tables with a natural language prompt that AI can reason on.
Important
When performing JOIN operations that utilize the AI_FILTER function, each table in the JOIN canβt exceed 500 rows.
To enable larger scale joins, contact your account manager to enable adaptive optimization preview.
The following example joins the RESUMES table with the JOBS table using a prompt with the AI_FILTER function.
SELECT *
FROM RESUMES
JOIN JOBS
ON AI_FILTER(PROMPT('Evaluate if this resume {0} fits this job description {1}', RESUME.contents, JOBS.jd));
AI_FILTER: ImagesΒΆ
The following examples filter image files based on an instruction.
Filter images by providing an instruction predicate and the image file column:
WITH pictures AS (
SELECT
TO_FILE(file_url) AS img
FROM DIRECTORY(@file_stage)
)
SELECT
FL_GET_RELATIVE_PATH(img) AS file_path FROM pictures
WHERE AI_FILTER('Is this a picture of a cat?', img);
WITH pictures AS (
SELECT
TO_FILE(file_url) AS img
FROM DIRECTORY(@file_stage)
)
SELECT
FL_GET_RELATIVE_PATH(img) AS file_path FROM pictures
WHERE AI_FILTER(PROMPT('{0} is a cat picture', img));
+--------------------------+
| FILE_PATH |
|--------------------------+
| 2cats.jpg |
+--------------------------+
| cat1.png |
+--------------------------+
| orange_cat.jpg |
+--------------------------+
LimitationsΒΆ
Snowflake AI functions donβt support dynamic table incremental refresh.
Snowflake AI functions donβt work on FILEs created from stage files from the following stage types:
Internal stages with encryption mode
TYPE = 'SNOWFLAKE_FULL'
External stages with any customer-side encrypted mode:
TYPE = 'AWS_CSE'
TYPE = 'AZURE_CSE'
User stage, table stage
Stage with double-quoted names