AI_CreateModel
Creates an AI model and registers it in the metadata table for subsequent use.
Syntax
table AI_CreateModel(text model_id, text model_url, text model_provider, text model_type, text model_name, json model_config, regprocedure model_headers_fn, regprocedure model_in_transform_fn, regprocedure model_out_transform_fn);
Parameters
|
Parameter |
Description |
|
model_id |
A custom name for the model. It must be unique for easy model management. Make sure to distinguish it from Note
The custom model name cannot start with an underscore (_). When the Polar_AI extension is created, the system automatically creates a set of built-in models whose names start with an underscore (_). You can run the following statement to view the created models: |
|
model_url |
The endpoint of the model. It cannot be empty. HTTP, HTTPS, and FILE protocols are supported. Example: the HTTP endpoint of the general-purpose text embedding model in Model Studio |
|
model_provider |
The model provider. It can be empty. Examples: AWS, Alibaba, Baidu, and Tencent. |
|
model_type |
The model type. It can be empty. Examples: LSTM and GRU. |
|
model_name |
The name of the model to call. It cannot be empty. Example: text-embedding-v2. |
|
model_config |
The model configuration information in JSON format. It cannot be empty. Format: { "author_type":"token", "token":"<YOUR_API_KEY>" }, where:
|
|
model_headers_fn |
The request header function that constructs request headers. The return type must be JSONB. If the model has no special header requirements, leave this parameter empty. Default: empty. |
|
model_in_transform_fn |
The input transform function. It cannot be empty. It is used to construct request data. For more information, see Input transform function. |
|
model_out_transform_fn |
The output transform function. It cannot be empty. It is used to parse the data returned by the model. For more information, see Output transform function. |
Return values
Returns the result of the AI model creation as a table row. The table structure is as follows:
|
Field |
Description |
|
model_seq |
The model sequence number. |
|
model_schema |
The workspace to which the model belongs. |
|
model_id |
The custom name of the model. |
|
model_qname |
The model name. |
|
created |
Whether the AI model was created successfully:
|
Function description
-
AI_CreateModelonly registers the model metadata in the metadata table and does not call the model. To call a model, see AI_CallModel. -
The input transform function (
model_in_transform_fn) converts input content into the HTTP request content required by the model. The return type must be JSONB. The following example uses the general-purpose text embedding modeltext-embedding-v2in Model Studio. Its HTTP request content is:{ "model": "text-embedding-v2", "input": { "texts": [ "The wind is high, the sky is vast" ] }, "parameters": { "text_type": "query" } }In the request body, the
modelandinput.textsparameters are required. The model input function can be defined as:-
model_name: the name of the model to call. -
content: the text content for which to generate embeddings.
CREATE OR REPLACE FUNCTION my_text_embedding_in(model_name text, content text) RETURNS jsonb LANGUAGE plpgsql AS $function$ BEGIN RETURN ('{"model": "'|| model_name ||'","input":{"texts":["'|| content ||'"]},"parameters":{"text_type": "query"}}')::jsonb; END; $function$; -
-
The output transform function (
model_out_transform_fn) converts the model output (typically JSON) into the format required by your application. For example, the model returns the following JSON response:{ "usage": { "total_tokens": 7 }, "output": { "embeddings": [{ "embedding": [0.004930191827757042, -0.008629344325205105, 0.041976027360927766], "text_index": 0 }] }, "request_id": "317ba0d4-6c08-9c24-8725-eebd445def51" }Because only the vector content in
output.embeddings.embeddingis needed and thejsonbformat must be retained, the model output function can be defined as:-
model_id: the parameter type must be TEXT. -
response_json: the parameter type must be JSONB.
CREATE OR REPLACE FUNCTION my_text_embedding_out(model_id text, response_json jsonb) RETURNS jsonb AS $$ select ((((response_json->>'output')::jsonb->>'embeddings')::jsonb)->0->>'embedding')::jsonb as result $$ LANGUAGE 'sql' IMMUTABLE; -
Examples
The following example creates a Text and multimodal embedding model in Model Studio. The registration steps are as follows:
-
Create the input transform function.
CREATE OR REPLACE FUNCTION my_text_embedding_in(model_name text, content text) RETURNS jsonb LANGUAGE plpgsql AS $function$ BEGIN RETURN ('{"model": "'|| model_name ||'","input":{"texts":["'|| content ||'"]},"parameters":{"text_type": "query"}}')::jsonb; END; $function$; -
Create the output transform function.
CREATE OR REPLACE FUNCTION my_text_embedding_out(model_id text, response_json jsonb) RETURNS jsonb AS $$ select ((((response_json->>'output')::jsonb->>'embeddings')::jsonb)->0->>'embedding')::jsonb as result $$ LANGUAGE 'sql' IMMUTABLE; -
Create the general-purpose text embedding model.
When creating the model, adjust the following parameters:
-
Model endpoint: modify the
model_urldomain name by replacing the HTTP endpoint of the general-purpose text embedding model with the PrivateLink endpoint of Model Studio. That is, replacehttps://dashscope.aliyuncs.comwithhttp://vpc-cn-beijing.dashscope.aliyuncs.com. -
Model configuration: replace the
tokeninmodel_configwith your API key in Model Studio. -
Input transform function: set
model_in_transform_fnto theai_text_embedding_in_fnfunction created in the preceding step. -
Output transform function: set
model_out_transform_fnto theai_text_embedding_out_fnfunction created in the preceding step
SELECT polar_ai.AI_CreateModel('my_text_embedding_model', 'http://vpc-cn-beijing.dashscope.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding','Alibaba','General-purpose text embedding model','text-embedding-v2','{"author_type": "token", "token": "<YOUR_API_KEY>"}', NULL, 'ai_text_embedding_in_fn'::regproc, 'ai_text_embedding_out_fn'::regproc);The following result is returned. For more information about the return values, see Return values:
ai_createmodel -------------------------------------------------------- (1,my_text_embedding_model,polar_ai,text-embedding-v2,t) -
-
(Optional) View the created AI models:
SELECT * from polar_ai._ai_models;Sample output:
model_seq | model_id | model_url | model_provider | model_type | model_name | model_config | model_headers_fn | model_in_transform_fn | model_out_transform_fn ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 1 | my_text_embedding_model | http://vpc-cn-beijing.dashscope.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding | Alibaba | General-purpose text embedding model | text-embedding-v2 | {"token": "20E0BE9E5438xxxxxxxxxxxxxxxxxxxx", "author_type": "token"} | - | ai_text_embedding_in_fn(text,text) | ai_text_embedding_out_fn(text,jsonb)