This topic describes the SQL functions for Retrieval-Augmented Generation (RAG) systems.
polar_ai.ai_rag_chunking
Splits the binary content of a document into Chunks.
Syntax
polar_ai.ai_rag_chunking(
content BYTEA,
doctype TEXT,
deploymentName TEXT DEFAULT 'RAG',
saveType TEXT DEFAULT 'binary',
chunkoptions JSONB DEFAULT NULL
)
RETURNS SETOF record (chunk_id INTEGER, chunk_content TEXT)Parameters
Parameter | Type | Required | Default | Description |
|
| Yes | - | The binary content of the document. |
|
| Yes | - | Document types, such as |
|
| No |
| The deployment name of the RAG service. |
|
| No |
| Document type, such as |
|
| No |
| Chunking options, provided in JSON format. For example,
|
Examples
-- Chunk with default options
SELECT * FROM polar_ai.ai_rag_chunking(content, 'pdf');
-- Customize chunkSize and chunkOverlap
SELECT * FROM polar_ai.ai_rag_chunking(content, 'pdf', chunkoptions := '{"chunkSize": 512, "chunkOverlap": 64}');polar_ai.ai_rag_text_embedding
Converts text content into a Vector.
You must first call polar_ai.ai_nl2sql_deployModel('RAG'); to deploy the model.
Syntax
polar_ai.ai_rag_text_embedding(
content TEXT,
model TEXT DEFAULT 'Qwen-8B-embedding',
dimension INTEGER DEFAULT 1024,
deploymentName TEXT DEFAULT 'RAG'
)
RETURNS float4[]Parameters
Parameter | Type | Required | Default | Description |
|
| Yes | - | The text content to be converted into a Vector. |
|
| No |
| The embedding model to use. Valid values:
|
|
| No |
| The dimension of the generated vector must be strictly consistent with the dimension defined in the
|
|
| No |
| The deployment name of the RAG service. |
Examples
-- Generate a 1024-dimensional vector
select polar_ai.ai_rag_text_embedding('It is a lovely film with lovely performances.');
-- Result:
{0.15527344,-0.09667969,-0.08984375,-0.13476562,0.087890625,-0.09423828, ....}polar_ai.ai_rag_rerank
Reranks a list of retrieved Text Chunks based on their relevance to a question.
You must first call polar_ai.ai_nl2sql_deployModel('RAG'); to deploy the model.
Syntax
polar_ai.ai_rag_rerank(
question TEXT,
contents TEXT[],
model TEXT DEFAULT 'gte',
deploymentName TEXT DEFAULT 'RAG'
)
RETURNS TEXT[]Parameters
Parameter | Type | Required | Default | Description |
|
| Yes | - | The original question. |
|
| Yes | - | An Array of Text Chunks to rerank. |
|
| No |
| The reranking model to use. Valid values:
|
|
| No |
| The deployment name of the RAG service. |
Examples
-- Rerank an array of text
SELECT polar_ai.ai_rag_rerank(
'What fruit do you like to eat?',
ARRAY['Apple', 'Crystal', 'Soccer', 'Banana']
);
-- Result:
{'Apple','Banana','Crystal','Soccer'}