tencent cloud

Using tencentdb_ai Plug-In to Call a Large Model
Last updated: 2025-04-30 18:12:05
Using tencentdb_ai Plug-In to Call a Large Model
Last updated: 2025-04-30 18:12:05
Currently, both basic large models and AI application development are developing rapidly. TencentDB for PostgreSQL provides the tencentdb_ai plug-in to facilitate your easy calling of accessible large model APIs in TencentDB for PostgreSQL instances and complete application development in various scenarios. This plug-in supports instances with a major version of PostgreSQL 17.
Note:
Using tencentdb_ai requires that you have already created an instance with a major version of PostgreSQL 17.
Creating the tencentdb_ai plug-in will associate with the creation of the pgcrypto plug-in. Please be informed.

tencentdb_ai Plug-In Function Description

Adding a Model

The function definition of add_model is as follows:
tencentdb_ai.add_model(
model_name NAME,
version TEXT,
region TEXT,
json_path JSONPATH
);
The parameter interpretations are as follows:
model name: model name, such as hunyuan-lite, deepseek-r1.
version: common parameters of this model. If this model does not require the version parameter, it can be left blank.
region: the common parameter region of this model. If this model does not require the region parameter, it can be left blank.
json_path: Specifies how to extract the JSON response of this model when calling it. Equivalent to calling the jsonb_path_query_first function on the model's output. If this parameter is not set, the original response of the model will be output as is.

View Registered Models

The function definition of list_models is as follows:
tencentdb_ai.list_models(void);

Updating Model

The function definition of update_model_attr is as follows:
tencentdb_ai.update_model_attr(
model_name NAME,
attr_name text,
attr_value text
);
The parameter interpretations are as follows:
model name: model name.
attr_name: model attribute that needs to be updated. The updatable model attributes are version, region, json_path, SecretId, SecretKey.
attr_value: value of the attribute that needs to be updated.
View specific SecretId and SecretKey in Cloud Access Management (CAM).

Deleting a Model

The function definition of delete_model is as follows:
tencentdb_ai.delete_model(
model_name NAME
);
The parameter interpretations are 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 parameter interpretations are as follows:
model name: model name.
common_params: common parameters needed to call the model, such as ARRAY['Action: ChatCompletions', 'Version: 2023-09-01']
api_params: proprietary parameters required to invoke the model, for example ARRAY['"Stream": false', '"Model": "hunyuan-lite"', '"Messages": [{"Role": "user", "Content": "Hello"}]']
Besides using the call_model function to proceed with model invocation, you can also use the following APIs to perform model invocation in specific scenarios.

Fixed Scenario Interface

Dialogue

chat_completions function definition is as follows:
tencentdb_ai.chat_completions(
model_name NAME,
content TEXT,
args TEXT[] default NULL
);
The parameter interpretations are as follows:
model name: model name.
content: conversation content.
args: other parameters for invoking the model, default value is NULL.
Call example:
SELECT tencentdb_ai.chat_completions('hunyuan-lite', 'Hello');
chat_completions
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------
{"Response":{"RequestId":"*******-****-****-****-***********","Note":"The above content is AI generation. It does not represent the developer's 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 with me today?
nTokens":17,"TotalTokens":20}}}
(1 row)

Text to Vector

The get_embedding function is defined as follows:
tencentdb_ai.get_embedding(
model_name NAME,
content TEXT[]
);
The parameter interpretations are as follows:
model name: model name.
content: transformed content.
Call example:
damoxing1 => SELECT tencentdb_ai.get_embedding('hunyuan-embedding',ARRAY['Hello', 'PostgreSQL']);

Text Sorting

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

Plugin Usage Example

The following describes the plugin usage process one by one:
1. Create a plugin
damoxing=> CREATE EXTENSION tencentdb_ai CASCADE;
NOTICE: installing required extension "pgcrypto"
CREATE EXTENSION
Check plug-in 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. Add 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
View specific SecretId and SecretKey in Cloud Access Management (CAM).
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 currently 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"}]']);damoxing(> damoxing(>
call_model
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------
{"Response":{"RequestId":"*******-****-****-****-***********","Note":"The above content is AI generation. It does not represent the developer's standpoint. Do not delete or modify this tag.","Ch
Voices":[{"Index":0,"Message":{"Role":"assistant","Content":"Hello! Glad to talk with you. What can I help you with? Whether it's about life, work, or study.
For any problems, whether related to Xi or other aspects, I will do my best to offer you help.
","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