tencent cloud

TencentDB for PostgreSQL

tencentdb_ai RAG

Download
Mode fokus
Ukuran font
Terakhir diperbarui: 2026-08-20 17:45:10
Diterjemahkan oleh AI
TencentDB for PostgreSQL provides the tencentdb_ai RAG feature. This document describes the overview and usage instructions for the tencentdb_ai RAG feature.

Overview

tencentdb_ai provides the RAG (Retrieval-Augmented Generation) feature, which enables the complete workflow of "vector search → prompt construction → LLM generation" directly within the database.
The RAG feature architecture is divided into three layers:
Component
Description
tencentdb_ai.retrieve_result
Composite return type (chunk TEXT, distance FLOAT8)
tencentdb_ai.retrieve()
Vector embedding + pgvector similarity search TopN
tencentdb_ai.rag()
One-stop RAG Q&A: retrieve → build Prompt → chat_completions generation

Use Cases

Enterprise Knowledge Base Q&A: stores document chunks as vectors to enable intelligent Q&A based on proprietary knowledge.
Customer Service System: search the historical ticket knowledge base to assist in answering user questions.
Codebase Search: After code snippets are vectorized, similar implementations can be searched.
Product Documentation Assistant: product manuals are vectorized in chunks, and user natural language queries are processed.

Prerequisites

The database version must be PostgreSQL 14 - 18, and the tencentdb_ai extension version must be ≥ 1.4 (subject to the actual extversion in the instance).
The pgvector extension (automatically installed when the tencentdb_ai plugin is created).
An embedding model (for example, kinfra-text-embedding-0.6b) and a generation model (for example, hy3 or glm-5.2) have been configured. For configuration instructions, see tencentdb_ai Plugin Features.

Function Details

1. retrieve() - Vector Similarity Search

Function Signature

tencentdb_ai.retrieve(
embed_model NAME, -- Embedding model name
question TEXT, -- Query question
source_schema TEXT, -- schema of the knowledge base table
source_table TEXT, -- Knowledge base table name
chunk_col TEXT, -- Text chunk column name
vector_col TEXT, -- Vector column name
topn INT DEFAULT 10, -- Return TopN results
distance_type TEXT DEFAULT 'L2' -- Distance algorithm
) RETURNS SETOF tencentdb_ai.retrieve_result

Parameter Description

Parameter
Type
Description
embed_model
NAME
Embedding model name, such as 'kinfra-text-embedding-0.6b'.
question
TEXT
Query question text (cannot be empty)
source_schema
TEXT
schema of the knowledge base table
source_table
TEXT
Knowledge base table name
chunk_col
TEXT
Text content column name
vector_col
TEXT
Vector column name
topn
INT
Number of most similar results returned, 10 by default
distance_type
TEXT
Distance metric algorithm supporting L2, cosine, L1, inner product, and ip, with L2 as the default.

Return Type

retrieve_result AS (chunk TEXT, distance FLOAT8)
chunk: Retrieved text block content.
distance: Distance value from the query vector (smaller value indicates higher similarity).

Supported Distance Metrics

distance_type
pgvector Operator
Description
L2
<->
Euclidean distance (default)
cosine
<=>
Cosine distance
L1
<+>
Manhattan distance
inner product
<#>
Inner product (negative inner product)
ip
<#>
Inner product alias
Note:
distance_type is case-insensitive (for example, L2 and l2 are equivalent).

Use Case

-- Euclidean distance search, returns Top 3
SELECT chunk, TRUNC(distance::numeric, 4) AS distance
FROM tencentdb_ai.retrieve(
'kinfra-text-embedding-0.6b',
Which data types are supported by PostgreSQL?
'public', 'knowledge_base', 'chunk', 'embedding',
3, 'L2'
)
ORDER BY distance, chunk;

-- Cosine distance search, returns Top 5
SELECT chunk, TRUNC(distance::numeric, 4) AS distance
FROM tencentdb_ai.retrieve(
'kinfra-text-embedding-0.6b',
How is vector search used?
'public', 'knowledge_base', 'chunk', 'embedding',
5, 'cosine'
)
ORDER BY distance, chunk;

-- Inner product search
SELECT chunk, TRUNC(distance::numeric, 4) AS distance
FROM tencentdb_ai.retrieve(
'kinfra-text-embedding-0.6b',
What is full-text search?
'public', 'knowledge_base', 'chunk', 'embedding',
10, 'inner product'
)
ORDER BY distance, chunk;

Error Handling

Error Scenario
Trigger Condition
Error Message
Empty question.
question is NULL or an empty string.
question must not be empty
Invalid TopN.
topn is NULL or <= 0.
topn must be positive
Missing pgvector.
The pgvector extension is not installed.
tencentdb_ai RAG requires the pgvector extension
Invalid distance type.
distance_type is not in the allowlist.
invalid distance_type
Empty embedding result.
The embedding model returns an empty result.
empty embedding for model
Vector dimension mismatch.
Query vector dimension does not match the knowledge base vector column dimension (for example, the model is 1024 dimensions while the column is another dimension).
different vector dimensions N and M thrown by pgvector.

2. rag() - One-Stop RAG Q&A

Function Signature

tencentdb_ai.rag(
embed_model NAME, -- Embedding model name
prompt_model NAME, -- Generation model name
question TEXT, -- User question
source_schema TEXT, -- schema of the knowledge base table
source_table TEXT, -- Knowledge base table name
chunk_col TEXT, -- Text chunk column name
vector_col TEXT, -- Vector column name
topn INT DEFAULT 10, -- TopN recall
distance_type TEXT DEFAULT 'L2' -- Distance algorithm
) RETURNS TEXT

Parameter Description

Parameter
Type
Description
embed_model
NAME
Embedding model name, such as 'kinfra-text-embedding-0.6b'
prompt_model
NAME
Generation model name, such as 'hy3' or 'glm-5.2'.
question
TEXT
User question
source_schema
TEXT
schema of the knowledge base table
source_table
TEXT
Knowledge base table name
chunk_col
TEXT
Text content column name
vector_col
TEXT
Vector column name
topn
INT
Number of TopN document chunks recalled, 10 by default
distance_type
TEXT
Distance algorithm, L2 by default.

Return Values

Returns TEXT, which is the response text generated by the large language model.

Workflow

User questionretrieve() recalls TopN document chunks
Concatenate all chunks to construct the context
Build the Prompt according to the template
Call chat_completions() to invoke the large language model for generation
Return the generated result

Internal Prompt Template

You are a rigorous Q&A assistant. Please answer the question based solely on the following reference materials.
If the available information is insufficient to answer, explicitly state that the question cannot be answered based on the available information.

[Reference Materials]
{All retrieved document chunks, separated by ---}

[Question]
{User question}

Empty Knowledge Base Handling

When the knowledge base table is empty, the retrieval result is empty, and rag() does not raise an error. Instead, it places a placeholder in the Prompt as (no retrieved content). The built-in Prompt instructs the model to answer solely based on the reference materials and to explicitly state "The question cannot be answered based on the available information" when the information is insufficient.

Usage Examples

The following demonstrates a complete end-to-end example covering model registration, knowledge base preparation, vector search, and RAG Q&A:

Step 1: Installing the Extension and Registering the Model

-- Install the extension (pgcrypto, vector, and pgmq are automatically installed as dependencies)
CREATE EXTENSION IF NOT EXISTS tencentdb_ai CASCADE;

-- Register the embedding model (json_path must be NULL, and the response is parsed by get_embedding itself)
SELECT tencentdb_ai.add_model('kinfra-text-embedding-0.6b', NULL, NULL, NULL, 'tokenhub');
SELECT tencentdb_ai.update_model_attr('kinfra-text-embedding-0.6b', 'api_key', 'your_api_key');

-- Register the generation model (json_path specifies the response parsing path)
SELECT tencentdb_ai.add_model('glm-5.2', NULL, NULL,
'$.choices[0].message.content'::jsonpath, 'tokenhub');
SELECT tencentdb_ai.update_model_attr('glm-5.2', 'api_key', 'your_api_key');
Note:
Obtain the api_key from the TokenHub console.

Step 2: Creating a Knowledge Base and Populating Data

-- Create the knowledge base table
CREATE TABLE kb_docs (
id bigserial PRIMARY KEY,
title text,
chunk text,
embedding vector(1024)
);

-- Insert document chunks and vectorize them in real time
INSERT INTO kb_docs (title, chunk, embedding)
SELECT
'Introduction to PostgreSQL',
'PostgreSQL is a powerful open-source object-relational database system with over 30 years of active development history, earning a strong reputation for reliability, feature robustness, and performance.',
(SELECT e::vector(1024) FROM tencentdb_ai.get_embedding(
'kinfra-text-embedding-0.6b',
ARRAY['PostgreSQL is a powerful open-source object-relational database system with over 30 years of active development history, earning a strong reputation for reliability, feature robustness, and performance.']
) AS e LIMIT 1);

INSERT INTO kb_docs (title, chunk, embedding)
SELECT
'PostgreSQL Features',
'PostgreSQL supports a rich set of data types: numeric types (integer, numeric, real), character types (text, varchar), date/time types (timestamp, date), JSON/JSONB, arrays, range types, and more.'
(SELECT e::vector(1024) FROM tencentdb_ai.get_embedding(
'kinfra-text-embedding-0.6b',
ARRAY['PostgreSQL supports a rich set of data types: numeric types (integer, numeric, real), character types (text, varchar), date/time types (timestamp, date), JSON/JSONB, arrays, range types, and more.']
) AS e LIMIT 1);

INSERT INTO kb_docs (title, chunk, embedding)
SELECT
'Introduction to pgvector',
'pgvector is a vector extension for PostgreSQL that supports exact and approximate nearest neighbor search, as well as multiple similarity metrics such as L2 distance, inner product, and cosine distance.'
(SELECT e::vector(1024) FROM tencentdb_ai.get_embedding(
'kinfra-text-embedding-0.6b',
ARRAY['pgvector is a vector extension for PostgreSQL that supports exact and approximate nearest neighbor search, as well as multiple similarity metrics such as L2 distance, inner product, and cosine distance.']
) AS e LIMIT 1);

-- Create a vector index (optional, to improve search performance)
CREATE INDEX ON kb_docs USING ivfflat (embedding vector_l2_ops) WITH (lists = 100);
Note:
When an ivfflat index is created with a small amount of data, a message ivfflat index created with little data will be prompted, which indicates low recall. This is normal. It is recommended to reindex after the data volume grows.

Step 3: Vector Search

SELECT chunk, TRUNC(distance::numeric, 6) AS distance
FROM tencentdb_ai.retrieve(
'kinfra-text-embedding-0.6b',
Which data types does PostgreSQL support?
'public', 'kb_docs', 'chunk', 'embedding', 3, 'cosine'
)
ORDER BY distance, chunk;
The returned results are as follows (a smaller distance indicates higher similarity):
chunk | distance
---------------------------------------------------------------------------------------------------------------------------------------------------------------+----------
PostgreSQL supports a rich set of data types: numeric types (integer, numeric, real), character types (text, varchar), date/time types (timestamp, date), JSON/JSONB, arrays, range types, and more.
'PostgreSQL is a powerful open-source object-relational database system with over 30 years of active development history, earning a strong reputation for reliability, feature robustness, and performance.'
pgvector is a vector extension for PostgreSQL that supports exact and approximate nearest neighbor search, as well as multiple similarity metrics such as L2 distance, inner product, and cosine distance.
(3 rows)

Step 4: RAG Q&A

-- One-stop RAG Q&A
SELECT tencentdb_ai.rag(
'kinfra-text-embedding-0.6b',
'glm-5.2',
'Please introduce the main features of pgvector.',
'public', 'kb_docs', 'chunk', 'embedding', 2, 'cosine'
) AS answer;
The returned results are as follows (the model answers based on the retrieved knowledge base content):
answer
------------------------------------------------------------------------------------------------------------------------------------------------------------------
According to the reference materials, the main features of pgvector are as follows: 1. Acts as a vector extension for PostgreSQL. 2. Supports exact and approximate nearest neighbor search. 3. Supports multiple similarity metrics, including L2 distance, inner product, and cosine distance.
(1 row)
Empty knowledge base Q&A (when the knowledge base has no matching content, the model will explicitly state that it cannot answer):
-- kb_empty is an empty table with the same structure as kb_docs
SELECT tencentdb_ai.rag(
'kinfra-text-embedding-0.6b', 'glm-5.2', 'What is a database?',
'public', 'kb_empty', 'chunk', 'embedding', 5, 'cosine'
) AS answer;
answer
--------------------------
Based on the available information, the question cannot be answered.
(1 row)


Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan