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> )
Copy

Specifying the config object:

AI_SIMILARITY( <input1>, <input2>, <config_object> )
Copy

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

model

STRING

For STRING input, default to 'snowflake-arctic-embed-l-v2'. For IMAGE input, default to 'voyage-multimodal-3'

The embedding model used for embedding. Supported values are:

  • 'snowflake-arctic-embed-l-v2'

  • 'nv-embed-qa-4'

  • 'multilingual-e5-large'

  • 'voyage-multilingual-2'

  • 'snowflake-arctic-embed-m-v1.5'

  • 'snowflake-arctic-embed-m'

  • 'e5-base-v2'

  • 'voyage-multimodal-3' (IMAGE)

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');
Copy

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!');
Copy

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'));
Copy

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;
Copy

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