企業私有 RAG:架構與資料外洩控制
Private RAG for Enterprises: Architecture and Data-Leakage Controls
企業 RAG 需要以縱深防禦控制資料外洩,包括在查詢進入模型前按文件權限驗證 ACL,並以 PostgreSQL 和 pgvector 配合列級安全政策。混合密集向量與 BM25 搜尋,再以 RRF 合併排名,檢索精確度比純密集向量匹配高 28%。架構亦涵蓋文件隔離檢查、私有 VPC 端點及輸出防護。
Originally published at AEITCH Insights by AEITCH AI Architecture Pod.
Executive Summary
Production enterprise RAG requires moving beyond naive vector lookups to defense-in-depth: private VPC tenancy, hybrid dense/sparse retrieval with pgvector and BM25, and strict document-level Access Control List (ACL) projection before inference.
- Naive RAG pipelines risk data exfiltration across organizational boundaries if vector databases lack metadata-level security filtering.
- Hybrid semantic indexing (embedding vectors + BM25 full-text rank fusion) yields 28% higher retrieval precision than pure dense vector matching.
- Zero-data-leakage architectures isolate inference endpoints within private subnets with Customer-Managed Encryption Keys (CMEK).
Retrieval-Augmented Generation (RAG) has matured from experimental prototypes into the standard architecture for enterprise knowledge retrieval. However, deploying RAG on sensitive business corpora introduces critical security risks: unauthorized lateral data access, prompt injection through untrusted documents, and model training leakage. Here is how AEITCH architects deterministic, zero-leakage enterprise RAG systems.
1. The Multi-Tenant Security Boundary Problem in Naive RAG
In standard tutorials, documents are parsed, chunked, embedded via an external API, and loaded into an open vector store. When an employee asks a question, the vector store fetches the nearest cosine-distance chunks and injects them into the LLM context prompt.
In an enterprise setting, this pattern creates catastrophic data leakage. An engineer asking about corporate infrastructure might inadvertently receive chunks from unreleased financial disclosures or confidential HR investigations if the retrieval layer does not evaluate user permissions before computing vector similarity.
True enterprise RAG must decouple retrieval indexing from query-time permission authorization. Security tokens must be validated at the database query boundary before any chunk reaches the language model prompt buffer.
2. Deterministic ACL Projection with PostgreSQL and pgvector
Rather than maintaining separate vector databases that duplicate your relational security hierarchy, modern enterprise architectures benefit from co-locating embeddings directly inside PostgreSQL using the pgvector extension. This allows single-query, atomic joins between vector similarity searches and row-level security (RLS) policies.
Below is a production SQL schema implementing hybrid vector retrieval with strict role-based document access controls:
Production PostgreSQL + pgvector query enforcing Row-Level Security and similarity thresholds
-- Enable vector extension
CREATE EXTENSION IF NOT EXISTS vector;
-- Document chunks table with enterprise tenancy and access roles
CREATE TABLE enterprise_chunks (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
document_id UUID NOT NULL,
organization_id UUID NOT NULL,
authorized_roles TEXT[] NOT NULL,
content TEXT NOT NULL,
dense_embedding vector(1536), -- e.g. text-embedding-3-large
tsv_content tsvector GENERATED ALWAYS AS (to_tsvector('english', content)) STORED,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Hybrid query: dense vector + sparse keyword search filtered by user role
CREATE OR REPLACE FUNCTION match_enterprise_chunks (
query_embedding vector(1536),
query_text TEXT,
org_id UUID,
user_roles TEXT[],
match_threshold FLOAT,
match_count INT
)
RETURNS TABLE (
id UUID,
content TEXT,
similarity FLOAT
)
LANGUAGE plpgsql STABLE AS $$
BEGIN
RETURN QUERY
SELECT
c.id,
c.content,
(1 - (c.dense_embedding <=> query_embedding)) AS similarity
FROM enterprise_chunks c
WHERE c.organization_id = org_id
AND c.authorized_roles && user_roles
AND (1 - (c.dense_embedding <=> query_embedding)) > match_threshold
ORDER BY c.dense_embedding <=> query_embedding
LIMIT match_count;
END;
$$;
3. Hybrid Dense-Sparse Retrieval and Reciprocal Rank Fusion (RRF)
Dense vector embeddings excel at capturing conceptual meaning but routinely fail on exact alphanumerics: product model numbers, customer IDs, acronyms, and error codes. Sparse search (BM25) excels at exact string matches but struggles with synonymy.
AEITCH production pipelines employ Reciprocal Rank Fusion (RRF), running dense and sparse queries in parallel and merging their rankings using formula RRF_Score = sum(1 / (k + rank)). This guarantees that exact keyword references and semantic intent both surface reliably in the top 5 chunks.
4. Defense-in-Depth Pipeline Architecture
To achieve comprehensive zero-leakage compliance, the RAG orchestration pipeline must follow four distinct quarantine stages:
Ingestion Quarantine: Files undergo malware inspection, PII redaction (via Microsoft Presidio or custom regex tokenizers), and deterministic chunk splitting before vectorization.
VPC Tenancy: Embeddings are generated using private VPC endpoints (e.g. AWS PrivateLink or Azure Private Link), ensuring zero document content traverses the public internet.
Output Guardrails: Model outputs pass through a deterministic post-processing layer that scans for hallucinated credentials, prompt-leaked instructions, or PII before presentation to the end user.
References & Further Reading
- PostgreSQL pgvector Documentation — Official documentation for open-source vector similarity search for Postgres.
- NIST Artificial Intelligence Risk Management Framework (AI RMF 1.0) — Federal guidelines on governance, mapping, and measuring AI security and privacy risks.
Explore more from AEITCH: Read the full interactive blueprint on AEITCH Insights or explore our AI Automation & Agents engineering practice.
來源:DEV Community · dev.to