RAG SQL

更新时间:
复制 MD 格式

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

content

BYTEA

Yes

-

The binary content of the document.

doctype

TEXT

Yes

-

Document types, such as pdf, docx, and xlsx.

deploymentName

TEXT

No

RAG

The deployment name of the RAG service.

saveType

TEXT

No

binary

Document type, such as txt, url, or binary.

chunkoptions

JSONB

No

NULL

Chunking options, provided in JSON format. For example, '{ "chunkSize": 512, "chunkOverlap": 64 }'.

  • chunkSize: The maximum length of each chunk in bytes. The default value is 128.

  • chunkOverlap: The length of the overlap between adjacent chunks, in bytes.

  • separator: The separator used to split text. The default separator is a space.

  • headersToSplitOn: Chunks Markdown files based on heading levels.

    • Valid values: an integer from -1 to 6. Default value: -1.

    • If the value is -1, the file is split by all heading levels.

    • For other values, such as x, the file is split into chunks based on headings with a level less than or equal to x.

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.

Note

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

content

TEXT

Yes

-

The text content to be converted into a Vector.

model

TEXT

No

Qwen-8B-embedding

The embedding model to use. Valid values:

  • Qwen-8B-embedding

  • GTE

dimension

INTEGER

No

1024

The dimension of the generated vector must be strictly consistent with the dimension defined in the VECTOR column of the data table. A mismatch in dimensions will cause insertion or query errors.

  • Qwen-8B-embedding: 2048, 1536, 1024 (default), 768, 512, 256, 128, 64.

  • GTE: 768

deploymentName

TEXT

No

RAG

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.

Note

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

question

TEXT

Yes

-

The original question.

contents

TEXT[]

Yes

-

An Array of Text Chunks to rerank.

model

TEXT

No

gte

The reranking model to use. Valid values:

  • gte

  • Qwen3-Reranker

deploymentName

TEXT

No

RAG

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'}