AI_SIMILARITY¶
Computes a similarity score based on the vector cosine similarity value of the inputs’ embedding vectors. Currently supports both text and image similarity computation.
Syntax¶
Applying AI_SIMILARITY to string or image inputs:
AI_SIMILARITY( <input1>, <input2> )
Specifying the config object:
AI_SIMILARITY( <input1>, <input2>, <config_object> )
Arguments¶
Required:
If you’re specifying input strings:
input1
,{input2}
The strings with the text that you’re comparing and using to compute the similarity score.
If you’re specifying input images:
input1
,input2
FILE data type referencing the images to be compared.
Note
AI_SIMILARITY does not support computing the similarity between text and image inputs.
Optional:
config_object
An OBJECT containing key-value pairs used to configure the model.
Key |
Type |
Default |
Description |
---|---|---|---|
|
For STRING input, default to |
The embedding model used for embedding. Supported values are:
|
Returns¶
Returns a float value of range -1 to 1 that represents the similarity score computed using vector similarity between two embedding vectors for the inputs.
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.
Examples¶
AI_SIMILARITY: Text¶
In this example, the function is computing a similarity score between the two statement inputs 'I like this dish'
and 'This dish is very good'
.
SELECT AI_SIMILARITY('I like this dish', 'This dish is very good');
We can also compute similarity on text columns.
SELECT
review
FROM restaurant_reviews
ORDER BY SNOWFLAKE.CORTEX.AI_SIMILARITY(review, 'I love the food here!');
AI_SIMILARITY: Images¶
In this example, the function computes a similarity score between the two images, cat.jpg
and 2cats.jpg
, stored in a Snowflake stage @file_stage
.
SELECT AI_SIMILARITY(TO_FILE('@file_stage', 'cat.jpg'), TO_FILE('@file_stage', '2cats.jpg'));
We can also compute similarity among the images using Snowflake Directory Table for the stage containing the images.
SELECT
to_file('@file_stage', relative_path)
FROM directory(@file_stage)
WHERE snowflake.cortex.ai_similarity(f, to_file(@file_stage, 'cat.jpg')) >= 0.5;
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