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 |
tencentdb_ai.retrieve(embed_model NAME, -- Embedding model namequestion TEXT, -- Query questionsource_schema TEXT, -- schema of the knowledge base tablesource_table TEXT, -- Knowledge base table namechunk_col TEXT, -- Text chunk column namevector_col TEXT, -- Vector column nametopn INT DEFAULT 10, -- Return TopN resultsdistance_type TEXT DEFAULT 'L2' -- Distance algorithm) RETURNS SETOF tencentdb_ai.retrieve_result
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. |
retrieve_result AS (chunk TEXT, distance FLOAT8)
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 |
-- Euclidean distance search, returns Top 3SELECT chunk, TRUNC(distance::numeric, 4) AS distanceFROM 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 5SELECT chunk, TRUNC(distance::numeric, 4) AS distanceFROM 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 searchSELECT chunk, TRUNC(distance::numeric, 4) AS distanceFROM 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 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. |
tencentdb_ai.rag(embed_model NAME, -- Embedding model nameprompt_model NAME, -- Generation model namequestion TEXT, -- User questionsource_schema TEXT, -- schema of the knowledge base tablesource_table TEXT, -- Knowledge base table namechunk_col TEXT, -- Text chunk column namevector_col TEXT, -- Vector column nametopn INT DEFAULT 10, -- TopN recalldistance_type TEXT DEFAULT 'L2' -- Distance algorithm) RETURNS TEXT
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. |
User question → retrieve() 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
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}
-- 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');
-- Create the knowledge base tableCREATE TABLE kb_docs (id bigserial PRIMARY KEY,title text,chunk text,embedding vector(1024));-- Insert document chunks and vectorize them in real timeINSERT 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);
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.SELECT chunk, TRUNC(distance::numeric, 6) AS distanceFROM 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;
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)
-- One-stop RAG Q&ASELECT 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;
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)
-- kb_empty is an empty table with the same structure as kb_docsSELECT 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)
피드백