You need to enable JavaScript to run this app.
文档中心
AI 原生 BaaS 平台 Supabase 版

AI 原生 BaaS 平台 Supabase 版

复制全文
下载 pdf
AI Function 智能处理(rds_ai)
检索增强生成(ai_rag)
复制全文
下载 pdf
检索增强生成(ai_rag)
  1. 定义​
rds_ai.ai_rag 依次执行问题向量化、向量检索、可选重排序和文本生成,并返回基于检索上下文的答案。​
  1. 语法​
​
rds_ai.ai_rag(​
question text,​
source_schema text,​
source_table text,​
chunk_col text,​
vector_col text,​
topn integer DEFAULT 10,​
distance_type text DEFAULT 'cosine',​
embed_model text DEFAULT NULL,​
embed_args jsonb DEFAULT '{}'::jsonb,​
prompt_model text DEFAULT NULL,​
prompt_args jsonb DEFAULT '{}'::jsonb,​
rank_model text DEFAULT NULL,​
rank_args jsonb DEFAULT '{}'::jsonb​
) RETURNS text​
​
  1. 参数​
​
参数​
类型​
默认值​
说明​
question​
text​
无​
用户问题,不得为 SQL NULL。​
source_schema​
text​
无​
知识表所在 schema。​
source_table​
text​
无​
知识表名称。​
chunk_col​
text​
无​
作为上下文的文本列。​
vector_col​
text​
无​
用于检索的向量列。​
topn​
integer​
10​
检索候选数,必须大于 0。​
distance_type​
text​
cosine​
cosine、l2、l1 或 negative。​
embed_model​
text​
SQL NULL​
embedding 模型;NULL 时使用 ai_embed 默认模型。​
embed_args​
jsonb​
{}​
传给 embedding 阶段的请求体参数。​
prompt_model​
text​
SQL NULL​
生成答案的 chat/LLM 模型;NULL 时使用 ai_query 默认模型。​
prompt_args​
jsonb​
{}​
传给答案生成阶段的请求体参数。​
rank_model​
text​
SQL NULL​
可选 rerank 模型;NULL 时尝试使用 ai_rank 默认模型。​
rank_args​
jsonb​
{}​
传给 rerank 阶段的参数。​
​
  1. 返回​
返回一个 text 答案。未提供且未配置 rerank 模型时,会发出 WARNING 并按原始向量检索顺序生成答案;该函数不返回检索片段或距离。​
  1. 错误行为​
  • 问题、schema、表名、列名为空,topn 非正数,距离类型或三个参数对象不合法时抛出错误。​
  • embedding 或 prompt 默认模型缺失、显式模型不存在或类型不兼容时抛出错误;缺少可用 rank 默认模型只警告并跳过重排序。​
  • 函数为 SECURITY INVOKER;调用者缺少来源列权限时抛出错误,RLS 会限制参与检索的行。​
  • 向量类型/维度不匹配、表列不存在、模型响应解析失败,或任一必需 HTTP 调用失败时抛出错误。​
  • 达到并发上限时等待;达到 token 配额时,涉及的模型请求被拒绝。​
  1. 示例​
显式指定检索与生成模型​
​
SELECT rds_ai.ai_rag(​
question => 'What does PostgreSQL support?',​
source_schema => 'public',​
source_table => 'rag_docs',​
chunk_col => 'content',​
vector_col => 'embedding',​
topn => 2,​
distance_type => 'cosine',​
embed_model => 'Doc-Embed-4',​
embed_args => '{}'::jsonb,​
prompt_model => 'Doc-RAG-Chat',​
prompt_args => '{"temperature":0}'::jsonb,​
rank_model => NULL,​
rank_args => '{}'::jsonb​
);​
​
​
最近更新时间:2026.10.21 12:50:08
这个页面对您有帮助吗?
有用
有用
无用
无用