tencent cloud

TencentDB for PostgreSQL

Release Notes and Announcements
Release Notes
Product Announcements
Product Introduction
Overview
Features
Strengths
Scenarios
Information Security
Regions and AZs
Product Feature List
Large version lifecycle description
MSSQL Compatible Version
Billing
Billing Overview
Instance Type and Specification
Purchase Methods
Refund
Overdue Payments
Backup Space Billing
Database Audit Billing Overview
Getting Started
Creating TencentDB for PostgreSQL Instance
Connecting to TencentDB for PostgreSQL Instance
Managing TencentDB for PostgreSQL Instance
Importing Data
Migrating Data with DTS
Kernel Version Introduction
Kernel Version Overview
Kernel Version Release Notes
Viewing Kernel Version
Proprietary Kernel Features
Database Audit
Audit Service Description
Activating Audit Service
View Audit Logs
Modify audit services
Audit Performance Description
User Guide
Instance Management
Upgrading Instance
CPU Elastic Scaling
Read-Only Instance
Account Management
Database Management
Parameter Management
Log Management and Analysis
Backup and Restoration
Data Migration
Extension Management
Network Management
Access Management
Data Security
Tenant and Resource Isolation
Security Groups
Monitoring and Alarms
Tag
AI Practice
Using the Tencentdb_ai Plug-In to Call Large Models
Building Ai Applications with the Tencentdb Ai Plug-In
Combining Supabase to Quickly Build Backend Service Based on TencentDB for PostgreSQL
Use Cases
postgres_fdw Extension for Cross-database Access
Automatically Creating Partition in PostgreSQL
Searching in High Numbers of Tags Based on pg_roaringbitmap
Querying People Nearby with One SQL Statement
Configuring TencentDB for PostgreSQL as GitLab's External Data Source
Supporting Tiered Storage Based on cos_fdw Extension
Implement Read/Write Separation via pgpool
Implementing Slow SQL Analysis Using the Auto_explain Plugin
Using pglogical for Logical Replication
Using Debezium to Collect PostgreSQL Data
Set Up a Remote Disaster Recovery Environment for PostgreSQL Locally on CVM
Read-Only Instance and Read-Only Group Practical Tutorial
How to Use SCF for Scheduled Database Operations
Fix Table Bloat
Performance White Paper
Test Methods
Test Results
API Documentation
History
Introduction
API Category
Making API Requests
Instance APIs
Read-Only Instance APIs
Backup and Recovery APIs
Parameter Management APIs
Security Group APIs
Performance Optimization APIs
Account APIs
Specification APIs
Network APIs
Data Types
Error Codes
FAQs
Service Agreement
Service Level Agreement
Terms of Service
Glossary
Contact Us
문서TencentDB for PostgreSQLAI PracticeUsing the Tencentdb_ai Plug-In to Call Large Models

Using the Tencentdb_ai Plug-In to Call Large Models

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 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=>

도움말 및 지원

문제 해결에 도움이 되었나요?

피드백