AI_Text_Embedding

更新时间:
复制 MD 格式

This function calls a general text embedding model to convert text to embeddings.

Important

Only PolarDB for PostgreSQL Standard Edition clusters in the China (Beijing) region support calling built-in models.

Syntax

real[] AI_Text_Embedding(text content);
real[] AI_Text_Embedding(text content, text model_id);

Parameters

Parameter Name

Description

content

The text to convert to an embedding. The text can be up to 2048 characters long.

model_id

The ID of the model. If you do not specify this parameter, the built-in model _dashscope/text_embedding/text_embedding_v2 is called by default.

Return value

Returns a vector of the real[] type.

Description

  • You can use this function directly without creating a model. By default, the function calls the built-in model _dashscope/text_embedding/text_embedding_v2 to convert text to an embedding.

  • You can specify the model_id parameter to call other existing text embedding models, such as _dashscope/text_embedding/text_embedding_v3.

  • For more information about the built-in models _dashscope/text_embedding/text_embedding_v2 and _dashscope/text_embedding/text_embedding_v3, see Text and multimodal embeddings.

Preparations

The built-in models are integrated with the Natural Language Processing models provided by Alibaba Cloud Model Studio.

  1. Set the model token: You can use the AI_SetModelToken function to attach your Alibaba Cloud Model Studio API key.

  2. Modify the model call address: You can use the AI_AlterModel function to modify the call address of the built-in model. Replace the original address with the PrivateLink endpoint of Alibaba Cloud Model Studio. Change https://dashscope.aliyuncs.com to http://vpc-cn-beijing.dashscope.aliyuncs.com.

    For example:

    • _dashscope/text_embedding/text_embedding_v2:

      SELECT polar_ai.AI_AlterModel('_dashscope/text_embedding/text_embedding_v2', model_url=>'http://vpc-cn-beijing.dashscope.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding');
    • _dashscope/text_embedding/text_embedding_v3:

      SELECT polar_ai.AI_AlterModel('_dashscope/text_embedding/text_embedding_v3', model_url=>'http://vpc-cn-beijing.dashscope.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding');

Examples

  • In this example, the model_id parameter is not specified.

    SELECT polar_ai.ai_text_embedding('The wind is sharp, the sky is high, apes shriek sadly.');

    The following result is returned:

    ---
    {0.004930191827757042, -0.012394876394385515, 0.041976027360927766, -0.01943111099801429, 0.03970705290898573, -0.03082425930989348, 
        ..., 
        -0.008285377453501126, 0.001457333324851072, -0.01758455200255082, -0.03808980516132491, -0.03719669819619879, -0.008629344325205105
    }
  • In this example, the model_id parameter is set to the built-in model _dashscope/text_embedding/text_embedding_v3.

    SELECT polar_ai.ai_text_embedding('The wind is swift and the sky is high.', '_dashscope/text_embedding/text_embedding_v3');

    The following result is returned:

    ---
    {0.004930191827757042, -0.012394876394385515, 0.041976027360927766, -0.01943111099801429, 0.03970705290898573, -0.03082425930989348, 
        ..., 
        -0.008285377453501126, 0.001457333324851072, -0.01758455200255082, -0.03808980516132491, -0.03719669819619879, -0.008629344325205105
    }