AI_SIMILARITY

Updated at:

AI_SIMILARITY is an AI function in MaxCompute. This function calls a model to measure the semantic similarity between two pieces of text. The function returns a floating-point number between 0 and 1. A higher value indicates greater semantic similarity.

Syntax

FLOAT AI_SIMILARITY(
  STRING <model_name>,
  STRING <version_name>,
  STRING <input1>,
  STRING <input2>
  [, STRING <model_parameters>]
);

Parameters

  • model_name: Required. A STRING. The name of the model to use. For more information, see SQL AI functions.

  • version_name: Required. A STRING. The name of the model version to use. To call the default version, specify DEFAULT_VERSION.

  • input1: Required. A STRING. The first text for the semantic similarity comparison.

  • input2: Required. A STRING. The second text for the semantic similarity comparison.

  • model_parameters: Optional. STRING. Specifies model parameters such as max_tokens, temperature, and top_p. The format is a JSON string:

    '{"max_tokens": 500, "temperature": 0.6, "top_p": 0.95}'.

    • max_tokens: The maximum number of tokens to generate in a single model call. For MaxCompute public models, the default value is 4,096.

    • temperature: A value between 0 and 1 that controls the randomness of the output. A higher value results in more creative and diverse output, while a lower value produces more deterministic and conservative output.

    • top_p: A value between 0 and 1 that limits the range of candidate labels the model can choose from. A higher value allows for a broader range and more diversity, while a lower value narrows the range and produces more focused results.

Return value

Returns a FLOAT value between 0 and 1 that represents the similarity score of the two input texts. The return values are described as follows:

  • A score of 1.0 indicates that the two texts are identical. A score of 0.0 indicates that they are completely unrelated.

  • If input1 or input2 is NULL, the function returns NULL.

  • If input1 or input2 is not a STRING, the function returns an error.

Examples

Example 1: Compare the similarity of two texts

Call the public model qwen3.7-max provided by MaxCompute to calculate the semantic similarity between two pieces of text about MaxCompute.

SET odps.namespace.schema=true;

SELECT AI_SIMILARITY(
    bigdata_public_modelset.default.`qwen3.7-max`,
    DEFAULT_VERSION,
    'MaxCompute is a big data computing platform.',
    'MaxCompute provides large-scale data processing capabilities.'
) AS similarity_score;
-- Result
+------------------+
| similarity_score |
+------------------+
| 0.75             |
+------------------+

Example 2: Compare and sort multiple text pairs by similarity

Call the public model deepseek-v4-pro provided by MaxCompute to compare multiple text pairs and sort them by similarity in descending order. This method is suitable for identifying the most semantically relevant text pairs from a dataset.

-- Sample data
CREATE TABLE text_pairs (
    text1 STRING,
    text2 STRING
);

INSERT INTO text_pairs VALUES
    ('Cloud computing supports scalable infrastructure.', 'Enterprises can flexibly scale IT resources using cloud services.'),
    ('The weather is sunny today.', 'Machine learning algorithms require large datasets.'),
    ('A data warehouse stores historical data for analysis.', 'A data warehouse provides analytical processing capabilities for large-scale data.'),
    ('I like to read books.', 'Reading is my favorite hobby.');

-- Compare the semantic similarity of multiple text pairs
SET odps.namespace.schema=true;

SELECT
    text1,
    text2,
    AI_SIMILARITY(
        bigdata_public_modelset.default.`deepseek-v4-pro`,
        DEFAULT_VERSION,
        text1,
        text2
    ) AS similarity_score
FROM text_pairs
ORDER BY similarity_score DESC;

-- Result:
+----------------------------------------------------+--------------------------------------------------------------------------+------------------+
| text1                                              | text2                                                                    | similarity_score |
+----------------------------------------------------+--------------------------------------------------------------------------+------------------+
| A data warehouse stores historical data for analysis. | A data warehouse provides analytical processing capabilities for large-scale data. | 0.92             |
| I like to read books.                              | Reading is my favorite hobby.                                            | 0.88             |
| Cloud computing supports scalable infrastructure.  | Enterprises can flexibly scale IT resources using cloud services.        | 0.82             |
| The weather is sunny today.                        | Machine learning algorithms require large datasets.                      | 0.05             |
+----------------------------------------------------+--------------------------------------------------------------------------+------------------+

FAQ

Troubleshoot common issues with public models

Symptom: When you call a public model that the model computing service supports, you receive the following error message.

FAILED: ODPS-0130071:[1,8] Semantic analysis exception - inference quota status check failed, error message: region cn-shanghai not found in inference quota

Cause: The model computing service is not activated in the region, such as cn-shanghai, where the current project is located. As a result, you cannot call AI inference resources.

Solution: Go to the Alibaba Cloud Management Console and activate the model computing service for the region where your project is located. For more information, see Purchase and use the MaxCompute model computing service.