tencent cloud

Using the Tencentdb_ai Plug-In to Call Large Models
Last updated: 2025-11-07 17:27:28
Using the Tencentdb_ai Plug-In to Call Large Models
Last updated: 2025-11-07 17:27:28
Currently, both basic large models and AI application development are advancing rapidly. TencentDB for PostgreSQL provides the tencentdb_ai plug-in, enabling easy API calls to accessible large models over the network within TencentDB for PostgreSQL instances, supporting application development in various scenarios. This plug-in supports instances running PostgreSQL 17.
Note:
Using tencentdb_ai is the premise for an instance of PostgreSQL 17 already created.
Creating the tencentdb_ai plug-in will associate the creation of the pgcrypto plug-in. Please be informed.

TencentDB AI Plug-In Function Description

Adding a Model

add_model function defined as follows:
tencentdb_ai.add_model(
model_name NAME,
version TEXT,
region TEXT,
json_path JSONPATH
);
The parameters are explained as follows:
model_name: model name, such as hunyuan-lite, deepseek-r1
version: common parameter of this model version. Leave it blank if version is not required.
region: common parameter of this model region. Leave it blank if region is not required.
json_path: specifies how to extract the JSON response from this model during a call. It is equivalent to performing the jsonb_path_query_first function on the model output. If this parameter is not set, the original response of the model is output by default.

Viewing Registered Models

list_models function defined as follows:
tencentdb_ai.list_models(void);

Updating a Model

update_model_attr function defined as follows:
tencentdb_ai.update_model_attr(
model_name NAME,
attr_name text,
attr_value text
);
The parameters are explained as follows:
model_name: model name.
attr_name: model attribute to be updated. Updatable model attributes include version, region, json_path, SecretId, SecretKey.
attr_value: attribute's value to be updated.
For specific SecretId and SecretKey, check Cloud Access Management.

Deleting a Model

delete_model function defined as follows:
tencentdb_ai.delete_model(
model_name NAME
);
The parameters are explained as follows:
model_name: model name.

Model Invocation

The call_model function is defined as follows:
tencentdb_ai.call_model(
model_name NAME,
common_params TEXT[],
api_params TEXT[]
);
The parameters are explained as follows:
model_name: model name.
common_params: common parameters required to invoke the model, such as ARRAY['Action: ChatCompletions', 'Version: 2023-09-01']
api_params: proprietary parameters required to invoke the model, such as ARRAY['"Stream": false', '"Model": "hunyuan-lite"', '"Messages": [{"Role": "user", "Content": "Hello"}]']
In addition to using the call_model function for model invocation, you can also use the following APIs for model invocation in specific scenarios.

Fixed Scenario API

Dialogue

chat_completions function definition is as follows:
tencentdb_ai.chat_completions(
model_name NAME,
content TEXT,
args TEXT[] default NULL
);
The parameters are explained as follows:
model_name: model name.
content: conversation content.
args: other parameters to invoke the model, defaults to NULL.
Sample call:
SELECT tencentdb_ai.chat_completions('hunyuan-lite', 'Hello');
chat_completions
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------
{"Response":{"RequestId":"*******-****-****-****-***********","Note":"The above content is AI generation and does not mean the developer standpoint, do not delete or modify this tag","Choices":[{"Index":0,"Message":{"Role":"assistant","Co
Hello. Glad to chat with you. What would you like to share today?
nTokens":17,"TotalTokens":20}}}
(1 row)

Text to Vector

get_embedding function definition as follows:
tencentdb_ai.get_embedding(
model_name NAME,
content TEXT[]
);
The parameters are explained as follows:
model_name: model name.
content: conversion content.
Sample call:
damoxing1=> SELECT tencentdb_ai.get_embedding('hunyuan-embedding',ARRAY['Hello', 'PostgreSQL']);

Text Sorting

run_rerank function defined as follows:
tencentdb_ai.run_rerank(
model_name NAME,
query TEXT,
documents TEXT[],
args TEXT[] DEFAULT NULL
);
The parameters are explained as follows:
model_name: model name.
query: text sorting issue.
documents: content to be sorted.
args: specified parameters for a specific model.
damoxing1=> SELECT COUNT(*) FROM tencentdb_ai.run_rerank('lke-reranker-base', 'knowledge engine large model', ARRAY['HunYuan Large Model', 'Tencent knowledge engine']);
count
-------
2
(1 row)

Generating Text From Images

The function definition of image_to_text is as follows:
tencentdb_ai.image_to_text
(
model_name NAME,
query TEXT,
image TEXT,
args TEXT[] DEFAULT NULL
);
The parameters are explained as follows:
model_name: model name
query: image-to-text problem
image: image address
args: specific model parameters

Plugin Usage Example

The plugin use process is described as follows:
1. Create plugin
damoxing=> CREATE EXTENSION tencentdb_ai CASCADE;
NOTICE: installing required extension "pgcrypto"
CREATE EXTENSION
Check plugin creation completed
damoxing=> SELECT * FROM pg_extension;
oid | extname | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition
-------+--------------+----------+--------------+----------------+------------+-----------+--------------
14275 | plpgsql | 10 | 11 | f | 1.0 | |
16440 | pgcrypto | 16437 | 2200 | t | 1.3 | |
16477 | tencentdb_ai | 16437 | 2200 | t | 1.0 | {16479} | {""}
(3 rows)
2. Adding a large model
damoxing=> SELECT tencentdb_ai.add_model('hunyuan-lite','2023-09-01', NULL, NULL);
add_model
-----------
(1 row)
3. Add SecretId and SecretKey for large model API calls
For specific SecretId and SecretKey, check Cloud Access Management.
damoxing=> SELECT tencentdb_ai.update_model_attr('hunyuan-lite', 'SecretId', 'AKID***************');
update_model_attr
-------------------
(1 row)
damoxing=> SELECT tencentdb_ai.update_model_attr('hunyuan-lite', 'SecretKey', '********************');
update_model_attr
-------------------
(1 row)
4. View the current added model list
damoxing=> SELECT * FROM tencentdb_ai.list_models();
-[ RECORD 1 ]--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
model_name | hunyuan-lite
json_path |
secretid | ***************************
secretkey | ***************************
version |
region |
id_random | 516234453022
key_random | 134577899689
5. Call a large model
In a chat scenario, you can use the call_model API.
damoxing=> SELECT tencentdb_ai.call_model('hunyuan-lite',
ARRAY['Action: ChatCompletions', 'Version: 2023-09-01'],
ARRAY['"Stream": false', '"Model": "hunyuan-lite"', '"Messages": [{"Role": "user", "Content": "Hello"}]']);
call_model
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------
{"Response":{"RequestId":"*******-****-****-****-***********","Note":"The above content is AI generation and does not mean the developer standpoint, do not delete or modify this tag","Ch
choices":[{"Index":0,"Message":{"Role":"assistant","Content":"Hello! I'm happy to chat with you. What can I help you with? Whether it's about life, work, or learning."}}]}
study or other issues, I'll do my best to offer help."},"FinishReason":"stop"}],"Created":1739793838,"Id":"*******-****-****-****-***********
","Usage":{"PromptTokens":3,"CompletionTokens":33,"TotalTokens":36}}}
(1 row)

damoxing=>
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback